Improve synthetic username handling for golem logging (WIP)

This commit is contained in:
Intelli 2025-11-19 15:12:33 -07:00
parent 5276dc57d2
commit 1f60dcc3cf
4 changed files with 55 additions and 11 deletions

View file

@ -15,6 +15,7 @@ import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Consumer;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.utility.SyntheticUsernames;
public class Process {
@ -99,8 +100,9 @@ public class Process {
if (data != null) {
String user = data[0];
String uuid = data[1];
if (user != null && ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT)) == null) {
UserStatement.loadId(connection, user, uuid);
String normalizedUser = SyntheticUsernames.normalize(user);
if (normalizedUser != null && ConfigHandler.playerIdCache.get(normalizedUser.toLowerCase(Locale.ROOT)) == null) {
UserStatement.loadId(connection, normalizedUser, uuid);
}
}
}

View file

@ -25,6 +25,7 @@ import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.utility.BlockUtils;
import net.coreprotect.utility.ItemUtils;
import net.coreprotect.utility.MaterialUtils;
import net.coreprotect.utility.SyntheticUsernames;
import net.coreprotect.utility.WorldUtils;
import net.coreprotect.utility.serialize.ItemMetaHandler;
@ -57,7 +58,13 @@ public class ContainerLogger extends Queue {
return;
}
String loggingContainerId = player.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
String uniqueUser = player;
String canonicalUser = SyntheticUsernames.normalize(uniqueUser);
if (canonicalUser == null) {
canonicalUser = uniqueUser;
}
String loggingContainerId = uniqueUser.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
List<ItemStack[]> oldList = ConfigHandler.oldContainer.get(loggingContainerId);
ItemStack[] oi1 = oldList.get(0);
ItemStack[] oldInventory = ItemUtils.getContainerState(oi1);
@ -67,7 +74,7 @@ public class ContainerLogger extends Queue {
}
// Check if this is a dispenser with no actual changes
if (player.equals("#dispenser") && ItemUtils.compareContainers(oldInventory, newInventory)) {
if ("#dispenser".equals(canonicalUser) && ItemUtils.compareContainers(oldInventory, newInventory)) {
// No changes detected, mark this dispenser in the dispenserNoChange map
// Extract the location key from the loggingContainerId
// Format: #dispenser.x.y.z
@ -95,7 +102,7 @@ public class ContainerLogger extends Queue {
// If we reach here, the dispenser event resulted in changes
// Remove any pending event for this dispenser
if (player.equals("#dispenser")) {
if ("#dispenser".equals(canonicalUser)) {
String[] parts = loggingContainerId.split("\\.");
if (parts.length >= 4) {
int x = Integer.parseInt(parts[1]);
@ -191,12 +198,12 @@ public class ContainerLogger extends Queue {
ItemUtils.mergeItems(type, newInventory);
if (type != Material.ENDER_CHEST) {
logTransaction(preparedStmtContainer, batchCount, player, type, faceData, oldInventory, 0, location);
logTransaction(preparedStmtContainer, batchCount, player, type, faceData, newInventory, 1, location);
logTransaction(preparedStmtContainer, batchCount, canonicalUser, type, faceData, oldInventory, 0, location);
logTransaction(preparedStmtContainer, batchCount, canonicalUser, type, faceData, newInventory, 1, location);
}
else { // pass ender chest transactions to item logger
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, player, location, oldInventory, ItemLogger.ITEM_REMOVE_ENDER);
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, player, location, newInventory, ItemLogger.ITEM_ADD_ENDER);
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, canonicalUser, location, oldInventory, ItemLogger.ITEM_REMOVE_ENDER);
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, canonicalUser, location, newInventory, ItemLogger.ITEM_ADD_ENDER);
}
oldList.remove(0);

View file

@ -12,18 +12,20 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;
import io.papermc.paper.event.entity.ItemTransportingEntityValidateTargetEvent;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.listener.player.InventoryChangeListener;
import net.coreprotect.utility.SyntheticUsernames;
public final class CopperGolemChestListener implements Listener {
private static final String COPPER_GOLEM_NAME = "COPPER_GOLEM";
private static final String USERNAME = "#copper_golem";
private static final long DELAY_TICKS = 50L;
private static final long DELAY_TICKS = 60L;
private final CoreProtect plugin;
private final Map<UUID, PendingTransaction> pendingTransactions = new ConcurrentHashMap<>();
@ -67,6 +69,7 @@ public final class CopperGolemChestListener implements Listener {
private void scheduleTransaction(Entity entity, Location location) {
UUID entityId = entity.getUniqueId();
String username = SyntheticUsernames.qualifyWithUuid(USERNAME, entityId);
PendingTransaction pendingTransaction = pendingTransactions.remove(entityId);
if (pendingTransaction != null) {
pendingTransaction.cancel();
@ -84,7 +87,7 @@ public final class CopperGolemChestListener implements Listener {
return;
}
InventoryChangeListener.inventoryTransaction(USERNAME, targetLocation, null);
InventoryChangeListener.inventoryTransaction(username, targetLocation, null);
}, DELAY_TICKS);
scheduled.setTask(task);

View file

@ -0,0 +1,32 @@
package net.coreprotect.utility;
import java.util.UUID;
public final class SyntheticUsernames {
private static final String UUID_SUFFIX = "_uuid:";
private SyntheticUsernames() {
}
public static String normalize(String user) {
if (user == null) {
return null;
}
int index = user.indexOf(UUID_SUFFIX);
if (index == -1) {
return user;
}
return user.substring(0, index);
}
public static String qualifyWithUuid(String base, UUID uuid) {
if (base == null || uuid == null) {
return base;
}
return base + UUID_SUFFIX + uuid;
}
}