Commit 93a69142 authored by KingRainbow44's avatar KingRainbow44
Browse files

Generic help command

parent f11138de
...@@ -63,6 +63,23 @@ public final class CommandMap { ...@@ -63,6 +63,23 @@ public final class CommandMap {
return this; return this;
} }
/**
* Returns a list of all registered commands.
* @return All command handlers as a list.
*/
public List<CommandHandler> getHandlers() {
return new LinkedList<>(this.commands.values());
}
/**
* Returns a handler by label/alias.
* @param label The command label.
* @return The command handler.
*/
public CommandHandler getHandler(String label) {
return this.commands.get(label);
}
/** /**
* Invoke a command handler with the given arguments. * Invoke a command handler with the given arguments.
* @param player The player invoking the command or null for the server console. * @param player The player invoking the command or null for the server console.
......
...@@ -6,6 +6,7 @@ import emu.grasscutter.game.Account; ...@@ -6,6 +6,7 @@ import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.GenshinPlayer;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* A container for server-related commands. * A container for server-related commands.
...@@ -57,7 +58,7 @@ public final class ServerCommands { ...@@ -57,7 +58,7 @@ public final class ServerCommands {
@Override @Override
public void execute(GenshinPlayer player, List<String> args) { public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 2) { if(args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: sendmessage <player> <message>"); return; CommandHandler.sendMessage(player, "Usage: sendmessage <player> <message>"); return;
} }
try { try {
...@@ -66,13 +67,13 @@ public final class ServerCommands { ...@@ -66,13 +67,13 @@ public final class ServerCommands {
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) { if(targetPlayer == null) {
CommandHandler.sendMessage(null, "Player not found."); return; CommandHandler.sendMessage(player, "Player not found."); return;
} }
targetPlayer.sendMessage(player, message); targetPlayer.sendMessage(player, message);
CommandHandler.sendMessage(null, "Message sent."); CommandHandler.sendMessage(player, "Message sent.");
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid player ID."); CommandHandler.sendMessage(player, "Invalid player ID.");
} }
} }
} }
...@@ -118,4 +119,53 @@ public final class ServerCommands { ...@@ -118,4 +119,53 @@ public final class ServerCommands {
} }
} }
} }
@Command(label = "help",
usage = "Usage: help [command]")
public static class HelpCommand implements CommandHandler {
@Override
public void execute(List<String> args) {
List<CommandHandler> handlers = CommandMap.getInstance().getHandlers();
List<Command> annotations = handlers.stream()
.map(handler -> handler.getClass().getAnnotation(Command.class))
.collect(Collectors.toList());
if(args.size() < 1) {
StringBuilder builder = new StringBuilder("Available commands:\n");
annotations.forEach(annotation -> builder.append(annotation.usage()).append("\n"));
CommandHandler.sendMessage(null, builder.toString());
} else {
String command = args.get(0);
CommandHandler handler = CommandMap.getInstance().getHandler(command);
if(handler == null) {
CommandHandler.sendMessage(null, "Command not found."); return;
}
Command annotation = handler.getClass().getAnnotation(Command.class);
CommandHandler.sendMessage(null, annotation.usage());
}
}
@Override
public void execute(GenshinPlayer player, List<String> args) {
List<CommandHandler> handlers = CommandMap.getInstance().getHandlers();
List<Command> annotations = handlers.stream()
.map(handler -> handler.getClass().getAnnotation(Command.class))
.collect(Collectors.toList());
if(args.size() < 1) {
annotations.forEach(annotation -> player.dropMessage(annotation.usage()));
} else {
String command = args.get(0);
CommandHandler handler = CommandMap.getInstance().getHandler(command);
if(handler == null) {
CommandHandler.sendMessage(player, "Command not found."); return;
}
Command annotation = handler.getClass().getAnnotation(Command.class);
CommandHandler.sendMessage(player, annotation.usage());
}
}
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment