From 3be50b5b01ebc2c4456647b7ad0d6efe5dc8beaf Mon Sep 17 00:00:00 2001 From: Benjamin Elsdon Date: Mon, 18 Apr 2022 23:47:26 +0800 Subject: [PATCH] Refactored PlayerCommands, added commands and more Added player "Help" command, added server "sendmsg" command, changed getAcccountByPlayerID to public in DatabaseHelper, and allowed player commands to be submitted with "/" and "!" --- .../grasscutter/commands/PlayerCommands.java | 61 +++++++++++++------ .../grasscutter/commands/ServerCommands.java | 31 ++++++++++ .../grasscutter/database/DatabaseHelper.java | 2 +- .../game/managers/ChatManager.java | 3 +- 4 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/main/java/emu/grasscutter/commands/PlayerCommands.java b/src/main/java/emu/grasscutter/commands/PlayerCommands.java index 366b5e2ee..73681491c 100644 --- a/src/main/java/emu/grasscutter/commands/PlayerCommands.java +++ b/src/main/java/emu/grasscutter/commands/PlayerCommands.java @@ -4,6 +4,7 @@ import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import emu.grasscutter.data.GenshinData; import emu.grasscutter.data.def.ItemData; @@ -23,7 +24,9 @@ import emu.grasscutter.server.packet.send.PacketItemAddHintNotify; import emu.grasscutter.utils.Position; public class PlayerCommands { - private static HashMap list = new HashMap(); + private static HashMap commandList = new HashMap(); + private static HashMap commandAliasList = new HashMap(); + static { try { @@ -36,22 +39,19 @@ public class PlayerCommands { if (commandAnnotation != null) { command.setLevel(commandAnnotation.gmLevel()); - for (String alias : commandAnnotation.aliases()) { + command.setHelpText(commandAnnotation.helpText()); + for (String alias : commandAnnotation.aliases()) { if (alias.length() == 0) { continue; } - - String commandName = "!" + alias; - list.put(commandName, command); - commandName = "/" + alias; - list.put(commandName, command); + + String commandName = alias; + commandAliasList.put(commandName, command); } } - - String commandName = "!" + cls.getSimpleName().toLowerCase(); - list.put(commandName, command); - commandName = "/" + cls.getSimpleName().toLowerCase(); - list.put(commandName, command); + + String commandName = cls.getSimpleName().toLowerCase(); + commandList.put(commandName, command); } } @@ -67,33 +67,54 @@ public class PlayerCommands { if (split.length == 0) { return; } + + String first = split[0].toLowerCase().substring(1); + PlayerCommand c = PlayerCommands.commandList.get(first); + PlayerCommand a = PlayerCommands.commandAliasList.get(first); - // - String first = split[0].toLowerCase(); - PlayerCommand c = PlayerCommands.list.get(first); - - if (c != null) { + if (c != null || a != null) { + PlayerCommand cmd = c != null ? c : a; // Level check - if (player.getGmLevel() < c.getLevel()) { + if (player.getGmLevel() < cmd.getLevel()) { return; } // Execute int len = Math.min(first.length() + 1, msg.length()); - c.execute(player, msg.substring(len)); + cmd.execute(player, msg.substring(len)); } } public static abstract class PlayerCommand { // GM level required to use this command private int level; + private String helpText; + protected int getLevel() { return this.level; } protected void setLevel(int minLevel) { this.level = minLevel; } - + + protected String getHelpText() { return this.helpText; } + protected void setHelpText(String helpText) { this.helpText = helpText; } + // Main public abstract void execute(GenshinPlayer player, String raw); } // ================ Commands ================ + + @Command(aliases = {"h"}, helpText = "Shows this command") + public static class Help extends PlayerCommand { + + @Override + public void execute(GenshinPlayer player, String raw) { + String helpMessage = "Grasscutter Commands: "; + for (Map.Entry cmd : commandList.entrySet()) { + + helpMessage += "\n" + cmd.getKey() + " - " + cmd.getValue().helpText; + } + + player.dropMessage(helpMessage); + } + } @Command(aliases = {"g", "item", "additem"}, helpText = "/give [item id] [count] - Gives {count} amount of {item id}") public static class Give extends PlayerCommand { diff --git a/src/main/java/emu/grasscutter/commands/ServerCommands.java b/src/main/java/emu/grasscutter/commands/ServerCommands.java index 94835d5bb..2039d5227 100644 --- a/src/main/java/emu/grasscutter/commands/ServerCommands.java +++ b/src/main/java/emu/grasscutter/commands/ServerCommands.java @@ -1,9 +1,11 @@ package emu.grasscutter.commands; import java.lang.reflect.Modifier; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; import emu.grasscutter.Grasscutter; import emu.grasscutter.data.GenshinData; @@ -67,6 +69,35 @@ public class ServerCommands { Grasscutter.getLogger().info("Reload complete."); } } + + public static class sendMsg extends ServerCommand { + @Override + public void execute(String raw) { + List split = Arrays.asList(raw.split(" ")); + + if (split.size() < 2) { + Grasscutter.getLogger().error("Invalid amount of args"); + return; + } + + String playerID = split.get(0); + String message = split.stream().skip(1).collect(Collectors.joining(" ")); + + + emu.grasscutter.game.Account account = DatabaseHelper.getAccountByPlayerId(Integer.parseInt(playerID)); + if (account != null) { + GenshinPlayer player = Grasscutter.getGameServer().getPlayerById(Integer.parseInt(playerID)); + if(player != null) { + player.dropMessage(message); + Grasscutter.getLogger().info(String.format("Successfully sent message to %s: %s", playerID, message)); + } else { + Grasscutter.getLogger().error("Player not online"); + } + } else { + Grasscutter.getLogger().error(String.format("Player %s does not exist", playerID)); + } + } + } public static class Account extends ServerCommand { @Override diff --git a/src/main/java/emu/grasscutter/database/DatabaseHelper.java b/src/main/java/emu/grasscutter/database/DatabaseHelper.java index 6c622c2b8..d27e47030 100644 --- a/src/main/java/emu/grasscutter/database/DatabaseHelper.java +++ b/src/main/java/emu/grasscutter/database/DatabaseHelper.java @@ -95,7 +95,7 @@ public class DatabaseHelper { return cursor.next(); } - private static Account getAccountByPlayerId(int playerId) { + public static Account getAccountByPlayerId(int playerId) { MorphiaCursor cursor = DatabaseManager.getDatastore().createQuery(Account.class).field("playerId").equal(playerId).find(FIND_ONE); if (!cursor.hasNext()) return null; return cursor.next(); diff --git a/src/main/java/emu/grasscutter/game/managers/ChatManager.java b/src/main/java/emu/grasscutter/game/managers/ChatManager.java index a896006b7..d2d1acac4 100644 --- a/src/main/java/emu/grasscutter/game/managers/ChatManager.java +++ b/src/main/java/emu/grasscutter/game/managers/ChatManager.java @@ -1,5 +1,6 @@ package emu.grasscutter.game.managers; +import emu.grasscutter.Grasscutter; import emu.grasscutter.commands.PlayerCommands; import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.net.packet.GenshinPacket; @@ -25,7 +26,7 @@ public class ChatManager { } // Check if command - if (message.charAt(0) == '!') { + if (message.charAt(0) == '!' || message.charAt(0) == '/') { PlayerCommands.handle(player, message); return; }