Skip to content

Commit

Permalink
Improved hopper logging to prevent infinite attempts on failed pushes
Browse files Browse the repository at this point in the history
E.g. when a hopper is repeatedly trying to push into a smoker, but no items are actually being transferred
  • Loading branch information
Intelli committed May 7, 2024
1 parent 6023c21 commit 5426e69
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/net/coreprotect/config/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private static <K, V> Map<K, V> syncMap() {
public static ConcurrentHashMap<String, List<ItemStack>> itemsSell = new ConcurrentHashMap<>();
public static ConcurrentHashMap<String, List<ItemStack>> itemsBuy = 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, Integer> lookupType = syncMap();
public static Map<String, Object[]> lookupThrottle = syncMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package net.coreprotect.consumer.process;

import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import net.coreprotect.config.ConfigHandler;
Expand Down Expand Up @@ -41,6 +45,41 @@ static void process(PreparedStatement preparedStmtContainer, PreparedStatement p
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ protected static void logTransaction(PreparedStatement preparedStmt, int batchCo
if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null) {
return;
}
boolean success = false;
int slot = 0;
for (ItemStack item : items) {
if (item != null) {
Expand Down Expand Up @@ -190,10 +191,17 @@ protected static void logTransaction(PreparedStatement preparedStmt, int batchCo
int data = 0;
int amount = item.getAmount();
ContainerStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, typeId, data, amount, metadata, action, 0);
success = true;
}
}
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) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
public final class HopperPushListener {

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);
if (lastAbort != null) {
ItemStack[] destinationContents = destinationHolder.getInventory().getContents();
Expand Down Expand Up @@ -55,6 +56,9 @@ static void processHopperPush(Location location, InventoryHolder sourceHolder, I
ConfigHandler.hopperAbort.put(loggingChestId, new Object[] { movedItems, Util.getContainerState(destinationContents) });
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());
if (list != null) {
Expand Down

0 comments on commit 5426e69

Please sign in to comment.