From 5b351b955cb3b304cc4995dcf354357eecd31074 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 21:24:37 -0400 Subject: [PATCH] Add `get` methods to the server hook --- .../grasscutter/plugin/api/ServerHook.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java index 0ba4d7f17..8346903f4 100644 --- a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java +++ b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java @@ -1,5 +1,7 @@ package emu.grasscutter.plugin.api; +import emu.grasscutter.command.Command; +import emu.grasscutter.command.CommandHandler; import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.server.dispatch.DispatchServer; import emu.grasscutter.server.game.GameServer; @@ -35,6 +37,20 @@ public final class ServerHook { instance = this; } + /** + * @return The game server. + */ + public GameServer getGameServer() { + return this.gameServer; + } + + /** + * @return The dispatch server. + */ + public DispatchServer getDispatchServer() { + return this.dispatchServer; + } + /** * Gets all online players. * @return Players connected to the server. @@ -42,4 +58,16 @@ public final class ServerHook { public List getOnlinePlayers() { return new LinkedList<>(this.gameServer.getPlayers().values()); } + + /** + * Registers a command to the {@link emu.grasscutter.command.CommandMap}. + * @param handler The command handler. + */ + public void registerCommand(CommandHandler handler) { + Class clazz = handler.getClass(); + if(!clazz.isAnnotationPresent(Command.class)) + throw new IllegalArgumentException("Command handler must be annotated with @Command."); + Command commandData = clazz.getAnnotation(Command.class); + this.gameServer.getCommandMap().registerCommand(commandData.label(), handler); + } } \ No newline at end of file