Skip to content

Commit 728c746

Browse files
JRoymdcfe
andauthored
Add commands info to /ess dump (#4965)
See #4961 and EssentialsX/Website#71. Adds `commands.yml`, the known command map and `AlternativeCommandHandler` handover information to `/ess dump`. Co-authored-by: MD <1917406+mdcfe@users.noreply.github.com>
1 parent 6dfa18c commit 728c746

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.earth2me.essentials.UserMap;
77
import com.earth2me.essentials.economy.EconomyLayer;
88
import com.earth2me.essentials.economy.EconomyLayers;
9+
import com.earth2me.essentials.utils.CommandMapUtil;
910
import com.earth2me.essentials.utils.DateUtil;
1011
import com.earth2me.essentials.utils.EnumUtil;
1112
import com.earth2me.essentials.utils.FloatUtil;
@@ -25,6 +26,7 @@
2526
import org.bukkit.Server;
2627
import org.bukkit.Sound;
2728
import org.bukkit.World;
29+
import org.bukkit.command.Command;
2830
import org.bukkit.entity.Player;
2931
import org.bukkit.plugin.Plugin;
3032
import org.bukkit.plugin.PluginDescriptionFile;
@@ -42,6 +44,7 @@
4244
import java.util.Collection;
4345
import java.util.Collections;
4446
import java.util.Comparator;
47+
import java.util.HashMap;
4548
import java.util.List;
4649
import java.util.Locale;
4750
import java.util.Map;
@@ -270,6 +273,9 @@ private void runDump(Server server, CommandSource sender, String commandLabel, S
270273
final Plugin essDiscord = Bukkit.getPluginManager().getPlugin("EssentialsDiscord");
271274
final Plugin essSpawn = Bukkit.getPluginManager().getPlugin("EssentialsSpawn");
272275

276+
final Map<String, Command> knownCommandsCopy = new HashMap<>(ess.getKnownCommandsProvider().getKnownCommands());
277+
final Map<String, String> disabledCommandsCopy = new HashMap<>(ess.getAlternativeCommandsHandler().disabledCommands());
278+
273279
// Further operations will be heavy IO
274280
ess.runTaskAsynchronously(() -> {
275281
boolean config = false;
@@ -279,6 +285,7 @@ private void runDump(Server server, CommandSource sender, String commandLabel, S
279285
boolean worth = false;
280286
boolean tpr = false;
281287
boolean spawns = false;
288+
boolean commands = false;
282289
for (final String arg : args) {
283290
if (arg.equals("*") || arg.equalsIgnoreCase("all")) {
284291
config = true;
@@ -288,6 +295,7 @@ private void runDump(Server server, CommandSource sender, String commandLabel, S
288295
worth = true;
289296
tpr = true;
290297
spawns = true;
298+
commands = true;
291299
break;
292300
} else if (arg.equalsIgnoreCase("config")) {
293301
config = true;
@@ -303,6 +311,8 @@ private void runDump(Server server, CommandSource sender, String commandLabel, S
303311
tpr = true;
304312
} else if (arg.equalsIgnoreCase("spawns")) {
305313
spawns = true;
314+
} else if (arg.equalsIgnoreCase("commands")) {
315+
commands = true;
306316
}
307317
}
308318

@@ -366,6 +376,16 @@ private void runDump(Server server, CommandSource sender, String commandLabel, S
366376
}
367377
}
368378

379+
if (commands) {
380+
try {
381+
files.add(new PasteUtil.PasteFile("commands.yml", new String(Files.readAllBytes(Paths.get("commands.yml")), StandardCharsets.UTF_8)));
382+
files.add(new PasteUtil.PasteFile("commandmap.json", CommandMapUtil.toJsonPretty(ess, knownCommandsCopy)));
383+
files.add(new PasteUtil.PasteFile("commandoverride.json", disabledCommandsCopy.toString()));
384+
} catch (IOException e) {
385+
sender.sendMessage(tl("dumpErrorUpload", "commands.yml", e.getMessage()));
386+
}
387+
}
388+
369389
final CompletableFuture<PasteUtil.PasteResult> future = PasteUtil.createPaste(files);
370390
future.thenAccept(result -> {
371391
if (result != null) {
@@ -769,7 +789,7 @@ protected List<String> getTabCompleteOptions(final Server server, final CommandS
769789
}
770790
break;
771791
case "dump":
772-
final List<String> list = Lists.newArrayList("config", "kits", "log", "discord", "worth", "tpr", "spawns", "all");
792+
final List<String> list = Lists.newArrayList("config", "kits", "log", "discord", "worth", "tpr", "spawns", "commands", "all");
773793
for (String arg : args) {
774794
if (arg.equals("*") || arg.equalsIgnoreCase("all")) {
775795
list.clear();
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.earth2me.essentials.utils;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonArray;
6+
import com.google.gson.JsonObject;
7+
import net.ess3.api.IEssentials;
8+
import org.bukkit.command.Command;
9+
import org.bukkit.command.FormattedCommandAlias;
10+
import org.bukkit.command.PluginCommand;
11+
import org.bukkit.command.defaults.BukkitCommand;
12+
import org.bukkit.plugin.Plugin;
13+
14+
import java.util.Map;
15+
16+
public final class CommandMapUtil {
17+
18+
private static final Gson GSON = new GsonBuilder()
19+
.setPrettyPrinting()
20+
.create();
21+
22+
private CommandMapUtil() {
23+
throw new UnsupportedOperationException();
24+
}
25+
26+
public static String toJsonPretty(IEssentials ess, Map<String, Command> knownCommandMap) {
27+
final JsonObject json = toJson(ess, knownCommandMap);
28+
return GSON.toJson(json);
29+
}
30+
31+
public static JsonObject toJson(IEssentials ess, Map<String, Command> knownCommandMap) {
32+
final JsonObject json = new JsonObject();
33+
for (Map.Entry<String, Command> entry : knownCommandMap.entrySet()) {
34+
json.add(entry.getKey(), toJson(ess, entry.getValue()));
35+
}
36+
return json;
37+
}
38+
39+
public static JsonObject toJson(IEssentials ess, Command value) {
40+
if (value == null) {
41+
return null;
42+
}
43+
44+
final JsonObject json = new JsonObject();
45+
json.addProperty("name", value.getName());
46+
json.addProperty("description", value.getDescription());
47+
json.addProperty("type", value.getClass().getSimpleName());
48+
json.addProperty("raw", value.toString());
49+
50+
if (value.getClass().getSimpleName().equals("VanillaCommandWrapper")) {
51+
json.addProperty("source", "vanilla");
52+
} else if (value.getClass().getName().contains("org.spigotmc")) {
53+
json.addProperty("source", "spigot");
54+
} else if (value instanceof PluginCommand) {
55+
final Plugin plugin = ((PluginCommand) value).getPlugin();
56+
json.addProperty("source", plugin.getName() + " " + plugin.getDescription().getVersion());
57+
} else if (value instanceof BukkitCommand) {
58+
json.addProperty("source", "bukkit");
59+
} else if (value instanceof FormattedCommandAlias) {
60+
json.addProperty("source", "commands.yml");
61+
final JsonArray formatStrings = new JsonArray();
62+
for (String entry : ess.getFormattedCommandAliasProvider().getFormatStrings((FormattedCommandAlias) value)) {
63+
formatStrings.add(entry);
64+
}
65+
json.add("bukkit_aliases", formatStrings);
66+
}
67+
return json;
68+
}
69+
70+
}

0 commit comments

Comments
 (0)