mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-17 01:15:52 +01:00
[WIP] Command Targeting overhaul
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user