-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAxeItem.java
More file actions
124 lines (101 loc) · 4.92 KB
/
AxeItem.java
File metadata and controls
124 lines (101 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package gregtech.common.tools;
import alexiil.mc.lib.attributes.Simulation;
import gregtech.api.capability.item.CustomDamageItem;
import gregtech.api.items.toolitem.MiningToolItem;
import gregtech.api.items.toolitem.ToolItemSettings;
import gregtech.api.items.toolitem.ToolItemType;
import gregtech.api.unification.material.Material;
import gregtech.api.util.TaskScheduler;
import gregtech.mixin.accessor.AxeItemAccessor;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Oxidizable;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.HoneycombItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.tag.BlockTags;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldEvents;
import java.util.Map;
import java.util.Optional;
public class AxeItem extends MiningToolItem {
public AxeItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
super(settings, toolItemType, material, BlockTags.AXE_MINEABLE);
}
private static Optional<BlockState> tryConvertBlockAndPlayEffects(ItemUsageContext context) {
World world = context.getWorld();
BlockPos blockPos = context.getBlockPos();
PlayerEntity playerEntity = context.getPlayer();
BlockState blockState = world.getBlockState(blockPos);
Map<Block, Block> unwaxedBlockMap = HoneycombItem.WAXED_TO_UNWAXED_BLOCKS.get();
Optional<BlockState> strippedState = ((AxeItemAccessor) Items.IRON_AXE).getStrippedState(blockState);
if (strippedState.isPresent()) {
world.playSound(playerEntity, blockPos, SoundEvents.ITEM_AXE_STRIP, SoundCategory.BLOCKS, 1.0F, 1.0F);
return strippedState;
}
Optional<BlockState> deoxidizedState = Oxidizable.getDecreasedOxidationState(blockState);
if (deoxidizedState.isPresent()) {
world.playSound(playerEntity, blockPos, SoundEvents.ITEM_AXE_SCRAPE, SoundCategory.BLOCKS, 1.0F, 1.0F);
world.syncWorldEvent(playerEntity, WorldEvents.BLOCK_SCRAPED, blockPos, 0);
return deoxidizedState;
}
Optional<BlockState> unwaxedState = Optional.ofNullable(unwaxedBlockMap.get(blockState.getBlock()))
.map(block -> block.getStateWithProperties(blockState));
if (unwaxedState.isPresent()) {
world.playSound(playerEntity, blockPos, SoundEvents.ITEM_AXE_WAX_OFF, SoundCategory.BLOCKS, 1.0F, 1.0F);
world.syncWorldEvent(playerEntity, WorldEvents.WAX_REMOVED, blockPos, 0);
return unwaxedState;
}
return Optional.empty();
}
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();
ItemStack itemStack = context.getStack();
BlockPos blockPos = context.getBlockPos();
PlayerEntity playerEntity = context.getPlayer();
if (playerEntity == null) {
return ActionResult.PASS;
}
if (!canDamageItem(itemStack, damagePerSpecialAction, playerEntity)) {
return ActionResult.PASS;
}
Optional<BlockState> convertedState = tryConvertBlockAndPlayEffects(context);
if (convertedState.isPresent()) {
if (!world.isClient) {
world.setBlockState(blockPos, convertedState.get(), Block.NOTIFY_ALL | Block.REDRAW_ON_MAIN_THREAD);
CustomDamageItem.damageItem(playerEntity, context.getHand(), damagePerSpecialAction, Simulation.ACTION);
}
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
}
public static boolean applyTimberAxe(ItemStack itemStack, World world, BlockState state, BlockPos blockPos, LivingEntity player) {
if(TreeChopTask.isLogBlock(state) == 1) {
if(!world.isClient()) {
ServerPlayerEntity playerMP = (ServerPlayerEntity) player;
if (playerMP.getItemCooldownManager().isCoolingDown(itemStack.getItem()))
return false;
TreeChopTask treeChopTask = new TreeChopTask(blockPos, world, playerMP, itemStack);
TaskScheduler.scheduleTask(world, treeChopTask);
}
return true;
}
return false;
}
@Override
public boolean postMine(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity miner) {
return miner.isSneaking() ?
CustomDamageItem.damageItem(miner, Hand.MAIN_HAND, damagePerBlockBreak, Simulation.ACTION) :
applyTimberAxe(stack, world, state, pos, miner);
}
}