Allow setting flags via config; add flags with long values

This commit is contained in:
MD 2023-03-12 15:33:00 +00:00
parent 1a09c49b29
commit 9e5bdeb7d5
No known key found for this signature in database
GPG Key ID: 7458A1E1C8FB76B1
3 changed files with 63 additions and 18 deletions

View File

@ -3,6 +3,7 @@ package com.earth2me.essentials;
import com.earth2me.essentials.commands.IEssentialsCommand; import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.signs.EssentialsSign; import com.earth2me.essentials.signs.EssentialsSign;
import com.earth2me.essentials.textreader.IText; import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.utils.NumberUtil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.CommentedConfigurationNode;
@ -145,6 +146,8 @@ public interface ISettings extends IConf {
boolean isDebug(DebugFlag flag); boolean isDebug(DebugFlag flag);
Long getDebugLong(DebugFlag flag);
void setDebug(boolean debug); void setDebug(boolean debug);
boolean isEcoDisabled(); boolean isEcoDisabled();
@ -420,30 +423,51 @@ public interface ISettings extends IConf {
OFF OFF
} }
// TODO: consider separating out non-bool values? or replace this with an object-mapped class?
enum DebugFlag { enum DebugFlag {
GENERIC("debug.generic", true), GENERIC("debug.generic", true),
USERMAP_PRINT_STACK("usermap.print-stack", false), USERMAP_PRINT_STACK("usermap.print-stack", false),
USERMAP_MAX_WARNS("usermap.max-warns", false),
; ;
private final String propertyKey; private final String flagKey;
private final boolean isSetByConfig; private final boolean isSetByGlobal;
DebugFlag(final String propertyKey, final boolean isSetByConfig) { DebugFlag(final String flagKey, final boolean isSetByGlobal) {
this.propertyKey = propertyKey; this.flagKey = flagKey;
this.isSetByConfig = isSetByConfig; this.isSetByGlobal = isSetByGlobal;
} }
public String getSystemProperty() { public String getFlagKey() {
return DEBUG_FLAG_NAMESPACE + "." + propertyKey; return flagKey;
} }
public boolean getSystemPropertyValue() { public boolean isSetByGlobal() {
final String value = System.getProperty(getSystemProperty(), "false"); return isSetByGlobal;
return Boolean.parseBoolean(value);
} }
public boolean isSetByConfig() { public String getSystemPropertyKey() {
return isSetByConfig; return DEBUG_FLAG_NAMESPACE + "." + flagKey;
}
public String getConfigKey() {
return "debug." + (flagKey.replace("debug.", ""));
}
public String getSystemPropertyValue() {
return System.getProperty(getSystemPropertyKey(), "false");
}
public boolean getSystemPropertyBoolean() {
return Boolean.parseBoolean(getSystemPropertyValue());
}
public Long getSystemPropertyLong() {
final String value = getSystemPropertyValue();
if (NumberUtil.isLong(value)) {
return Long.parseLong(value);
}
return null;
} }
} }

View File

@ -855,9 +855,17 @@ public class Settings implements net.ess3.api.ISettings {
} }
private boolean _isDebug() { private boolean _isDebug() {
if (config.isBoolean("debug")) {
return config.getBoolean("debug", false); return config.getBoolean("debug", false);
} }
if (config.isBoolean("debug.enabled")) {
return config.getBoolean("debug.enabled", false);
}
return false;
}
@Override @Override
public boolean isDebug() { public boolean isDebug() {
return isDebug(DebugFlag.GENERIC); return isDebug(DebugFlag.GENERIC);
@ -865,10 +873,23 @@ public class Settings implements net.ess3.api.ISettings {
@Override @Override
public boolean isDebug(DebugFlag flag) { public boolean isDebug(DebugFlag flag) {
if (flag.isSetByConfig() && (debug || configDebug)) { if (flag.isSetByGlobal() && (debug || configDebug)) {
return true; return true;
} }
return flag.getSystemPropertyValue(); final String flagConfigPath = flag.getConfigKey();
if (config.isBoolean(flagConfigPath)) {
return config.getBoolean(flagConfigPath, false);
}
return flag.getSystemPropertyBoolean();
}
@Override
public Long getDebugLong(DebugFlag flag) {
final String flagConfigPath = flag.getConfigKey();
if (config.hasProperty(flagConfigPath)) {
return config.getLong(flagConfigPath, 0);
}
return flag.getSystemPropertyLong();
} }
@Override @Override

View File

@ -3,7 +3,6 @@ package com.earth2me.essentials.userstorage;
import com.earth2me.essentials.ISettings; import com.earth2me.essentials.ISettings;
import com.earth2me.essentials.OfflinePlayer; import com.earth2me.essentials.OfflinePlayer;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.NumberUtil;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
@ -38,11 +37,12 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {
.build(this); .build(this);
// -Dnet.essentialsx.usermap.max-warns=20 // -Dnet.essentialsx.usermap.max-warns=20
final String maxWarnProperty = System.getProperty("net.essentialsx.usermap.max-warns", "100"); final Long maxWarnSetting = ess.getSettings().getDebugLong(ISettings.DebugFlag.USERMAP_MAX_WARNS);
this.debugMaxWarnsPerType = maxWarnSetting != null ? maxWarnSetting : -1;
this.debugMaxWarnsPerType = NumberUtil.isLong(maxWarnProperty) ? Long.parseLong(maxWarnProperty) : -1;
// -Dnet.essentialsx.usermap.print-stack=true // -Dnet.essentialsx.usermap.print-stack=true
this.debugPrintStackWithWarn = ess.getSettings().isDebug(ISettings.DebugFlag.USERMAP_PRINT_STACK); this.debugPrintStackWithWarn = ess.getSettings().isDebug(ISettings.DebugFlag.USERMAP_PRINT_STACK);
this.debugNonPlayerWarnCounts = new ConcurrentHashMap<>(); this.debugNonPlayerWarnCounts = new ConcurrentHashMap<>();
} }