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

@ -16,160 +16,162 @@ import java.util.Set;
*
*/
public class LinkedHashMapCOW<K, V> implements Map<K, V> {
private LinkedHashMap<K, V> 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<K, V>(initialCapacity, loadFactor, false);
}
/**
* Uses: 16, 0.75f, false (default settings, insertion ordered).
* @param map
*/
public LinkedHashMapCOW(Map<K, V> map) {
this();
this.map.putAll(map);
}
/**
* 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 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<java.util.Map.Entry<K, V>> entrySet() {
return Collections.unmodifiableSet(map.entrySet());
}
private LinkedHashMap<K, V> 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<K> 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<K, V> 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<? extends K, ? extends V> m) {
synchronized (this) {
final LinkedHashMap<K, V> 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<K, V>(initialCapacity, loadFactor, false);
}
@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;
}
/**
* 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;
}
}
/**
* Uses: 16, 0.75f, false (default settings, insertion ordered).
* @param map
*/
public LinkedHashMapCOW(Map<K, V> map) {
this();
this.map.putAll(map);
}
@Override
public int size() {
return map.size();
}
/**
* Unmodifiable version of the values (Collection).
*/
@Override
public Collection<V> values() {
return Collections.unmodifiableCollection(map.values());
}
/**
* 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 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<java.util.Map.Entry<K, V>> 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<K> keySet() {
return Collections.unmodifiableSet(map.keySet());
}
@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 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 remove(final Object key) {
final V out;
synchronized (this) {
final LinkedHashMap<K, V> newMap = copyMap();
out = newMap.remove(key);
this.map = newMap;
}
return out;
}
/**
* 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 int size() {
return map.size();
}
/**
* Unmodifiable version of the values (Collection).
*/
@Override
public Collection<V> values() {
return Collections.unmodifiableCollection(map.values());
}
}

View File

@ -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<String, Object> library);
}
private static ActionFactoryFactory actionFactoryFactory = new ActionFactoryFactory() {
@Override
public final ActionFactory newActionFactory(final Map<String, Object> library) {
return new ActionFactory(library);
}
};
/** The map containing the configuration files per world. */
private static Map<String, ConfigFile> worldsMap = new LinkedHashMap<String, ConfigFile>();
private static final WorldConfigProvider<ConfigFile> worldConfigProvider = new WorldConfigProvider<ConfigFile>() {
@Override
public ConfigFile getDefaultConfig() {
return ConfigManager.getConfigFile();
}
@Override
public ConfigFile getConfig(String worldName) {
return ConfigManager.getConfigFile(worldName);
}
@Override
public Collection<ConfigFile> 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<ConfigFile> 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<String, Object> 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<String, Object> 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<ConfigFile> getWorldConfigProvider() {
return worldConfigProvider;
}
public static WorldConfigProvider<ConfigFile> 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<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;
}
}
/**
* (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<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,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<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,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<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;
}
/**
* 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.
* <br>
@ -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.
* <br>
@ -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)
}

View File

@ -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<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 moved to other paths.
private static final Map<String, String> movedPaths = new LinkedHashMap<String, String>();
static{
initPaths();
}
// 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 moved to other paths.
private static final Map<String, String> movedPaths = new LinkedHashMap<String, String>();
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<? 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,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<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);
}
}
}
}
/**
* 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<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) {
}
}
/**
* Set paths.
* @param config
* @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).
* @param config
* @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);
}
/**
* 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<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);
}
/**
* 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<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);
}
}
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<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 mayBeInConfig(final String path){
if (deprecatedPrefixes.hasPrefix(path)) return false;
if (movedPaths.containsKey(path)) return false;
return true;
}
}

View File

@ -11,25 +11,25 @@ import java.util.Collection;
*
*/
public interface WorldConfigProvider <C extends RawConfigFile>{
/**
* 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<C> 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<C> getAllConfigs();
// TODO: Add operations for all configs, like setForAllConfigs, get(Max|min)NumberForAllConfigs, ....
}