/
RedstoneChargerItem.java
80 lines (66 loc) · 2.83 KB
/
RedstoneChargerItem.java
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
package knightminer.inspirations.tools.item;
import knightminer.inspirations.tools.InspirationsTools;
import knightminer.inspirations.tools.block.RedstoneChargeBlock;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.DirectionalPlaceContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.item.Items;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import static knightminer.inspirations.tools.InspirationsTools.redstoneCharge;
public class RedstoneChargerItem extends Item {
public RedstoneChargerItem() {
super(new Item.Properties()
.maxDamage(120)
.group(ItemGroup.TOOLS)
);
}
@Override
public ActionResultType onItemUse(ItemUseContext context) {
BlockPos pos = context.getPos();
Direction facing = context.getFace();
World world = context.getWorld();
// we clicked a block, but want the position in front of the block
if(world.getBlockState(pos).isSolid()) {
pos = pos.offset(facing);
}
// stop if we cannot edit
PlayerEntity player = context.getPlayer();
if (player == null || !player.canPlayerEdit(pos, facing, ItemStack.EMPTY)) {
return ActionResultType.FAIL;
}
BlockState state = InspirationsTools.redstoneCharge.getDefaultState()
.with(RedstoneChargeBlock.FACING, facing.getOpposite())
.with(RedstoneChargeBlock.QUICK, player.isCrouching());
DirectionalPlaceContext blockContext = new DirectionalPlaceContext(world, pos, facing, ItemStack.EMPTY, facing);
// try placing a redstone charge
if (world.getBlockState(pos).isReplaceable(blockContext)) {
world.playSound(context.getPlayer(), pos, SoundEvents.ITEM_FLINTANDSTEEL_USE, SoundCategory.BLOCKS, 1.0F, world.rand.nextFloat() * 0.4F + 0.8F);
world.setBlockState(pos, state, Constants.BlockFlags.DEFAULT_AND_RERENDER);
redstoneCharge.onBlockPlacedBy(world, pos, state, null, ItemStack.EMPTY);
}
// mark we used the item
ItemStack stack = context.getItem();
if (context.getPlayer() instanceof ServerPlayerEntity) {
CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayerEntity)context.getPlayer(), pos, stack);
}
// damage it and return
stack.damageItem(1, context.getPlayer(), cPlayer -> cPlayer.sendBreakAnimation(context.getHand()));
return ActionResultType.SUCCESS;
}
@Override
public boolean getIsRepairable(ItemStack toRepair, ItemStack repair) {
return repair.getItem() == Items.REDSTONE;
}
}