diff --git a/src/main/java/emu/grasscutter/Grasscutter.java b/src/main/java/emu/grasscutter/Grasscutter.java index 10ebeb7f1..f322e5fdf 100644 --- a/src/main/java/emu/grasscutter/Grasscutter.java +++ b/src/main/java/emu/grasscutter/Grasscutter.java @@ -221,7 +221,7 @@ public final class Grasscutter { isLastInterrupted = false; try { - CommandMap.getInstance().invoke(null, input); + CommandMap.getInstance().invoke(null, null, input); } catch (Exception e) { Grasscutter.getLogger().error(language.Command_error, e); } diff --git a/src/main/java/emu/grasscutter/command/Command.java b/src/main/java/emu/grasscutter/command/Command.java index a0edc5cdc..734f454ea 100644 --- a/src/main/java/emu/grasscutter/command/Command.java +++ b/src/main/java/emu/grasscutter/command/Command.java @@ -14,6 +14,8 @@ public @interface Command { String[] aliases() default {}; String permission() default ""; + + String permissionTargeted() default ""; boolean threading() default false; } diff --git a/src/main/java/emu/grasscutter/command/CommandHandler.java b/src/main/java/emu/grasscutter/command/CommandHandler.java index 9fc7d20c1..ffe21c9be 100644 --- a/src/main/java/emu/grasscutter/command/CommandHandler.java +++ b/src/main/java/emu/grasscutter/command/CommandHandler.java @@ -25,6 +25,6 @@ public interface CommandHandler { * @param sender The player/console that invoked the command. * @param args The arguments to the command. */ - default void execute(Player sender, List args) { + default void execute(Player sender, Player targetPlayer, List args) { } } diff --git a/src/main/java/emu/grasscutter/command/CommandMap.java b/src/main/java/emu/grasscutter/command/CommandMap.java index a9b7ac4fa..26a3552e8 100644 --- a/src/main/java/emu/grasscutter/command/CommandMap.java +++ b/src/main/java/emu/grasscutter/command/CommandMap.java @@ -12,6 +12,8 @@ import java.util.*; public final class CommandMap { private final Map commands = new HashMap<>(); private final Map annotations = new HashMap<>(); + private final Map targetPlayers = new HashMap<>(); + private static final String consoleId = "console"; public CommandMap() { this(false); } @@ -104,7 +106,7 @@ public final class CommandMap { * @param player The player invoking the command or null for the server console. * @param rawMessage The messaged used to invoke the command. */ - public void invoke(Player player, String rawMessage) { + public void invoke(Player player, Player targetPlayer, String rawMessage) { rawMessage = rawMessage.trim(); if (rawMessage.length() == 0) { CommandHandler.sendMessage(player, Grasscutter.getLanguage().No_command_specified); @@ -119,6 +121,29 @@ public final class CommandMap { String[] split = rawMessage.split(" "); List args = new LinkedList<>(Arrays.asList(split)); String label = args.remove(0); + // Check for special case + String playerId = (player == null) ? consoleId : player.getAccount().getId(); + if (label == "target") { // Sets or clears default targetPlayer + if (args.size() < 1) { + targetPlayers.remove(playerId); + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_cleared); + } else { + try { + String sUid = args.get(0); + int uid = Integer.parseInt(sUid); + targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid); + if (targetPlayer == null) { + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Player_not_found_or_offline); + } else { + targetPlayers.put(playerId, targetPlayer); + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_set.replace("{uid}", sUid)); + } + } catch (NumberFormatException e) { + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Invalid_UID); + } + } + return; + } // Get command handler. CommandHandler handler = this.commands.get(label); if (handler == null) { @@ -126,10 +151,43 @@ public final class CommandMap { return; } + // If any @UID argument is present, override targetPlayer with it + for (int i = 0; i < args.size(); i++) { + String arg = args.get(i); + if (!arg.startsWith("@")) { + continue; + } else { + arg = args.remove(i).substring(1); + try { + int uid = Integer.parseInt(arg); + targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid); + if (targetPlayer == null) { + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Player_not_found_or_offline); + return; + } + break; + } catch (NumberFormatException e) { + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Invalid_UID); + return; + } + } + } + // If there's still no targetPlayer at this point, use previously-set target + if (targetPlayer == null) { + targetPlayer = targetPlayers.getOrDefault(playerId, null); + } + // Check for permission. if (player != null) { String permissionNode = this.annotations.get(label).permission(); + String permissionNodeTargeted = this.annotations.get(label).permissionTargeted(); Account account = player.getAccount(); + if (player != targetPlayer) { // Additional permission required for targeting another player + if (!permissionNodeTargeted.isEmpty() && !account.hasPermission(permissionNodeTargeted)) { + CommandHandler.sendMessage(player, Grasscutter.getLanguage().You_not_permission_run_command); + return; + } + } if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) { CommandHandler.sendMessage(player, Grasscutter.getLanguage().You_not_permission_run_command); return; @@ -138,7 +196,8 @@ public final class CommandMap { // Invoke execute method for handler. boolean threading = this.annotations.get(label).threading(); - Runnable runnable = () -> handler.execute(player, args); + final Player targetPlayerF = targetPlayer; // Is there a better way to do this? + Runnable runnable = () -> handler.execute(player, targetPlayerF, args); if(threading) { Thread command = new Thread(runnable); command.start(); diff --git a/src/main/java/emu/grasscutter/command/commands/AccountCommand.java b/src/main/java/emu/grasscutter/command/commands/AccountCommand.java index bfe0d0a66..a26e52ff0 100644 --- a/src/main/java/emu/grasscutter/command/commands/AccountCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/AccountCommand.java @@ -12,7 +12,7 @@ import java.util.List; public final class AccountCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender != null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().This_command_can_only_run_from_console); return; diff --git a/src/main/java/emu/grasscutter/command/commands/BroadcastCommand.java b/src/main/java/emu/grasscutter/command/commands/BroadcastCommand.java index 539de96ab..b6eeac422 100644 --- a/src/main/java/emu/grasscutter/command/commands/BroadcastCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/BroadcastCommand.java @@ -12,7 +12,7 @@ import java.util.List; public final class BroadcastCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (args.size() < 1) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Broadcast_command_usage); return; diff --git a/src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java b/src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java index 3c1d91e52..404b05a43 100644 --- a/src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java @@ -12,7 +12,7 @@ import java.util.List; description = "Changes your scene", aliases = {"scene"}, permission = "player.changescene") public final class ChangeSceneCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/ClearCommand.java b/src/main/java/emu/grasscutter/command/commands/ClearCommand.java index 20f1bfb18..d79d94c4b 100644 --- a/src/main/java/emu/grasscutter/command/commands/ClearCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ClearCommand.java @@ -16,7 +16,7 @@ import java.util.List; public final class ClearCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int target; String cmdSwitch = ""; if (sender == null) { @@ -32,7 +32,6 @@ public final class ClearCommand implements CommandHandler { cmdSwitch = args.get(1); target = Integer.parseInt(args.get(0)); } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); switch (cmdSwitch) { case "wp" -> { playerInventory.getItems().values().stream() @@ -99,7 +98,6 @@ public final class ClearCommand implements CommandHandler { return; } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); return; diff --git a/src/main/java/emu/grasscutter/command/commands/CoopCommand.java b/src/main/java/emu/grasscutter/command/commands/CoopCommand.java index c90d19543..c192aedda 100644 --- a/src/main/java/emu/grasscutter/command/commands/CoopCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/CoopCommand.java @@ -11,7 +11,7 @@ import java.util.List; description = "Forces someone to join the world of others", permission = "server.coop") public final class CoopCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (args.size() < 2) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_usage); return; diff --git a/src/main/java/emu/grasscutter/command/commands/DropCommand.java b/src/main/java/emu/grasscutter/command/commands/DropCommand.java index 2e675824a..859b360f9 100644 --- a/src/main/java/emu/grasscutter/command/commands/DropCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/DropCommand.java @@ -16,7 +16,7 @@ import java.util.List; public final class DropCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/EnterDungeonCommand.java b/src/main/java/emu/grasscutter/command/commands/EnterDungeonCommand.java index 10092d856..7151b03c2 100644 --- a/src/main/java/emu/grasscutter/command/commands/EnterDungeonCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/EnterDungeonCommand.java @@ -11,7 +11,7 @@ import java.util.List; description = "Enter a dungeon", aliases = {"dungeon"}, permission = "player.enterdungeon") public final class EnterDungeonCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java index b5f723754..35c1fafef 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java @@ -18,7 +18,7 @@ import java.util.*; public final class GiveAllCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int target, amount = 99999; switch (args.size()) { @@ -63,7 +63,6 @@ public final class GiveAllCommand implements CommandHandler { return; } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); return; diff --git a/src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java index c347f80cf..e431a30cc 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java @@ -17,7 +17,7 @@ import java.util.List; @Command(label = "giveart", usage = "giveart [player] [[,]]... [level]", description = "Gives the player a specified artifact", aliases = {"gart"}, permission = "player.giveart") public final class GiveArtifactCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int size = args.size(), target, itemId, mainPropId, level = 1; ArrayList appendPropIdList = new ArrayList<>(); String msg = Grasscutter.getLanguage().GiveArtifact_usage; @@ -71,7 +71,6 @@ public final class GiveArtifactCommand implements CommandHandler { return; } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); return; diff --git a/src/main/java/emu/grasscutter/command/commands/GiveCharCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveCharCommand.java index ac53a7202..c27b215e9 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveCharCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveCharCommand.java @@ -15,7 +15,7 @@ import java.util.List; public final class GiveCharCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int target, avatarId, level = 1, ascension; if (sender == null && args.size() < 2) { @@ -61,7 +61,6 @@ public final class GiveCharCommand implements CommandHandler { break; } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); return; diff --git a/src/main/java/emu/grasscutter/command/commands/GiveCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveCommand.java index 630183d9b..71c8e7f0e 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveCommand.java @@ -18,7 +18,7 @@ import java.util.List; public final class GiveCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int target, item, lvl, amount = 1, refinement = 0; if (sender == null && args.size() < 2) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Give_usage); @@ -121,7 +121,6 @@ public final class GiveCommand implements CommandHandler { break; } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); diff --git a/src/main/java/emu/grasscutter/command/commands/GodModeCommand.java b/src/main/java/emu/grasscutter/command/commands/GodModeCommand.java index 513d59fc5..b4c78dccd 100644 --- a/src/main/java/emu/grasscutter/command/commands/GodModeCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GodModeCommand.java @@ -12,7 +12,7 @@ import java.util.List; public final class GodModeCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; // TODO: toggle player's godmode statue from console or other players @@ -32,7 +32,6 @@ public final class GodModeCommand implements CommandHandler { } else { target = sender.getUid(); } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); return; diff --git a/src/main/java/emu/grasscutter/command/commands/HealCommand.java b/src/main/java/emu/grasscutter/command/commands/HealCommand.java index e99c0404e..3965540e9 100644 --- a/src/main/java/emu/grasscutter/command/commands/HealCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/HealCommand.java @@ -14,7 +14,7 @@ import java.util.List; description = "Heal all characters in your current team.", permission = "player.heal") public final class HealCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/HelpCommand.java b/src/main/java/emu/grasscutter/command/commands/HelpCommand.java index 7e9f933bf..8397eae3e 100644 --- a/src/main/java/emu/grasscutter/command/commands/HelpCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/HelpCommand.java @@ -13,7 +13,7 @@ import java.util.*; public final class HelpCommand implements CommandHandler { @Override - public void execute(Player player, List args) { + public void execute(Player player, Player targetPlayer, List args) { if (args.size() < 1) { HashMap handlers = CommandMap.getInstance().getHandlers(); List annotations = new ArrayList<>(); diff --git a/src/main/java/emu/grasscutter/command/commands/KickCommand.java b/src/main/java/emu/grasscutter/command/commands/KickCommand.java index 39aad62be..30b9ff624 100644 --- a/src/main/java/emu/grasscutter/command/commands/KickCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/KickCommand.java @@ -12,10 +12,9 @@ import java.util.List; public final class KickCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int target = Integer.parseInt(args.get(0)); - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); return; diff --git a/src/main/java/emu/grasscutter/command/commands/KillAllCommand.java b/src/main/java/emu/grasscutter/command/commands/KillAllCommand.java index 4a42f94ae..fd6d36011 100644 --- a/src/main/java/emu/grasscutter/command/commands/KillAllCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/KillAllCommand.java @@ -15,9 +15,8 @@ import java.util.List; public final class KillAllCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { Scene mainScene; - Player targetPlayer; try { switch (args.size()) { diff --git a/src/main/java/emu/grasscutter/command/commands/KillCharacterCommand.java b/src/main/java/emu/grasscutter/command/commands/KillCharacterCommand.java index dcc598465..aa3671bcb 100644 --- a/src/main/java/emu/grasscutter/command/commands/KillCharacterCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/KillCharacterCommand.java @@ -17,7 +17,7 @@ import java.util.List; public final class KillCharacterCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int target; if (sender == null) { // from console @@ -48,7 +48,6 @@ public final class KillCharacterCommand implements CommandHandler { } } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found_or_offline); return; diff --git a/src/main/java/emu/grasscutter/command/commands/ListCommand.java b/src/main/java/emu/grasscutter/command/commands/ListCommand.java index 0adfd815c..3119f728f 100644 --- a/src/main/java/emu/grasscutter/command/commands/ListCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ListCommand.java @@ -13,7 +13,7 @@ import java.util.Map; public final class ListCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { Map playersMap = Grasscutter.getGameServer().getPlayers(); boolean needUID = false; diff --git a/src/main/java/emu/grasscutter/command/commands/PermissionCommand.java b/src/main/java/emu/grasscutter/command/commands/PermissionCommand.java index 27f59aff2..b76d50da5 100644 --- a/src/main/java/emu/grasscutter/command/commands/PermissionCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/PermissionCommand.java @@ -13,7 +13,7 @@ import java.util.List; public final class PermissionCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (args.size() < 3) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Permission_usage); return; diff --git a/src/main/java/emu/grasscutter/command/commands/PositionCommand.java b/src/main/java/emu/grasscutter/command/commands/PositionCommand.java index 737bc6752..14e3a896d 100644 --- a/src/main/java/emu/grasscutter/command/commands/PositionCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/PositionCommand.java @@ -12,7 +12,7 @@ import java.util.List; public final class PositionCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/ReloadCommand.java b/src/main/java/emu/grasscutter/command/commands/ReloadCommand.java index f6c608199..0e45c4efc 100644 --- a/src/main/java/emu/grasscutter/command/commands/ReloadCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ReloadCommand.java @@ -12,7 +12,7 @@ import java.util.List; public final class ReloadCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Reload_reload_start); Grasscutter.loadConfig(); Grasscutter.loadLanguage(); diff --git a/src/main/java/emu/grasscutter/command/commands/ResetConstCommand.java b/src/main/java/emu/grasscutter/command/commands/ResetConstCommand.java index 627f5e103..90b542cef 100644 --- a/src/main/java/emu/grasscutter/command/commands/ResetConstCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ResetConstCommand.java @@ -15,7 +15,7 @@ import java.util.List; public final class ResetConstCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/ResetShopLimitCommand.java b/src/main/java/emu/grasscutter/command/commands/ResetShopLimitCommand.java index 12cd6437d..69170db65 100644 --- a/src/main/java/emu/grasscutter/command/commands/ResetShopLimitCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ResetShopLimitCommand.java @@ -11,14 +11,13 @@ import java.util.List; description = "Reset target player's shop refresh time.", permission = "server.resetshop") public final class ResetShopLimitCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (args.size() < 1) { CommandHandler.sendMessage(sender,Grasscutter.getLanguage().ResetShopLimit_usage); return; } int target = Integer.parseInt(args.get(0)); - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); return; diff --git a/src/main/java/emu/grasscutter/command/commands/RestartCommand.java b/src/main/java/emu/grasscutter/command/commands/RestartCommand.java index 0b73ba861..156a0cd93 100644 --- a/src/main/java/emu/grasscutter/command/commands/RestartCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/RestartCommand.java @@ -10,7 +10,7 @@ import java.util.List; public final class RestartCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { sender.getSession().close(); } } diff --git a/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java b/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java index 0e0788ae0..d62eeba0e 100644 --- a/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java @@ -23,7 +23,7 @@ public final class SendMailCommand implements CommandHandler { // Yes this is awful and I hate it. @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int senderId; if(sender != null) { senderId = sender.getUid(); diff --git a/src/main/java/emu/grasscutter/command/commands/SendMessageCommand.java b/src/main/java/emu/grasscutter/command/commands/SendMessageCommand.java index 44b39b667..3d023000a 100644 --- a/src/main/java/emu/grasscutter/command/commands/SendMessageCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SendMessageCommand.java @@ -12,7 +12,7 @@ import java.util.List; public final class SendMessageCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (args.size() < 2) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().SendMessage_usage); return; @@ -22,7 +22,6 @@ public final class SendMessageCommand implements CommandHandler { int target = Integer.parseInt(args.get(0)); String message = String.join(" ", args.subList(1, args.size())); - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); return; diff --git a/src/main/java/emu/grasscutter/command/commands/SetFetterLevelCommand.java b/src/main/java/emu/grasscutter/command/commands/SetFetterLevelCommand.java index fc9a4e993..43326ca17 100644 --- a/src/main/java/emu/grasscutter/command/commands/SetFetterLevelCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SetFetterLevelCommand.java @@ -16,7 +16,7 @@ import emu.grasscutter.server.packet.send.PacketAvatarFetterDataNotify; public final class SetFetterLevelCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/SetStatsCommand.java b/src/main/java/emu/grasscutter/command/commands/SetStatsCommand.java index 05277ad83..fa7637d7b 100644 --- a/src/main/java/emu/grasscutter/command/commands/SetStatsCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SetStatsCommand.java @@ -174,50 +174,26 @@ public final class SetStatsCommand implements CommandHandler { } @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { Language lang = Grasscutter.getLanguage(); String syntax = sender == null ? lang.SetStats_usage_console : lang.SetStats_usage_console; String usage = syntax + lang.SetStats_help_message; - Player targetPlayer = sender; - String uidStr = ""; String statStr; String valueStr; + if (targetPlayer == null) { + CommandHandler.sendMessage(sender, usage); + return; + } + switch (args.size()) { default: CommandHandler.sendMessage(sender, usage); return; case 2: - if (sender == null) { - // When run by the server, first parameter is not optional - CommandHandler.sendMessage(sender, usage); - return; - } statStr = args.get(0).toLowerCase(); valueStr = args.get(1); break; - case 3: - uidStr = args.get(0); - if (uidStr.startsWith("@")) { - uidStr = uidStr.substring(1); - } else { - CommandHandler.sendMessage(sender, usage); - return; - } - try { - int uid = Integer.parseInt(uidStr); - targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid); - if (targetPlayer == null) { - CommandHandler.sendMessage(sender, lang.SetStats_player_error); - return; - } - } catch (NumberFormatException e) { - CommandHandler.sendMessage(sender, lang.SetStats_uid_error); - return; - } - statStr = args.get(1).toLowerCase(); - valueStr = args.get(2); - break; }; EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity(); @@ -248,6 +224,7 @@ public final class SetStatsCommand implements CommandHandler { if (targetPlayer == sender) { CommandHandler.sendMessage(sender, lang.SetStats_set_self.replace("{name}", stat.name).replace("{value}", valueStr)); } else { + String uidStr = targetPlayer.getAccount().getId(); CommandHandler.sendMessage(sender, lang.SetStats_set_for_uid.replace("{name}", stat.name).replace("{uid}", uidStr).replace("{value}", valueStr)); } return; diff --git a/src/main/java/emu/grasscutter/command/commands/SetWorldLevelCommand.java b/src/main/java/emu/grasscutter/command/commands/SetWorldLevelCommand.java index 5d77c94ac..64819a60e 100644 --- a/src/main/java/emu/grasscutter/command/commands/SetWorldLevelCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SetWorldLevelCommand.java @@ -13,7 +13,7 @@ import java.util.List; public final class SetWorldLevelCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; // TODO: set player's world level from console or other players diff --git a/src/main/java/emu/grasscutter/command/commands/SpawnCommand.java b/src/main/java/emu/grasscutter/command/commands/SpawnCommand.java index 3c045f086..2eb9f6f95 100644 --- a/src/main/java/emu/grasscutter/command/commands/SpawnCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SpawnCommand.java @@ -24,7 +24,7 @@ import java.util.Random; public final class SpawnCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/StopCommand.java b/src/main/java/emu/grasscutter/command/commands/StopCommand.java index c301cd1a4..4d0f8c1c5 100644 --- a/src/main/java/emu/grasscutter/command/commands/StopCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/StopCommand.java @@ -12,7 +12,7 @@ import java.util.List; public final class StopCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Stop_message); for (Player p : Grasscutter.getGameServer().getPlayers().values()) { CommandHandler.sendMessage(p, Grasscutter.getLanguage().Stop_message); diff --git a/src/main/java/emu/grasscutter/command/commands/TalentCommand.java b/src/main/java/emu/grasscutter/command/commands/TalentCommand.java index 41774b6ec..ec5828261 100644 --- a/src/main/java/emu/grasscutter/command/commands/TalentCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/TalentCommand.java @@ -17,7 +17,7 @@ import java.util.List; public final class TalentCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/TeleportAllCommand.java b/src/main/java/emu/grasscutter/command/commands/TeleportAllCommand.java index 0cfddf19e..56f0f4453 100644 --- a/src/main/java/emu/grasscutter/command/commands/TeleportAllCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/TeleportAllCommand.java @@ -12,7 +12,7 @@ import java.util.List; description = "Teleports all players in your world to your position", permission = "player.tpall") public final class TeleportAllCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/command/commands/TeleportCommand.java b/src/main/java/emu/grasscutter/command/commands/TeleportCommand.java index 430cfcdd4..7ca7a2c33 100644 --- a/src/main/java/emu/grasscutter/command/commands/TeleportCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/TeleportCommand.java @@ -13,7 +13,7 @@ import java.util.List; public final class TeleportCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { int target; if (args.size() < (sender == null ? 4 : 3)) { CommandHandler.sendMessage(sender, sender == null ? Grasscutter.getLanguage().Teleport_usage_server : @@ -35,7 +35,6 @@ public final class TeleportCommand implements CommandHandler { target = sender.getUid(); } - Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if (targetPlayer == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found_or_offline); return; diff --git a/src/main/java/emu/grasscutter/command/commands/WeatherCommand.java b/src/main/java/emu/grasscutter/command/commands/WeatherCommand.java index 2e590a387..c85359a24 100644 --- a/src/main/java/emu/grasscutter/command/commands/WeatherCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/WeatherCommand.java @@ -14,7 +14,7 @@ import java.util.List; public final class WeatherCommand implements CommandHandler { @Override - public void execute(Player sender, List args) { + public void execute(Player sender, Player targetPlayer, List args) { if (sender == null) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); return; diff --git a/src/main/java/emu/grasscutter/game/managers/ChatManager.java b/src/main/java/emu/grasscutter/game/managers/ChatManager.java index ababbf2e1..3236e6f35 100644 --- a/src/main/java/emu/grasscutter/game/managers/ChatManager.java +++ b/src/main/java/emu/grasscutter/game/managers/ChatManager.java @@ -28,15 +28,15 @@ public class ChatManager { if (message == null || message.length() == 0) { return; } - - // Check if command - if (PREFIXES.contains(message.charAt(0))) { - CommandMap.getInstance().invoke(player, message); - return; - } // Get target Player target = getServer().getPlayerByUid(targetUid); + + // Check if command + if (PREFIXES.contains(message.charAt(0))) { + CommandMap.getInstance().invoke(player, target, message); + return; + } if (target == null) { return; @@ -72,7 +72,7 @@ public class ChatManager { // Check if command if (PREFIXES.contains(message.charAt(0))) { - CommandMap.getInstance().invoke(player, message); + CommandMap.getInstance().invoke(player, null, message); return; } diff --git a/src/main/java/emu/grasscutter/languages/Language.java b/src/main/java/emu/grasscutter/languages/Language.java index 74b1df191..50f269ad3 100644 --- a/src/main/java/emu/grasscutter/languages/Language.java +++ b/src/main/java/emu/grasscutter/languages/Language.java @@ -58,6 +58,8 @@ public final class Language { public String Invalid_arguments = "Invalid arguments."; public String Success = "Success"; public String Invalid_entity_id = "Invalid entity id."; + public String Target_cleared = "Target cleared."; + public String Target_set = "Subsequent commands will target @{uid} by default."; // Help public String Help_usage = " Usage: "; @@ -215,8 +217,6 @@ public final class Language { \t(cont.) Elemental RES: respyro | rescryo | reshydro | resgeo | resdendro | reselectro | resphys """; public String SetStats_value_error = "Invalid stat value."; - public String SetStats_uid_error = "Invalid UID."; - public String SetStats_player_error = "Player not found or offline."; public String SetStats_set_self = "{name} set to {value}."; public String SetStats_set_for_uid = "{name} for {uid} set to {value}."; public String Stats_FIGHT_PROP_MAX_HP = "Max HP";