forked from zhdev/griefus
Improve synthetic username handling for golem logging (WIP)
This commit is contained in:
parent
5276dc57d2
commit
1f60dcc3cf
4 changed files with 55 additions and 11 deletions
|
|
@ -15,6 +15,7 @@ import net.coreprotect.config.ConfigHandler;
|
||||||
import net.coreprotect.consumer.Consumer;
|
import net.coreprotect.consumer.Consumer;
|
||||||
import net.coreprotect.database.Database;
|
import net.coreprotect.database.Database;
|
||||||
import net.coreprotect.database.statement.UserStatement;
|
import net.coreprotect.database.statement.UserStatement;
|
||||||
|
import net.coreprotect.utility.SyntheticUsernames;
|
||||||
|
|
||||||
public class Process {
|
public class Process {
|
||||||
|
|
||||||
|
|
@ -99,8 +100,9 @@ public class Process {
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
String user = data[0];
|
String user = data[0];
|
||||||
String uuid = data[1];
|
String uuid = data[1];
|
||||||
if (user != null && ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT)) == null) {
|
String normalizedUser = SyntheticUsernames.normalize(user);
|
||||||
UserStatement.loadId(connection, user, uuid);
|
if (normalizedUser != null && ConfigHandler.playerIdCache.get(normalizedUser.toLowerCase(Locale.ROOT)) == null) {
|
||||||
|
UserStatement.loadId(connection, normalizedUser, uuid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import net.coreprotect.event.CoreProtectPreLogEvent;
|
||||||
import net.coreprotect.utility.BlockUtils;
|
import net.coreprotect.utility.BlockUtils;
|
||||||
import net.coreprotect.utility.ItemUtils;
|
import net.coreprotect.utility.ItemUtils;
|
||||||
import net.coreprotect.utility.MaterialUtils;
|
import net.coreprotect.utility.MaterialUtils;
|
||||||
|
import net.coreprotect.utility.SyntheticUsernames;
|
||||||
import net.coreprotect.utility.WorldUtils;
|
import net.coreprotect.utility.WorldUtils;
|
||||||
import net.coreprotect.utility.serialize.ItemMetaHandler;
|
import net.coreprotect.utility.serialize.ItemMetaHandler;
|
||||||
|
|
||||||
|
|
@ -57,7 +58,13 @@ public class ContainerLogger extends Queue {
|
||||||
return;
|
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);
|
List<ItemStack[]> oldList = ConfigHandler.oldContainer.get(loggingContainerId);
|
||||||
ItemStack[] oi1 = oldList.get(0);
|
ItemStack[] oi1 = oldList.get(0);
|
||||||
ItemStack[] oldInventory = ItemUtils.getContainerState(oi1);
|
ItemStack[] oldInventory = ItemUtils.getContainerState(oi1);
|
||||||
|
|
@ -67,7 +74,7 @@ public class ContainerLogger extends Queue {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this is a dispenser with no actual changes
|
// 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
|
// No changes detected, mark this dispenser in the dispenserNoChange map
|
||||||
// Extract the location key from the loggingContainerId
|
// Extract the location key from the loggingContainerId
|
||||||
// Format: #dispenser.x.y.z
|
// Format: #dispenser.x.y.z
|
||||||
|
|
@ -95,7 +102,7 @@ public class ContainerLogger extends Queue {
|
||||||
|
|
||||||
// If we reach here, the dispenser event resulted in changes
|
// If we reach here, the dispenser event resulted in changes
|
||||||
// Remove any pending event for this dispenser
|
// Remove any pending event for this dispenser
|
||||||
if (player.equals("#dispenser")) {
|
if ("#dispenser".equals(canonicalUser)) {
|
||||||
String[] parts = loggingContainerId.split("\\.");
|
String[] parts = loggingContainerId.split("\\.");
|
||||||
if (parts.length >= 4) {
|
if (parts.length >= 4) {
|
||||||
int x = Integer.parseInt(parts[1]);
|
int x = Integer.parseInt(parts[1]);
|
||||||
|
|
@ -191,12 +198,12 @@ public class ContainerLogger extends Queue {
|
||||||
ItemUtils.mergeItems(type, newInventory);
|
ItemUtils.mergeItems(type, newInventory);
|
||||||
|
|
||||||
if (type != Material.ENDER_CHEST) {
|
if (type != Material.ENDER_CHEST) {
|
||||||
logTransaction(preparedStmtContainer, batchCount, player, type, faceData, oldInventory, 0, location);
|
logTransaction(preparedStmtContainer, batchCount, canonicalUser, type, faceData, oldInventory, 0, location);
|
||||||
logTransaction(preparedStmtContainer, batchCount, player, type, faceData, newInventory, 1, location);
|
logTransaction(preparedStmtContainer, batchCount, canonicalUser, type, faceData, newInventory, 1, location);
|
||||||
}
|
}
|
||||||
else { // pass ender chest transactions to item logger
|
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, canonicalUser, 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, newInventory, ItemLogger.ITEM_ADD_ENDER);
|
||||||
}
|
}
|
||||||
|
|
||||||
oldList.remove(0);
|
oldList.remove(0);
|
||||||
|
|
|
||||||
|
|
@ -12,18 +12,20 @@ import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.inventory.InventoryHolder;
|
import org.bukkit.inventory.InventoryHolder;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.scheduler.BukkitTask;
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
import io.papermc.paper.event.entity.ItemTransportingEntityValidateTargetEvent;
|
import io.papermc.paper.event.entity.ItemTransportingEntityValidateTargetEvent;
|
||||||
import net.coreprotect.CoreProtect;
|
import net.coreprotect.CoreProtect;
|
||||||
import net.coreprotect.config.Config;
|
import net.coreprotect.config.Config;
|
||||||
import net.coreprotect.listener.player.InventoryChangeListener;
|
import net.coreprotect.listener.player.InventoryChangeListener;
|
||||||
|
import net.coreprotect.utility.SyntheticUsernames;
|
||||||
|
|
||||||
public final class CopperGolemChestListener implements Listener {
|
public final class CopperGolemChestListener implements Listener {
|
||||||
|
|
||||||
private static final String COPPER_GOLEM_NAME = "COPPER_GOLEM";
|
private static final String COPPER_GOLEM_NAME = "COPPER_GOLEM";
|
||||||
private static final String USERNAME = "#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 CoreProtect plugin;
|
||||||
private final Map<UUID, PendingTransaction> pendingTransactions = new ConcurrentHashMap<>();
|
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) {
|
private void scheduleTransaction(Entity entity, Location location) {
|
||||||
UUID entityId = entity.getUniqueId();
|
UUID entityId = entity.getUniqueId();
|
||||||
|
String username = SyntheticUsernames.qualifyWithUuid(USERNAME, entityId);
|
||||||
PendingTransaction pendingTransaction = pendingTransactions.remove(entityId);
|
PendingTransaction pendingTransaction = pendingTransactions.remove(entityId);
|
||||||
if (pendingTransaction != null) {
|
if (pendingTransaction != null) {
|
||||||
pendingTransaction.cancel();
|
pendingTransaction.cancel();
|
||||||
|
|
@ -84,7 +87,7 @@ public final class CopperGolemChestListener implements Listener {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
InventoryChangeListener.inventoryTransaction(USERNAME, targetLocation, null);
|
InventoryChangeListener.inventoryTransaction(username, targetLocation, null);
|
||||||
}, DELAY_TICKS);
|
}, DELAY_TICKS);
|
||||||
|
|
||||||
scheduled.setTask(task);
|
scheduled.setTask(task);
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue