From c014901b7a4741c3a88290c2a08537bf6fe9dccd Mon Sep 17 00:00:00 2001 From: Frank van der Heijden <22407829+FrankHeijden@users.noreply.github.com> Date: Thu, 10 Dec 2020 20:13:44 +0100 Subject: [PATCH] Prevent doublechests forming on claim boundary (#1054) --- .../GriefPrevention/BlockEventHandler.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/BlockEventHandler.java b/src/main/java/me/ryanhamshire/GriefPrevention/BlockEventHandler.java index 76252fe..868bad2 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/BlockEventHandler.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/BlockEventHandler.java @@ -34,6 +34,7 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Hopper; import org.bukkit.block.PistonMoveReaction; import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.type.Chest; import org.bukkit.block.data.type.Dispenser; import org.bukkit.entity.Fireball; import org.bukkit.entity.Item; @@ -287,6 +288,10 @@ public class BlockEventHandler implements Listener //if the block is being placed within or under an existing claim PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId()); Claim claim = this.dataStore.getClaimAt(block.getLocation(), true, playerData.lastClaim); + + //If block is a chest, don't allow a DoubleChest to form across a claim boundary + denyConnectingDoubleChestsAcrossClaimBoundary(claim, block, player); + if (claim != null) { playerData.lastClaim = claim; @@ -491,6 +496,49 @@ public class BlockEventHandler implements Listener return false; } + private static final BlockFace[] HORIZONTAL_DIRECTIONS = new BlockFace[] { + BlockFace.NORTH, + BlockFace.EAST, + BlockFace.SOUTH, + BlockFace.WEST + }; + private void denyConnectingDoubleChestsAcrossClaimBoundary(Claim claim, Block block, Player player) + { + UUID claimOwner = null; + if (claim != null) + claimOwner = claim.getOwnerID(); + + // Check for double chests placed just outside the claim boundary + if (block.getBlockData() instanceof Chest) + { + for (BlockFace face : HORIZONTAL_DIRECTIONS) + { + Block relative = block.getRelative(face); + if (!(relative.getBlockData() instanceof Chest)) continue; + + Claim relativeClaim = this.dataStore.getClaimAt(relative.getLocation(), true, claim); + UUID relativeClaimOwner = relativeClaim == null ? null : relativeClaim.getOwnerID(); + + // Chests outside claims should connect (both null) + // and chests inside the same claim should connect (equal) + if (Objects.equals(claimOwner, relativeClaimOwner)) break; + + // Change both chests to singular chests + Chest chest = (Chest) block.getBlockData(); + chest.setType(Chest.Type.SINGLE); + block.setBlockData(chest); + + Chest relativeChest = (Chest) relative.getBlockData(); + relativeChest.setType(Chest.Type.SINGLE); + relative.setBlockData(relativeChest); + + // Resend relative chest block to prevent visual bug + player.sendBlockChange(relative.getLocation(), relativeChest); + break; + } + } + } + // Prevent pistons pushing blocks into or out of claims. @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onBlockPistonExtend(BlockPistonExtendEvent event)