This commit is contained in:
asofold 2015-01-05 22:02:00 +01:00
parent 41e9d89efa
commit bd5794d0bd
4 changed files with 501 additions and 499 deletions

View File

@ -17,159 +17,161 @@ import java.util.Set;
*/ */
public class LinkedHashMapCOW<K, V> implements Map<K, V> { 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 LinkedHashMap<K, V> map;
private final float loadFactor;
/** private final int initialCapacity;
* Uses: 16, 0.75f, false (default settings, insertion ordered). private final float loadFactor;
*/
public LinkedHashMapCOW() {
this(16, 0.75f);
}
/** /**
* Uses extra: 0.75f, false (default settings, insertion ordered). * Uses: 16, 0.75f, false (default settings, insertion ordered).
* @param initialCapacity */
*/ public LinkedHashMapCOW() {
public LinkedHashMapCOW(int initialCapacity) { this(16, 0.75f);
this(initialCapacity, 0.75f); }
}
/** /**
* Uses extra: false (default settings, insertion ordered). * Uses extra: 0.75f, false (default settings, insertion ordered).
* @param initialCapacity * @param initialCapacity
* @param loadFactor */
*/ public LinkedHashMapCOW(int initialCapacity) {
public LinkedHashMapCOW(int initialCapacity, float loadFactor) { this(initialCapacity, 0.75f);
this.initialCapacity = initialCapacity; }
this.loadFactor = loadFactor;
this.map = new LinkedHashMap<K, V>(initialCapacity, loadFactor, false);
}
/** /**
* Uses: 16, 0.75f, false (default settings, insertion ordered). * Uses extra: false (default settings, insertion ordered).
* @param map * @param initialCapacity
*/ * @param loadFactor
public LinkedHashMapCOW(Map<K, V> map) { */
this(); public LinkedHashMapCOW(int initialCapacity, float loadFactor) {
this.map.putAll(map); this.initialCapacity = initialCapacity;
} this.loadFactor = loadFactor;
this.map = new LinkedHashMap<K, V>(initialCapacity, loadFactor, false);
}
/** /**
* Not synchronized: return a copy of the internal map. * Uses: 16, 0.75f, false (default settings, insertion ordered).
* @return * @param map
*/ */
private LinkedHashMap<K, V> copyMap() { public LinkedHashMapCOW(Map<K, V> map) {
final LinkedHashMap<K, V> newMap = new LinkedHashMap<K, V>(initialCapacity, loadFactor, false); this();
newMap.putAll(this.map); this.map.putAll(map);
return newMap; }
}
@Override /**
public void clear() { * Not synchronized: return a copy of the internal map.
synchronized (this) { * @return
this.map.clear(); */
} private LinkedHashMap<K, V> copyMap() {
} final LinkedHashMap<K, V> newMap = new LinkedHashMap<K, V>(initialCapacity, loadFactor, false);
newMap.putAll(this.map);
return newMap;
}
@Override @Override
public boolean containsKey(Object key) { public void clear() {
return this.map.containsKey(key); synchronized (this) {
} this.map.clear();
}
}
@Override @Override
public boolean containsValue(Object value) { public boolean containsKey(Object key) {
return this.map.containsValue(value); return this.map.containsKey(key);
} }
/** @Override
* Unmodifiable version of the EntrySet. Entry.setValue might be possible, but dangerous :p public boolean containsValue(Object value) {
*/ return this.map.containsValue(value);
@Override }
public Set<java.util.Map.Entry<K, V>> entrySet() {
return Collections.unmodifiableSet(map.entrySet());
}
@Override /**
public V get(Object key) { * Unmodifiable version of the EntrySet. Entry.setValue might be possible, but dangerous :p
// 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 Set<java.util.Map.Entry<K, V>> entrySet() {
return Collections.unmodifiableSet(map.entrySet());
}
@Override @Override
public boolean isEmpty() { public V get(Object key) {
return map.isEmpty(); // 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
* Unmodifiable version of the KeySet. public boolean isEmpty() {
*/ return map.isEmpty();
@Override }
public Set<K> keySet() {
return Collections.unmodifiableSet(map.keySet());
}
@Override /**
public V put(final K key, final V value) { * Unmodifiable version of the KeySet.
final V out; */
synchronized (this) { @Override
final LinkedHashMap<K, V> newMap = copyMap(); public Set<K> keySet() {
out = newMap.put(key, value); return Collections.unmodifiableSet(map.keySet());
this.map = newMap; }
}
return out;
}
@Override @Override
public void putAll(final Map<? extends K, ? extends V> m) { public V put(final K key, final V value) {
synchronized (this) { final V out;
final LinkedHashMap<K, V> newMap = copyMap(); synchronized (this) {
newMap.putAll(m); final LinkedHashMap<K, V> newMap = copyMap();
this.map = newMap; out = newMap.put(key, value);
} this.map = newMap;
} }
return out;
}
@Override @Override
public V remove(final Object key) { public void putAll(final Map<? extends K, ? extends V> m) {
final V out; synchronized (this) {
synchronized (this) { final LinkedHashMap<K, V> newMap = copyMap();
final LinkedHashMap<K, V> newMap = copyMap(); newMap.putAll(m);
out = newMap.remove(key); this.map = newMap;
this.map = newMap; }
} }
return out;
}
/** @Override
* Remove all given keys.<br> public V remove(final Object key) {
* Not the most efficient implementation, copying the map and then removing final V out;
* keys, but still better than iterating remove(key). synchronized (this) {
* final LinkedHashMap<K, V> newMap = copyMap();
* @param keys out = newMap.remove(key);
*/ this.map = newMap;
public void removeAll(final Collection<K> keys) { }
synchronized (this) { return out;
final LinkedHashMap<K, V> newMap = copyMap(); }
for (final K key : keys) {
newMap.remove(key);
}
this.map = newMap;
}
}
@Override /**
public int size() { * Remove all given keys.<br>
return map.size(); * 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
* Unmodifiable version of the values (Collection). public int size() {
*/ return map.size();
@Override }
public Collection<V> values() {
return Collections.unmodifiableCollection(map.values()); /**
} * Unmodifiable version of the values (Collection).
*/
@Override
public Collection<V> values() {
return Collections.unmodifiableCollection(map.values());
}
} }

