From ce36bcc9517e726cbdee561101b7eb7f147d8c04 Mon Sep 17 00:00:00 2001 From: CocoTheOwner Date: Fri, 13 Aug 2021 22:41:25 +0200 Subject: [PATCH] JSON Cleaner in util & Decree tag & beatify DecreeCMD --- .../studio/CommandIrisStudioBeautify.java | 2 +- .../iris/core/decrees/DecIrisStudio.java | 36 +++++++++- .../volmit/iris/util/decree/DecreeSystem.java | 2 +- .../decree/virtual/VirtualDecreeCommand.java | 2 +- .../volmit/iris/util/json/JSONCleaner.java | 66 +++++++++++++++++++ 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/volmit/iris/util/json/JSONCleaner.java diff --git a/src/main/java/com/volmit/iris/core/command/studio/CommandIrisStudioBeautify.java b/src/main/java/com/volmit/iris/core/command/studio/CommandIrisStudioBeautify.java index 16b465c3b..6c4fcd4a8 100644 --- a/src/main/java/com/volmit/iris/core/command/studio/CommandIrisStudioBeautify.java +++ b/src/main/java/com/volmit/iris/core/command/studio/CommandIrisStudioBeautify.java @@ -73,7 +73,6 @@ public class CommandIrisStudioBeautify extends MortarCommand { return true; } - private int clean(VolmitSender s, File clean) { int c = 0; if (clean.isDirectory()) { @@ -131,6 +130,7 @@ public class CommandIrisStudioBeautify extends MortarCommand { } + @Override protected String getArgsUsage() { return "[project]"; diff --git a/src/main/java/com/volmit/iris/core/decrees/DecIrisStudio.java b/src/main/java/com/volmit/iris/core/decrees/DecIrisStudio.java index a59564ce9..7563bb8b0 100644 --- a/src/main/java/com/volmit/iris/core/decrees/DecIrisStudio.java +++ b/src/main/java/com/volmit/iris/core/decrees/DecIrisStudio.java @@ -24,6 +24,14 @@ import com.volmit.iris.util.decree.DecreeExecutor; import com.volmit.iris.util.decree.annotations.Decree; import com.volmit.iris.util.decree.annotations.Param; import com.volmit.iris.util.format.C; +import com.volmit.iris.util.format.Form; +import com.volmit.iris.util.io.IO; +import com.volmit.iris.util.json.JSONCleaner; +import com.volmit.iris.util.json.JSONObject; +import com.volmit.iris.util.plugin.VolmitSender; + +import java.io.File; +import java.io.IOException; @Decree(name = "studio", aliases = {"std", "s"}, description = "Studio Commands", studio = true) public class DecIrisStudio implements DecreeExecutor @@ -35,7 +43,7 @@ public class DecIrisStudio implements DecreeExecutor @Param(name = "seed", defaultValue = "1337", description = "The seed to generate the studio with", aliases = "s") long seed) { - sender().sendMessage(C.GREEN + "Opening studio for the " + dimension.getName() + " pack (seed: " + seed + ")"); + sender().sendMessage(C.GREEN + "Opening studio for the \"" + dimension.getName() + "\" pack (seed: " + seed + ")"); Iris.proj.open(sender(), seed, dimension.getLoadKey()); } @@ -50,4 +58,30 @@ public class DecIrisStudio implements DecreeExecutor Iris.proj.close(); sender().sendMessage(C.GREEN + "Project Closed."); } + + @Decree(description = "Get the version of a pack", aliases = {"v", "ver"}) + public void version( + @Param(name = "dimension", defaultValue = "overworld", description = "The dimension get the version of", aliases = "dim") + IrisDimension dimension + ) + { + sender().sendMessage(C.GREEN + "The \"" + dimension.getName() + "\" pack has version: " + dimension.getVersion()); + } + + @Decree(description = "Beatify a pack", aliases = {"beauty", "prettify"}) + public void beautify( + @Param(name = "dimension", defaultValue = "overworld", description = "The to-beautify dimension", aliases = "dim") + IrisDimension dimension + ) + { + File folder = dimension.getLoadFile(); + sender().sendMessage("Cleaned " + Form.f(JSONCleaner.clean(sender(), folder)) + " JSON Files"); + } + + @Decree(description = "Beatify a pack - must be in studio!", aliases = {"beauty", "prettify"}) + public void beautify() + { + File folder = Iris.proj.getActiveProject().getPath(); + sender().sendMessage("Cleaned " + Form.f(JSONCleaner.clean(sender(), folder)) + " JSON Files"); + } } diff --git a/src/main/java/com/volmit/iris/util/decree/DecreeSystem.java b/src/main/java/com/volmit/iris/util/decree/DecreeSystem.java index 341ef6009..6b177e455 100644 --- a/src/main/java/com/volmit/iris/util/decree/DecreeSystem.java +++ b/src/main/java/com/volmit/iris/util/decree/DecreeSystem.java @@ -53,7 +53,7 @@ public interface DecreeSystem extends CommandExecutor, TabCompleter { @Override default boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { J.aBukkit(() -> { - if(!call(new VolmitSender(sender), args)) + if(!call(new VolmitSender(sender, Iris.instance.getTag()), args)) { sender.sendMessage(C.RED + "Unknown Iris Command"); } diff --git a/src/main/java/com/volmit/iris/util/decree/virtual/VirtualDecreeCommand.java b/src/main/java/com/volmit/iris/util/decree/virtual/VirtualDecreeCommand.java index 1e1a88211..499363d0d 100644 --- a/src/main/java/com/volmit/iris/util/decree/virtual/VirtualDecreeCommand.java +++ b/src/main/java/com/volmit/iris/util/decree/virtual/VirtualDecreeCommand.java @@ -276,7 +276,7 @@ public class VirtualDecreeCommand { if(isStudio() && !IrisSettings.get().isStudio()) { sender.sendMessage(C.RED + "To use Iris Studio Commands, please enable studio in Iris/settings.json (settings auto-reload)"); - return false; + return true; // Must still return true because command exists but is not enabled } Iris.debug("@ " + getPath() + " with " + args.toString(", ")); diff --git a/src/main/java/com/volmit/iris/util/json/JSONCleaner.java b/src/main/java/com/volmit/iris/util/json/JSONCleaner.java new file mode 100644 index 000000000..d75b360e3 --- /dev/null +++ b/src/main/java/com/volmit/iris/util/json/JSONCleaner.java @@ -0,0 +1,66 @@ +package com.volmit.iris.util.json; + +import com.volmit.iris.Iris; +import com.volmit.iris.util.io.IO; +import com.volmit.iris.util.plugin.VolmitSender; + +import java.io.File; +import java.io.IOException; + +public class JSONCleaner { + public static int clean(VolmitSender s, File clean) { + int c = 0; + if (clean.isDirectory()) { + for (File i : clean.listFiles()) { + c += clean(s, i); + } + } else if (clean.getName().endsWith(".json")) { + try { + clean(clean); + } catch (Throwable e) { + Iris.reportError(e); + Iris.error("Failed to beautify " + clean.getAbsolutePath() + " You may have errors in your json!"); + } + + c++; + } + + return c; + } + + public static void clean(File clean) throws IOException { + JSONObject obj = new JSONObject(IO.readAll(clean)); + fixBlocks(obj, clean); + + IO.writeAll(clean, obj.toString(4)); + } + + public static void fixBlocks(JSONObject obj, File f) { + for (String i : obj.keySet()) { + Object o = obj.get(i); + + if (i.equals("block") && o instanceof String && !o.toString().trim().isEmpty() && !o.toString().contains(":")) { + obj.put(i, "minecraft:" + o); + Iris.debug("Updated Block Key: " + o + " to " + obj.getString(i) + " in " + f.getPath()); + } + + if (o instanceof JSONObject) { + fixBlocks((JSONObject) o, f); + } else if (o instanceof JSONArray) { + fixBlocks((JSONArray) o, f); + } + } + } + + public static void fixBlocks(JSONArray obj, File f) { + for (int i = 0; i < obj.length(); i++) { + Object o = obj.get(i); + + if (o instanceof JSONObject) { + fixBlocks((JSONObject) o, f); + } else if (o instanceof JSONArray) { + fixBlocks((JSONArray) o, f); + } + } + } +}