Skip to content

Commit 1f60dcc

Browse files
committed
Improve synthetic username handling for golem logging (WIP)
1 parent 5276dc5 commit 1f60dcc

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

src/main/java/net/coreprotect/consumer/process/Process.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.coreprotect.consumer.Consumer;
1616
import net.coreprotect.database.Database;
1717
import net.coreprotect.database.statement.UserStatement;
18+
import net.coreprotect.utility.SyntheticUsernames;
1819

1920
public class Process {
2021

@@ -99,8 +100,9 @@ protected static void processConsumer(int processId, boolean lastRun) {
99100
if (data != null) {
100101
String user = data[0];
101102
String uuid = data[1];
102-
if (user != null && ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT)) == null) {
103-
UserStatement.loadId(connection, user, uuid);
103+
String normalizedUser = SyntheticUsernames.normalize(user);
104+
if (normalizedUser != null && ConfigHandler.playerIdCache.get(normalizedUser.toLowerCase(Locale.ROOT)) == null) {
105+
UserStatement.loadId(connection, normalizedUser, uuid);
104106
}
105107
}
106108
}

src/main/java/net/coreprotect/database/logger/ContainerLogger.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import net.coreprotect.utility.BlockUtils;
2626
import net.coreprotect.utility.ItemUtils;
2727
import net.coreprotect.utility.MaterialUtils;
28+
import net.coreprotect.utility.SyntheticUsernames;
2829
import net.coreprotect.utility.WorldUtils;
2930
import net.coreprotect.utility.serialize.ItemMetaHandler;
3031

@@ -57,7 +58,13 @@ else if (type == Material.JUKEBOX || type == Material.ARMOR_STAND) {
5758
return;
5859
}
5960

60-
String loggingContainerId = player.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
61+
String uniqueUser = player;
62+
String canonicalUser = SyntheticUsernames.normalize(uniqueUser);
63+
if (canonicalUser == null) {
64+
canonicalUser = uniqueUser;
65+
}
66+
67+
String loggingContainerId = uniqueUser.toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ();
6168
List<ItemStack[]> oldList = ConfigHandler.oldContainer.get(loggingContainerId);
6269
ItemStack[] oi1 = oldList.get(0);
6370
ItemStack[] oldInventory = ItemUtils.getContainerState(oi1);
@@ -67,7 +74,7 @@ else if (type == Material.JUKEBOX || type == Material.ARMOR_STAND) {
6774
}
6875

6976
// Check if this is a dispenser with no actual changes
70-
if (player.equals("#dispenser") && ItemUtils.compareContainers(oldInventory, newInventory)) {
77+
if ("#dispenser".equals(canonicalUser) && ItemUtils.compareContainers(oldInventory, newInventory)) {
7178
// No changes detected, mark this dispenser in the dispenserNoChange map
7279
// Extract the location key from the loggingContainerId
7380
// Format: #dispenser.x.y.z
@@ -95,7 +102,7 @@ else if (type == Material.JUKEBOX || type == Material.ARMOR_STAND) {
95102

96103
// If we reach here, the dispenser event resulted in changes
97104
// Remove any pending event for this dispenser
98-
if (player.equals("#dispenser")) {
105+
if ("#dispenser".equals(canonicalUser)) {
99106
String[] parts = loggingContainerId.split("\\.");
100107
if (parts.length >= 4) {
101108
int x = Integer.parseInt(parts[1]);
@@ -191,12 +198,12 @@ else if (item != null) {
191198
ItemUtils.mergeItems(type, newInventory);
192199

193200
if (type != Material.ENDER_CHEST) {
194-
logTransaction(preparedStmtContainer, batchCount, player, type, faceData, oldInventory, 0, location);
195-
logTransaction(preparedStmtContainer, batchCount, player, type, faceData, newInventory, 1, location);
201+
logTransaction(preparedStmtContainer, batchCount, canonicalUser, type, faceData, oldInventory, 0, location);
202+
logTransaction(preparedStmtContainer, batchCount, canonicalUser, type, faceData, newInventory, 1, location);
196203
}
197204
else { // pass ender chest transactions to item logger
198-
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, player, location, oldInventory, ItemLogger.ITEM_REMOVE_ENDER);
199-
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, player, location, newInventory, ItemLogger.ITEM_ADD_ENDER);
205+
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, canonicalUser, location, oldInventory, ItemLogger.ITEM_REMOVE_ENDER);
206+
ItemLogger.logTransaction(preparedStmtItems, batchCount, 0, canonicalUser, location, newInventory, ItemLogger.ITEM_ADD_ENDER);
200207
}
201208

202209
oldList.remove(0);

src/main/java/net/coreprotect/paper/listener/CopperGolemChestListener.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
import org.bukkit.event.EventPriority;
1313
import org.bukkit.event.Listener;
1414
import org.bukkit.inventory.InventoryHolder;
15+
import org.bukkit.inventory.ItemStack;
1516
import org.bukkit.scheduler.BukkitTask;
1617

1718
import io.papermc.paper.event.entity.ItemTransportingEntityValidateTargetEvent;
1819
import net.coreprotect.CoreProtect;
1920
import net.coreprotect.config.Config;
2021
import net.coreprotect.listener.player.InventoryChangeListener;
22+
import net.coreprotect.utility.SyntheticUsernames;
2123

2224
public final class CopperGolemChestListener implements Listener {
2325

2426
private static final String COPPER_GOLEM_NAME = "COPPER_GOLEM";
2527
private static final String USERNAME = "#copper_golem";
26-
private static final long DELAY_TICKS = 50L;
28+
private static final long DELAY_TICKS = 60L;
2729

2830
private final CoreProtect plugin;
2931
private final Map<UUID, PendingTransaction> pendingTransactions = new ConcurrentHashMap<>();
@@ -67,6 +69,7 @@ public void onValidate(ItemTransportingEntityValidateTargetEvent event) {
6769

6870
private void scheduleTransaction(Entity entity, Location location) {
6971
UUID entityId = entity.getUniqueId();
72+
String username = SyntheticUsernames.qualifyWithUuid(USERNAME, entityId);
7073
PendingTransaction pendingTransaction = pendingTransactions.remove(entityId);
7174
if (pendingTransaction != null) {
7275
pendingTransaction.cancel();
@@ -84,7 +87,7 @@ private void scheduleTransaction(Entity entity, Location location) {
8487
return;
8588
}
8689

87-
InventoryChangeListener.inventoryTransaction(USERNAME, targetLocation, null);
90+
InventoryChangeListener.inventoryTransaction(username, targetLocation, null);
8891
}, DELAY_TICKS);
8992

9093
scheduled.setTask(task);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.coreprotect.utility;
2+
3+
import java.util.UUID;
4+
5+
public final class SyntheticUsernames {
6+
7+
private static final String UUID_SUFFIX = "_uuid:";
8+
9+
private SyntheticUsernames() {
10+
}
11+
12+
public static String normalize(String user) {
13+
if (user == null) {
14+
return null;
15+
}
16+
17+
int index = user.indexOf(UUID_SUFFIX);
18+
if (index == -1) {
19+
return user;
20+
}
21+
22+
return user.substring(0, index);
23+
}
24+
25+
public static String qualifyWithUuid(String base, UUID uuid) {
26+
if (base == null || uuid == null) {
27+
return base;
28+
}
29+
30+
return base + UUID_SUFFIX + uuid;
31+
}
32+
}

0 commit comments

Comments
 (0)