Skip to content

Commit

Permalink
(experimental) holdable yaml command
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 15, 2019
1 parent 98a9c37 commit a99486b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 23 deletions.
Expand Up @@ -4451,6 +4451,7 @@ public void registerCoreMembers() {
// When loading a script, optionally add 'fix_formatting' to run the file through
// Denizen's built in script preparser to correct common YAML errors,
// such as tabs instead of spaces or comments inside braced blocks.
// Use holdable syntax ("- ~yaml load:...") with load or savefile actions to avoid locking up the server during file IO.
//
// @Tags
// <yaml[<idname>].contains[<path>]>
Expand Down
Expand Up @@ -4,6 +4,7 @@
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.scripts.commands.Holdable;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
Expand All @@ -16,13 +17,14 @@
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import net.aufdemrand.denizencore.utilities.text.StringHolder;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.JSONObject;

import java.io.*;
import java.net.URLDecoder;
import java.util.*;

public class YamlCommand extends AbstractCommand {
public class YamlCommand extends AbstractCommand implements Holdable {

@Override
public void onEnable() {
Expand Down Expand Up @@ -231,38 +233,66 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
Action action = Action.valueOf(actionElement.asString().toUpperCase());
String id = idElement.asString().toUpperCase();

if (action != Action.LOAD && action != Action.SAVE && scriptEntry.shouldWaitFor()) {
scriptEntry.setFinished(true);
}
switch (action) {

case LOAD:
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename.asString());
if (!Utilities.canReadFile(file)) {
dB.echoError("Server config denies reading files in that location.");
scriptEntry.setFinished(true);
return;
}
if (!file.exists()) {
dB.echoError("File cannot be found!");
scriptEntry.setFinished(true);
return;
}
try {
FileInputStream fis = new FileInputStream(file);
String str = ScriptHelper.convertStreamToString(fis);
if (fixFormatting.asBoolean()) {
str = ScriptHelper.ClearComments("", str, false);
YamlConfiguration[] runnableConfigs = new YamlConfiguration[1];
BukkitRunnable onLoadCompleted = new BukkitRunnable() {
@Override
public void run() {
if (yamls.containsKey(id)) {
yamls.remove(id);
}
yamls.put(id, runnableConfigs[0]);
scriptEntry.setFinished(true);
}
yamlConfiguration = YamlConfiguration.load(str);
fis.close();
}
catch (Exception e) {
dB.echoError("Failed to load yaml file: " + e);
return;
}
if (yamls.containsKey(id)) {
yamls.remove(id);
};
BukkitRunnable loadRunnable = new BukkitRunnable() {
@Override
public void run() {
try {
FileInputStream fis = new FileInputStream(file);
String str = ScriptHelper.convertStreamToString(fis);
if (fixFormatting.asBoolean()) {
str = ScriptHelper.ClearComments("", str, false);
}
runnableConfigs[0] = YamlConfiguration.load(str);
fis.close();
if (runnableConfigs[0] == null) {
runnableConfigs[0] = new YamlConfiguration();
}
if (scriptEntry.shouldWaitFor()) {
onLoadCompleted.runTask(DenizenAPI.getCurrentInstance());
}
else {
onLoadCompleted.run();
}
}
catch (Exception e) {
dB.echoError("Failed to load yaml file: " + e);
}
}
};
if (scriptEntry.shouldWaitFor()) {
loadRunnable.runTaskAsynchronously(DenizenAPI.getCurrentInstance());
}
if (yamlConfiguration == null) {
yamlConfiguration = new YamlConfiguration();
else {
loadRunnable.run();
}
yamls.put(id, yamlConfiguration);
break;

case UNLOAD:
Expand All @@ -283,6 +313,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
String directory = URLDecoder.decode(System.getProperty("user.dir"));
if (!fileObj.getCanonicalPath().startsWith(directory)) {
dB.echoError("Outside-the-main-folder YAML saves disabled by administrator.");
scriptEntry.setFinished(true);
return;
}
}
Expand All @@ -291,20 +322,40 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
fileObj.getParentFile().mkdirs();
if (!Utilities.isSafeFile(fileObj)) {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot edit that file!");
scriptEntry.setFinished(true);
return;
}
FileWriter fw = new FileWriter(fileObj.getAbsoluteFile());
BufferedWriter writer = new BufferedWriter(fw);
writer.write(yamls.get(id).saveToString());
writer.close();
fw.close();
String outp = yamls.get(id).saveToString();
BukkitRunnable saveRunnable = new BukkitRunnable() {
@Override
public void run() {
try {
FileWriter fw = new FileWriter(fileObj.getAbsoluteFile());
BufferedWriter writer = new BufferedWriter(fw);
writer.write(outp);
writer.close();
fw.close();
}
catch (IOException e) {
dB.echoError(e);
}
scriptEntry.setFinished(true);
}
};
if (scriptEntry.shouldWaitFor()) {
saveRunnable.runTaskAsynchronously(DenizenAPI.getCurrentInstance());
}
else {
saveRunnable.run();
}
}
catch (IOException e) {
dB.echoError(e);
}
}
else {
dB.echoError("Unknown YAML ID '" + id + "'");
scriptEntry.setFinished(true);
}
break;

Expand Down

0 comments on commit a99486b

Please sign in to comment.