/
BuildingEvents.java
102 lines (89 loc) · 3.84 KB
/
BuildingEvents.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package knightminer.inspirations.building;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.building.block.RopeBlock;
import knightminer.inspirations.common.Config;
import net.minecraft.block.BlockState;
import net.minecraft.block.SoundType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.items.ItemHandlerHelper;
import javax.annotation.Nullable;
@SuppressWarnings({"unused"})
@EventBusSubscriber(modid = Inspirations.modID, bus = Bus.FORGE)
public class BuildingEvents {
/**
* Rope ladder event is used to allow sneak right click interaction to have special logic, as opposed to the standard interaction method
*/
@SubscribeEvent
static void toggleRopeLadder(PlayerInteractEvent.RightClickBlock event) {
if (!Config.enableRopeLadder.get() || event.getWorld().isRemote()) {
return;
}
World world = event.getWorld();
BlockPos pos = event.getPos();
BlockState state = world.getBlockState(pos);
if (!(state.getBlock() instanceof RopeBlock)) {
return;
}
PlayerEntity player = event.getPlayer();
if (state.get(RopeBlock.RUNGS) != RopeBlock.Rungs.NONE) {
if (removeRopeLadder(world, pos, state, player)) {
event.setCanceled(true);
event.setCancellationResult(ActionResultType.SUCCESS);
}
return;
}
if (makeRopeLadder(world, pos, state, event.getFace(), player, event.getItemStack())) {
event.setCanceled(true);
event.setCancellationResult(ActionResultType.SUCCESS);
}
}
/**
* Logic to remove rungs from a rope ladder
*/
private static boolean removeRopeLadder(World world, BlockPos pos, BlockState state, PlayerEntity player) {
// only remove rungs when sneaking
if (!player.isCrouching()) {
return false;
}
// remove rungs
world.setBlockState(pos, state.with(RopeBlock.RUNGS, RopeBlock.Rungs.NONE));
RopeBlock rope = (RopeBlock)state.getBlock();
SoundType soundtype = rope.getSoundType(state, world, pos, player);
world.playSound(player, pos, soundtype.getBreakSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
ItemHandlerHelper.giveItemToPlayer(player, new ItemStack(rope.getRungsItem(), RopeBlock.RUNG_ITEM_COUNT), player.inventory.currentItem);
player.resetActiveHand();
return true;
}
/**
* Logic to add rungs to a rope ladder
*/
private static boolean makeRopeLadder(World world, BlockPos pos, BlockState state, @Nullable Direction side, PlayerEntity player, ItemStack stack) {
// must have not clicked the bottom and we must have 4 items
if (side == null || side.getAxis() == Direction.Axis.Y || (stack.getCount() < RopeBlock.RUNG_ITEM_COUNT && !player.isCreative())) {
return false;
}
// ensure we hae the right item
RopeBlock rope = (RopeBlock)state.getBlock();
if (stack.getItem() != rope.getRungsItem()) {
return false;
}
// add rungs
world.setBlockState(pos, state.with(RopeBlock.RUNGS, RopeBlock.Rungs.fromAxis(side.rotateY().getAxis())));
SoundType soundtype = state.getBlock().getSoundType(state, world, pos, player);
world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
if (!player.isCreative()) {
stack.shrink(RopeBlock.RUNG_ITEM_COUNT);
}
return true;
}
}