mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-16 14:33:09 +01:00
Adds removeProperty(String path) and getEmptyNode()
By: Simon Rigby <rigby@rigbydev.co.uk>
This commit is contained in:
parent
204952eed0
commit
13a3b24c47
2 changed files with 40 additions and 2 deletions
|
@ -120,4 +120,13 @@ public class Configuration extends ConfigurationNode {
|
|||
throw new ConfigurationException("Root document must be an key-value structure");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns an empty ConfigurationNode for using as a
|
||||
* default in methods that select a node from a node list.
|
||||
* @return
|
||||
*/
|
||||
public static ConfigurationNode getEmptyNode() {
|
||||
return new ConfigurationNode(new HashMap<String, Object>());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import java.util.Map;
|
|||
public class ConfigurationNode {
|
||||
protected Map<String, Object> root;
|
||||
|
||||
ConfigurationNode(Map<String, Object> root) {
|
||||
protected ConfigurationNode(Map<String, Object> root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
|
@ -478,4 +478,33 @@ public class ConfigurationNode {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the property at a location. This will override existing
|
||||
* configuration data to have it conform to key/value mappings.
|
||||
*
|
||||
* @param path
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void removeProperty(String path) {
|
||||
if (!path.contains(".")) {
|
||||
root.remove(path);
|
||||
return;
|
||||
}
|
||||
|
||||
String[] parts = path.split("\\.");
|
||||
Map<String, Object> node = root;
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
Object o = node.get(parts[i]);
|
||||
|
||||
// Found our target!
|
||||
if (i == parts.length - 1) {
|
||||
node.remove(parts[i]);
|
||||
return;
|
||||
}
|
||||
|
||||
node = (Map<String, Object>)o;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue