From a27934b3d49f4c426741e55869efcd9b64438baf Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Fri, 25 Jun 2021 16:40:24 +0200 Subject: [PATCH] Implement double-chest convenience functions --- .../w1zzrd/spigot/wizcompat/block/Chests.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/dev/w1zzrd/spigot/wizcompat/block/Chests.java diff --git a/src/dev/w1zzrd/spigot/wizcompat/block/Chests.java b/src/dev/w1zzrd/spigot/wizcompat/block/Chests.java new file mode 100644 index 0000000..f7710a3 --- /dev/null +++ b/src/dev/w1zzrd/spigot/wizcompat/block/Chests.java @@ -0,0 +1,34 @@ +package dev.w1zzrd.spigot.wizcompat.block; + +import org.bukkit.block.Block; +import org.bukkit.block.Chest; +import org.bukkit.block.DoubleChest; +import org.bukkit.inventory.InventoryHolder; + +import java.util.Objects; + +public final class Chests { + private Chests() { throw new UnsupportedOperationException("Functional class"); } + + public static InventoryHolder getChestInventoryHolder(final Chest chest) { + return Objects.requireNonNull(chest.getBlockInventory().getHolder()).getInventory().getHolder(); + } + + public static boolean isDoubleChest(final Block block) { + return block.getState() instanceof Chest && getChestInventoryHolder((Chest) block.getState()) instanceof DoubleChest; + } + + public static Block getLeftChest(final Chest chest) { + if (isDoubleChest(chest.getBlock())) + return ((Chest) Objects.requireNonNull(((DoubleChest) getChestInventoryHolder(chest)).getLeftSide())).getBlock(); + else + return chest.getBlock(); + } + + public static Block getRightChest(final Chest chest) { + if (isDoubleChest(chest.getBlock())) + return ((Chest) Objects.requireNonNull(((DoubleChest) getChestInventoryHolder(chest)).getRightSide())).getBlock(); + else + return chest.getBlock(); + } +}