From 63c3d91ece867611016e4c453dd705c1291b27a3 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Fri, 12 Apr 2024 21:36:32 +0800 Subject: [PATCH] Improve SetBlockPacket logic --- .../axiom/packet/SetBlockPacketListener.java | 79 +++++++++++++------ 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java index e424156..91bb155 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java @@ -16,8 +16,7 @@ import net.minecraft.world.entity.ai.village.poi.PoiTypes; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.BlockPlaceContext; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.EntityBlock; +import net.minecraft.world.level.block.*; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.chunk.LevelChunk; @@ -25,6 +24,8 @@ import net.minecraft.world.level.chunk.LevelChunkSection; import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.level.lighting.LightEngine; import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.HitResult; +import net.minecraft.world.phys.Vec3; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; @@ -89,26 +90,29 @@ public class SetBlockPacketListener implements PluginMessageListener { ServerPlayer player = ((CraftPlayer)bukkitPlayer).getHandle(); - org.bukkit.inventory.ItemStack heldItem; - if (hand == InteractionHand.MAIN_HAND) { - heldItem = bukkitPlayer.getInventory().getItemInMainHand(); - } else { - heldItem = bukkitPlayer.getInventory().getItemInOffHand(); + if (sequenceId >= 0) { + player.connection.ackBlockChangesUpTo(sequenceId); } - org.bukkit.block.Block blockClicked = bukkitPlayer.getWorld().getBlockAt(blockHit.getBlockPos().getX(), - blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ()); - - BlockFace blockFace = CraftBlock.notchToBlockFace(blockHit.getDirection()); - - // Call interact event - PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(bukkitPlayer, - breaking ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK, heldItem, blockClicked, blockFace); - if (!playerInteractEvent.callEvent()) { - if (sequenceId >= 0) { - player.connection.ackBlockChangesUpTo(sequenceId); + if (!blockHit.getLocation().equals(Vec3.ZERO)) { + org.bukkit.inventory.ItemStack heldItem; + if (hand == InteractionHand.MAIN_HAND) { + heldItem = bukkitPlayer.getInventory().getItemInMainHand(); + } else { + heldItem = bukkitPlayer.getInventory().getItemInOffHand(); + } + + org.bukkit.block.Block blockClicked = bukkitPlayer.getWorld().getBlockAt(blockHit.getBlockPos().getX(), + blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ()); + + BlockFace blockFace = CraftBlock.notchToBlockFace(blockHit.getDirection()); + + // Call interact event + PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(bukkitPlayer, + breaking ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK, heldItem, blockClicked, blockFace); + if (!playerInteractEvent.callEvent()) { + return; } - return; } CraftWorld world = player.level().getWorld(); @@ -124,6 +128,11 @@ public class SetBlockPacketListener implements PluginMessageListener { BlockPos blockPos = entry.getKey(); BlockState blockState = entry.getValue(); + // Disallow in unloaded chunks + if (!player.level().isLoaded(blockPos)) { + continue; + } + // Check PlotSquared if (blockState.isAir()) { if (!PlotSquaredIntegration.canBreakBlock(bukkitPlayer, world.getBlockAt(blockPos.getX(), blockPos.getY(), blockPos.getZ()))) { @@ -144,6 +153,11 @@ public class SetBlockPacketListener implements PluginMessageListener { BlockPos blockPos = entry.getKey(); BlockState blockState = entry.getValue(); + // Disallow in unloaded chunks + if (!player.level().isLoaded(blockPos)) { + continue; + } + // Check PlotSquared if (blockState.isAir()) { if (!PlotSquaredIntegration.canBreakBlock(bukkitPlayer, world.getBlockAt(blockPos.getX(), blockPos.getY(), blockPos.getZ()))) { @@ -254,15 +268,32 @@ public class SetBlockPacketListener implements PluginMessageListener { if (!breaking) { BlockPos clickedPos = blockPlaceContext.getClickedPos(); + + // Disallow in unloaded chunks + if (!player.level().isLoaded(clickedPos)) { + return; + } + + BlockState desiredBlockState = blocks.get(clickedPos); + BlockState actualBlockState = player.level().getBlockState(clickedPos); + Block actualBlock = actualBlockState.getBlock(); + + // Ensure block is correct + if (desiredBlockState == null || desiredBlockState.isAir() || actualBlockState.isAir()) return; + if (desiredBlockState.getBlock() != actualBlock) return; + + // Check plot squared + if (!PlotSquaredIntegration.canPlaceBlock(bukkitPlayer, new Location(world, clickedPos.getX(), clickedPos.getY(), clickedPos.getZ()))) { + return; + } + ItemStack inHand = player.getItemInHand(hand); - BlockState blockState = player.level().getBlockState(clickedPos); BlockItem.updateCustomBlockEntityTag(player.level(), player, clickedPos, inHand); - blockState.getBlock().setPlacedBy(player.level(), clickedPos, blockState, player, inHand); - } - if (sequenceId >= 0) { - player.connection.ackBlockChangesUpTo(sequenceId); + if (!(actualBlock instanceof BedBlock) && !(actualBlock instanceof DoublePlantBlock) && !(actualBlock instanceof DoorBlock)) { + actualBlock.setPlacedBy(player.level(), clickedPos, actualBlockState, player, inHand); + } } }