Refactor ServerUtilsConfig#set to delete existing objects at path
This commit is contained in:
parent
90b248f321
commit
d2928b4a04
5 changed files with 67 additions and 21 deletions
|
|
@ -54,10 +54,15 @@ public class BukkitYamlConfig implements ServerUtilsConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void set(String path, Object value) {
|
public void setUnsafe(String path, Object value) {
|
||||||
config.set(path, value);
|
config.set(path, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(String path) {
|
||||||
|
config.set(path, null);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getString(String path) {
|
public String getString(String path) {
|
||||||
return config.getString(path);
|
return config.getString(path);
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,13 @@ public class BungeeYamlConfig implements ServerUtilsConfig {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object get(String path) {
|
public Object get(String path) {
|
||||||
Object obj = config.get(path);
|
Object obj;
|
||||||
|
try {
|
||||||
|
obj = config.get(path);
|
||||||
|
} catch (ClassCastException ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (obj instanceof Configuration) {
|
if (obj instanceof Configuration) {
|
||||||
return new BungeeYamlConfig((Configuration) obj);
|
return new BungeeYamlConfig((Configuration) obj);
|
||||||
}
|
}
|
||||||
|
|
@ -61,10 +67,15 @@ public class BungeeYamlConfig implements ServerUtilsConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void set(String path, Object value) {
|
public void setUnsafe(String path, Object value) {
|
||||||
config.set(path, value);
|
config.set(path, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(String path) {
|
||||||
|
config.set(path, null);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getString(String path) {
|
public String getString(String path) {
|
||||||
return config.getString(path);
|
return config.getString(path);
|
||||||
|
|
|
||||||
|
|
@ -137,7 +137,7 @@ public class JsonConfig implements ServerUtilsConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void set(String path, Object value) {
|
public void setUnsafe(String path, Object value) {
|
||||||
int lastDotIndex = path.lastIndexOf('.');
|
int lastDotIndex = path.lastIndexOf('.');
|
||||||
|
|
||||||
String memberName = path;
|
String memberName = path;
|
||||||
|
|
@ -158,6 +158,20 @@ public class JsonConfig implements ServerUtilsConfig {
|
||||||
jsonObject.add(memberName, gson.toJsonTree(value));
|
jsonObject.add(memberName, gson.toJsonTree(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(String path) {
|
||||||
|
int lastDotIndex = path.lastIndexOf('.');
|
||||||
|
|
||||||
|
JsonObject object;
|
||||||
|
if (lastDotIndex == -1) {
|
||||||
|
object = config;
|
||||||
|
} else {
|
||||||
|
object = ((JsonConfig) get(path.substring(0, lastDotIndex))).config;
|
||||||
|
}
|
||||||
|
|
||||||
|
object.remove(path.substring(lastDotIndex + 1));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getString(String path) {
|
public String getString(String path) {
|
||||||
JsonElement element = getJsonElement(path);
|
JsonElement element = getJsonElement(path);
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,31 @@ public interface ServerUtilsConfig {
|
||||||
* @param path The path.
|
* @param path The path.
|
||||||
* @param value The object to set the path's value to.
|
* @param value The object to set the path's value to.
|
||||||
*/
|
*/
|
||||||
void set(String path, Object value);
|
default void set(String path, Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
remove(path);
|
||||||
|
} else {
|
||||||
|
String pathSegment = path;
|
||||||
|
|
||||||
|
int lastDotIndex;
|
||||||
|
while ((lastDotIndex = pathSegment.lastIndexOf('.')) != -1) {
|
||||||
|
String parentPath = path.substring(0, lastDotIndex);
|
||||||
|
if (!(get(parentPath) instanceof ServerUtilsConfig)) {
|
||||||
|
remove(parentPath);
|
||||||
|
}
|
||||||
|
pathSegment = parentPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
setUnsafe(path, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUnsafe(String path, Object value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a path.
|
||||||
|
*/
|
||||||
|
void remove(String path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a string from a path.
|
* Retrieves a string from a path.
|
||||||
|
|
@ -122,7 +146,7 @@ public interface ServerUtilsConfig {
|
||||||
String defKey = (root.isEmpty() ? "" : root + ".") + key;
|
String defKey = (root.isEmpty() ? "" : root + ".") + key;
|
||||||
Object value = conf.get(key);
|
Object value = conf.get(key);
|
||||||
if (def.get(defKey) == null) {
|
if (def.get(defKey) == null) {
|
||||||
conf.set(key, null);
|
conf.remove(key);
|
||||||
} else if (value instanceof ServerUtilsConfig) {
|
} else if (value instanceof ServerUtilsConfig) {
|
||||||
removeOldKeys(def, (ServerUtilsConfig) value, defKey);
|
removeOldKeys(def, (ServerUtilsConfig) value, defKey);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,21 +56,13 @@ public class VelocityTomlConfig implements ServerUtilsConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void set(String path, Object value) {
|
public void setUnsafe(String path, Object value) {
|
||||||
if (value == null) {
|
config.set(path, value);
|
||||||
config.remove(path);
|
}
|
||||||
} else {
|
|
||||||
int lastDotIndex = path.lastIndexOf('.');
|
@Override
|
||||||
if (lastDotIndex != -1) {
|
public void remove(String path) {
|
||||||
String parentPath = path.substring(0, lastDotIndex);
|
config.remove(path);
|
||||||
try {
|
|
||||||
CommentedConfig parent = config.get(parentPath);
|
|
||||||
} catch (ClassCastException ex) {
|
|
||||||
config.remove(parentPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config.set(path, value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue