Fixed hopper transactions not always being logged (fixes #490)

This commit is contained in:
Intelli 2024-01-24 17:44:18 -07:00
parent f7fea2b298
commit af1d4402eb
5 changed files with 35 additions and 76 deletions

View File

@ -6,8 +6,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -30,11 +28,7 @@ public final class HopperPullListener {
} }
} }
ItemStack[] containerState = null; ItemStack[] sourceContainer = Util.getContainerState(sourceHolder.getInventory().getContents());
if (!ConfigHandler.isPaper) {
containerState = Util.getContainerState(sourceHolder.getInventory().getContents());
}
ItemStack[] sourceContainer = containerState;
ItemStack movedItem = item.clone(); ItemStack movedItem = item.clone();
final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet(); final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
@ -44,48 +38,10 @@ public final class HopperPullListener {
return; return;
} }
boolean hopperTransactions = Config.getConfig(location.getWorld()).HOPPER_TRANSACTIONS;
int itemHash = Util.getItemStackHashCode(item);
boolean abort = false; boolean abort = false;
boolean addedInventory = Util.canAddContainer(sourceContainer, movedItem, sourceHolder.getInventory().getMaxStackSize());
if (ConfigHandler.isPaper) { if (!addedInventory) {
for (ItemStack itemStack : sourceHolder.getInventory().getContents()) { abort = true;
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;
}
} }
if (abort) { if (abort) {
@ -104,6 +60,7 @@ public final class HopperPullListener {
ConfigHandler.hopperAbort.remove(loggingChestId); ConfigHandler.hopperAbort.remove(loggingChestId);
} }
boolean hopperTransactions = Config.getConfig(location.getWorld()).HOPPER_TRANSACTIONS;
if (!hopperTransactions) { if (!hopperTransactions) {
List<Object> list = ConfigHandler.transactingChest.get(location.getWorld().getUID().toString() + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ()); List<Object> list = ConfigHandler.transactingChest.get(location.getWorld().getUID().toString() + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ());
if (list != null) { if (list != null) {

View File

@ -6,8 +6,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -30,11 +28,7 @@ public final class HopperPushListener {
} }
} }
ItemStack[] containerState = null; ItemStack[] destinationContainer = Util.getContainerState(destinationHolder.getInventory().getContents());
if (!ConfigHandler.isPaper) {
containerState = Util.getContainerState(destinationHolder.getInventory().getContents());
}
ItemStack[] destinationContainer = containerState;
ItemStack movedItem = item.clone(); ItemStack movedItem = item.clone();
final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet(); final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
@ -44,26 +38,10 @@ public final class HopperPushListener {
return; return;
} }
int itemHash = Util.getItemStackHashCode(item);
boolean abort = false; boolean abort = false;
boolean addedInventory = Util.canAddContainer(destinationContainer, movedItem, destinationHolder.getInventory().getMaxStackSize());
if (ConfigHandler.isPaper) { if (!addedInventory) {
for (ItemStack itemStack : sourceHolder.getInventory().getContents()) { abort = true;
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;
}
} }
if (abort) { if (abort) {

View File

@ -203,7 +203,7 @@ public final class InventoryChangeListener extends Queue implements Listener {
return false; return false;
} }
private static void onInventoryInteractAsync(Player player, Inventory inventory, boolean enderChest) { static void onInventoryInteractAsync(Player player, Inventory inventory, boolean enderChest) {
if (inventory == null) { if (inventory == null) {
return; return;
} }

View File

@ -680,6 +680,30 @@ public class Util extends Queue {
return false; 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) { public static int getArtId(String name, boolean internal) {
int id = -1; int id = -1;
name = name.toLowerCase(Locale.ROOT).trim(); name = name.toLowerCase(Locale.ROOT).trim();

View File

@ -120,7 +120,7 @@ public class ItemMetaHandler {
List<Map<String, Object>> list = new ArrayList<>(); List<Map<String, Object>> list = new ArrayList<>();
List<Object> modifiers = new ArrayList<>(); List<Object> modifiers = new ArrayList<>();
if (item.hasItemMeta() && item.getItemMeta() != null) { if (item != null && item.hasItemMeta() && item.getItemMeta() != null) {
ItemMeta itemMeta = item.getItemMeta().clone(); ItemMeta itemMeta = item.getItemMeta().clone();
if (itemMeta.hasAttributeModifiers()) { if (itemMeta.hasAttributeModifiers()) {