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>
This commit is contained in:
Josh Roy 2022-08-07 06:43:59 -04:00 committed by GitHub
parent 6dfa18ca53
commit 728c746200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import com.earth2me.essentials.User;
import com.earth2me.essentials.UserMap;
import com.earth2me.essentials.economy.EconomyLayer;
import com.earth2me.essentials.economy.EconomyLayers;
import com.earth2me.essentials.utils.CommandMapUtil;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.FloatUtil;
@ -25,6 +26,7 @@ import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
@ -42,6 +44,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -270,6 +273,9 @@ public class Commandessentials extends EssentialsCommand {
final Plugin essDiscord = Bukkit.getPluginManager().getPlugin("EssentialsDiscord");
final Plugin essSpawn = Bukkit.getPluginManager().getPlugin("EssentialsSpawn");
final Map<String, Command> knownCommandsCopy = new HashMap<>(ess.getKnownCommandsProvider().getKnownCommands());
final Map<String, String> disabledCommandsCopy = new HashMap<>(ess.getAlternativeCommandsHandler().disabledCommands());
// Further operations will be heavy IO
ess.runTaskAsynchronously(() -> {
boolean config = false;
@ -279,6 +285,7 @@ public class Commandessentials extends EssentialsCommand {
boolean worth = false;
boolean tpr = false;
boolean spawns = false;
boolean commands = false;
for (final String arg : args) {
if (arg.equals("*") || arg.equalsIgnoreCase("all")) {
config = true;
@ -288,6 +295,7 @@ public class Commandessentials extends EssentialsCommand {
worth = true;
tpr = true;
spawns = true;
commands = true;
break;
} else if (arg.equalsIgnoreCase("config")) {
config = true;
@ -303,6 +311,8 @@ public class Commandessentials extends EssentialsCommand {
tpr = true;
} else if (arg.equalsIgnoreCase("spawns")) {
spawns = true;
} else if (arg.equalsIgnoreCase("commands")) {
commands = true;
}
}
@ -366,6 +376,16 @@ public class Commandessentials extends EssentialsCommand {
}
}
if (commands) {
try {
files.add(new PasteUtil.PasteFile("commands.yml", new String(Files.readAllBytes(Paths.get("commands.yml")), StandardCharsets.UTF_8)));
files.add(new PasteUtil.PasteFile("commandmap.json", CommandMapUtil.toJsonPretty(ess, knownCommandsCopy)));
files.add(new PasteUtil.PasteFile("commandoverride.json", disabledCommandsCopy.toString()));
} catch (IOException e) {
sender.sendMessage(tl("dumpErrorUpload", "commands.yml", e.getMessage()));
}
}
final CompletableFuture<PasteUtil.PasteResult> future = PasteUtil.createPaste(files);
future.thenAccept(result -> {
if (result != null) {
@ -769,7 +789,7 @@ public class Commandessentials extends EssentialsCommand {
}
break;
case "dump":
final List<String> list = Lists.newArrayList("config", "kits", "log", "discord", "worth", "tpr", "spawns", "all");
final List<String> list = Lists.newArrayList("config", "kits", "log", "discord", "worth", "tpr", "spawns", "commands", "all");
for (String arg : args) {
if (arg.equals("*") || arg.equalsIgnoreCase("all")) {
list.clear();

View File

@ -0,0 +1,70 @@
package com.earth2me.essentials.utils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.ess3.api.IEssentials;
import org.bukkit.command.Command;
import org.bukkit.command.FormattedCommandAlias;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.plugin.Plugin;
import java.util.Map;
public final class CommandMapUtil {
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.create();
private CommandMapUtil() {
throw new UnsupportedOperationException();
}
public static String toJsonPretty(IEssentials ess, Map<String, Command> knownCommandMap) {
final JsonObject json = toJson(ess, knownCommandMap);
return GSON.toJson(json);
}
public static JsonObject toJson(IEssentials ess, Map<String, Command> knownCommandMap) {
final JsonObject json = new JsonObject();
for (Map.Entry<String, Command> entry : knownCommandMap.entrySet()) {
json.add(entry.getKey(), toJson(ess, entry.getValue()));
}
return json;
}
public static JsonObject toJson(IEssentials ess, Command value) {
if (value == null) {
return null;
}
final JsonObject json = new JsonObject();
json.addProperty("name", value.getName());
json.addProperty("description", value.getDescription());
json.addProperty("type", value.getClass().getSimpleName());
json.addProperty("raw", value.toString());
if (value.getClass().getSimpleName().equals("VanillaCommandWrapper")) {
json.addProperty("source", "vanilla");
} else if (value.getClass().getName().contains("org.spigotmc")) {
json.addProperty("source", "spigot");
} else if (value instanceof PluginCommand) {
final Plugin plugin = ((PluginCommand) value).getPlugin();
json.addProperty("source", plugin.getName() + " " + plugin.getDescription().getVersion());
} else if (value instanceof BukkitCommand) {
json.addProperty("source", "bukkit");
} else if (value instanceof FormattedCommandAlias) {
json.addProperty("source", "commands.yml");
final JsonArray formatStrings = new JsonArray();
for (String entry : ess.getFormattedCommandAliasProvider().getFormatStrings((FormattedCommandAlias) value)) {
formatStrings.add(entry);
}
json.add("bukkit_aliases", formatStrings);
}
return json;
}
}