View File

@ -35,24 +35,24 @@ public class ConfigManager {
private static final WorldConfigProvider<ConfigFile> worldConfigProvider = new WorldConfigProvider<ConfigFile>() { private static final WorldConfigProvider<ConfigFile> worldConfigProvider = new WorldConfigProvider<ConfigFile>() {
@Override @Override
public ConfigFile getDefaultConfig() { public ConfigFile getDefaultConfig() {
return ConfigManager.getConfigFile(); return ConfigManager.getConfigFile();
} }
@Override @Override
public ConfigFile getConfig(String worldName) { public ConfigFile getConfig(String worldName) {
return ConfigManager.getConfigFile(worldName); return ConfigManager.getConfigFile(worldName);
} }
@Override @Override
public Collection<ConfigFile> getAllConfigs() { public Collection<ConfigFile> getAllConfigs() {
return ConfigManager.worldsMap.values(); return ConfigManager.worldsMap.values();
} }
}; };
private static boolean isInitialized = false; private static boolean isInitialized = false;
/** /**
* Factory method. * Factory method.
@ -75,15 +75,15 @@ public class ConfigManager {
*/ */
public static void setActionFactoryFactory(ActionFactoryFactory factory){ public static void setActionFactoryFactory(ActionFactoryFactory factory){
if (factory != null){ if (factory != null){
actionFactoryFactory = factory; actionFactoryFactory = factory;
} }
else{ else{
actionFactoryFactory = new ActionFactoryFactory() { actionFactoryFactory = new ActionFactoryFactory() {
@Override @Override
public final ActionFactory newActionFactory(final Map<String, Object> library) { public final ActionFactory newActionFactory(final Map<String, Object> library) {
return new ActionFactory(library); return new ActionFactory(library);
} }
}; };
} }
// Use lazy resetting. // Use lazy resetting.
for (final ConfigFile config : worldsMap.values()){ for (final ConfigFile config : worldsMap.values()){
@ -99,18 +99,18 @@ public class ConfigManager {
* Force setting up all configs action factories. * Force setting up all configs action factories.
*/ */
public static void setAllActionFactories(){ public static void setAllActionFactories(){
for (final ConfigFile config : worldsMap.values()){ for (final ConfigFile config : worldsMap.values()){
config.setActionFactory(); config.setActionFactory();
} }
} }
/** /**
* Get the WorldConfigProvider in use. * Get the WorldConfigProvider in use.
* @return * @return
*/ */
public static WorldConfigProvider<ConfigFile> getWorldConfigProvider() { public static WorldConfigProvider<ConfigFile> getWorldConfigProvider() {
return worldConfigProvider; return worldConfigProvider;
} }
/** /**
* Cleanup. * Cleanup.
@ -137,7 +137,7 @@ public class ConfigManager {
*/ */
@Deprecated @Deprecated
public static synchronized ConfigFile getConfigFileSync() { public static synchronized ConfigFile getConfigFileSync() {
return getConfigFile(); return getConfigFile();
} }
/** /**
@ -148,23 +148,23 @@ public class ConfigManager {
* @return the configuration file * @return the configuration file
*/ */
public static ConfigFile getConfigFile(final String worldName) { public static ConfigFile getConfigFile(final String worldName) {
final ConfigFile configFile = worldsMap.get(worldName); final ConfigFile configFile = worldsMap.get(worldName);
if (configFile != null){ if (configFile != null){
return configFile; return configFile;
} }
// Expensive only once per world, for the rest of the runtime the file is returned fast. // Expensive only once per world, for the rest of the runtime the file is returned fast.
synchronized(ConfigManager.class){ synchronized(ConfigManager.class){
// Need to check again. // Need to check again.
if (worldsMap.containsKey(worldName)){ if (worldsMap.containsKey(worldName)){
return worldsMap.get(worldName); return worldsMap.get(worldName);
} }
// Copy the whole map with the default configuration set for this world. // Copy the whole map with the default configuration set for this world.
final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap); final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap);
final ConfigFile globalConfig = newWorldsMap.get(null); final ConfigFile globalConfig = newWorldsMap.get(null);
newWorldsMap.put(worldName, globalConfig); newWorldsMap.put(worldName, globalConfig);
ConfigManager.worldsMap = newWorldsMap; ConfigManager.worldsMap = newWorldsMap;
return globalConfig; return globalConfig;
} }
} }
/** /**
@ -175,7 +175,7 @@ public class ConfigManager {
*/ */
@Deprecated @Deprecated
public static synchronized ConfigFile getConfigFileSync(final String worldName) { 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 * the instance of NoCheatPlus
*/ */
public static synchronized void init(final Plugin plugin) { public static synchronized void init(final Plugin plugin) {
// (This can lead to minor problems with async checks during reloading.) // (This can lead to minor problems with async checks during reloading.)
LinkedHashMap<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(); LinkedHashMap<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>();
// Try to obtain and parse the global configuration file. // Try to obtain and parse the global configuration file.
final File globalFile = new File(plugin.getDataFolder(), "config.yml"); final File globalFile = new File(plugin.getDataFolder(), "config.yml");
PathUtils.processPaths(globalFile, "global config", false); PathUtils.processPaths(globalFile, "global config", false);
@ -194,39 +194,39 @@ public class ConfigManager {
globalConfig.setDefaults(new DefaultConfig()); globalConfig.setDefaults(new DefaultConfig());
globalConfig.options().copyDefaults(true); globalConfig.options().copyDefaults(true);
if (globalFile.exists()){ if (globalFile.exists()){
try { try {
globalConfig.load(globalFile); globalConfig.load(globalFile);
// Quick shallow ugly fix: only save back if loading was successful. // Quick shallow ugly fix: only save back if loading was successful.
try { try {
if (globalConfig.getBoolean(ConfPaths.SAVEBACKCONFIG)){ if (globalConfig.getBoolean(ConfPaths.SAVEBACKCONFIG)){
if (!globalConfig.contains(ConfPaths.CONFIGVERSION_CREATED)){ if (!globalConfig.contains(ConfPaths.CONFIGVERSION_CREATED)){
// Workaround. // Workaround.
globalConfig.set(ConfPaths.CONFIGVERSION_CREATED, DefaultConfig.buildNumber); globalConfig.set(ConfPaths.CONFIGVERSION_CREATED, DefaultConfig.buildNumber);
} }
globalConfig.set(ConfPaths.CONFIGVERSION_SAVED, DefaultConfig.buildNumber); globalConfig.set(ConfPaths.CONFIGVERSION_SAVED, DefaultConfig.buildNumber);
globalConfig.save(globalFile); globalConfig.save(globalFile);
} }
} catch (final Exception e) { } 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); StaticLog.logSevere(e);
} }
} catch (final Exception e) { } catch (final Exception e) {
StaticLog.logSevere("[NoCheatPlus] Could not load config.yml (see exception below). Continue with default settings..."); StaticLog.logSevere("[NoCheatPlus] Could not load config.yml (see exception below). Continue with default settings...");
StaticLog.logSevere(e); StaticLog.logSevere(e);
} }
} }
else { else {
globalConfig.options().header("This configuration was auto-generated by NoCheatPlus."); globalConfig.options().header("This configuration was auto-generated by NoCheatPlus.");
globalConfig.options().copyHeader(true); globalConfig.options().copyHeader(true);
try { try {
globalConfig.set(ConfPaths.CONFIGVERSION_CREATED, DefaultConfig.buildNumber); globalConfig.set(ConfPaths.CONFIGVERSION_CREATED, DefaultConfig.buildNumber);
globalConfig.set(ConfPaths.CONFIGVERSION_SAVED, DefaultConfig.buildNumber); globalConfig.set(ConfPaths.CONFIGVERSION_SAVED, DefaultConfig.buildNumber);
globalConfig.save(globalFile); globalConfig.save(globalFile);
} catch (final Exception e) { } catch (final Exception e) {
StaticLog.logSevere(e); StaticLog.logSevere(e);
} }
} }
// globalConfig.setActionFactory(); // globalConfig.setActionFactory();
newWorldsMap.put(null, globalConfig); newWorldsMap.put(null, globalConfig);
@ -235,8 +235,8 @@ public class ConfigManager {
// Try to obtain and parse the world-specific configuration files. // Try to obtain and parse the world-specific configuration files.
final HashMap<String, File> worldFiles = new HashMap<String, File>(); final HashMap<String, File> worldFiles = new HashMap<String, File>();
if (plugin.getDataFolder().isDirectory()){ if (plugin.getDataFolder().isDirectory()){
for (final File file : plugin.getDataFolder().listFiles()){ for (final File file : plugin.getDataFolder().listFiles()){
if (file.isFile()) { if (file.isFile()) {
final String fileName = file.getName(); final String fileName = file.getName();
if (fileName.matches(".+_config.yml$")) { if (fileName.matches(".+_config.yml$")) {
final String worldname = fileName.substring(0, fileName.length() - 11); final String worldname = fileName.substring(0, fileName.length() - 11);
@ -252,21 +252,21 @@ public class ConfigManager {
worldConfig.setDefaults(worldDefaults); worldConfig.setDefaults(worldDefaults);
worldConfig.options().copyDefaults(true); worldConfig.options().copyDefaults(true);
try { try {
worldConfig.load(worldFile); worldConfig.load(worldFile);
newWorldsMap.put(worldEntry.getKey(), worldConfig); newWorldsMap.put(worldEntry.getKey(), worldConfig);
try{ try{
if (worldConfig.getBoolean(ConfPaths.SAVEBACKCONFIG)) worldConfig.save(worldFile); if (worldConfig.getBoolean(ConfPaths.SAVEBACKCONFIG)) worldConfig.save(worldFile);
} catch (final Exception e){ } catch (final Exception e){
StaticLog.logSevere("[NoCheatPlus] Couldn't save back world-specific configuration for " + worldEntry.getKey() + " (see exception below)."); StaticLog.logSevere("[NoCheatPlus] Couldn't save back world-specific configuration for " + worldEntry.getKey() + " (see exception below).");
StaticLog.logSevere(e); StaticLog.logSevere(e);
} }
} catch (final Exception 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("[NoCheatPlus] Couldn't load world-specific configuration for " + worldEntry.getKey() + " (see exception below). Continue with global default settings...");
StaticLog.logSevere(e); StaticLog.logSevere(e);
} }
worldConfig.setDefaults(globalConfig); worldConfig.setDefaults(globalConfig);
worldConfig.options().copyDefaults(true); worldConfig.options().copyDefaults(true);
// worldConfig.setActionFactory(); // worldConfig.setActionFactory();
} }
ConfigManager.worldsMap = newWorldsMap; ConfigManager.worldsMap = newWorldsMap;
isInitialized = true; isInitialized = true;
@ -286,11 +286,11 @@ public class ConfigManager {
* @param value * @param value
*/ */
public static synchronized void setForAllConfigs(String path, Object value){ public static synchronized void setForAllConfigs(String path, Object value){
final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap); final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap);
for (final ConfigFile cfg : newWorldsMap.values()){ for (final ConfigFile cfg : newWorldsMap.values()){
cfg.set(path, value); cfg.set(path, value);
} }
ConfigManager.worldsMap = newWorldsMap; 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. * @return True if any config has a boolean set to true for the given path.
*/ */
public static boolean isTrueForAnyConfig(String path) { public static boolean isTrueForAnyConfig(String path) {
for (final ConfigFile cfg : worldsMap.values()){ for (final ConfigFile cfg : worldsMap.values()){
if (cfg.getBoolean(path, false)) { if (cfg.getBoolean(path, false)) {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
@ -315,22 +315,22 @@ public class ConfigManager {
* @return Value or null. * @return Value or null.
*/ */
public static Double getMaxNumberForAllConfigs(final String path){ public static Double getMaxNumberForAllConfigs(final String path){
Number max = null; Number max = null;
for (final ConfigFile config : worldsMap.values()){ for (final ConfigFile config : worldsMap.values()){
try{ try{
final Object obj = config.get(path); final Object obj = config.get(path);
if (obj instanceof Number){ if (obj instanceof Number){
final Number num = (Number) obj; final Number num = (Number) obj;
if (max == null || num.doubleValue() > max.doubleValue()){ if (max == null || num.doubleValue() > max.doubleValue()){
max = num; max = num;
} }
} }
} }
catch (Throwable t){ catch (Throwable t){
// Holzhammer // Holzhammer
} }
} }
return max.doubleValue(); return max.doubleValue();
} }
/** /**
@ -341,22 +341,22 @@ public class ConfigManager {
* @return Value or null. * @return Value or null.
*/ */
public static Double getMinNumberForAllConfigs(final String path){ public static Double getMinNumberForAllConfigs(final String path){
Number min = null; Number min = null;
for (final ConfigFile config : worldsMap.values()){ for (final ConfigFile config : worldsMap.values()){
try{ try{
final Object obj = config.get(path); final Object obj = config.get(path);
if (obj instanceof Number){ if (obj instanceof Number){
final Number num = (Number) obj; final Number num = (Number) obj;
if (min == null || num.doubleValue() < min.doubleValue()){ if (min == null || num.doubleValue() < min.doubleValue()){
min = num; min = num;
} }
} }
} }
catch (Throwable t){ catch (Throwable t){
// Holzhammer // Holzhammer
} }
} }
return min.doubleValue(); return min.doubleValue();
} }
// TODO: consider: filter(Max|Min)NumberForAllConfigs(String path, String filerPath, boolean filterPreset) // TODO: consider: filter(Max|Min)NumberForAllConfigs(String path, String filerPath, boolean filterPreset)

View File

@ -25,82 +25,82 @@ import fr.neatmonster.nocheatplus.utilities.ds.prefixtree.SimpleCharPrefixTree;
public class PathUtils { public class PathUtils {
// Deprecated paths. // Deprecated paths.
private static final Set<String> deprecatedFields = new LinkedHashSet<String>(); private static final Set<String> deprecatedFields = new LinkedHashSet<String>();
private static final SimpleCharPrefixTree deprecatedPrefixes = new SimpleCharPrefixTree(); private static final SimpleCharPrefixTree deprecatedPrefixes = new SimpleCharPrefixTree();
// Paths only for the global config. // Paths only for the global config.
private static final Set<String> globalOnlyFields = new HashSet<String>(); private static final Set<String> globalOnlyFields = new HashSet<String>();
private static final SimpleCharPrefixTree globalOnlyPrefixes = new SimpleCharPrefixTree(); private static final SimpleCharPrefixTree globalOnlyPrefixes = new SimpleCharPrefixTree();
// Paths moved to other paths. // Paths moved to other paths.
private static final Map<String, String> movedPaths = new LinkedHashMap<String, String>(); private static final Map<String, String> movedPaths = new LinkedHashMap<String, String>();
static{ static{
initPaths(); initPaths();
} }
/** /**
* Initialize annotation-based path properties. * Initialize annotation-based path properties.
* @return * @return
*/ */
private static void initPaths(){ private static void initPaths(){
deprecatedFields.clear(); deprecatedFields.clear();
deprecatedPrefixes.clear(); deprecatedPrefixes.clear();
globalOnlyFields.clear(); globalOnlyFields.clear();
globalOnlyPrefixes.clear(); globalOnlyPrefixes.clear();
movedPaths.clear(); movedPaths.clear();
for (final Field field : ConfPaths.class.getDeclaredFields()){ for (final Field field : ConfPaths.class.getDeclaredFields()){
if (field.getType() != String.class){ if (field.getType() != String.class){
// Only process strings. // Only process strings.
continue; continue;
} }
final String fieldName = field.getName(); final String fieldName = field.getName();
checkAddPrefixes(field, fieldName, GlobalConfig.class, globalOnlyFields, globalOnlyPrefixes); checkAddPrefixes(field, fieldName, GlobalConfig.class, globalOnlyFields, globalOnlyPrefixes);
checkAddPrefixes(field, fieldName, Deprecated.class, deprecatedFields, deprecatedPrefixes); checkAddPrefixes(field, fieldName, Deprecated.class, deprecatedFields, deprecatedPrefixes);
if (field.isAnnotationPresent(Moved.class)){ if (field.isAnnotationPresent(Moved.class)){
// TODO: Prefixes: Might later support relocating entire sections with one annotation? // TODO: Prefixes: Might later support relocating entire sections with one annotation?
addMoved(field, field.getAnnotation(Moved.class)); addMoved(field, field.getAnnotation(Moved.class));
} }
} }
} }
private static void checkAddPrefixes(Field field, String fieldName, Class<? extends Annotation> annotation, Set<String> fieldNames, SimpleCharPrefixTree pathPrefixes) { private static void checkAddPrefixes(Field field, String fieldName, Class<? extends Annotation> annotation, Set<String> fieldNames, SimpleCharPrefixTree pathPrefixes) {
if (field.isAnnotationPresent(annotation)){ if (field.isAnnotationPresent(annotation)){
fieldNames.add(fieldName); fieldNames.add(fieldName);
addPrefixesField(field, pathPrefixes); addPrefixesField(field, pathPrefixes);
} }
else{ else{
for (final String refName : fieldNames){ for (final String refName : fieldNames){
if (fieldName.startsWith(refName)){ if (fieldName.startsWith(refName)){
addPrefixesField(field, pathPrefixes); addPrefixesField(field, pathPrefixes);
} }
} }
} }
} }
private static void addPrefixesField(Field field, SimpleCharPrefixTree pathPrefixes) { private static void addPrefixesField(Field field, SimpleCharPrefixTree pathPrefixes) {
try { try {
final String path = field.get(null).toString(); final String path = field.get(null).toString();
if (path != null){ if (path != null){
pathPrefixes.feed(path); pathPrefixes.feed(path);
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
} }
} }
private static void addMoved(final Field field, final Moved rel) { private static void addMoved(final Field field, final Moved rel) {
try { try {
final String path = field.get(null).toString(); final String path = field.get(null).toString();
movedPaths.put(path, rel.newPath()); movedPaths.put(path, rel.newPath());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
} }
} }
/** /**
* Warn on the console if paths are used. * Warn on the console if paths are used.
* @param config * @param config
* @param paths * @param paths
@ -108,15 +108,15 @@ public class PathUtils {
* @param warnedPaths Paths which were found, can be null. * @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){ protected static void warnPaths(final ConfigFile config, final CharPrefixTree<?, ?> paths, final String msgPrefix, final Set<String> warnedPaths){
final Logger logger = Bukkit.getLogger(); final Logger logger = Bukkit.getLogger();
for (final String path : config.getKeys(true)){ for (final String path : config.getKeys(true)){
if (paths.hasPrefix(path)){ if (paths.hasPrefix(path)){
logger.warning("[NoCheatPlus] Config path '" + path + "'" + msgPrefix); logger.warning("[NoCheatPlus] Config path '" + path + "'" + msgPrefix);
if (warnedPaths != null){ if (warnedPaths != null){
warnedPaths.add(path); warnedPaths.add(path);
} }
} }
} }
} }
/** /**
@ -125,40 +125,40 @@ public class PathUtils {
* @param configName * @param configName
*/ */
public static void processPaths(File file, String configName, boolean isWorldConfig){ public static void processPaths(File file, String configName, boolean isWorldConfig){
ConfigFile config = new ConfigFile(); ConfigFile config = new ConfigFile();
try { try {
config.load(file); config.load(file);
final Set<String> removePaths = new LinkedHashSet<String>(); final Set<String> removePaths = new LinkedHashSet<String>();
final Map<String, Object> addPaths = new LinkedHashMap<String, Object>(); final Map<String, Object> addPaths = new LinkedHashMap<String, Object>();
if (isWorldConfig){ if (isWorldConfig){
// TODO: might remove these [though some global only paths might actually work]. // TODO: might remove these [though some global only paths might actually work].
processGlobalOnlyPaths(config, configName, null); processGlobalOnlyPaths(config, configName, null);
} }
processDeprecatedPaths(config, configName, removePaths); processDeprecatedPaths(config, configName, removePaths);
processMovedPaths(config, configName, removePaths, addPaths); processMovedPaths(config, configName, removePaths, addPaths);
boolean changed = false; boolean changed = false;
if (!removePaths.isEmpty()){ if (!removePaths.isEmpty()){
config = removePaths(config, removePaths); config = removePaths(config, removePaths);
changed = true; changed = true;
} }
if (!addPaths.isEmpty()){ if (!addPaths.isEmpty()){
setPaths(config, addPaths); setPaths(config, addPaths);
changed = true; changed = true;
} }
if (changed){ if (changed){
try{ try{
config.save(file); config.save(file);
} }
catch(Throwable t){ catch(Throwable t){
// Do log this one. // Do log this one.
StaticLog.logSevere("[NoCheatPlus] Failed to save configuration (" + configName + ") with changes: " + t.getClass().getSimpleName()); StaticLog.logSevere("[NoCheatPlus] Failed to save configuration (" + configName + ") with changes: " + t.getClass().getSimpleName());
StaticLog.logSevere(t); StaticLog.logSevere(t);
} }
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
} catch (IOException e) { } catch (IOException e) {
} catch (InvalidConfigurationException e) { } catch (InvalidConfigurationException e) {
} }
} }
/** /**
@ -167,10 +167,10 @@ public class PathUtils {
* @param addPaths * @param addPaths
*/ */
public static void setPaths(final ConfigFile config, final Map<String, Object> setPaths) { public static void setPaths(final ConfigFile config, final Map<String, Object> setPaths) {
for (final Entry<String, Object> entry : setPaths.entrySet()){ for (final Entry<String, Object> entry : setPaths.entrySet()){
config.set(entry.getKey(), entry.getValue()); config.set(entry.getKey(), entry.getValue());
} }
} }
/** /**
* Get a new ConfigFile instance with all paths removed (recursively by prefix). * Get a new ConfigFile instance with all paths removed (recursively by prefix).
@ -178,63 +178,63 @@ public class PathUtils {
* @param removePaths * @param removePaths
* @return * @return
*/ */
public static ConfigFile removePaths(final ConfigFile config, final Collection<String> removePaths) { public static ConfigFile removePaths(final ConfigFile config, final Collection<String> removePaths) {
final SimpleCharPrefixTree prefixes = new SimpleCharPrefixTree(); final SimpleCharPrefixTree prefixes = new SimpleCharPrefixTree();
for (final String path : removePaths){ for (final String path : removePaths){
prefixes.feed(path); prefixes.feed(path);
} }
final ConfigFile newConfig = new ConfigFile(); final ConfigFile newConfig = new ConfigFile();
for (final Entry<String, Object> entry : config.getValues(true).entrySet()){ for (final Entry<String, Object> entry : config.getValues(true).entrySet()){
final String path = entry.getKey(); final String path = entry.getKey();
final Object value = entry.getValue(); final Object value = entry.getValue();
if (value instanceof ConfigurationSection){ if (value instanceof ConfigurationSection){
continue; continue;
} }
if (!prefixes.hasPrefix(path)){ if (!prefixes.hasPrefix(path)){
newConfig.set(path, value); newConfig.set(path, value);
} }
} }
return newConfig; return newConfig;
} }
/** /**
* *
* @param config * @param config
* @param configName * @param configName
* @param removePaths * @param removePaths
* @param addPaths * @param addPaths
* @return If entries were added (paths to be removed are processed later). * @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) { protected static void processMovedPaths(final ConfigFile config, final String configName, final Set<String> removePaths, final Map<String, Object> addPaths) {
final Logger logger = Bukkit.getLogger(); final Logger logger = Bukkit.getLogger();
for (final Entry<String, String> entry : movedPaths.entrySet()){ for (final Entry<String, String> entry : movedPaths.entrySet()){
final String path = entry.getKey(); final String path = entry.getKey();
if (config.contains(path)){ if (config.contains(path)){
final String newPath = entry.getValue(); final String newPath = entry.getValue();
final String to; final String to;
if (newPath == null | newPath.isEmpty()){ if (newPath == null | newPath.isEmpty()){
to = "."; to = ".";
} }
else{ else{
to = " to '" + newPath + "'."; to = " to '" + newPath + "'.";
final Object value = config.get(path); final Object value = config.get(path);
config.set(newPath, value); config.set(newPath, value);
addPaths.put(newPath, value); addPaths.put(newPath, value);
removePaths.add(path); removePaths.add(path);
} }
logger.warning("[NoCheatPlus] Config path '" + path + "' (" + configName + ") has been moved" + to); logger.warning("[NoCheatPlus] Config path '" + path + "' (" + configName + ") has been moved" + to);
} }
} }
} }
/** /**
* Warn about paths that are deprecated (not in use). * Warn about paths that are deprecated (not in use).
* @param config * @param config
* @param paths * @param paths
* @param configName * @param configName
*/ */
protected static void processDeprecatedPaths(ConfigFile config, String configName, final Set<String> removePaths){ 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 * @param configName
*/ */
protected static void processGlobalOnlyPaths(ConfigFile config, String configName, final Set<String> removePaths){ 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 * @return
*/ */
public static MemoryConfiguration getWorldsDefaultConfig(final ConfigFile defaultConfig){ public static MemoryConfiguration getWorldsDefaultConfig(final ConfigFile defaultConfig){
final char sep = defaultConfig.options().pathSeparator(); final char sep = defaultConfig.options().pathSeparator();
final MemoryConfiguration config = new ConfigFile(); final MemoryConfiguration config = new ConfigFile();
config.options().pathSeparator(sep); config.options().pathSeparator(sep);
final Map<String, Object> defaults = defaultConfig.getValues(false); final Map<String, Object> defaults = defaultConfig.getValues(false);
for (final Entry<String, Object> entry : defaults.entrySet()){ for (final Entry<String, Object> entry : defaults.entrySet()){
final String part = entry.getKey(); final String part = entry.getKey();
if (!part.isEmpty() && !mayBeInWorldConfig(part)) continue; if (!part.isEmpty() && !mayBeInWorldConfig(part)) continue;
final Object value = entry.getValue(); final Object value = entry.getValue();
if (value instanceof ConfigurationSection) addWorldConfigSection(config, (ConfigurationSection) value, part, sep); if (value instanceof ConfigurationSection) addWorldConfigSection(config, (ConfigurationSection) value, part, sep);
else config.set(part, value); else config.set(part, value);
} }
return config; return config;
} }
protected static void addWorldConfigSection(final MemoryConfiguration config, final ConfigurationSection section, final String path, final char sep) { protected static void addWorldConfigSection(final MemoryConfiguration config, final ConfigurationSection section, final String path, final char sep) {
final Map<String, Object> values = section.getValues(false); final Map<String, Object> values = section.getValues(false);
for (final Entry<String, Object> entry : values.entrySet()){ for (final Entry<String, Object> entry : values.entrySet()){
final String fullPath = path + sep + entry.getKey(); final String fullPath = path + sep + entry.getKey();
if (!mayBeInWorldConfig(fullPath)) continue; if (!mayBeInWorldConfig(fullPath)) continue;
final Object value = entry.getValue(); final Object value = entry.getValue();
if (value instanceof ConfigurationSection) addWorldConfigSection(config, (ConfigurationSection) value, fullPath, sep); if (value instanceof ConfigurationSection) addWorldConfigSection(config, (ConfigurationSection) value, fullPath, sep);
else config.set(fullPath, value); else config.set(fullPath, value);
} }
} }
public static boolean mayBeInWorldConfig(final String path){ public static boolean mayBeInWorldConfig(final String path){
if (globalOnlyPrefixes.hasPrefix(path)) return false; if (globalOnlyPrefixes.hasPrefix(path)) return false;
return mayBeInConfig(path); return mayBeInConfig(path);
} }
public static boolean mayBeInConfig(final String path){ public static boolean mayBeInConfig(final String path){
if (deprecatedPrefixes.hasPrefix(path)) return false; if (deprecatedPrefixes.hasPrefix(path)) return false;
if (movedPaths.containsKey(path)) return false; if (movedPaths.containsKey(path)) return false;
return true; return true;
} }
} }

View File

@ -12,24 +12,24 @@ import java.util.Collection;
*/ */
public interface WorldConfigProvider <C extends RawConfigFile>{ public interface WorldConfigProvider <C extends RawConfigFile>{
/** /**
* Get the default configuration. * Get the default configuration.
* @return * @return
*/ */
public C getDefaultConfig(); public C getDefaultConfig();
/** /**
* Get the world configuration. * Get the world configuration.
* @param worldName The default config has null as world. The default config is returned, if the world is not known. * @param worldName The default config has null as world. The default config is returned, if the world is not known.
* @return * @return
*/ */
public C getConfig(String worldName); public C getConfig(String worldName);
/** /**
* Get a Collection-view of all worlds config files, including the default configuration. * Get a Collection-view of all worlds config files, including the default configuration.
* @return * @return
*/ */
public Collection<C> getAllConfigs(); 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, ....
} }