From af1d4402eb186c99ce2a1ce0b7f1ccf8afa19e32 Mon Sep 17 00:00:00 2001 From: Intelli Date: Wed, 24 Jan 2024 17:44:18 -0700 Subject: [PATCH] Fixed hopper transactions not always being logged (fixes #490) --- .../listener/player/HopperPullListener.java | 53 ++----------------- .../listener/player/HopperPushListener.java | 30 ++--------- .../player/InventoryChangeListener.java | 2 +- .../java/net/coreprotect/utility/Util.java | 24 +++++++++ .../utility/serialize/ItemMetaHandler.java | 2 +- 5 files changed, 35 insertions(+), 76 deletions(-) diff --git a/src/main/java/net/coreprotect/listener/player/HopperPullListener.java b/src/main/java/net/coreprotect/listener/player/HopperPullListener.java index d1dedda2..2c9b143e 100644 --- a/src/main/java/net/coreprotect/listener/player/HopperPullListener.java +++ b/src/main/java/net/coreprotect/listener/player/HopperPullListener.java @@ -6,8 +6,6 @@ import java.util.Set; import org.bukkit.Location; -import org.bukkit.inventory.BrewerInventory; -import org.bukkit.inventory.FurnaceInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -30,11 +28,7 @@ static void processHopperPull(Location location, InventoryHolder sourceHolder, I } } - ItemStack[] containerState = null; - if (!ConfigHandler.isPaper) { - containerState = Util.getContainerState(sourceHolder.getInventory().getContents()); - } - ItemStack[] sourceContainer = containerState; + ItemStack[] sourceContainer = Util.getContainerState(sourceHolder.getInventory().getContents()); ItemStack movedItem = item.clone(); final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet(); @@ -44,48 +38,10 @@ static void processHopperPull(Location location, InventoryHolder sourceHolder, I return; } - boolean hopperTransactions = Config.getConfig(location.getWorld()).HOPPER_TRANSACTIONS; - int itemHash = Util.getItemStackHashCode(item); boolean abort = false; - - if (ConfigHandler.isPaper) { - for (ItemStack itemStack : sourceHolder.getInventory().getContents()) { - if (itemStack != null && Util.getItemStackHashCode(itemStack) == itemHash) { - if (itemHash != Util.getItemStackHashCode(movedItem) || destinationHolder.getInventory().firstEmpty() == -1 || destinationHolder.getInventory() instanceof BrewerInventory || destinationHolder.getInventory() instanceof FurnaceInventory) { - abort = true; - } - - break; - } - } - - /* - for (ItemStack itemStack : sourceHolder.getInventory().getContents()) { - if (itemStack != null && Util.getItemStackHashCode(itemStack) == itemHash) { - abort = true; - break; - } - } - - if (abort) { - for (ItemStack itemStack : destinationHolder.getInventory().getContents()) { - if (itemStack != null && Util.getItemStackHashCode(itemStack) == Util.getItemStackHashCode(movedItem)) { - if (itemHash == Util.getItemStackHashCode(itemStack) && destinationHolder.getInventory().firstEmpty() > -1) { - abort = false; - } - - break; - } - } - } - */ - } - else { - ItemStack[] sourceContents = sourceHolder.getInventory().getContents(); - boolean addedInventory = Util.addedContainer(sourceContainer, sourceContents); - if (addedInventory) { - abort = true; - } + boolean addedInventory = Util.canAddContainer(sourceContainer, movedItem, sourceHolder.getInventory().getMaxStackSize()); + if (!addedInventory) { + abort = true; } if (abort) { @@ -104,6 +60,7 @@ static void processHopperPull(Location location, InventoryHolder sourceHolder, I ConfigHandler.hopperAbort.remove(loggingChestId); } + boolean hopperTransactions = Config.getConfig(location.getWorld()).HOPPER_TRANSACTIONS; if (!hopperTransactions) { List list = ConfigHandler.transactingChest.get(location.getWorld().getUID().toString() + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ()); if (list != null) { diff --git a/src/main/java/net/coreprotect/listener/player/HopperPushListener.java b/src/main/java/net/coreprotect/listener/player/HopperPushListener.java index 0acaa4c7..4dbce18c 100644 --- a/src/main/java/net/coreprotect/listener/player/HopperPushListener.java +++ b/src/main/java/net/coreprotect/listener/player/HopperPushListener.java @@ -6,8 +6,6 @@ import java.util.Set; import org.bukkit.Location; -import org.bukkit.inventory.BrewerInventory; -import org.bukkit.inventory.FurnaceInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -30,11 +28,7 @@ static void processHopperPush(Location location, InventoryHolder sourceHolder, I } } - ItemStack[] containerState = null; - if (!ConfigHandler.isPaper) { - containerState = Util.getContainerState(destinationHolder.getInventory().getContents()); - } - ItemStack[] destinationContainer = containerState; + ItemStack[] destinationContainer = Util.getContainerState(destinationHolder.getInventory().getContents()); ItemStack movedItem = item.clone(); final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet(); @@ -44,26 +38,10 @@ static void processHopperPush(Location location, InventoryHolder sourceHolder, I return; } - int itemHash = Util.getItemStackHashCode(item); boolean abort = false; - - if (ConfigHandler.isPaper) { - for (ItemStack itemStack : sourceHolder.getInventory().getContents()) { - if (itemStack != null && Util.getItemStackHashCode(itemStack) == itemHash) { - if (itemHash != Util.getItemStackHashCode(movedItem) || destinationHolder.getInventory().firstEmpty() == -1 || destinationHolder.getInventory() instanceof BrewerInventory || destinationHolder.getInventory() instanceof FurnaceInventory) { - abort = true; - } - - break; - } - } - } - else { - ItemStack[] destinationContents = destinationHolder.getInventory().getContents(); - boolean addedInventory = Util.addedContainer(destinationContainer, destinationContents); - if (!addedInventory) { - abort = true; - } + boolean addedInventory = Util.canAddContainer(destinationContainer, movedItem, destinationHolder.getInventory().getMaxStackSize()); + if (!addedInventory) { + abort = true; } if (abort) { diff --git a/src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java b/src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java index 4cb79061..fac8ac4b 100644 --- a/src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java +++ b/src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java @@ -203,7 +203,7 @@ else if (inventoryHolder instanceof DoubleChest) { return false; } - private static void onInventoryInteractAsync(Player player, Inventory inventory, boolean enderChest) { + static void onInventoryInteractAsync(Player player, Inventory inventory, boolean enderChest) { if (inventory == null) { return; } diff --git a/src/main/java/net/coreprotect/utility/Util.java b/src/main/java/net/coreprotect/utility/Util.java index 03c5d1b2..b0e3f853 100755 --- a/src/main/java/net/coreprotect/utility/Util.java +++ b/src/main/java/net/coreprotect/utility/Util.java @@ -680,6 +680,30 @@ public static boolean addedContainer(ItemStack[] oldContainer, ItemStack[] newCo return false; } + /* return true if item can be added to container */ + public static boolean canAddContainer(ItemStack[] container, ItemStack item, int forceMaxStack) { + for (ItemStack containerItem : container) { + if (containerItem == null || containerItem.getType() == Material.AIR) { + return true; + } + + int maxStackSize = containerItem.getMaxStackSize(); + if (forceMaxStack > 0 && (forceMaxStack < maxStackSize || maxStackSize == -1)) { + maxStackSize = forceMaxStack; + } + + if (maxStackSize == -1) { + maxStackSize = 1; + } + + if (containerItem.isSimilar(item) && containerItem.getAmount() < maxStackSize) { + return true; + } + } + + return false; + } + public static int getArtId(String name, boolean internal) { int id = -1; name = name.toLowerCase(Locale.ROOT).trim(); diff --git a/src/main/java/net/coreprotect/utility/serialize/ItemMetaHandler.java b/src/main/java/net/coreprotect/utility/serialize/ItemMetaHandler.java index 33f66b74..34127c86 100644 --- a/src/main/java/net/coreprotect/utility/serialize/ItemMetaHandler.java +++ b/src/main/java/net/coreprotect/utility/serialize/ItemMetaHandler.java @@ -120,7 +120,7 @@ public static List>> seralize(ItemStack item, Material List> list = new ArrayList<>(); List modifiers = new ArrayList<>(); - if (item.hasItemMeta() && item.getItemMeta() != null) { + if (item != null && item.hasItemMeta() && item.getItemMeta() != null) { ItemMeta itemMeta = item.getItemMeta().clone(); if (itemMeta.hasAttributeModifiers()) {