diff --git a/src/main/java/com/mohistmc/eventhandler/dispatcher/BlockEventDispatcher.java b/src/main/java/com/mohistmc/eventhandler/dispatcher/BlockEventDispatcher.java index 4b749c5e7f..d61f225a39 100644 --- a/src/main/java/com/mohistmc/eventhandler/dispatcher/BlockEventDispatcher.java +++ b/src/main/java/com/mohistmc/eventhandler/dispatcher/BlockEventDispatcher.java @@ -63,27 +63,29 @@ public void onBreakBlockEvent(BlockEvent.BreakEvent event) { @SubscribeEvent(receiveCanceled = true) public void onBlockPlace(BlockEvent.EntityPlaceEvent event) { Entity entity = event.getEntity(); - if (entity instanceof ServerPlayerEntity) { - ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity; - Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity); - Direction direction = event.direction; - if (direction != null) { - Hand hand = event.hand == null ? Hand.MAIN_HAND : event.hand; - CraftBlock placedBlock = CraftCustomSnapshot.fromBlockSnapshot(event.getBlockSnapshot(), true); - CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().relative(direction.getOpposite())); - ItemStack bukkitStack; - EquipmentSlot bukkitHand; - if (hand == Hand.MAIN_HAND) { - bukkitStack = player.getInventory().getItemInMainHand(); - bukkitHand = EquipmentSlot.HAND; - } else { - bukkitStack = player.getInventory().getItemInOffHand(); - bukkitHand = EquipmentSlot.OFF_HAND; + if (!event.getWorld().isClientSide() && event.getWorld() instanceof ServerWorld) { + if (entity instanceof ServerPlayerEntity) { + ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity; + Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity); + Direction direction = event.getPlaceEventDirection(); + if (direction != null) { + Hand hand = event.getPlaceEventHand(); + CraftBlock placedBlock = CraftCustomSnapshot.fromBlockSnapshot(event.getBlockSnapshot(), true); + CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().relative(direction.getOpposite())); + ItemStack bukkitStack; + EquipmentSlot bukkitHand; + if (hand == Hand.MAIN_HAND) { + bukkitStack = player.getInventory().getItemInMainHand(); + bukkitHand = EquipmentSlot.HAND; + } else { + bukkitStack = player.getInventory().getItemInOffHand(); + bukkitHand = EquipmentSlot.OFF_HAND; + } + BlockPlaceEvent placeEvent = new BlockPlaceEvent(placedBlock, placedBlock.getState(), againstBlock, bukkitStack, player, !event.isCanceled(), bukkitHand); + placeEvent.setCancelled(event.isCanceled()); + Bukkit.getPluginManager().callEvent(placeEvent); + event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild()); } - BlockPlaceEvent placeEvent = new BlockPlaceEvent(placedBlock, placedBlock.getState(), againstBlock, bukkitStack, player, !event.isCanceled(), bukkitHand); - placeEvent.setCancelled(event.isCanceled()); - Bukkit.getPluginManager().callEvent(placeEvent); - event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild()); } } } @@ -91,27 +93,29 @@ public void onBlockPlace(BlockEvent.EntityPlaceEvent event) { @SubscribeEvent(receiveCanceled = true) public void onMultiPlace(BlockEvent.EntityMultiPlaceEvent event) { Entity entity = event.getEntity(); - if (entity instanceof ServerPlayerEntity) { - ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity; - Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity); - Direction direction = event.direction; - if (direction != null) { - Hand hand = event.hand == null ? Hand.MAIN_HAND : event.hand; - List placedBlocks = new ArrayList<>(event.getReplacedBlockSnapshots().size()); - for (BlockSnapshot snapshot : event.getReplacedBlockSnapshots()) { - placedBlocks.add(CraftCustomSnapshot.fromBlockSnapshot(snapshot, true).getState()); - } - CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().relative(direction.getOpposite())); - ItemStack bukkitStack; - if (hand == Hand.MAIN_HAND) { - bukkitStack = player.getInventory().getItemInMainHand(); - } else { - bukkitStack = player.getInventory().getItemInOffHand(); + if (!event.getWorld().isClientSide() && event.getWorld() instanceof ServerWorld) { + if (entity instanceof ServerPlayerEntity) { + ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity; + Player player = ((CraftServer) Bukkit.getServer()).getPlayer(playerEntity); + Direction direction = event.getPlaceEventDirection(); + if (direction != null) { + Hand hand = event.getPlaceEventHand(); + List placedBlocks = new ArrayList<>(event.getReplacedBlockSnapshots().size()); + for (BlockSnapshot snapshot : event.getReplacedBlockSnapshots()) { + placedBlocks.add(CraftCustomSnapshot.fromBlockSnapshot(snapshot, true).getState()); + } + CraftBlock againstBlock = CraftBlock.at(event.getWorld(), event.getPos().relative(direction.getOpposite())); + ItemStack bukkitStack; + if (hand == Hand.MAIN_HAND) { + bukkitStack = player.getInventory().getItemInMainHand(); + } else { + bukkitStack = player.getInventory().getItemInOffHand(); + } + BlockPlaceEvent placeEvent = new BlockMultiPlaceEvent(placedBlocks, againstBlock, bukkitStack, player, !event.isCanceled()); + placeEvent.setCancelled(event.isCanceled()); + Bukkit.getPluginManager().callEvent(placeEvent); + event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild()); } - BlockPlaceEvent placeEvent = new BlockMultiPlaceEvent(placedBlocks, againstBlock, bukkitStack, player, !event.isCanceled()); - placeEvent.setCancelled(event.isCanceled()); - Bukkit.getPluginManager().callEvent(placeEvent); - event.setCanceled(placeEvent.isCancelled() || !placeEvent.canBuild()); } } } diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index 84bc0a84ae..452fe3a242 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -625,7 +625,6 @@ public static int onBlockBreakEvent(World world, GameType gameType, ServerPlayer public static ActionResultType onPlaceItemIntoWorld(@Nonnull ItemUseContext context) { - EntityPlaceEvent.hand = context.getHand(); ItemStack itemstack = context.getItemInHand(); World world = context.getLevel(); @@ -668,15 +667,16 @@ public static ActionResultType onPlaceItemIntoWorld(@Nonnull ItemUseContext cont itemstack.setTag(nbt); Direction side = context.getClickedFace(); + Hand hand = context.getHand(); // Mohist boolean eventResult = false; if (blockSnapshots.size() > 1) { - eventResult = ForgeEventFactory.onMultiBlockPlace(player, blockSnapshots, side); + eventResult = ForgeEventFactory.onMultiBlockPlace(player, blockSnapshots, side, hand); // Mohist add hand } else if (blockSnapshots.size() == 1) { - eventResult = ForgeEventFactory.onBlockPlace(player, blockSnapshots.get(0), side); + eventResult = ForgeEventFactory.onBlockPlace(player, blockSnapshots.get(0), side, hand); // Mohist add hand } if (eventResult) @@ -709,7 +709,6 @@ else if (blockSnapshots.size() == 1) } } world.capturedBlockSnapshots.clear(); - EntityPlaceEvent.hand = Hand.MAIN_HAND; return ret; } diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 2d4259875b..38496eed7a 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -149,11 +149,29 @@ public static boolean onMultiBlockPlace(@Nullable Entity entity, List blockSnapshots, Direction direction, Hand hand) + { + BlockSnapshot snap = blockSnapshots.get(0); + BlockState placedAgainst = snap.getWorld().getBlockState(snap.getPos().relative(direction.getOpposite())); + EntityMultiPlaceEvent event = new EntityMultiPlaceEvent(blockSnapshots, placedAgainst, entity); + event.setPlaceEventDirection(direction); // Mohist + event.setPlaceEventHand(hand); // Mohist + return MinecraftForge.EVENT_BUS.post(event); + } + public static boolean onBlockPlace(@Nullable Entity entity, @Nonnull BlockSnapshot blockSnapshot, @Nonnull Direction direction) { BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(direction.getOpposite())); EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, entity); - event.direction = direction; + return MinecraftForge.EVENT_BUS.post(event); + } + + public static boolean onBlockPlace(@Nullable Entity entity, @Nonnull BlockSnapshot blockSnapshot, @Nonnull Direction direction, Hand hand) + { + BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(direction.getOpposite())); + EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, entity); + event.setPlaceEventDirection(direction); // Mohist + event.setPlaceEventHand(hand); // Mohist return MinecraftForge.EVENT_BUS.post(event); } diff --git a/src/main/java/net/minecraftforge/event/world/BlockEvent.java b/src/main/java/net/minecraftforge/event/world/BlockEvent.java index f30c2a7e68..46589ca042 100644 --- a/src/main/java/net/minecraftforge/event/world/BlockEvent.java +++ b/src/main/java/net/minecraftforge/event/world/BlockEvent.java @@ -132,8 +132,8 @@ public static class EntityPlaceEvent extends BlockEvent private final BlockState placedBlock; private final BlockState placedAgainst; // Mohist start - public static Direction direction = null; - public static Hand hand = null; + public static Direction placeEventDirection = null; + public static Hand placeEventHand = Hand.MAIN_HAND; // Mohist end public EntityPlaceEvent(@Nonnull BlockSnapshot blockSnapshot, @Nonnull BlockState placedAgainst, @Nullable Entity entity) @@ -155,6 +155,20 @@ public EntityPlaceEvent(@Nonnull BlockSnapshot blockSnapshot, @Nonnull BlockStat public BlockSnapshot getBlockSnapshot() { return blockSnapshot; } public BlockState getPlacedBlock() { return placedBlock; } public BlockState getPlacedAgainst() { return placedAgainst; } + // Mohist start + public void setPlaceEventDirection(Direction placeEventDirection) { + this.placeEventDirection = placeEventDirection; + } + public Direction getPlaceEventDirection() { + return placeEventDirection; + } + public void setPlaceEventHand(Hand placeEventHand) { + this.placeEventHand = placeEventHand; + } + public Hand getPlaceEventHand() { + return placeEventHand; + } + // Mohist end } /**