Commit 82ec63c4 authored by AnimeGitB's avatar AnimeGitB Committed by Melledy
Browse files

[WIP] Command Targeting overhaul

parent 4e0ebe56
...@@ -221,7 +221,7 @@ public final class Grasscutter { ...@@ -221,7 +221,7 @@ public final class Grasscutter {
isLastInterrupted = false; isLastInterrupted = false;
try { try {
CommandMap.getInstance().invoke(null, input); CommandMap.getInstance().invoke(null, null, input);
} catch (Exception e) { } catch (Exception e) {
Grasscutter.getLogger().error(language.Command_error, e); Grasscutter.getLogger().error(language.Command_error, e);
} }
......
...@@ -15,5 +15,7 @@ public @interface Command { ...@@ -15,5 +15,7 @@ public @interface Command {
String permission() default ""; String permission() default "";
String permissionTargeted() default "";
boolean threading() default false; boolean threading() default false;
} }
...@@ -25,6 +25,6 @@ public interface CommandHandler { ...@@ -25,6 +25,6 @@ public interface CommandHandler {
* @param sender The player/console that invoked the command. * @param sender The player/console that invoked the command.
* @param args The arguments to the command. * @param args The arguments to the command.
*/ */
default void execute(Player sender, List<String> args) { default void execute(Player sender, Player targetPlayer, List<String> args) {
} }
} }
...@@ -12,6 +12,8 @@ import java.util.*; ...@@ -12,6 +12,8 @@ import java.util.*;
public final class CommandMap { public final class CommandMap {
private final Map<String, CommandHandler> commands = new HashMap<>(); private final Map<String, CommandHandler> commands = new HashMap<>();
private final Map<String, Command> annotations = new HashMap<>(); private final Map<String, Command> annotations = new HashMap<>();
private final Map<String, Player> targetPlayers = new HashMap<>();
private static final String consoleId = "console";
public CommandMap() { public CommandMap() {
this(false); this(false);
} }
...@@ -104,7 +106,7 @@ public final class CommandMap { ...@@ -104,7 +106,7 @@ public final class CommandMap {
* @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.
* @param rawMessage The messaged used to invoke the command. * @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(); rawMessage = rawMessage.trim();
if (rawMessage.length() == 0) { if (rawMessage.length() == 0) {
CommandHandler.sendMessage(player, Grasscutter.getLanguage().No_command_specified); CommandHandler.sendMessage(player, Grasscutter.getLanguage().No_command_specified);
...@@ -119,6 +121,29 @@ public final class CommandMap { ...@@ -119,6 +121,29 @@ public final class CommandMap {
String[] split = rawMessage.split(" "); String[] split = rawMessage.split(" ");
List<String> args = new LinkedList<>(Arrays.asList(split)); List<String> args = new LinkedList<>(Arrays.asList(split));
String label = args.remove(0); 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. // Get command handler.
CommandHandler handler = this.commands.get(label); CommandHandler handler = this.commands.get(label);
if (handler == null) { if (handler == null) {
...@@ -126,10 +151,43 @@ public final class CommandMap { ...@@ -126,10 +151,43 @@ public final class CommandMap {
return; 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. // Check for permission.
if (player != null) { if (player != null) {
String permissionNode = this.annotations.get(label).permission(); String permissionNode = this.annotations.get(label).permission();
String permissionNodeTargeted = this.annotations.get(label).permissionTargeted();
Account account = player.getAccount(); 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)) { if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) {
CommandHandler.sendMessage(player, Grasscutter.getLanguage().You_not_permission_run_command); CommandHandler.sendMessage(player, Grasscutter.getLanguage().You_not_permission_run_command);
return; return;
...@@ -138,7 +196,8 @@ public final class CommandMap { ...@@ -138,7 +196,8 @@ public final class CommandMap {
// Invoke execute method for handler. // Invoke execute method for handler.
boolean threading = this.annotations.get(label).threading(); 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) { if(threading) {
Thread command = new Thread(runnable); Thread command = new Thread(runnable);
command.start(); command.start();
......
...@@ -12,7 +12,7 @@ import java.util.List; ...@@ -12,7 +12,7 @@ import java.util.List;
public final class AccountCommand implements CommandHandler { public final class AccountCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender != null) { if (sender != null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().This_command_can_only_run_from_console); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().This_command_can_only_run_from_console);
return; return;
......
...@@ -12,7 +12,7 @@ import java.util.List; ...@@ -12,7 +12,7 @@ import java.util.List;
public final class BroadcastCommand implements CommandHandler { public final class BroadcastCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) { if (args.size() < 1) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Broadcast_command_usage); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Broadcast_command_usage);
return; return;
......
...@@ -12,7 +12,7 @@ import java.util.List; ...@@ -12,7 +12,7 @@ import java.util.List;
description = "Changes your scene", aliases = {"scene"}, permission = "player.changescene") description = "Changes your scene", aliases = {"scene"}, permission = "player.changescene")
public final class ChangeSceneCommand implements CommandHandler { public final class ChangeSceneCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) { if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return; return;
......
...@@ -16,7 +16,7 @@ import java.util.List; ...@@ -16,7 +16,7 @@ import java.util.List;
public final class ClearCommand implements CommandHandler { public final class ClearCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
int target; int target;
String cmdSwitch = ""; String cmdSwitch = "";
if (sender == null) { if (sender == null) {
...@@ -32,7 +32,6 @@ public final class ClearCommand implements CommandHandler { ...@@ -32,7 +32,6 @@ public final class ClearCommand implements CommandHandler {
cmdSwitch = args.get(1); cmdSwitch = args.get(1);
target = Integer.parseInt(args.get(0)); target = Integer.parseInt(args.get(0));
} }
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
switch (cmdSwitch) { switch (cmdSwitch) {
case "wp" -> { case "wp" -> {
playerInventory.getItems().values().stream() playerInventory.getItems().values().stream()
...@@ -99,7 +98,6 @@ public final class ClearCommand implements CommandHandler { ...@@ -99,7 +98,6 @@ public final class ClearCommand implements CommandHandler {
return; return;
} }
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if (targetPlayer == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return; return;
......
...@@ -11,7 +11,7 @@ import java.util.List; ...@@ -11,7 +11,7 @@ import java.util.List;
description = "Forces someone to join the world of others", permission = "server.coop") description = "Forces someone to join the world of others", permission = "server.coop")
public final class CoopCommand implements CommandHandler { public final class CoopCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 2) { if (args.size() < 2) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_usage); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_usage);
return; return;
......
...@@ -16,7 +16,7 @@ import java.util.List; ...@@ -16,7 +16,7 @@ import java.util.List;
public final class DropCommand implements CommandHandler { public final class DropCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) { if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return; return;
......
...@@ -11,7 +11,7 @@ import java.util.List; ...@@ -11,7 +11,7 @@ import java.util.List;
description = "Enter a dungeon", aliases = {"dungeon"}, permission = "player.enterdungeon") description = "Enter a dungeon", aliases = {"dungeon"}, permission = "player.enterdungeon")
public final class EnterDungeonCommand implements CommandHandler { public final class EnterDungeonCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) { if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return; return;
......
...@@ -18,7 +18,7 @@ import java.util.*; ...@@ -18,7 +18,7 @@ import java.util.*;
public final class GiveAllCommand implements CommandHandler { public final class GiveAllCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
int target, amount = 99999; int target, amount = 99999;
switch (args.size()) { switch (args.size()) {
...@@ -63,7 +63,6 @@ public final class GiveAllCommand implements CommandHandler { ...@@ -63,7 +63,6 @@ public final class GiveAllCommand implements CommandHandler {
return; return;
} }
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if (targetPlayer == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return; return;
......
...@@ -17,7 +17,7 @@ import java.util.List; ...@@ -17,7 +17,7 @@ import java.util.List;
@Command(label = "giveart", usage = "giveart [player] <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", description = "Gives the player a specified artifact", aliases = {"gart"}, permission = "player.giveart") @Command(label = "giveart", usage = "giveart [player] <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", description = "Gives the player a specified artifact", aliases = {"gart"}, permission = "player.giveart")
public final class GiveArtifactCommand implements CommandHandler { public final class GiveArtifactCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
int size = args.size(), target, itemId, mainPropId, level = 1; int size = args.size(), target, itemId, mainPropId, level = 1;
ArrayList<Integer> appendPropIdList = new ArrayList<>(); ArrayList<Integer> appendPropIdList = new ArrayList<>();
String msg = Grasscutter.getLanguage().GiveArtifact_usage; String msg = Grasscutter.getLanguage().GiveArtifact_usage;
...@@ -71,7 +71,6 @@ public final class GiveArtifactCommand implements CommandHandler { ...@@ -71,7 +71,6 @@ public final class GiveArtifactCommand implements CommandHandler {
return; return;
} }
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if (targetPlayer == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return; return;
......
...@@ -15,7 +15,7 @@ import java.util.List; ...@@ -15,7 +15,7 @@ import java.util.List;
public final class GiveCharCommand implements CommandHandler { public final class GiveCharCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
int target, avatarId, level = 1, ascension; int target, avatarId, level = 1, ascension;
if (sender == null && args.size() < 2) { if (sender == null && args.size() < 2) {
...@@ -61,7 +61,6 @@ public final class GiveCharCommand implements CommandHandler { ...@@ -61,7 +61,6 @@ public final class GiveCharCommand implements CommandHandler {
break; break;
} }
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if (targetPlayer == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return; return;
......
...@@ -18,7 +18,7 @@ import java.util.List; ...@@ -18,7 +18,7 @@ import java.util.List;
public final class GiveCommand implements CommandHandler { public final class GiveCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
int target, item, lvl, amount = 1, refinement = 0; int target, item, lvl, amount = 1, refinement = 0;
if (sender == null && args.size() < 2) { if (sender == null && args.size() < 2) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Give_usage); CommandHandler.sendMessage(null, Grasscutter.getLanguage().Give_usage);
...@@ -121,7 +121,6 @@ public final class GiveCommand implements CommandHandler { ...@@ -121,7 +121,6 @@ public final class GiveCommand implements CommandHandler {
break; break;
} }
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if (targetPlayer == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
......
...@@ -12,7 +12,7 @@ import java.util.List; ...@@ -12,7 +12,7 @@ import java.util.List;
public final class GodModeCommand implements CommandHandler { public final class GodModeCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) { if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return; // TODO: toggle player's godmode statue from console or other players return; // TODO: toggle player's godmode statue from console or other players
...@@ -32,7 +32,6 @@ public final class GodModeCommand implements CommandHandler { ...@@ -32,7 +32,6 @@ public final class GodModeCommand implements CommandHandler {
} else { } else {
target = sender.getUid(); target = sender.getUid();
} }
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if (targetPlayer == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return; return;
......
...@@ -14,7 +14,7 @@ import java.util.List; ...@@ -14,7 +14,7 @@ import java.util.List;
description = "Heal all characters in your current team.", permission = "player.heal") description = "Heal all characters in your current team.", permission = "player.heal")
public final class HealCommand implements CommandHandler { public final class HealCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) { if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game); CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return; return;
......
...@@ -13,7 +13,7 @@ import java.util.*; ...@@ -13,7 +13,7 @@ import java.util.*;
public final class HelpCommand implements CommandHandler { public final class HelpCommand implements CommandHandler {
@Override @Override
public void execute(Player player, List<String> args) { public void execute(Player player, Player targetPlayer, List<String> args) {
if (args.size() < 1) { if (args.size() < 1) {
HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers(); HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
List<Command> annotations = new ArrayList<>(); List<Command> annotations = new ArrayList<>();
......
...@@ -12,10 +12,9 @@ import java.util.List; ...@@ -12,10 +12,9 @@ import java.util.List;
public final class KickCommand implements CommandHandler { public final class KickCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
int target = Integer.parseInt(args.get(0)); int target = Integer.parseInt(args.get(0));
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if (targetPlayer == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return; return;
......
...@@ -15,9 +15,8 @@ import java.util.List; ...@@ -15,9 +15,8 @@ import java.util.List;
public final class KillAllCommand implements CommandHandler { public final class KillAllCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
Scene mainScene; Scene mainScene;
Player targetPlayer;
try { try {
switch (args.size()) { switch (args.size()) {
......
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