|
2 | 2 |
|
3 | 3 | import com.earth2me.essentials.OfflinePlayer; |
4 | 4 | import com.earth2me.essentials.User; |
| 5 | +import com.earth2me.essentials.utils.NumberUtil; |
5 | 6 | import com.google.common.cache.CacheBuilder; |
6 | 7 | import com.google.common.cache.CacheLoader; |
7 | 8 | import com.google.common.cache.LoadingCache; |
|
12 | 13 | import java.util.Map; |
13 | 14 | import java.util.Set; |
14 | 15 | import java.util.UUID; |
| 16 | +import java.util.concurrent.ConcurrentHashMap; |
| 17 | +import java.util.concurrent.ConcurrentMap; |
15 | 18 | import java.util.concurrent.ExecutionException; |
| 19 | +import java.util.concurrent.atomic.AtomicLong; |
16 | 20 | import java.util.logging.Level; |
17 | 21 |
|
18 | 22 | public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap { |
19 | 23 | private final transient IEssentials ess; |
20 | 24 | private final transient ModernUUIDCache uuidCache; |
21 | 25 | private final transient LoadingCache<UUID, User> userCache; |
22 | 26 |
|
| 27 | + private final boolean debugPrintStackWithWarn; |
| 28 | + private final long debugMaxWarnsPerType; |
| 29 | + private final ConcurrentMap<String, AtomicLong> debugNonPlayerWarnCounts; |
| 30 | + |
23 | 31 | public ModernUserMap(final IEssentials ess) { |
24 | 32 | this.ess = ess; |
25 | 33 | this.uuidCache = new ModernUUIDCache(ess); |
26 | 34 | this.userCache = CacheBuilder.newBuilder() |
27 | 35 | .maximumSize(ess.getSettings().getMaxUserCacheCount()) |
28 | 36 | .softValues() |
29 | 37 | .build(this); |
| 38 | + |
| 39 | + // -Dnet.essentialsx.usermap.print-stack=true |
| 40 | + final String printStackProperty = System.getProperty("net.essentialsx.usermap.print-stack", "false"); |
| 41 | + // -Dnet.essentialsx.usermap.max-warns=20 |
| 42 | + final String maxWarnProperty = System.getProperty("net.essentialsx.usermap.max-warns", "100"); |
| 43 | + |
| 44 | + this.debugMaxWarnsPerType = NumberUtil.isLong(maxWarnProperty) ? Long.parseLong(maxWarnProperty) : -1; |
| 45 | + this.debugPrintStackWithWarn = Boolean.parseBoolean(printStackProperty); |
| 46 | + this.debugNonPlayerWarnCounts = new ConcurrentHashMap<>(); |
30 | 47 | } |
31 | 48 |
|
32 | 49 | @Override |
@@ -111,7 +128,7 @@ public User loadUncachedUser(final Player base) { |
111 | 128 |
|
112 | 129 | User user = getUser(base.getUniqueId()); |
113 | 130 | if (user == null) { |
114 | | - ess.getLogger().log(Level.INFO, "Essentials created a User for " + base.getName() + " (" + base.getUniqueId() + ") for non Bukkit type: " + base.getClass().getName()); |
| 131 | + debugLogUncachedNonPlayer(base); |
115 | 132 | user = new User(base, ess); |
116 | 133 | } else if (!base.equals(user.getBase())) { |
117 | 134 | ess.getLogger().log(Level.INFO, "Essentials updated the underlying Player object for " + user.getUUID()); |
@@ -178,4 +195,16 @@ private File getUserFile(final UUID uuid) { |
178 | 195 | public void shutdown() { |
179 | 196 | uuidCache.shutdown(); |
180 | 197 | } |
| 198 | + |
| 199 | + private void debugLogUncachedNonPlayer(final Player base) { |
| 200 | + final String typeName = base.getClass().getName(); |
| 201 | + final long count = debugNonPlayerWarnCounts.computeIfAbsent(typeName, name -> new AtomicLong(0)).getAndIncrement(); |
| 202 | + if (debugMaxWarnsPerType < 0 || count <= debugMaxWarnsPerType) { |
| 203 | + final Throwable throwable = debugPrintStackWithWarn ? new Throwable() : null; |
| 204 | + ess.getLogger().log(Level.INFO, "Created a User for " + base.getName() + " (" + base.getUniqueId() + ") for non Bukkit type: " + typeName, throwable); |
| 205 | + if (count == debugMaxWarnsPerType) { |
| 206 | + ess.getLogger().log(Level.WARNING, "Essentials will not log any more warnings for " + typeName + ". Please report this to the EssentialsX team."); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
181 | 210 | } |
0 commit comments