diff --git a/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java b/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java index 23f9207..dcd42ba 100644 --- a/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java +++ b/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java @@ -366,8 +366,18 @@ public class BukkitAdapter implements BukkitInterface { 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 c66d7fc..9ccebed 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 4d52be8..4d0e252 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 @@ import net.coreprotect.model.BlockGroup; * - 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 class Bukkit_v1_21 extends Bukkit_v1_20 implements BukkitInterface { BlockGroup.INTERACT_BLOCKS.addAll(copperChestMaterials()); BlockGroup.CONTAINERS.addAll(copperChestMaterials()); BlockGroup.UPDATE_STATE.addAll(copperChestMaterials()); + BlockGroup.CONTAINERS.addAll(shelfMaterials()); } /** @@ -111,6 +113,7 @@ public class Bukkit_v1_21 extends Bukkit_v1_20 implements BukkitInterface { 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 class Bukkit_v1_21 extends Bukkit_v1_20 implements BukkitInterface { 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 class Bukkit_v1_21 extends Bukkit_v1_20 implements BukkitInterface { 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 680aa61..9f43ff5 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.Jukebox; 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.BlockInventoryHolder; 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 @@ public final class PlayerInteractListener extends Queue implements Listener { 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();