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 {
isLastInterrupted = false;
try {
CommandMap.getInstance().invoke(null, input);
CommandMap.getInstance().invoke(null, null, input);
} catch (Exception e) {
Grasscutter.getLogger().error(language.Command_error, e);
}
......
......@@ -14,6 +14,8 @@ public @interface Command {
String[] aliases() default {};
String permission() default "";
String permissionTargeted() default "";
boolean threading() default false;
}
......@@ -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<String> args) {
default void execute(Player sender, Player targetPlayer, List<String> args) {
}
}
......@@ -12,6 +12,8 @@ import java.util.*;
public final class CommandMap {
private final Map<String, CommandHandler> commands = 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() {
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<String> 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();
......
......@@ -12,7 +12,7 @@ import java.util.List;
public final class AccountCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender != null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().This_command_can_only_run_from_console);
return;
......
......@@ -12,7 +12,7 @@ import java.util.List;
public final class BroadcastCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Broadcast_command_usage);
return;
......
......@@ -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<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return;
......
......@@ -16,7 +16,7 @@ import java.util.List;
public final class ClearCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> 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;
......
......@@ -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<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 2) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_usage);
return;
......
......@@ -16,7 +16,7 @@ import java.util.List;
public final class DropCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return;
......
......@@ -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<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return;
......
......@@ -18,7 +18,7 @@ import java.util.*;
public final class GiveAllCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> 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;
......
......@@ -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")
public final class GiveArtifactCommand implements CommandHandler {
@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;
ArrayList<Integer> 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;
......
......@@ -15,7 +15,7 @@ import java.util.List;
public final class GiveCharCommand implements CommandHandler {
@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;
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;
......
......@@ -18,7 +18,7 @@ import java.util.List;
public final class GiveCommand implements CommandHandler {
@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;
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);
......
......@@ -12,7 +12,7 @@ import java.util.List;
public final class GodModeCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> 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;
......
......@@ -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<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return;
......
......@@ -13,7 +13,7 @@ import java.util.*;
public final class HelpCommand implements CommandHandler {
@Override
public void execute(Player player, List<String> args) {
public void execute(Player player, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
List<Command> annotations = new ArrayList<>();
......
......@@ -12,10 +12,9 @@ import java.util.List;
public final class KickCommand implements CommandHandler {
@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));
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return;
......
......@@ -15,9 +15,8 @@ import java.util.List;
public final class KillAllCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
Scene mainScene;
Player targetPlayer;
try {
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