Element split = scriptEntry.getElement("split");
YAML_Action yaml_action = (YAML_Action) scriptEntry.getObject("yaml_action");
Element actionElement = scriptEntry.getElement("action");
Element idElement = scriptEntry.getElement("id");
YamlConfiguration yamlConfiguration;
dB.report(scriptEntry, getName(),
idElement.debug()
+ actionElement.debug()
+ (filename != null ? filename.debug() : "")
+ (yaml_action != null ? aH.debugObj("yaml_action", yaml_action.name()): "")
+ (key != null ? key.debug() : "")
+ (value != null ? value.debug() : "")
+ (split != null ? split.debug() : ""));
// Do action
Action action = Action.valueOf(actionElement.asString().toUpperCase());
String id = idElement.asString().toUpperCase();
switch (action) {
case LOAD:
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename.asString());
if (!file.exists()) {
dB.echoError("File cannot be found!");
return;
}
try {
FileInputStream fis = new FileInputStream(file);
yamlConfiguration = YamlConfiguration.load(ScriptHelper.convertStreamToString(fis));
fis.close();
}
catch (Exception e) {
dB.echoError(e);
return;
}
if (yamls.containsKey(id))
yamls.remove(id);
if (yamlConfiguration != null)
yamls.put(id, yamlConfiguration);
break;
case UNLOAD:
if (yamls.containsKey(id))
yamls.remove(id);
else
dB.echoError("Unknown YAML ID '" + id + "'");
break;
case SAVE:
if (yamls.containsKey(id)) {
try {
if (!Settings.allowStrangeYAMLSaves()) {
File fileObj = new File(DenizenAPI.getCurrentInstance().
getDataFolder().getAbsolutePath() + "/" + filename.asString());
String directory = URLDecoder.decode(System.getProperty("user.dir"));
if (!fileObj.getCanonicalPath().startsWith(directory)) {
dB.echoError("Outside-the-main-folder YAML saves disabled by administrator.");
return;
}
}
File fileObj = new File(DenizenAPI.getCurrentInstance().
getDataFolder().getAbsolutePath() + "/" + filename.asString());
fileObj.getParentFile().mkdirs();
FileWriter fw = new FileWriter(DenizenAPI.getCurrentInstance()
.getDataFolder().getAbsolutePath() + "/" + filename.asString());
BufferedWriter writer = new BufferedWriter(fw);
writer.write(yamls.get(id).saveToString());
writer.close();
fw.close();
} catch (IOException e) {
dB.echoError(e);
}
}
else
dB.echoError("Unknown YAML ID '" + id + "'");
break;
case WRITE:
if (yamls.containsKey(id)) {
if (value instanceof Element)
yamls.get(id).set(key.asString(), ((Element) value).asString());
else if (split != null && split.asBoolean())
yamls.get(id).set(key.asString(), value);
else
yamls.get(id).set(key.asString(), value.identify());
}
else
dB.echoError("Unknown YAML ID '" + id + "'");
break;
case SET:
if (yamls.containsKey(id)) {
if (yaml_action == null || key == null || value == null) {
dB.echoError("Must specify a YAML action and value!");
return;
}
YamlConfiguration yaml = yamls.get(id);
int index = -1;
if (key.asString().contains("[")) {
try {
index = Integer.valueOf(key.asString().split("\\[")[1].replace("]", "")) - 1;
} catch (Exception e) { index = -1; }
key = Element.valueOf(key.asString().split("\\[")[0]);
}
String keyStr = key.asString();
String valueStr = value.identify();
switch (yaml_action) {
case INCREASE:
Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "0")) + aH.getIntegerFrom(valueStr)));
break;
case DECREASE:
Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "0")) - aH.getIntegerFrom(valueStr)));
break;
case MULTIPLY:
Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "1")) * aH.getIntegerFrom(valueStr)));
break;
case DIVIDE:
Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "1")) / aH.getIntegerFrom(valueStr)));
break;
case DELETE:
yaml.set(keyStr, null);
break;
case SET_VALUE:
Set(yaml, index, keyStr, valueStr);
break;
case INSERT:
{
List<String> list = yaml.getStringList(keyStr);
if (list == null)
list = new ArrayList<String>();
list.add(valueStr);
yaml.set(keyStr, list);
break;
}
case REMOVE:
{
List<String> list = yaml.getStringList(keyStr);
if (list == null)
break;
if (index > -1 && index < list.size()) {
list.remove(index);
}
else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equalsIgnoreCase(valueStr)) {
list.remove(i);
break;
}
}
yaml.set(keyStr, list);
break;
}
}
case SPLIT:
{
List<String> list = yaml.getStringList(keyStr);
if (list == null)
list = new ArrayList<String>();
list.addAll(dList.valueOf(valueStr));
yaml.set(keyStr, list);
break;
}
}
}
else
dB.echoError("Unknown YAML ID '" + id + "'");
break;
case CREATE:
if (yamls.containsKey(id))
yamls.remove(id);
yamlConfiguration = new YamlConfiguration();
yamls.put(id.toUpperCase(), yamlConfiguration);
break;
}
}