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 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 @@ public final class HopperPullListener {
}
}
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,50 +38,12 @@ public final class HopperPullListener {
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) {
boolean addedInventory = Util.canAddContainer(sourceContainer, movedItem, sourceHolder.getInventory().getMaxStackSize());
if (!addedInventory) {
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) {
Set<ItemStack> movedItems = new HashSet<>();
ItemStack[] destinationContents = destinationHolder.getInventory().getContents();
@ -104,6 +60,7 @@ public final class HopperPullListener {
ConfigHandler.hopperAbort.remove(loggingChestId);
}
boolean hopperTransactions = Config.getConfig(location.getWorld()).HOPPER_TRANSACTIONS;
if (!hopperTransactions) {
List<Object> list = ConfigHandler.transactingChest.get(location.getWorld().getUID().toString() + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ());
if (list != null) {

View File

@ -6,8 +6,6 @@ import java.util.List;
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 @@ public final class HopperPushListener {
}
}
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,27 +38,11 @@ public final class HopperPushListener {
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);
boolean addedInventory = Util.canAddContainer(destinationContainer, movedItem, destinationHolder.getInventory().getMaxStackSize());
if (!addedInventory) {
abort = true;
}
}
if (abort) {
Set<ItemStack> movedItems = new HashSet<>();

View File

@ -203,7 +203,7 @@ public final class InventoryChangeListener extends Queue implements Listener {
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;
}

View File

@ -680,6 +680,30 @@ public class Util extends Queue {
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();

View File

@ -120,7 +120,7 @@ public class ItemMetaHandler {
List<Map<String, Object>> list = 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();
if (itemMeta.hasAttributeModifiers()) {