From 2ff2f3df4cf833a7b2ca83c8419de36a2ff97eef Mon Sep 17 00:00:00 2001 From: MD <1917406+mdcfe@users.noreply.github.com> Date: Fri, 7 Oct 2022 11:31:56 +0100 Subject: [PATCH] Make usermap non-player warnings configurable Adds system properties to control the non-player usermap warnings. `-Dnet.essentialsx.usermap.max-warns=N` (default: 100) limits how many warnings EssentialsX will print per non-real Player type. `-Dnet.essentialsx.usermap.print-stack=true` (default: false) enables printing stack traces with the warning message. --- .../essentials/userstorage/ModernUserMap.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java b/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java index 2ea21e48758..d19175457a4 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java +++ b/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java @@ -2,6 +2,7 @@ import com.earth2me.essentials.OfflinePlayer; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.NumberUtil; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -12,7 +13,10 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; public class ModernUserMap extends CacheLoader implements IUserMap { @@ -20,6 +24,10 @@ public class ModernUserMap extends CacheLoader implements IUserMap { private final transient ModernUUIDCache uuidCache; private final transient LoadingCache userCache; + private final boolean debugPrintStackWithWarn; + private final long debugMaxWarnsPerType; + private final ConcurrentMap debugNonPlayerWarnCounts; + public ModernUserMap(final IEssentials ess) { this.ess = ess; this.uuidCache = new ModernUUIDCache(ess); @@ -27,6 +35,15 @@ public ModernUserMap(final IEssentials ess) { .maximumSize(ess.getSettings().getMaxUserCacheCount()) .softValues() .build(this); + + // -Dnet.essentialsx.usermap.print-stack=true + final String printStackProperty = System.getProperty("net.essentialsx.usermap.print-stack", "false"); + // -Dnet.essentialsx.usermap.max-warns=20 + final String maxWarnProperty = System.getProperty("net.essentialsx.usermap.max-warns", "100"); + + this.debugMaxWarnsPerType = NumberUtil.isLong(maxWarnProperty) ? Long.parseLong(maxWarnProperty) : -1; + this.debugPrintStackWithWarn = Boolean.parseBoolean(printStackProperty); + this.debugNonPlayerWarnCounts = new ConcurrentHashMap<>(); } @Override @@ -111,7 +128,7 @@ public User loadUncachedUser(final Player base) { User user = getUser(base.getUniqueId()); if (user == null) { - ess.getLogger().log(Level.INFO, "Essentials created a User for " + base.getName() + " (" + base.getUniqueId() + ") for non Bukkit type: " + base.getClass().getName()); + debugLogUncachedNonPlayer(base); user = new User(base, ess); } else if (!base.equals(user.getBase())) { ess.getLogger().log(Level.INFO, "Essentials updated the underlying Player object for " + user.getUUID()); @@ -174,4 +191,16 @@ private File getUserFile(final UUID uuid) { public void shutdown() { uuidCache.shutdown(); } + + private void debugLogUncachedNonPlayer(final Player base) { + final String typeName = base.getClass().getName(); + final long count = debugNonPlayerWarnCounts.computeIfAbsent(typeName, name -> new AtomicLong(0)).getAndIncrement(); + if (debugMaxWarnsPerType < 0 || count <= debugMaxWarnsPerType) { + final Throwable throwable = debugPrintStackWithWarn ? new Throwable() : null; + ess.getLogger().log(Level.INFO, "Created a User for " + base.getName() + " (" + base.getUniqueId() + ") for non Bukkit type: " + typeName, throwable); + if (count == debugMaxWarnsPerType) { + ess.getLogger().log(Level.WARNING, "Essentials will not log any more warnings for " + typeName + ". Please report this to the EssentialsX team."); + } + } + } }