From 5a00cf388a62b06c386b7917c885992a8b16a9a5 Mon Sep 17 00:00:00 2001 From: Ghost_chu <30802565+Ghost-chu@users.noreply.github.com> Date: Sat, 17 Feb 2024 02:48:42 +0800 Subject: [PATCH] Performance improvements and adding Crafter to the default protection list (#184) * For non-double-boxed cubes, avoid fetch the BlockState, it's too slow * Add crafter to default protectableContainers --- .../impl/event/InteractListener.java | 36 ++++++++++++++++--- src/main/resources/config.yml | 1 + 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/nl/rutgerkok/blocklocker/impl/event/InteractListener.java b/src/main/java/nl/rutgerkok/blocklocker/impl/event/InteractListener.java index b4346b9..96542e7 100644 --- a/src/main/java/nl/rutgerkok/blocklocker/impl/event/InteractListener.java +++ b/src/main/java/nl/rutgerkok/blocklocker/impl/event/InteractListener.java @@ -3,10 +3,7 @@ import java.util.Optional; import java.util.Set; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.Material; -import org.bukkit.Tag; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; @@ -25,6 +22,7 @@ import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.entity.EntityInteractEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.Inventory; @@ -51,6 +49,7 @@ public final class InteractListener extends EventListener { private static Set AUTOPLACE_BLOCK_FACES = ImmutableSet.of(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST, BlockFace.UP); + private static Set COMPLEX_SEARCH_INVENTORY = ImmutableSet.of(InventoryType.CHEST); public InteractListener(BlockLockerPluginImpl plugin) { super(plugin); @@ -131,6 +130,35 @@ private boolean checkAllowed(Player player, Protection protection, boolean click * @return The block, or null. */ private Block getInventoryBlockOrNull(Inventory inventory) { + // Performs a fast Block object fetch for containers with only a single block, avoiding the need to fetch a + // BlockState. When creating a BlockState Spigot will create a snapshot, which is a rather slow process. + + if(!COMPLEX_SEARCH_INVENTORY.contains(inventory.getType())) { + Location inventoryLoc = inventory.getLocation(); + if (inventoryLoc != null) { + return inventoryLoc.getBlock(); + } + return null; // Custom GUI + } + + // Complex search trick for Single Chest + // If this is a single chest, we can do same fast fetch just like above + if(inventory.getType() == InventoryType.CHEST){ + Location inventoryLoc = inventory.getLocation(); + if(inventoryLoc != null){ + Location invBlockLoc = inventoryLoc.clone(); + invBlockLoc.setX(inventoryLoc.getBlockX()); + invBlockLoc.setY(inventoryLoc.getBlockY()); + invBlockLoc.setZ(inventoryLoc.getBlockZ()); + // If the two Locations are the same, it means it is not a double chest; + // The DoubleChest inventory location is similar like 94.5 64 88.5 (.5) + if(inventoryLoc.equals(invBlockLoc)){ + return inventoryLoc.getBlock(); + } + } + } + + // We've exhausted our means, and now we'll have to use the slowest method just like before. InventoryHolder holder = inventory.getHolder(); if (holder instanceof BlockState) { return ((BlockState) holder).getBlock(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7554685..2263d78 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -46,6 +46,7 @@ protectableContainers: - chipped_anvil - cyan_shulker_box - crafting_table +- crafter - damaged_anvil - dispenser - dropper