Skip to content

Commit 4c13ea3

Browse files
authored
Refactor command listing to dynamically load from plugin.yml and enhance 'test' command execution
1 parent 34f7723 commit 4c13ea3

1 file changed

Lines changed: 71 additions & 47 deletions

File tree

src/main/java/me/crazyg/everything/commands/EverythingCommand.java

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,34 @@
77
import org.bukkit.command.Command;
88
import org.bukkit.command.CommandExecutor;
99
import org.bukkit.command.CommandSender;
10-
import org.bukkit.plugin.PluginDescriptionFile;
1110

1211

1312
public class EverythingCommand implements CommandExecutor {
1413

14+
// Dynamically load command names from plugin.yml at runtime
15+
private List<String> getPluginCommands() {
16+
List<String> commandNames = new ArrayList<>();
17+
try {
18+
org.bukkit.configuration.file.YamlConfiguration yml = new org.bukkit.configuration.file.YamlConfiguration();
19+
java.io.InputStream in = plugin.getResource("plugin.yml");
20+
if (in != null) {
21+
java.io.InputStreamReader reader = new java.io.InputStreamReader(in);
22+
yml.load(reader);
23+
reader.close();
24+
in.close();
25+
if (yml.contains("commands")) {
26+
org.bukkit.configuration.ConfigurationSection section = yml.getConfigurationSection("commands");
27+
if (section != null) {
28+
commandNames.addAll(section.getKeys(false));
29+
}
30+
}
31+
}
32+
} catch (Exception e) {
33+
// fallback: do nothing, return empty list
34+
}
35+
return commandNames;
36+
}
37+
1538
private final Everything plugin;
1639
private final Updater updater;
1740

@@ -26,21 +49,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
2649
sender.sendMessage(Component.text("Everything Plugin - Help").color(NamedTextColor.GOLD));
2750
sender.sendMessage(Component.text("Available Commands:").color(NamedTextColor.YELLOW));
2851

29-
// NOTE: getDescription().getCommands() is deprecated, but no modern alternative exists for listing plugin commands
30-
@SuppressWarnings("deprecation")
31-
Map<String, Map<String, Object>> commands = plugin.getDescription().getCommands();
32-
if (commands != null && !commands.isEmpty()) {
33-
for (Map.Entry<String, Map<String, Object>> entry : commands.entrySet()) {
34-
String cmd = entry.getKey();
52+
List<String> commandNames = getPluginCommands();
53+
if (!commandNames.isEmpty()) {
54+
for (String cmd : commandNames) {
3555
org.bukkit.command.PluginCommand pluginCmd = plugin.getCommand(cmd);
3656
String usage = pluginCmd != null ? pluginCmd.getUsage() : "";
3757
String descText = pluginCmd != null ? pluginCmd.getDescription() : "";
38-
if ((usage == null || usage.isEmpty()) && entry.getValue().containsKey("usage")) {
39-
usage = entry.getValue().get("usage").toString();
40-
}
41-
if ((descText == null || descText.isEmpty()) && entry.getValue().containsKey("description")) {
42-
descText = entry.getValue().get("description").toString();
43-
}
4458
StringBuilder line = new StringBuilder();
4559
line.append(" /").append(cmd);
4660
if (usage != null && !usage.isEmpty() && !usage.equalsIgnoreCase("/" + cmd)) {
@@ -109,47 +123,57 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
109123
}
110124
return true;
111125

126+
112127
case "test":
113-
PluginDescriptionFile desc = plugin.getDescription();
114-
Map<String, Map<String, Object>> commands = desc.getCommands();
115128
sender.sendMessage(Component.text("[TEST] Listing and attempting to run all commands:").color(NamedTextColor.AQUA));
116-
if (commands != null) {
117-
for (Map.Entry<String, Map<String, Object>> entry : commands.entrySet()) {
118-
String cmd = entry.getKey();
119-
Map<String, Object> meta = entry.getValue();
120-
String usage = meta.getOrDefault("usage", "").toString();
121-
String descText = meta.getOrDefault("description", "").toString();
122-
StringBuilder line = new StringBuilder();
123-
line.append("/" + cmd);
124-
if (!usage.isEmpty()) {
125-
String usageLine = usage.split("\n")[0].trim();
126-
if (!usageLine.startsWith("/")) {
127-
line.append(" ").append(usageLine);
128-
}
129-
}
130-
if (!descText.isEmpty()) {
131-
line.append(" - ").append(descText);
129+
List<String> testCommands = getPluginCommands();
130+
for (String cmd : testCommands) {
131+
org.bukkit.command.PluginCommand pluginCmd = plugin.getCommand(cmd);
132+
if (pluginCmd == null) {
133+
sender.sendMessage(Component.text("/" + cmd + " (Not registered)").color(NamedTextColor.RED));
134+
continue;
135+
}
136+
String usage = pluginCmd.getUsage();
137+
String descText = pluginCmd.getDescription();
138+
StringBuilder line = new StringBuilder();
139+
line.append("/").append(cmd);
140+
if (usage != null && !usage.isEmpty() && !usage.equalsIgnoreCase("/" + cmd)) {
141+
String usageLine = usage.split("\n")[0].trim();
142+
if (!usageLine.startsWith("/")) {
143+
line.append(" ").append(usageLine);
132144
}
133-
sender.sendMessage(Component.text(line.toString()).color(NamedTextColor.GRAY));
134-
// Try to run the command with the sender as the player if possible
135-
try {
136-
String[] testArgs = new String[0];
137-
if (usage.contains("<player>") || usage.contains("[player]")) {
138-
if (sender instanceof org.bukkit.entity.Player) {
139-
testArgs = new String[] { ((org.bukkit.entity.Player)sender).getName() };
140-
} else {
141-
sender.sendMessage(Component.text("(Skipped: Needs player context)").color(NamedTextColor.DARK_GRAY));
142-
continue;
145+
}
146+
if (descText != null && !descText.isEmpty()) {
147+
line.append(" - ").append(descText);
148+
}
149+
sender.sendMessage(Component.text(line.toString()).color(NamedTextColor.GRAY));
150+
// Try to run the command with the sender as the player if possible
151+
try {
152+
String[] testArgs = new String[0];
153+
// Only add player argument if <player> or [player] is a standalone argument
154+
boolean needsPlayer = false;
155+
if (usage != null && !usage.isEmpty()) {
156+
String[] usageParts = usage.replace("/" + cmd, "").trim().split(" ");
157+
for (String part : usageParts) {
158+
if (part.equalsIgnoreCase("<player>") || part.equalsIgnoreCase("[player]")) {
159+
needsPlayer = true;
160+
break;
143161
}
144162
}
145-
plugin.getCommand(cmd).execute(sender, cmd, testArgs);
146-
sender.sendMessage(Component.text("(Executed)").color(NamedTextColor.DARK_GREEN));
147-
} catch (Exception ex) {
148-
sender.sendMessage(Component.text("(Error executing: " + ex.getMessage() + ")").color(NamedTextColor.RED));
149163
}
164+
if (needsPlayer) {
165+
if (sender instanceof org.bukkit.entity.Player) {
166+
testArgs = new String[] { ((org.bukkit.entity.Player)sender).getName() };
167+
} else {
168+
sender.sendMessage(Component.text("(Skipped: Needs player context)").color(NamedTextColor.DARK_GRAY));
169+
continue;
170+
}
171+
}
172+
pluginCmd.execute(sender, cmd, testArgs);
173+
sender.sendMessage(Component.text("(Executed)").color(NamedTextColor.DARK_GREEN));
174+
} catch (Exception ex) {
175+
sender.sendMessage(Component.text("(Error executing: " + ex.getMessage() + ")").color(NamedTextColor.RED));
150176
}
151-
} else {
152-
sender.sendMessage(Component.text("No commands found in plugin.yml.").color(NamedTextColor.RED));
153177
}
154178
return true;
155179

0 commit comments

Comments
 (0)