Improved hopper logging to prevent infinite attempts on failed pushes

E.g. when a hopper is repeatedly trying to push into a smoker, but no items are actually being transferred
This commit is contained in:
Intelli 2024-05-07 14:08:59 -06:00
parent 6023c21ba9
commit 5426e69fb8
4 changed files with 53 additions and 1 deletions

View File

@ -101,6 +101,7 @@ public class ConfigHandler extends Queue {
public static ConcurrentHashMap<String, List<ItemStack>> itemsSell = new ConcurrentHashMap<>(); public static ConcurrentHashMap<String, List<ItemStack>> itemsSell = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, List<ItemStack>> itemsBuy = new ConcurrentHashMap<>(); public static ConcurrentHashMap<String, List<ItemStack>> itemsBuy = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, Object[]> hopperAbort = new ConcurrentHashMap<>(); public static ConcurrentHashMap<String, Object[]> hopperAbort = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, Object[]> hopperSuccess = new ConcurrentHashMap<>();
public static Map<String, List<ItemStack[]>> forceContainer = syncMap(); public static Map<String, List<ItemStack[]>> forceContainer = syncMap();
public static Map<String, Integer> lookupType = syncMap(); public static Map<String, Integer> lookupType = syncMap();
public static Map<String, Object[]> lookupThrottle = syncMap(); public static Map<String, Object[]> lookupThrottle = syncMap();

View File

@ -1,12 +1,16 @@
package net.coreprotect.consumer.process; package net.coreprotect.consumer.process;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
@ -41,6 +45,41 @@ class ContainerTransactionProcess {
ConfigHandler.transactingChest.remove(transactingChestId); ConfigHandler.transactingChest.remove(transactingChestId);
} }
} }
else if (loggingChestId.startsWith("#hopper")) {
if (force_size == 0 && ConfigHandler.oldContainer.get(loggingChestId).size() == 1 && ConfigHandler.transactingChest.get(transactingChestId).isEmpty()) {
int loopCount = ConfigHandler.loggingChest.get(loggingChestId);
int maxInventorySize = (99 * 54);
try {
Inventory checkInventory = (Inventory) inventory;
maxInventorySize = checkInventory.getSize() * checkInventory.getMaxStackSize();
}
catch (Exception e) {
// use default of 5,346
}
if (loopCount > maxInventorySize) {
ItemStack[] destinationContents = null;
ItemStack movedItem = null;
String hopperPush = "#hopper-push." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
Object[] hopperPushData = ConfigHandler.hopperSuccess.remove(hopperPush);
if (hopperPushData != null) {
destinationContents = (ItemStack[]) hopperPushData[0];
movedItem = (ItemStack) hopperPushData[1];
}
if (destinationContents != null) {
Set<ItemStack> movedItems = new HashSet<>();
Object[] lastAbort = ConfigHandler.hopperAbort.get(hopperPush);
if (lastAbort != null && Arrays.equals(destinationContents, (ItemStack[]) lastAbort[1])) {
((Set<?>) lastAbort[0]).forEach(itemStack -> movedItems.add((ItemStack) itemStack));
}
movedItems.add(movedItem);
ConfigHandler.hopperAbort.put(hopperPush, new Object[] { movedItems, destinationContents });
}
}
}
}
} }
inventories.remove(id); inventories.remove(id);
} }

View File

@ -161,6 +161,7 @@ public class ContainerLogger extends Queue {
if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null) { if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null) {
return; return;
} }
boolean success = false;
int slot = 0; int slot = 0;
for (ItemStack item : items) { for (ItemStack item : items) {
if (item != null) { if (item != null) {
@ -190,10 +191,17 @@ public class ContainerLogger extends Queue {
int data = 0; int data = 0;
int amount = item.getAmount(); int amount = item.getAmount();
ContainerStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, typeId, data, amount, metadata, action, 0); ContainerStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, typeId, data, amount, metadata, action, 0);
success = true;
} }
} }
slot++; slot++;
} }
if (success && user.equals("#hopper")) {
String hopperPush = "#hopper-push." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
ConfigHandler.hopperSuccess.remove(hopperPush);
ConfigHandler.hopperAbort.remove(hopperPush);
}
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -19,7 +19,8 @@ import net.coreprotect.utility.Util;
public final class HopperPushListener { public final class HopperPushListener {
static void processHopperPush(Location location, InventoryHolder sourceHolder, InventoryHolder destinationHolder, ItemStack item) { static void processHopperPush(Location location, InventoryHolder sourceHolder, InventoryHolder destinationHolder, ItemStack item) {
String loggingChestId = "#hopper-push." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ(); Location destinationLocation = destinationHolder.getInventory().getLocation();
String loggingChestId = "#hopper-push." + destinationLocation.getBlockX() + "." + destinationLocation.getBlockY() + "." + destinationLocation.getBlockZ();
Object[] lastAbort = ConfigHandler.hopperAbort.get(loggingChestId); Object[] lastAbort = ConfigHandler.hopperAbort.get(loggingChestId);
if (lastAbort != null) { if (lastAbort != null) {
ItemStack[] destinationContents = destinationHolder.getInventory().getContents(); ItemStack[] destinationContents = destinationHolder.getInventory().getContents();
@ -55,6 +56,9 @@ public final class HopperPushListener {
ConfigHandler.hopperAbort.put(loggingChestId, new Object[] { movedItems, Util.getContainerState(destinationContents) }); ConfigHandler.hopperAbort.put(loggingChestId, new Object[] { movedItems, Util.getContainerState(destinationContents) });
return; return;
} }
else {
ConfigHandler.hopperSuccess.put(loggingChestId, new Object[] { destinationContainer, movedItem });
}
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) {