-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathShovelItem.java
More file actions
87 lines (72 loc) · 3.36 KB
/
ShovelItem.java
File metadata and controls
87 lines (72 loc) · 3.36 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
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.mixin.accessor.ShovelItemAccessor;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.CampfireBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.tag.BlockTags;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import net.minecraft.world.WorldEvents;
import java.util.Map;
import java.util.Optional;
public class ShovelItem extends MiningToolItem {
public ShovelItem(ToolItemSettings settings, ToolItemType toolItemType, Material material) {
super(settings, toolItemType, material, BlockTags.SHOVEL_MINEABLE);
}
private static Optional<BlockState> tryConvertBlockAndPlayEffects(ItemUsageContext context) {
World world = context.getWorld();
BlockPos blockPos = context.getBlockPos();
BlockState blockState = world.getBlockState(blockPos);
PlayerEntity playerEntity = context.getPlayer();
if (context.getSide() == Direction.DOWN) {
return Optional.empty();
}
Map<Block, BlockState> pathStatesMap = ShovelItemAccessor.getPathStates();
BlockState pathBlockState = pathStatesMap.get(blockState.getBlock());
if (pathBlockState != null && world.getBlockState(blockPos.up()).isAir()) {
world.playSound(playerEntity, blockPos, SoundEvents.ITEM_SHOVEL_FLATTEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
return Optional.of(pathBlockState);
}
if (blockState.getBlock() instanceof CampfireBlock && blockState.get(CampfireBlock.LIT)) {
if (!world.isClient()) {
world.syncWorldEvent(null, WorldEvents.FIRE_EXTINGUISHED, blockPos, 0);
}
CampfireBlock.extinguish(context.getPlayer(), world, blockPos, blockState);
return Optional.of(blockState.with(CampfireBlock.LIT, false));
}
return Optional.empty();
}
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();
BlockPos blockPos = context.getBlockPos();
PlayerEntity playerEntity = context.getPlayer();
if (playerEntity == null) {
return ActionResult.PASS;
}
if (!canDamageItem(context.getStack(), 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;
}
}