Skip to content

Commit

Permalink
Performance improvements and adding Crafter to the default protection…
Browse files Browse the repository at this point in the history
… list (#184)

* For non-double-boxed cubes, avoid fetch the BlockState, it's too slow
* Add crafter to default protectableContainers
  • Loading branch information
Ghost-chu authored Feb 16, 2024
1 parent fd68582 commit 5a00cf3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -51,6 +49,7 @@ public final class InteractListener extends EventListener {

private static Set<BlockFace> AUTOPLACE_BLOCK_FACES = ImmutableSet.of(BlockFace.NORTH, BlockFace.EAST,
BlockFace.SOUTH, BlockFace.WEST, BlockFace.UP);
private static Set<InventoryType> COMPLEX_SEARCH_INVENTORY = ImmutableSet.of(InventoryType.CHEST);

public InteractListener(BlockLockerPluginImpl plugin) {
super(plugin);
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protectableContainers:
- chipped_anvil
- cyan_shulker_box
- crafting_table
- crafter
- damaged_anvil
- dispenser
- dropper
Expand Down

0 comments on commit 5a00cf3

Please sign in to comment.