Skip to content

Commit

Permalink
Update YAML command
Browse files Browse the repository at this point in the history
Also quickfixadjust a midi bug
  • Loading branch information
mcmonkey4eva committed Sep 10, 2013
1 parent ae76ebc commit 7c7d7d1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
Expand Up @@ -25,7 +25,7 @@
* - yaml create:filename.yml ...
* - yaml read:filename.yml key:yaml-key ...
* - yaml write:filename.yml key:yaml-key value:value ...
* - yaml save:filename.yml
* - yaml savefile:filename.yml
*
*/

Expand All @@ -48,65 +48,65 @@ enum Action{ LOAD, CREATE, READ, WRITE, SAVE }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

Action action = null;
String key = null;
String value = null;
String filename = null;
String id = null;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("LOAD")) {
scriptEntry.addObject("action", new Element("LOAD"));
scriptEntry.addObject("filename", arg.asElement());
}

for (String arg : scriptEntry.getArguments()) {
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("SAVE, SAVEFILE, FILESAVE")) {
scriptEntry.addObject("action", new Element("SAVE"));
scriptEntry.addObject("filename", arg.asElement());
}

if (aH.matchesValueArg("LOAD, CREATE, SAVE", arg, aH.ArgumentType.Custom)) {
action = Action.valueOf(arg.split(":")[0].toUpperCase());
filename = aH.getStringFrom(arg);
else if (!scriptEntry.hasObject("action") &&
arg.matches("CREATE")) {
scriptEntry.addObject("action", new Element("CREATE"));
}

else if (aH.matchesValueArg("READ, WRITE", arg, aH.ArgumentType.Custom)) {
action = Action.valueOf(arg.split(":")[0].toUpperCase());
key = aH.getStringFrom(arg);
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("WRITE")) {
scriptEntry.addObject("action", new Element("WRITE"));
scriptEntry.addObject("key", arg.asElement());
}

else if (aH.matchesValueArg("VALUE", arg, aH.ArgumentType.Custom)) {
value = aH.getStringFrom(arg);
else if (!scriptEntry.hasObject("value") &&
arg.matchesPrefix("VALUE")) {
scriptEntry.addObject("value", arg.asElement());
}

else if (!scriptEntry.hasObject("id") &&
arg.matchesPrefix("ID")) {
scriptEntry.addObject("id", arg.asElement());
}
else
id = aH.getStringFrom(arg);

dB.echoError(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg.raw_value);
}

// Check for required arguments

if (id == null)
if (!scriptEntry.hasObject("id"))
throw new InvalidArgumentsException("Must specify an id!");

if (action == null)
if (!scriptEntry.hasObject("action"))
throw new InvalidArgumentsException("Must specify an action!");

if ((action == Action.READ || action == Action.WRITE) && key == null)
if (!scriptEntry.hasObject("key") &&
scriptEntry.getElement("action").asString().equalsIgnoreCase("write"))
throw new InvalidArgumentsException("Must specify a key!");

if ((action == Action.CREATE || action == Action.LOAD || action == Action.SAVE) && filename == null)
throw new InvalidArgumentsException("Must specify a filename!");

// Add objects back to script entry

scriptEntry.addObject("filename", filename)
.addObject("action", action)
.addObject("key", key)
.addObject("value", value)
.addObject("id", id);
}


@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {

String filename = (String) scriptEntry.getObject("filename");
String key = (String) scriptEntry.getObject("key");
String value = (String) scriptEntry.getObject("value");
Action action = (Action) scriptEntry.getObject("action");
String id = (String) scriptEntry.getObject("id");
Element filename = scriptEntry.getElement("filename");
Element key = scriptEntry.getElement("key");
Element value = scriptEntry.getElement("value");
Action action = Action.valueOf(scriptEntry.getElement("action").asString().toUpperCase());
String id = scriptEntry.getElement("id").asString();

YamlConfiguration yamlConfiguration;

Expand All @@ -115,8 +115,11 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
switch (action) {

case LOAD:
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename);
if (file == null) throw new CommandExecutionException("File cannot be found!");
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename.asString());
if (!file.exists()) {
dB.echoError("File cannot be found!");
return;
}
yamlConfiguration = YamlConfiguration.loadConfiguration(file);
if (yamlConfiguration != null)
yamls.put(id.toUpperCase(), yamlConfiguration);
Expand All @@ -125,7 +128,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
case SAVE:
if (yamls.containsKey(id.toUpperCase())) {
try {
yamls.get(id.toUpperCase()).save(new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename));
yamls.get(id.toUpperCase()).save(new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename.asString()));
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -134,14 +137,12 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept

case WRITE:
if (yamls.containsKey(id.toUpperCase()))
yamls.get(id.toUpperCase()).set(key, value);
yamls.get(id.toUpperCase()).set(key.asString(), value.asString());
break;

case CREATE:
yamlConfiguration = YamlConfiguration.loadConfiguration(
new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename));
if (yamlConfiguration != null)
yamls.put(id.toUpperCase(), yamlConfiguration);
yamlConfiguration = new YamlConfiguration();
yamls.put(id.toUpperCase(), yamlConfiguration);
break;
}

Expand Down
Expand Up @@ -93,6 +93,10 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dB.echoError("Invalid file " + scriptEntry.getElement("file").asString());
return;
}
if (!file.exists()) {
dB.echoError("Invalid file " + scriptEntry.getElement("file").asString());
return;
}
dList listeners = (dList) scriptEntry.getObject("listeners");
dLocation location = (dLocation) scriptEntry.getObject("location");
float tempo = (float) scriptEntry.getElement("tempo").asDouble();
Expand Down

0 comments on commit 7c7d7d1

Please sign in to comment.