Skip to content

Commit dc7fb91

Browse files
mdcfeJRoy
andauthored
Make usermap non-player warnings configurable (#5125)
Co-authored-by: Josh Roy <[email protected]>
1 parent e91ce0c commit dc7fb91

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.earth2me.essentials.OfflinePlayer;
44
import com.earth2me.essentials.User;
5+
import com.earth2me.essentials.utils.NumberUtil;
56
import com.google.common.cache.CacheBuilder;
67
import com.google.common.cache.CacheLoader;
78
import com.google.common.cache.LoadingCache;
@@ -12,21 +13,37 @@
1213
import java.util.Map;
1314
import java.util.Set;
1415
import java.util.UUID;
16+
import java.util.concurrent.ConcurrentHashMap;
17+
import java.util.concurrent.ConcurrentMap;
1518
import java.util.concurrent.ExecutionException;
19+
import java.util.concurrent.atomic.AtomicLong;
1620
import java.util.logging.Level;
1721

1822
public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
1923
private final transient IEssentials ess;
2024
private final transient ModernUUIDCache uuidCache;
2125
private final transient LoadingCache<UUID, User> userCache;
2226

27+
private final boolean debugPrintStackWithWarn;
28+
private final long debugMaxWarnsPerType;
29+
private final ConcurrentMap<String, AtomicLong> debugNonPlayerWarnCounts;
30+
2331
public ModernUserMap(final IEssentials ess) {
2432
this.ess = ess;
2533
this.uuidCache = new ModernUUIDCache(ess);
2634
this.userCache = CacheBuilder.newBuilder()
2735
.maximumSize(ess.getSettings().getMaxUserCacheCount())
2836
.softValues()
2937
.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<>();
3047
}
3148

3249
@Override
@@ -111,7 +128,7 @@ public User loadUncachedUser(final Player base) {
111128

112129
User user = getUser(base.getUniqueId());
113130
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);
115132
user = new User(base, ess);
116133
} else if (!base.equals(user.getBase())) {
117134
ess.getLogger().log(Level.INFO, "Essentials updated the underlying Player object for " + user.getUUID());
@@ -178,4 +195,16 @@ private File getUserFile(final UUID uuid) {
178195
public void shutdown() {
179196
uuidCache.shutdown();
180197
}
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+
}
181210
}

0 commit comments

Comments
 (0)