mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2024-10-06 02:17:25 +02:00
Spaces.
This commit is contained in:
parent
41e9d89efa
commit
bd5794d0bd
@ -17,159 +17,161 @@ import java.util.Set;
|
||||
*/
|
||||
public class LinkedHashMapCOW<K, V> implements Map<K, V> {
|
||||
|
||||
private LinkedHashMap<K, V> map;
|
||||
// TODO: Consider a) add removeEldest... b) add option: copyMap(K) -> only copy if needed.
|
||||
|
||||
private final int initialCapacity;
|
||||
private final float loadFactor;
|
||||
private LinkedHashMap<K, V> map;
|
||||
|
||||
/**
|
||||
* Uses: 16, 0.75f, false (default settings, insertion ordered).
|
||||
*/
|
||||
public LinkedHashMapCOW() {
|
||||
this(16, 0.75f);
|
||||
}
|
||||
private final int initialCapacity;
|
||||
private final float loadFactor;
|
||||
|
||||
/**
|
||||
* Uses extra: 0.75f, false (default settings, insertion ordered).
|
||||
* @param initialCapacity
|
||||
*/
|
||||
public LinkedHashMapCOW(int initialCapacity) {
|
||||
this(initialCapacity, 0.75f);
|
||||
}
|
||||
/**
|
||||
* Uses: 16, 0.75f, false (default settings, insertion ordered).
|
||||
*/
|
||||
public LinkedHashMapCOW() {
|
||||
this(16, 0.75f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses extra: false (default settings, insertion ordered).
|
||||
* @param initialCapacity
|
||||
* @param loadFactor
|
||||
*/
|
||||
public LinkedHashMapCOW(int initialCapacity, float loadFactor) {
|
||||
this.initialCapacity = initialCapacity;
|
||||
this.loadFactor = loadFactor;
|
||||
this.map = new LinkedHashMap<K, V>(initialCapacity, loadFactor, false);
|
||||
}
|
||||
/**
|
||||
* Uses extra: 0.75f, false (default settings, insertion ordered).
|
||||
* @param initialCapacity
|
||||
*/
|
||||
public LinkedHashMapCOW(int initialCapacity) {
|
||||
this(initialCapacity, 0.75f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses: 16, 0.75f, false (default settings, insertion ordered).
|
||||
* @param map
|
||||
*/
|
||||
public LinkedHashMapCOW(Map<K, V> map) {
|
||||
this();
|
||||
this.map.putAll(map);
|
||||
}
|
||||
/**
|
||||
* Uses extra: false (default settings, insertion ordered).
|
||||
* @param initialCapacity
|
||||
* @param loadFactor
|
||||
*/
|
||||
public LinkedHashMapCOW(int initialCapacity, float loadFactor) {
|
||||
this.initialCapacity = initialCapacity;
|
||||
this.loadFactor = loadFactor;
|
||||
this.map = new LinkedHashMap<K, V>(initialCapacity, loadFactor, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Not synchronized: return a copy of the internal map.
|
||||
* @return
|
||||
*/
|
||||
private LinkedHashMap<K, V> copyMap() {
|
||||
final LinkedHashMap<K, V> newMap = new LinkedHashMap<K, V>(initialCapacity, loadFactor, false);
|
||||
newMap.putAll(this.map);
|
||||
return newMap;
|
||||
}
|
||||
/**
|
||||
* Uses: 16, 0.75f, false (default settings, insertion ordered).
|
||||
* @param map
|
||||
*/
|
||||
public LinkedHashMapCOW(Map<K, V> map) {
|
||||
this();
|
||||
this.map.putAll(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
synchronized (this) {
|
||||
this.map.clear();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Not synchronized: return a copy of the internal map.
|
||||
* @return
|
||||
*/
|
||||
private LinkedHashMap<K, V> copyMap() {
|
||||
final LinkedHashMap<K, V> newMap = new LinkedHashMap<K, V>(initialCapacity, loadFactor, false);
|
||||
newMap.putAll(this.map);
|
||||
return newMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return this.map.containsKey(key);
|
||||
}
|
||||
@Override
|
||||
public void clear() {
|
||||
synchronized (this) {
|
||||
this.map.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return this.map.containsValue(value);
|
||||
}
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return this.map.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmodifiable version of the EntrySet. Entry.setValue might be possible, but dangerous :p
|
||||
*/
|
||||
@Override
|
||||
public Set<java.util.Map.Entry<K, V>> entrySet() {
|
||||
return Collections.unmodifiableSet(map.entrySet());
|
||||
}
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return this.map.containsValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
// NOTE: If accessOrder can be true, there needs to be synchronization here, defeating any purpose, better use Collections.synchronizedMap(LinkedHashMap...) for that case.
|
||||
return map.get(key);
|
||||
}
|
||||
/**
|
||||
* Unmodifiable version of the EntrySet. Entry.setValue might be possible, but dangerous :p
|
||||
*/
|
||||
@Override
|
||||
public Set<java.util.Map.Entry<K, V>> entrySet() {
|
||||
return Collections.unmodifiableSet(map.entrySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
// NOTE: If accessOrder can be true, there needs to be synchronization here, defeating any purpose, better use Collections.synchronizedMap(LinkedHashMap...) for that case.
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmodifiable version of the KeySet.
|
||||
*/
|
||||
@Override
|
||||
public Set<K> keySet() {
|
||||
return Collections.unmodifiableSet(map.keySet());
|
||||
}
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(final K key, final V value) {
|
||||
final V out;
|
||||
synchronized (this) {
|
||||
final LinkedHashMap<K, V> newMap = copyMap();
|
||||
out = newMap.put(key, value);
|
||||
this.map = newMap;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
/**
|
||||
* Unmodifiable version of the KeySet.
|
||||
*/
|
||||
@Override
|
||||
public Set<K> keySet() {
|
||||
return Collections.unmodifiableSet(map.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(final Map<? extends K, ? extends V> m) {
|
||||
synchronized (this) {
|
||||
final LinkedHashMap<K, V> newMap = copyMap();
|
||||
newMap.putAll(m);
|
||||
this.map = newMap;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public V put(final K key, final V value) {
|
||||
final V out;
|
||||
synchronized (this) {
|
||||
final LinkedHashMap<K, V> newMap = copyMap();
|
||||
out = newMap.put(key, value);
|
||||
this.map = newMap;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(final Object key) {
|
||||
final V out;
|
||||
synchronized (this) {
|
||||
final LinkedHashMap<K, V> newMap = copyMap();
|
||||
out = newMap.remove(key);
|
||||
this.map = newMap;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@Override
|
||||
public void putAll(final Map<? extends K, ? extends V> m) {
|
||||
synchronized (this) {
|
||||
final LinkedHashMap<K, V> newMap = copyMap();
|
||||
newMap.putAll(m);
|
||||
this.map = newMap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all given keys.<br>
|
||||
* Not the most efficient implementation, copying the map and then removing
|
||||
* keys, but still better than iterating remove(key).
|
||||
*
|
||||
* @param keys
|
||||
*/
|
||||
public void removeAll(final Collection<K> keys) {
|
||||
synchronized (this) {
|
||||
final LinkedHashMap<K, V> newMap = copyMap();
|
||||
for (final K key : keys) {
|
||||
newMap.remove(key);
|
||||
}
|
||||
this.map = newMap;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public V remove(final Object key) {
|
||||
final V out;
|
||||
synchronized (this) {
|
||||
final LinkedHashMap<K, V> newMap = copyMap();
|
||||
out = newMap.remove(key);
|
||||
this.map = newMap;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
/**
|
||||
* Remove all given keys.<br>
|
||||
* Not the most efficient implementation, copying the map and then removing
|
||||
* keys, but still better than iterating remove(key).
|
||||
*
|
||||
* @param keys
|
||||
*/
|
||||
public void removeAll(final Collection<K> keys) {
|
||||
synchronized (this) {
|
||||
final LinkedHashMap<K, V> newMap = copyMap();
|
||||
for (final K key : keys) {
|
||||
newMap.remove(key);
|
||||
}
|
||||
this.map = newMap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmodifiable version of the values (Collection).
|
||||
*/
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
return Collections.unmodifiableCollection(map.values());
|
||||
}
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmodifiable version of the values (Collection).
|
||||
*/
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
return Collections.unmodifiableCollection(map.values());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,24 +35,24 @@ public class ConfigManager {
|
||||
|
||||
private static final WorldConfigProvider<ConfigFile> worldConfigProvider = new WorldConfigProvider<ConfigFile>() {
|
||||
|
||||
@Override
|
||||
public ConfigFile getDefaultConfig() {
|
||||
return ConfigManager.getConfigFile();
|
||||
}
|
||||
@Override
|
||||
public ConfigFile getDefaultConfig() {
|
||||
return ConfigManager.getConfigFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigFile getConfig(String worldName) {
|
||||
return ConfigManager.getConfigFile(worldName);
|
||||
}
|
||||
@Override
|
||||
public ConfigFile getConfig(String worldName) {
|
||||
return ConfigManager.getConfigFile(worldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ConfigFile> getAllConfigs() {
|
||||
return ConfigManager.worldsMap.values();
|
||||
}
|
||||
@Override
|
||||
public Collection<ConfigFile> getAllConfigs() {
|
||||
return ConfigManager.worldsMap.values();
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
private static boolean isInitialized = false;
|
||||
private static boolean isInitialized = false;
|
||||
|
||||
/**
|
||||
* Factory method.
|
||||
@ -75,15 +75,15 @@ public class ConfigManager {
|
||||
*/
|
||||
public static void setActionFactoryFactory(ActionFactoryFactory factory){
|
||||
if (factory != null){
|
||||
actionFactoryFactory = factory;
|
||||
actionFactoryFactory = factory;
|
||||
}
|
||||
else{
|
||||
actionFactoryFactory = new ActionFactoryFactory() {
|
||||
@Override
|
||||
actionFactoryFactory = new ActionFactoryFactory() {
|
||||
@Override
|
||||
public final ActionFactory newActionFactory(final Map<String, Object> library) {
|
||||
return new ActionFactory(library);
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
// Use lazy resetting.
|
||||
for (final ConfigFile config : worldsMap.values()){
|
||||
@ -99,18 +99,18 @@ public class ConfigManager {
|
||||
* Force setting up all configs action factories.
|
||||
*/
|
||||
public static void setAllActionFactories(){
|
||||
for (final ConfigFile config : worldsMap.values()){
|
||||
config.setActionFactory();
|
||||
}
|
||||
for (final ConfigFile config : worldsMap.values()){
|
||||
config.setActionFactory();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the WorldConfigProvider in use.
|
||||
* @return
|
||||
*/
|
||||
public static WorldConfigProvider<ConfigFile> getWorldConfigProvider() {
|
||||
return worldConfigProvider;
|
||||
}
|
||||
public static WorldConfigProvider<ConfigFile> getWorldConfigProvider() {
|
||||
return worldConfigProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup.
|
||||
@ -137,7 +137,7 @@ public class ConfigManager {
|
||||
*/
|
||||
@Deprecated
|
||||
public static synchronized ConfigFile getConfigFileSync() {
|
||||
return getConfigFile();
|
||||
return getConfigFile();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -148,23 +148,23 @@ public class ConfigManager {
|
||||
* @return the configuration file
|
||||
*/
|
||||
public static ConfigFile getConfigFile(final String worldName) {
|
||||
final ConfigFile configFile = worldsMap.get(worldName);
|
||||
final ConfigFile configFile = worldsMap.get(worldName);
|
||||
if (configFile != null){
|
||||
return configFile;
|
||||
return configFile;
|
||||
}
|
||||
// Expensive only once per world, for the rest of the runtime the file is returned fast.
|
||||
synchronized(ConfigManager.class){
|
||||
// Need to check again.
|
||||
if (worldsMap.containsKey(worldName)){
|
||||
return worldsMap.get(worldName);
|
||||
}
|
||||
// Copy the whole map with the default configuration set for this world.
|
||||
final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap);
|
||||
final ConfigFile globalConfig = newWorldsMap.get(null);
|
||||
newWorldsMap.put(worldName, globalConfig);
|
||||
ConfigManager.worldsMap = newWorldsMap;
|
||||
return globalConfig;
|
||||
}
|
||||
synchronized(ConfigManager.class){
|
||||
// Need to check again.
|
||||
if (worldsMap.containsKey(worldName)){
|
||||
return worldsMap.get(worldName);
|
||||
}
|
||||
// Copy the whole map with the default configuration set for this world.
|
||||
final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap);
|
||||
final ConfigFile globalConfig = newWorldsMap.get(null);
|
||||
newWorldsMap.put(worldName, globalConfig);
|
||||
ConfigManager.worldsMap = newWorldsMap;
|
||||
return globalConfig;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -175,7 +175,7 @@ public class ConfigManager {
|
||||
*/
|
||||
@Deprecated
|
||||
public static synchronized ConfigFile getConfigFileSync(final String worldName) {
|
||||
return getConfigFile(worldName);
|
||||
return getConfigFile(worldName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -185,8 +185,8 @@ public class ConfigManager {
|
||||
* the instance of NoCheatPlus
|
||||
*/
|
||||
public static synchronized void init(final Plugin plugin) {
|
||||
// (This can lead to minor problems with async checks during reloading.)
|
||||
LinkedHashMap<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>();
|
||||
// (This can lead to minor problems with async checks during reloading.)
|
||||
LinkedHashMap<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>();
|
||||
// Try to obtain and parse the global configuration file.
|
||||
final File globalFile = new File(plugin.getDataFolder(), "config.yml");
|
||||
PathUtils.processPaths(globalFile, "global config", false);
|
||||
@ -194,39 +194,39 @@ public class ConfigManager {
|
||||
globalConfig.setDefaults(new DefaultConfig());
|
||||
globalConfig.options().copyDefaults(true);
|
||||
if (globalFile.exists()){
|
||||
try {
|
||||
try {
|
||||
globalConfig.load(globalFile);
|
||||
// Quick shallow ugly fix: only save back if loading was successful.
|
||||
try {
|
||||
if (globalConfig.getBoolean(ConfPaths.SAVEBACKCONFIG)){
|
||||
if (!globalConfig.contains(ConfPaths.CONFIGVERSION_CREATED)){
|
||||
// Workaround.
|
||||
globalConfig.set(ConfPaths.CONFIGVERSION_CREATED, DefaultConfig.buildNumber);
|
||||
}
|
||||
globalConfig.set(ConfPaths.CONFIGVERSION_SAVED, DefaultConfig.buildNumber);
|
||||
globalConfig.save(globalFile);
|
||||
if (!globalConfig.contains(ConfPaths.CONFIGVERSION_CREATED)){
|
||||
// Workaround.
|
||||
globalConfig.set(ConfPaths.CONFIGVERSION_CREATED, DefaultConfig.buildNumber);
|
||||
}
|
||||
globalConfig.set(ConfPaths.CONFIGVERSION_SAVED, DefaultConfig.buildNumber);
|
||||
globalConfig.save(globalFile);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
StaticLog.logSevere("[NoCheatPlus] Could not save back config.yml (see exception below).");
|
||||
StaticLog.logSevere("[NoCheatPlus] Could not save back config.yml (see exception below).");
|
||||
StaticLog.logSevere(e);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
StaticLog.logSevere("[NoCheatPlus] Could not load config.yml (see exception below). Continue with default settings...");
|
||||
StaticLog.logSevere(e);
|
||||
StaticLog.logSevere("[NoCheatPlus] Could not load config.yml (see exception below). Continue with default settings...");
|
||||
StaticLog.logSevere(e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
globalConfig.options().header("This configuration was auto-generated by NoCheatPlus.");
|
||||
globalConfig.options().copyHeader(true);
|
||||
try {
|
||||
globalConfig.set(ConfPaths.CONFIGVERSION_CREATED, DefaultConfig.buildNumber);
|
||||
globalConfig.set(ConfPaths.CONFIGVERSION_SAVED, DefaultConfig.buildNumber);
|
||||
globalConfig.set(ConfPaths.CONFIGVERSION_CREATED, DefaultConfig.buildNumber);
|
||||
globalConfig.set(ConfPaths.CONFIGVERSION_SAVED, DefaultConfig.buildNumber);
|
||||
globalConfig.save(globalFile);
|
||||
} catch (final Exception e) {
|
||||
StaticLog.logSevere(e);
|
||||
StaticLog.logSevere(e);
|
||||
}
|
||||
}
|
||||
// globalConfig.setActionFactory();
|
||||
// globalConfig.setActionFactory();
|
||||
newWorldsMap.put(null, globalConfig);
|
||||
|
||||
|
||||
@ -235,8 +235,8 @@ public class ConfigManager {
|
||||
// Try to obtain and parse the world-specific configuration files.
|
||||
final HashMap<String, File> worldFiles = new HashMap<String, File>();
|
||||
if (plugin.getDataFolder().isDirectory()){
|
||||
for (final File file : plugin.getDataFolder().listFiles()){
|
||||
if (file.isFile()) {
|
||||
for (final File file : plugin.getDataFolder().listFiles()){
|
||||
if (file.isFile()) {
|
||||
final String fileName = file.getName();
|
||||
if (fileName.matches(".+_config.yml$")) {
|
||||
final String worldname = fileName.substring(0, fileName.length() - 11);
|
||||
@ -252,21 +252,21 @@ public class ConfigManager {
|
||||
worldConfig.setDefaults(worldDefaults);
|
||||
worldConfig.options().copyDefaults(true);
|
||||
try {
|
||||
worldConfig.load(worldFile);
|
||||
newWorldsMap.put(worldEntry.getKey(), worldConfig);
|
||||
worldConfig.load(worldFile);
|
||||
newWorldsMap.put(worldEntry.getKey(), worldConfig);
|
||||
try{
|
||||
if (worldConfig.getBoolean(ConfPaths.SAVEBACKCONFIG)) worldConfig.save(worldFile);
|
||||
if (worldConfig.getBoolean(ConfPaths.SAVEBACKCONFIG)) worldConfig.save(worldFile);
|
||||
} catch (final Exception e){
|
||||
StaticLog.logSevere("[NoCheatPlus] Couldn't save back world-specific configuration for " + worldEntry.getKey() + " (see exception below).");
|
||||
StaticLog.logSevere(e);
|
||||
StaticLog.logSevere("[NoCheatPlus] Couldn't save back world-specific configuration for " + worldEntry.getKey() + " (see exception below).");
|
||||
StaticLog.logSevere(e);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
StaticLog.logSevere("[NoCheatPlus] Couldn't load world-specific configuration for " + worldEntry.getKey() + " (see exception below). Continue with global default settings...");
|
||||
StaticLog.logSevere(e);
|
||||
StaticLog.logSevere("[NoCheatPlus] Couldn't load world-specific configuration for " + worldEntry.getKey() + " (see exception below). Continue with global default settings...");
|
||||
StaticLog.logSevere(e);
|
||||
}
|
||||
worldConfig.setDefaults(globalConfig);
|
||||
worldConfig.options().copyDefaults(true);
|
||||
// worldConfig.setActionFactory();
|
||||
// worldConfig.setActionFactory();
|
||||
}
|
||||
ConfigManager.worldsMap = newWorldsMap;
|
||||
isInitialized = true;
|
||||
@ -286,11 +286,11 @@ public class ConfigManager {
|
||||
* @param value
|
||||
*/
|
||||
public static synchronized void setForAllConfigs(String path, Object value){
|
||||
final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap);
|
||||
for (final ConfigFile cfg : newWorldsMap.values()){
|
||||
cfg.set(path, value);
|
||||
}
|
||||
ConfigManager.worldsMap = newWorldsMap;
|
||||
final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap);
|
||||
for (final ConfigFile cfg : newWorldsMap.values()){
|
||||
cfg.set(path, value);
|
||||
}
|
||||
ConfigManager.worldsMap = newWorldsMap;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -299,12 +299,12 @@ public class ConfigManager {
|
||||
* @return True if any config has a boolean set to true for the given path.
|
||||
*/
|
||||
public static boolean isTrueForAnyConfig(String path) {
|
||||
for (final ConfigFile cfg : worldsMap.values()){
|
||||
if (cfg.getBoolean(path, false)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
for (final ConfigFile cfg : worldsMap.values()){
|
||||
if (cfg.getBoolean(path, false)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -315,22 +315,22 @@ public class ConfigManager {
|
||||
* @return Value or null.
|
||||
*/
|
||||
public static Double getMaxNumberForAllConfigs(final String path){
|
||||
Number max = null;
|
||||
for (final ConfigFile config : worldsMap.values()){
|
||||
try{
|
||||
final Object obj = config.get(path);
|
||||
if (obj instanceof Number){
|
||||
final Number num = (Number) obj;
|
||||
if (max == null || num.doubleValue() > max.doubleValue()){
|
||||
max = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Throwable t){
|
||||
// Holzhammer
|
||||
}
|
||||
}
|
||||
return max.doubleValue();
|
||||
Number max = null;
|
||||
for (final ConfigFile config : worldsMap.values()){
|
||||
try{
|
||||
final Object obj = config.get(path);
|
||||
if (obj instanceof Number){
|
||||
final Number num = (Number) obj;
|
||||
if (max == null || num.doubleValue() > max.doubleValue()){
|
||||
max = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Throwable t){
|
||||
// Holzhammer
|
||||
}
|
||||
}
|
||||
return max.doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -341,22 +341,22 @@ public class ConfigManager {
|
||||
* @return Value or null.
|
||||
*/
|
||||
public static Double getMinNumberForAllConfigs(final String path){
|
||||
Number min = null;
|
||||
for (final ConfigFile config : worldsMap.values()){
|
||||
try{
|
||||
final Object obj = config.get(path);
|
||||
if (obj instanceof Number){
|
||||
final Number num = (Number) obj;
|
||||
if (min == null || num.doubleValue() < min.doubleValue()){
|
||||
min = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Throwable t){
|
||||
// Holzhammer
|
||||
}
|
||||
}
|
||||
return min.doubleValue();
|
||||
Number min = null;
|
||||
for (final ConfigFile config : worldsMap.values()){
|
||||
try{
|
||||
final Object obj = config.get(path);
|
||||
if (obj instanceof Number){
|
||||
final Number num = (Number) obj;
|
||||
if (min == null || num.doubleValue() < min.doubleValue()){
|
||||
min = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Throwable t){
|
||||
// Holzhammer
|
||||
}
|
||||
}
|
||||
return min.doubleValue();
|
||||
}
|
||||
|
||||
// TODO: consider: filter(Max|Min)NumberForAllConfigs(String path, String filerPath, boolean filterPreset)
|
||||
|
@ -25,82 +25,82 @@ import fr.neatmonster.nocheatplus.utilities.ds.prefixtree.SimpleCharPrefixTree;
|
||||
|
||||
public class PathUtils {
|
||||
|
||||
// Deprecated paths.
|
||||
private static final Set<String> deprecatedFields = new LinkedHashSet<String>();
|
||||
private static final SimpleCharPrefixTree deprecatedPrefixes = new SimpleCharPrefixTree();
|
||||
// Deprecated paths.
|
||||
private static final Set<String> deprecatedFields = new LinkedHashSet<String>();
|
||||
private static final SimpleCharPrefixTree deprecatedPrefixes = new SimpleCharPrefixTree();
|
||||
|
||||
// Paths only for the global config.
|
||||
private static final Set<String> globalOnlyFields = new HashSet<String>();
|
||||
private static final SimpleCharPrefixTree globalOnlyPrefixes = new SimpleCharPrefixTree();
|
||||
// Paths only for the global config.
|
||||
private static final Set<String> globalOnlyFields = new HashSet<String>();
|
||||
private static final SimpleCharPrefixTree globalOnlyPrefixes = new SimpleCharPrefixTree();
|
||||
|
||||
// Paths moved to other paths.
|
||||
private static final Map<String, String> movedPaths = new LinkedHashMap<String, String>();
|
||||
// Paths moved to other paths.
|
||||
private static final Map<String, String> movedPaths = new LinkedHashMap<String, String>();
|
||||
|
||||
static{
|
||||
initPaths();
|
||||
}
|
||||
static{
|
||||
initPaths();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize annotation-based path properties.
|
||||
* @return
|
||||
*/
|
||||
private static void initPaths(){
|
||||
deprecatedFields.clear();
|
||||
deprecatedPrefixes.clear();
|
||||
globalOnlyFields.clear();
|
||||
globalOnlyPrefixes.clear();
|
||||
movedPaths.clear();
|
||||
for (final Field field : ConfPaths.class.getDeclaredFields()){
|
||||
if (field.getType() != String.class){
|
||||
// Only process strings.
|
||||
continue;
|
||||
}
|
||||
final String fieldName = field.getName();
|
||||
deprecatedFields.clear();
|
||||
deprecatedPrefixes.clear();
|
||||
globalOnlyFields.clear();
|
||||
globalOnlyPrefixes.clear();
|
||||
movedPaths.clear();
|
||||
for (final Field field : ConfPaths.class.getDeclaredFields()){
|
||||
if (field.getType() != String.class){
|
||||
// Only process strings.
|
||||
continue;
|
||||
}
|
||||
final String fieldName = field.getName();
|
||||
|
||||
checkAddPrefixes(field, fieldName, GlobalConfig.class, globalOnlyFields, globalOnlyPrefixes);
|
||||
checkAddPrefixes(field, fieldName, Deprecated.class, deprecatedFields, deprecatedPrefixes);
|
||||
if (field.isAnnotationPresent(Moved.class)){
|
||||
// TODO: Prefixes: Might later support relocating entire sections with one annotation?
|
||||
addMoved(field, field.getAnnotation(Moved.class));
|
||||
}
|
||||
}
|
||||
checkAddPrefixes(field, fieldName, GlobalConfig.class, globalOnlyFields, globalOnlyPrefixes);
|
||||
checkAddPrefixes(field, fieldName, Deprecated.class, deprecatedFields, deprecatedPrefixes);
|
||||
if (field.isAnnotationPresent(Moved.class)){
|
||||
// TODO: Prefixes: Might later support relocating entire sections with one annotation?
|
||||
addMoved(field, field.getAnnotation(Moved.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkAddPrefixes(Field field, String fieldName, Class<? extends Annotation> annotation, Set<String> fieldNames, SimpleCharPrefixTree pathPrefixes) {
|
||||
if (field.isAnnotationPresent(annotation)){
|
||||
fieldNames.add(fieldName);
|
||||
addPrefixesField(field, pathPrefixes);
|
||||
}
|
||||
else{
|
||||
for (final String refName : fieldNames){
|
||||
if (fieldName.startsWith(refName)){
|
||||
addPrefixesField(field, pathPrefixes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (field.isAnnotationPresent(annotation)){
|
||||
fieldNames.add(fieldName);
|
||||
addPrefixesField(field, pathPrefixes);
|
||||
}
|
||||
else{
|
||||
for (final String refName : fieldNames){
|
||||
if (fieldName.startsWith(refName)){
|
||||
addPrefixesField(field, pathPrefixes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addPrefixesField(Field field, SimpleCharPrefixTree pathPrefixes) {
|
||||
try {
|
||||
final String path = field.get(null).toString();
|
||||
if (path != null){
|
||||
pathPrefixes.feed(path);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
private static void addPrefixesField(Field field, SimpleCharPrefixTree pathPrefixes) {
|
||||
try {
|
||||
final String path = field.get(null).toString();
|
||||
if (path != null){
|
||||
pathPrefixes.feed(path);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addMoved(final Field field, final Moved rel) {
|
||||
try {
|
||||
final String path = field.get(null).toString();
|
||||
movedPaths.put(path, rel.newPath());
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
private static void addMoved(final Field field, final Moved rel) {
|
||||
try {
|
||||
final String path = field.get(null).toString();
|
||||
movedPaths.put(path, rel.newPath());
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Warn on the console if paths are used.
|
||||
* @param config
|
||||
* @param paths
|
||||
@ -108,15 +108,15 @@ public class PathUtils {
|
||||
* @param warnedPaths Paths which were found, can be null.
|
||||
*/
|
||||
protected static void warnPaths(final ConfigFile config, final CharPrefixTree<?, ?> paths, final String msgPrefix, final Set<String> warnedPaths){
|
||||
final Logger logger = Bukkit.getLogger();
|
||||
for (final String path : config.getKeys(true)){
|
||||
if (paths.hasPrefix(path)){
|
||||
logger.warning("[NoCheatPlus] Config path '" + path + "'" + msgPrefix);
|
||||
if (warnedPaths != null){
|
||||
warnedPaths.add(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
final Logger logger = Bukkit.getLogger();
|
||||
for (final String path : config.getKeys(true)){
|
||||
if (paths.hasPrefix(path)){
|
||||
logger.warning("[NoCheatPlus] Config path '" + path + "'" + msgPrefix);
|
||||
if (warnedPaths != null){
|
||||
warnedPaths.add(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,40 +125,40 @@ public class PathUtils {
|
||||
* @param configName
|
||||
*/
|
||||
public static void processPaths(File file, String configName, boolean isWorldConfig){
|
||||
ConfigFile config = new ConfigFile();
|
||||
try {
|
||||
config.load(file);
|
||||
final Set<String> removePaths = new LinkedHashSet<String>();
|
||||
final Map<String, Object> addPaths = new LinkedHashMap<String, Object>();
|
||||
if (isWorldConfig){
|
||||
// TODO: might remove these [though some global only paths might actually work].
|
||||
processGlobalOnlyPaths(config, configName, null);
|
||||
}
|
||||
processDeprecatedPaths(config, configName, removePaths);
|
||||
processMovedPaths(config, configName, removePaths, addPaths);
|
||||
boolean changed = false;
|
||||
if (!removePaths.isEmpty()){
|
||||
config = removePaths(config, removePaths);
|
||||
changed = true;
|
||||
}
|
||||
if (!addPaths.isEmpty()){
|
||||
setPaths(config, addPaths);
|
||||
changed = true;
|
||||
}
|
||||
if (changed){
|
||||
try{
|
||||
config.save(file);
|
||||
}
|
||||
catch(Throwable t){
|
||||
// Do log this one.
|
||||
StaticLog.logSevere("[NoCheatPlus] Failed to save configuration (" + configName + ") with changes: " + t.getClass().getSimpleName());
|
||||
StaticLog.logSevere(t);
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
} catch (IOException e) {
|
||||
} catch (InvalidConfigurationException e) {
|
||||
}
|
||||
ConfigFile config = new ConfigFile();
|
||||
try {
|
||||
config.load(file);
|
||||
final Set<String> removePaths = new LinkedHashSet<String>();
|
||||
final Map<String, Object> addPaths = new LinkedHashMap<String, Object>();
|
||||
if (isWorldConfig){
|
||||
// TODO: might remove these [though some global only paths might actually work].
|
||||
processGlobalOnlyPaths(config, configName, null);
|
||||
}
|
||||
processDeprecatedPaths(config, configName, removePaths);
|
||||
processMovedPaths(config, configName, removePaths, addPaths);
|
||||
boolean changed = false;
|
||||
if (!removePaths.isEmpty()){
|
||||
config = removePaths(config, removePaths);
|
||||
changed = true;
|
||||
}
|
||||
if (!addPaths.isEmpty()){
|
||||
setPaths(config, addPaths);
|
||||
changed = true;
|
||||
}
|
||||
if (changed){
|
||||
try{
|
||||
config.save(file);
|
||||
}
|
||||
catch(Throwable t){
|
||||
// Do log this one.
|
||||
StaticLog.logSevere("[NoCheatPlus] Failed to save configuration (" + configName + ") with changes: " + t.getClass().getSimpleName());
|
||||
StaticLog.logSevere(t);
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
} catch (IOException e) {
|
||||
} catch (InvalidConfigurationException e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -167,10 +167,10 @@ public class PathUtils {
|
||||
* @param addPaths
|
||||
*/
|
||||
public static void setPaths(final ConfigFile config, final Map<String, Object> setPaths) {
|
||||
for (final Entry<String, Object> entry : setPaths.entrySet()){
|
||||
config.set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
for (final Entry<String, Object> entry : setPaths.entrySet()){
|
||||
config.set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new ConfigFile instance with all paths removed (recursively by prefix).
|
||||
@ -178,63 +178,63 @@ public class PathUtils {
|
||||
* @param removePaths
|
||||
* @return
|
||||
*/
|
||||
public static ConfigFile removePaths(final ConfigFile config, final Collection<String> removePaths) {
|
||||
final SimpleCharPrefixTree prefixes = new SimpleCharPrefixTree();
|
||||
for (final String path : removePaths){
|
||||
prefixes.feed(path);
|
||||
}
|
||||
final ConfigFile newConfig = new ConfigFile();
|
||||
for (final Entry<String, Object> entry : config.getValues(true).entrySet()){
|
||||
final String path = entry.getKey();
|
||||
final Object value = entry.getValue();
|
||||
if (value instanceof ConfigurationSection){
|
||||
continue;
|
||||
}
|
||||
if (!prefixes.hasPrefix(path)){
|
||||
newConfig.set(path, value);
|
||||
}
|
||||
}
|
||||
return newConfig;
|
||||
}
|
||||
public static ConfigFile removePaths(final ConfigFile config, final Collection<String> removePaths) {
|
||||
final SimpleCharPrefixTree prefixes = new SimpleCharPrefixTree();
|
||||
for (final String path : removePaths){
|
||||
prefixes.feed(path);
|
||||
}
|
||||
final ConfigFile newConfig = new ConfigFile();
|
||||
for (final Entry<String, Object> entry : config.getValues(true).entrySet()){
|
||||
final String path = entry.getKey();
|
||||
final Object value = entry.getValue();
|
||||
if (value instanceof ConfigurationSection){
|
||||
continue;
|
||||
}
|
||||
if (!prefixes.hasPrefix(path)){
|
||||
newConfig.set(path, value);
|
||||
}
|
||||
}
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
* @param config
|
||||
* @param configName
|
||||
* @param removePaths
|
||||
* @param addPaths
|
||||
* @param addPaths
|
||||
* @return If entries were added (paths to be removed are processed later).
|
||||
*/
|
||||
protected static void processMovedPaths(final ConfigFile config, final String configName, final Set<String> removePaths, final Map<String, Object> addPaths) {
|
||||
final Logger logger = Bukkit.getLogger();
|
||||
for (final Entry<String, String> entry : movedPaths.entrySet()){
|
||||
final String path = entry.getKey();
|
||||
if (config.contains(path)){
|
||||
final String newPath = entry.getValue();
|
||||
final String to;
|
||||
if (newPath == null | newPath.isEmpty()){
|
||||
to = ".";
|
||||
}
|
||||
else{
|
||||
to = " to '" + newPath + "'.";
|
||||
final Object value = config.get(path);
|
||||
config.set(newPath, value);
|
||||
addPaths.put(newPath, value);
|
||||
removePaths.add(path);
|
||||
}
|
||||
logger.warning("[NoCheatPlus] Config path '" + path + "' (" + configName + ") has been moved" + to);
|
||||
}
|
||||
}
|
||||
}
|
||||
final Logger logger = Bukkit.getLogger();
|
||||
for (final Entry<String, String> entry : movedPaths.entrySet()){
|
||||
final String path = entry.getKey();
|
||||
if (config.contains(path)){
|
||||
final String newPath = entry.getValue();
|
||||
final String to;
|
||||
if (newPath == null | newPath.isEmpty()){
|
||||
to = ".";
|
||||
}
|
||||
else{
|
||||
to = " to '" + newPath + "'.";
|
||||
final Object value = config.get(path);
|
||||
config.set(newPath, value);
|
||||
addPaths.put(newPath, value);
|
||||
removePaths.add(path);
|
||||
}
|
||||
logger.warning("[NoCheatPlus] Config path '" + path + "' (" + configName + ") has been moved" + to);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Warn about paths that are deprecated (not in use).
|
||||
* @param config
|
||||
* @param paths
|
||||
* @param configName
|
||||
*/
|
||||
protected static void processDeprecatedPaths(ConfigFile config, String configName, final Set<String> removePaths){
|
||||
warnPaths(config, deprecatedPrefixes, " (" + configName + ") is not in use anymore.", removePaths);
|
||||
warnPaths(config, deprecatedPrefixes, " (" + configName + ") is not in use anymore.", removePaths);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -244,7 +244,7 @@ public class PathUtils {
|
||||
* @param configName
|
||||
*/
|
||||
protected static void processGlobalOnlyPaths(ConfigFile config, String configName, final Set<String> removePaths){
|
||||
warnPaths(config, globalOnlyPrefixes, " (" + configName + ") should only be set in the global configuration.", removePaths);
|
||||
warnPaths(config, globalOnlyPrefixes, " (" + configName + ") should only be set in the global configuration.", removePaths);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -253,40 +253,40 @@ public class PathUtils {
|
||||
* @return
|
||||
*/
|
||||
public static MemoryConfiguration getWorldsDefaultConfig(final ConfigFile defaultConfig){
|
||||
final char sep = defaultConfig.options().pathSeparator();
|
||||
final MemoryConfiguration config = new ConfigFile();
|
||||
config.options().pathSeparator(sep);
|
||||
final Map<String, Object> defaults = defaultConfig.getValues(false);
|
||||
for (final Entry<String, Object> entry : defaults.entrySet()){
|
||||
final String part = entry.getKey();
|
||||
if (!part.isEmpty() && !mayBeInWorldConfig(part)) continue;
|
||||
final Object value = entry.getValue();
|
||||
if (value instanceof ConfigurationSection) addWorldConfigSection(config, (ConfigurationSection) value, part, sep);
|
||||
else config.set(part, value);
|
||||
}
|
||||
return config;
|
||||
final char sep = defaultConfig.options().pathSeparator();
|
||||
final MemoryConfiguration config = new ConfigFile();
|
||||
config.options().pathSeparator(sep);
|
||||
final Map<String, Object> defaults = defaultConfig.getValues(false);
|
||||
for (final Entry<String, Object> entry : defaults.entrySet()){
|
||||
final String part = entry.getKey();
|
||||
if (!part.isEmpty() && !mayBeInWorldConfig(part)) continue;
|
||||
final Object value = entry.getValue();
|
||||
if (value instanceof ConfigurationSection) addWorldConfigSection(config, (ConfigurationSection) value, part, sep);
|
||||
else config.set(part, value);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
protected static void addWorldConfigSection(final MemoryConfiguration config, final ConfigurationSection section, final String path, final char sep) {
|
||||
final Map<String, Object> values = section.getValues(false);
|
||||
for (final Entry<String, Object> entry : values.entrySet()){
|
||||
final String fullPath = path + sep + entry.getKey();
|
||||
if (!mayBeInWorldConfig(fullPath)) continue;
|
||||
final Object value = entry.getValue();
|
||||
if (value instanceof ConfigurationSection) addWorldConfigSection(config, (ConfigurationSection) value, fullPath, sep);
|
||||
else config.set(fullPath, value);
|
||||
}
|
||||
}
|
||||
protected static void addWorldConfigSection(final MemoryConfiguration config, final ConfigurationSection section, final String path, final char sep) {
|
||||
final Map<String, Object> values = section.getValues(false);
|
||||
for (final Entry<String, Object> entry : values.entrySet()){
|
||||
final String fullPath = path + sep + entry.getKey();
|
||||
if (!mayBeInWorldConfig(fullPath)) continue;
|
||||
final Object value = entry.getValue();
|
||||
if (value instanceof ConfigurationSection) addWorldConfigSection(config, (ConfigurationSection) value, fullPath, sep);
|
||||
else config.set(fullPath, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean mayBeInWorldConfig(final String path){
|
||||
if (globalOnlyPrefixes.hasPrefix(path)) return false;
|
||||
return mayBeInConfig(path);
|
||||
}
|
||||
public static boolean mayBeInWorldConfig(final String path){
|
||||
if (globalOnlyPrefixes.hasPrefix(path)) return false;
|
||||
return mayBeInConfig(path);
|
||||
}
|
||||
|
||||
public static boolean mayBeInConfig(final String path){
|
||||
if (deprecatedPrefixes.hasPrefix(path)) return false;
|
||||
if (movedPaths.containsKey(path)) return false;
|
||||
return true;
|
||||
}
|
||||
public static boolean mayBeInConfig(final String path){
|
||||
if (deprecatedPrefixes.hasPrefix(path)) return false;
|
||||
if (movedPaths.containsKey(path)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,24 +12,24 @@ import java.util.Collection;
|
||||
*/
|
||||
public interface WorldConfigProvider <C extends RawConfigFile>{
|
||||
|
||||
/**
|
||||
* Get the default configuration.
|
||||
* @return
|
||||
*/
|
||||
public C getDefaultConfig();
|
||||
/**
|
||||
* Get the default configuration.
|
||||
* @return
|
||||
*/
|
||||
public C getDefaultConfig();
|
||||
|
||||
/**
|
||||
* Get the world configuration.
|
||||
* @param worldName The default config has null as world. The default config is returned, if the world is not known.
|
||||
* @return
|
||||
*/
|
||||
public C getConfig(String worldName);
|
||||
/**
|
||||
* Get the world configuration.
|
||||
* @param worldName The default config has null as world. The default config is returned, if the world is not known.
|
||||
* @return
|
||||
*/
|
||||
public C getConfig(String worldName);
|
||||
|
||||
/**
|
||||
* Get a Collection-view of all worlds config files, including the default configuration.
|
||||
* @return
|
||||
*/
|
||||
public Collection<C> getAllConfigs();
|
||||
/**
|
||||
* Get a Collection-view of all worlds config files, including the default configuration.
|
||||
* @return
|
||||
*/
|
||||
public Collection<C> getAllConfigs();
|
||||
|
||||
// TODO: Add operations for all configs, like setForAllConfigs, get(Max|min)NumberForAllConfigs, ....
|
||||
// TODO: Add operations for all configs, like setForAllConfigs, get(Max|min)NumberForAllConfigs, ....
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user