Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent chests connecting on claim boundary #1054

Merged
merged 7 commits into from Dec 10, 2020
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -82,6 +83,13 @@ public class BlockEventHandler implements Listener

private final EnumSet<Material> trashBlocks;

private static final BlockFace[] HORIZONTAL_DIRECTIONS = new BlockFace[] {
BlockFace.NORTH,
BlockFace.EAST,
BlockFace.SOUTH,
BlockFace.WEST
};

//constructor
public BlockEventHandler(DataStore dataStore)
{
Expand Down Expand Up @@ -285,6 +293,38 @@ public void onBlockPlace(BlockPlaceEvent placeEvent)
//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);
UUID claimOwner = claim == null ? null : 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;
FrankHeijden marked this conversation as resolved.
Show resolved Hide resolved

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;
FrankHeijden marked this conversation as resolved.
Show resolved Hide resolved

// 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;
}
}

if (claim != null)
{
playerData.lastClaim = claim;
Expand Down