diff --git a/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java b/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java index 23f92078c..dcd42baa4 100644 --- a/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java +++ b/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java @@ -366,8 +366,18 @@ public boolean isCopperChest(Material material) { return false; } + @Override + public boolean isShelf(Material material){ + return false; + } + @Override public Set copperChestMaterials() { return EMPTY_SET; } + + @Override + public Set shelfMaterials() { + return EMPTY_SET; + } } diff --git a/src/main/java/net/coreprotect/bukkit/BukkitInterface.java b/src/main/java/net/coreprotect/bukkit/BukkitInterface.java index c66d7fcbb..9ccebed8e 100644 --- a/src/main/java/net/coreprotect/bukkit/BukkitInterface.java +++ b/src/main/java/net/coreprotect/bukkit/BukkitInterface.java @@ -132,6 +132,17 @@ public interface BukkitInterface { */ boolean isChiseledBookshelf(Material material); + + /** + * Checks if a material is a shelf of any wood kind. + * + * @param material + * The material to check + * @return true if the material is a shelf, false otherwise + */ + boolean isShelf(Material material); + + /** * Checks if a material is a bookshelf book. * @@ -441,4 +452,6 @@ public interface BukkitInterface { Set copperChestMaterials(); + Set shelfMaterials(); + } diff --git a/src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java b/src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java index 4d52be823..4d0e2521d 100644 --- a/src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java +++ b/src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java @@ -23,9 +23,10 @@ * - Registry handling for named objects * - Updated interaction blocks */ -public class Bukkit_v1_21 extends Bukkit_v1_20 implements BukkitInterface { +public class Bukkit_v1_21 extends Bukkit_v1_20 { public static Set COPPER_CHESTS = new HashSet<>(Arrays.asList()); + public static Set SHELVES = new HashSet<>(Arrays.asList()); /** * Initializes the Bukkit_v1_21 adapter with 1.21-specific block groups and mappings. @@ -37,6 +38,7 @@ public Bukkit_v1_21() { BlockGroup.INTERACT_BLOCKS.addAll(copperChestMaterials()); BlockGroup.CONTAINERS.addAll(copperChestMaterials()); BlockGroup.UPDATE_STATE.addAll(copperChestMaterials()); + BlockGroup.CONTAINERS.addAll(shelfMaterials()); } /** @@ -111,6 +113,7 @@ public Object getRegistryKey(Object value) { return ((Keyed) value).getKey().toString(); } + /** * Gets a registry value from a key string and class. * Used for deserializing registry objects. @@ -177,6 +180,11 @@ public boolean isCopperChest(Material material) { return false; } + @Override + public boolean isShelf(Material material) { + return SHELVES.contains(material); + } + @Override public Set copperChestMaterials() { if (COPPER_CHESTS.isEmpty()) { @@ -198,4 +206,16 @@ public Set copperChestMaterials() { return COPPER_CHESTS; } + + @Override + public Set shelfMaterials() { + if (SHELVES.isEmpty()) { + Material shelf = Material.getMaterial("OAK_SHELF"); + if (shelf != null) { + SHELVES.addAll(Tag.WOODEN_SHELVES.getValues()); + } + } + + return SHELVES; + } } diff --git a/src/main/java/net/coreprotect/listener/player/PlayerInteractListener.java b/src/main/java/net/coreprotect/listener/player/PlayerInteractListener.java index 680aa6181..9f43ff5ea 100755 --- a/src/main/java/net/coreprotect/listener/player/PlayerInteractListener.java +++ b/src/main/java/net/coreprotect/listener/player/PlayerInteractListener.java @@ -18,12 +18,14 @@ import org.bukkit.block.Sign; import org.bukkit.block.data.Bisected; import org.bukkit.block.data.Bisected.Half; +import org.bukkit.block.data.SideChaining.ChainPart; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Lightable; import org.bukkit.block.data.Waterlogged; import org.bukkit.block.data.type.Bed; import org.bukkit.block.data.type.Bed.Part; import org.bukkit.block.data.type.Cake; +import org.bukkit.block.data.type.Shelf; import org.bukkit.entity.EnderCrystal; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -37,6 +39,7 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; import net.coreprotect.CoreProtect; import net.coreprotect.bukkit.BukkitAdapter; @@ -465,6 +468,49 @@ else if (event.getHand().equals(EquipmentSlot.OFF_HAND) && offHand != null && Bu InventoryChangeListener.inventoryTransaction(player.getName(), blockState.getLocation(), null); } } + } else if (BukkitAdapter.ADAPTER.isShelf(type) ){ + BlockData blockState = block.getBlockData(); + if (blockState instanceof Shelf){ + Shelf shelf = (Shelf) blockState; + + // ignore clicking on the back face + if (event.getBlockFace() != shelf.getFacing()){ + return; + } + + if (shelf.getSideChain() == ChainPart.UNCONNECTED){ + InventoryChangeListener.inventoryTransaction(player.getName(), block.getLocation(), null); + } else { + Block center = block; + Vector direction = shelf.getFacing().getDirection(); + + if (shelf.getSideChain() == ChainPart.LEFT){ + center = center.getRelative(direction.getBlockZ(), 0, -direction.getBlockX()); + } else if (shelf.getSideChain() == ChainPart.RIGHT){ + center = center.getRelative(-direction.getBlockZ(), 0, direction.getBlockX()); + } + + BlockData centerBlockData = center.getBlockData(); + if (centerBlockData instanceof Shelf){ + // log center + InventoryChangeListener.inventoryTransaction(player.getName(), center.getLocation(), null); + + if (((Shelf)centerBlockData).getSideChain() != ChainPart.CENTER){ + // if it's not the center it's just a chain of 2 + InventoryChangeListener.inventoryTransaction(player.getName(), block.getLocation(), null); + } else { + Block left = center.getRelative(-direction.getBlockZ(), 0, direction.getBlockX()); + InventoryChangeListener.inventoryTransaction(player.getName(), left.getLocation(), null); + + Block right = center.getRelative(direction.getBlockZ(), 0, -direction.getBlockX()); + InventoryChangeListener.inventoryTransaction(player.getName(), right.getLocation(), null); + } + } else { + // fallback if invalid block is found just log clicked shelf + InventoryChangeListener.inventoryTransaction(player.getName(), block.getLocation(), null); + } + } + } } else if (BukkitAdapter.ADAPTER.isDecoratedPot(type)) { BlockState blockState = block.getState();