From bd5794d0bd9f88d36efdbce586c3ad9b0e911422 Mon Sep 17 00:00:00 2001 From: asofold Date: Mon, 5 Jan 2015 22:02:00 +0100 Subject: [PATCH] Spaces. --- .../utilities/ds/corw/LinkedHashMapCOW.java | 296 ++++++------- .../nocheatplus/config/ConfigManager.java | 268 ++++++------ .../nocheatplus/config/PathUtils.java | 394 +++++++++--------- .../config/WorldConfigProvider.java | 42 +- 4 files changed, 501 insertions(+), 499 deletions(-) diff --git a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/corw/LinkedHashMapCOW.java b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/corw/LinkedHashMapCOW.java index ea74c668..80662e6a 100644 --- a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/corw/LinkedHashMapCOW.java +++ b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/corw/LinkedHashMapCOW.java @@ -16,160 +16,162 @@ import java.util.Set; * */ public class LinkedHashMapCOW implements Map { - - private LinkedHashMap map; - - private final int initialCapacity; - private final float loadFactor; - - /** - * Uses: 16, 0.75f, false (default settings, insertion ordered). - */ - public LinkedHashMapCOW() { - this(16, 0.75f); - } - - /** - * Uses extra: 0.75f, false (default settings, insertion ordered). - * @param initialCapacity - */ - public LinkedHashMapCOW(int initialCapacity) { - this(initialCapacity, 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(initialCapacity, loadFactor, false); - } - - /** - * Uses: 16, 0.75f, false (default settings, insertion ordered). - * @param map - */ - public LinkedHashMapCOW(Map map) { - this(); - this.map.putAll(map); - } - - /** - * Not synchronized: return a copy of the internal map. - * @return - */ - private LinkedHashMap copyMap() { - final LinkedHashMap newMap = new LinkedHashMap(initialCapacity, loadFactor, false); - newMap.putAll(this.map); - return newMap; - } - - @Override - public void clear() { - synchronized (this) { - this.map.clear(); - } - } - @Override - public boolean containsKey(Object key) { - return this.map.containsKey(key); - } + // TODO: Consider a) add removeEldest... b) add option: copyMap(K) -> only copy if needed. - @Override - public boolean containsValue(Object value) { - return this.map.containsValue(value); - } - - /** - * Unmodifiable version of the EntrySet. Entry.setValue might be possible, but dangerous :p - */ - @Override - public Set> entrySet() { - return Collections.unmodifiableSet(map.entrySet()); - } + private LinkedHashMap map; - @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); - } + private final int initialCapacity; + private final float loadFactor; - @Override - public boolean isEmpty() { - return map.isEmpty(); - } - - /** - * Unmodifiable version of the KeySet. - */ - @Override - public Set keySet() { - return Collections.unmodifiableSet(map.keySet()); - } + /** + * Uses: 16, 0.75f, false (default settings, insertion ordered). + */ + public LinkedHashMapCOW() { + this(16, 0.75f); + } - @Override - public V put(final K key, final V value) { - final V out; - synchronized (this) { - final LinkedHashMap newMap = copyMap(); - out = newMap.put(key, value); - this.map = newMap; - } - return out; - } + /** + * Uses extra: 0.75f, false (default settings, insertion ordered). + * @param initialCapacity + */ + public LinkedHashMapCOW(int initialCapacity) { + this(initialCapacity, 0.75f); + } - @Override - public void putAll(final Map m) { - synchronized (this) { - final LinkedHashMap newMap = copyMap(); - newMap.putAll(m); - this.map = newMap; - } - } + /** + * 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(initialCapacity, loadFactor, false); + } - @Override - public V remove(final Object key) { - final V out; - synchronized (this) { - final LinkedHashMap newMap = copyMap(); - out = newMap.remove(key); - this.map = newMap; - } - return out; - } - - /** - * Remove all given keys.
- * 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 keys) { - synchronized (this) { - final LinkedHashMap newMap = copyMap(); - for (final K key : keys) { - newMap.remove(key); - } - this.map = newMap; - } - } + /** + * Uses: 16, 0.75f, false (default settings, insertion ordered). + * @param map + */ + public LinkedHashMapCOW(Map map) { + this(); + this.map.putAll(map); + } - @Override - public int size() { - return map.size(); - } - - /** - * Unmodifiable version of the values (Collection). - */ - @Override - public Collection values() { - return Collections.unmodifiableCollection(map.values()); - } + /** + * Not synchronized: return a copy of the internal map. + * @return + */ + private LinkedHashMap copyMap() { + final LinkedHashMap newMap = new LinkedHashMap(initialCapacity, loadFactor, false); + newMap.putAll(this.map); + return newMap; + } + + @Override + public void clear() { + synchronized (this) { + this.map.clear(); + } + } + + @Override + public boolean containsKey(Object key) { + return this.map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return this.map.containsValue(value); + } + + /** + * Unmodifiable version of the EntrySet. Entry.setValue might be possible, but dangerous :p + */ + @Override + public Set> entrySet() { + return Collections.unmodifiableSet(map.entrySet()); + } + + @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); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + /** + * Unmodifiable version of the KeySet. + */ + @Override + public Set keySet() { + return Collections.unmodifiableSet(map.keySet()); + } + + @Override + public V put(final K key, final V value) { + final V out; + synchronized (this) { + final LinkedHashMap newMap = copyMap(); + out = newMap.put(key, value); + this.map = newMap; + } + return out; + } + + @Override + public void putAll(final Map m) { + synchronized (this) { + final LinkedHashMap newMap = copyMap(); + newMap.putAll(m); + this.map = newMap; + } + } + + @Override + public V remove(final Object key) { + final V out; + synchronized (this) { + final LinkedHashMap newMap = copyMap(); + out = newMap.remove(key); + this.map = newMap; + } + return out; + } + + /** + * Remove all given keys.
+ * 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 keys) { + synchronized (this) { + final LinkedHashMap newMap = copyMap(); + for (final K key : keys) { + newMap.remove(key); + } + this.map = newMap; + } + } + + @Override + public int size() { + return map.size(); + } + + /** + * Unmodifiable version of the values (Collection). + */ + @Override + public Collection values() { + return Collections.unmodifiableCollection(map.values()); + } } diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/ConfigManager.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/ConfigManager.java index 665f8f36..b3b7cd14 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/ConfigManager.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/ConfigManager.java @@ -18,42 +18,42 @@ import fr.neatmonster.nocheatplus.logging.StaticLog; * The synchronized methods are to ensure that changing the configurations won't lead to trouble for the asynchronous checks. */ public class ConfigManager { - + public static interface ActionFactoryFactory{ public ActionFactory newActionFactory(Map library); } - + private static ActionFactoryFactory actionFactoryFactory = new ActionFactoryFactory() { @Override public final ActionFactory newActionFactory(final Map library) { return new ActionFactory(library); } }; - + /** The map containing the configuration files per world. */ private static Map worldsMap = new LinkedHashMap(); - + private static final WorldConfigProvider worldConfigProvider = new WorldConfigProvider() { - - @Override - public ConfigFile getDefaultConfig() { - return ConfigManager.getConfigFile(); - } - - @Override - public ConfigFile getConfig(String worldName) { - return ConfigManager.getConfigFile(worldName); - } - - @Override - public Collection getAllConfigs() { - return ConfigManager.worldsMap.values(); - } - - }; - - private static boolean isInitialized = false; - + + @Override + public ConfigFile getDefaultConfig() { + return ConfigManager.getConfigFile(); + } + + @Override + public ConfigFile getConfig(String worldName) { + return ConfigManager.getConfigFile(worldName); + } + + @Override + public Collection getAllConfigs() { + return ConfigManager.worldsMap.values(); + } + + }; + + private static boolean isInitialized = false; + /** * Factory method. * @param library @@ -62,7 +62,7 @@ public class ConfigManager { public static ActionFactory getActionFactory(final Map library){ return actionFactoryFactory.newActionFactory(library); } - + /** * Set the factory to get actions from. * This will reset all ActionFactories to null (lazy initialization), @@ -75,42 +75,42 @@ 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 library) { return new ActionFactory(library); } - }; + }; } // Use lazy resetting. for (final ConfigFile config : worldsMap.values()){ config.setActionFactory(null); } } - + public static ActionFactoryFactory getActionFactoryFactory(){ return actionFactoryFactory; } - + /** * 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 getWorldConfigProvider() { - return worldConfigProvider; - } + public static WorldConfigProvider getWorldConfigProvider() { + return worldConfigProvider; + } /** * Cleanup. @@ -129,7 +129,7 @@ public class ConfigManager { public static ConfigFile getConfigFile() { return worldsMap.get(null); } - + /** * (Synchronized version). * @return @@ -137,7 +137,7 @@ public class ConfigManager { */ @Deprecated public static synchronized ConfigFile getConfigFileSync() { - return getConfigFile(); + return getConfigFile(); } /** @@ -148,25 +148,25 @@ 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 newWorldsMap = new LinkedHashMap(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 newWorldsMap = new LinkedHashMap(ConfigManager.worldsMap); + final ConfigFile globalConfig = newWorldsMap.get(null); + newWorldsMap.put(worldName, globalConfig); + ConfigManager.worldsMap = newWorldsMap; + return globalConfig; + } } - + /** * (Synchronized version). * @param worldName @@ -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 newWorldsMap = new LinkedHashMap(); + // (This can lead to minor problems with async checks during reloading.) + LinkedHashMap newWorldsMap = new LinkedHashMap(); // 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,49 +194,49 @@ 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); - + final MemoryConfiguration worldDefaults = PathUtils.getWorldsDefaultConfig(globalConfig); - + // Try to obtain and parse the world-specific configuration files. final HashMap worldFiles = new HashMap(); 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,26 +252,26 @@ 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; } - + /** * Informal test if the init method completed (no details are reflected). * @return @@ -279,34 +279,34 @@ public class ConfigManager { public static boolean isInitialized() { return isInitialized; } - + /** * Set a property for all configurations. Might use with DataManager.clearConfigs if check-configurations might already be in use. * @param path * @param value */ public static synchronized void setForAllConfigs(String path, Object value){ - final Map newWorldsMap = new LinkedHashMap(ConfigManager.worldsMap); - for (final ConfigFile cfg : newWorldsMap.values()){ - cfg.set(path, value); - } - ConfigManager.worldsMap = newWorldsMap; + final Map newWorldsMap = new LinkedHashMap(ConfigManager.worldsMap); + for (final ConfigFile cfg : newWorldsMap.values()){ + cfg.set(path, value); + } + ConfigManager.worldsMap = newWorldsMap; } - + /** * Check if any config has a boolean set to true for the given path. * @param path * @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; } - + /** * Get the maximally found number for the given config path. This does not throw errors. It will return null, if nothing is found or all lookups failed otherwise. *
@@ -315,24 +315,24 @@ 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(); } - + /** * Get the minimally found number for the given config path. This does not throw errors. It will return null, if nothing is found or all lookups failed otherwise. *
@@ -341,24 +341,24 @@ 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) - + } diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/PathUtils.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/PathUtils.java index 7be3161b..019273f1 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/PathUtils.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/PathUtils.java @@ -24,83 +24,83 @@ import fr.neatmonster.nocheatplus.utilities.ds.prefixtree.CharPrefixTree; import fr.neatmonster.nocheatplus.utilities.ds.prefixtree.SimpleCharPrefixTree; public class PathUtils { - - // Deprecated paths. - private static final Set deprecatedFields = new LinkedHashSet(); - private static final SimpleCharPrefixTree deprecatedPrefixes = new SimpleCharPrefixTree(); - - // Paths only for the global config. - private static final Set globalOnlyFields = new HashSet(); - private static final SimpleCharPrefixTree globalOnlyPrefixes = new SimpleCharPrefixTree(); - - // Paths moved to other paths. - private static final Map movedPaths = new LinkedHashMap(); - - static{ - initPaths(); - } - + + // Deprecated paths. + private static final Set deprecatedFields = new LinkedHashSet(); + private static final SimpleCharPrefixTree deprecatedPrefixes = new SimpleCharPrefixTree(); + + // Paths only for the global config. + private static final Set globalOnlyFields = new HashSet(); + private static final SimpleCharPrefixTree globalOnlyPrefixes = new SimpleCharPrefixTree(); + + // Paths moved to other paths. + private static final Map movedPaths = new LinkedHashMap(); + + 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(); - - 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)); - } - } + 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)); + } + } } - + private static void checkAddPrefixes(Field field, String fieldName, Class annotation, Set 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,135 +108,135 @@ 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 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); + } + } + } } - + /** * Run all warning checks and alter config if necessary (GlobalConfig, Deprecated, Moved). * @param file * @param configName */ public static void processPaths(File file, String configName, boolean isWorldConfig){ - ConfigFile config = new ConfigFile(); - try { - config.load(file); - final Set removePaths = new LinkedHashSet(); - final Map addPaths = new LinkedHashMap(); - 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 removePaths = new LinkedHashSet(); + final Map addPaths = new LinkedHashMap(); + 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) { + } } - + /** * Set paths. * @param config * @param addPaths */ public static void setPaths(final ConfigFile config, final Map setPaths) { - for (final Entry entry : setPaths.entrySet()){ - config.set(entry.getKey(), entry.getValue()); - } - } - + for (final Entry entry : setPaths.entrySet()){ + config.set(entry.getKey(), entry.getValue()); + } + } + /** * Get a new ConfigFile instance with all paths removed (recursively by prefix). * @param config * @param removePaths * @return */ - public static ConfigFile removePaths(final ConfigFile config, final Collection removePaths) { - final SimpleCharPrefixTree prefixes = new SimpleCharPrefixTree(); - for (final String path : removePaths){ - prefixes.feed(path); - } - final ConfigFile newConfig = new ConfigFile(); - for (final Entry 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 removePaths) { + final SimpleCharPrefixTree prefixes = new SimpleCharPrefixTree(); + for (final String path : removePaths){ + prefixes.feed(path); + } + final ConfigFile newConfig = new ConfigFile(); + for (final Entry 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 removePaths, final Map addPaths) { - final Logger logger = Bukkit.getLogger(); - for (final Entry 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 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 removePaths){ - warnPaths(config, deprecatedPrefixes, " (" + configName + ") is not in use anymore.", removePaths); + warnPaths(config, deprecatedPrefixes, " (" + configName + ") is not in use anymore.", removePaths); } - + /** * Warn about paths that are only to be set in the global config. * @param config @@ -244,49 +244,49 @@ public class PathUtils { * @param configName */ protected static void processGlobalOnlyPaths(ConfigFile config, String configName, final Set 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); } - + /** * A config file only containing the entries that are not set as global only. * @param defaultConfig * @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 defaults = defaultConfig.getValues(false); - for (final Entry 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 defaults = defaultConfig.getValues(false); + for (final Entry 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 values = section.getValues(false); + for (final Entry 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 mayBeInConfig(final String path){ + if (deprecatedPrefixes.hasPrefix(path)) return false; + if (movedPaths.containsKey(path)) return false; + return true; } - protected static void addWorldConfigSection(final MemoryConfiguration config, final ConfigurationSection section, final String path, final char sep) { - final Map values = section.getValues(false); - for (final Entry 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 mayBeInConfig(final String path){ - if (deprecatedPrefixes.hasPrefix(path)) return false; - if (movedPaths.containsKey(path)) return false; - return true; - } - } diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/WorldConfigProvider.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/WorldConfigProvider.java index 682ef2d9..03673061 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/WorldConfigProvider.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/WorldConfigProvider.java @@ -11,25 +11,25 @@ import java.util.Collection; * */ public interface WorldConfigProvider { - - /** - * 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 a Collection-view of all worlds config files, including the default configuration. - * @return - */ - public Collection getAllConfigs(); - - // TODO: Add operations for all configs, like setForAllConfigs, get(Max|min)NumberForAllConfigs, .... + + /** + * 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 a Collection-view of all worlds config files, including the default configuration. + * @return + */ + public Collection getAllConfigs(); + + // TODO: Add operations for all configs, like setForAllConfigs, get(Max|min)NumberForAllConfigs, .... }