From 2cfaeac6bcccd24e73da053420f935ebdee95c10 Mon Sep 17 00:00:00 2001 From: AnimeGitB Date: Thu, 5 May 2022 14:33:08 +0930 Subject: [PATCH] Command Targeting overhaul fixes --- .../emu/grasscutter/command/CommandMap.java | 63 ++++++++++++------- .../command/commands/ClearCommand.java | 20 +++--- .../command/commands/CoopCommand.java | 18 +++--- .../command/commands/GiveAllCommand.java | 2 +- .../command/commands/GiveArtifactCommand.java | 2 +- .../command/commands/GodModeCommand.java | 2 +- .../command/commands/PositionCommand.java | 2 +- .../command/commands/ResetConstCommand.java | 4 +- .../command/commands/RestartCommand.java | 3 + .../commands/SetWorldLevelCommand.java | 4 +- .../command/commands/TalentCommand.java | 2 +- .../command/commands/TeleportCommand.java | 4 +- .../command/commands/WeatherCommand.java | 6 +- 13 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/main/java/emu/grasscutter/command/CommandMap.java b/src/main/java/emu/grasscutter/command/CommandMap.java index 300c00b29..f17fb0443 100644 --- a/src/main/java/emu/grasscutter/command/CommandMap.java +++ b/src/main/java/emu/grasscutter/command/CommandMap.java @@ -12,7 +12,7 @@ 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 final Map targetPlayerIds = new HashMap<>(); private static final String consoleId = "console"; public CommandMap() { this(false); @@ -122,31 +122,37 @@ public final class CommandMap { String targetUidStr = null; if (label.startsWith("@")) { // @[UID] targetUidStr = label.substring(1); - } else if (label == "target") { // target [[@]UID] - targetUidStr = args.get(0); - if (targetUidStr.startsWith("@")) { - targetUidStr = targetUidStr.substring(1); + } else if (label.equalsIgnoreCase("target")) { // target [[@]UID] + if (args.size() > 0) { + targetUidStr = args.get(0); + if (targetUidStr.startsWith("@")) { + targetUidStr = targetUidStr.substring(1); + } + } else { + targetUidStr = ""; } } - if (targetUidStr == "") { // Clears default targetPlayer - targetPlayers.remove(playerId); - CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_cleared); - return; - } else if (targetUidStr != null) { // Sets default targetPlayer to the UID given - try { - int uid = Integer.parseInt(targetUidStr); - 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}", targetUidStr)); + if (targetUidStr != null) { + if (targetUidStr.equals("")) { // Clears default targetPlayer + targetPlayerIds.remove(playerId); + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_cleared); + return; + } else { // Sets default targetPlayer to the UID given + try { + int uid = Integer.parseInt(targetUidStr); + targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid); + if (targetPlayer == null) { + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Player_not_found_or_offline); + } else { + targetPlayerIds.put(playerId, uid); + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_set.replace("{uid}", targetUidStr)); + } + } catch (NumberFormatException e) { + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Invalid_UID); } - } catch (NumberFormatException e) { - CommandHandler.sendMessage(player, Grasscutter.getLanguage().Invalid_UID); + return; } - return; - } + } // Get command handler. CommandHandler handler = this.commands.get(label); @@ -178,7 +184,18 @@ public final class CommandMap { } // If there's still no targetPlayer at this point, use previously-set target if (targetPlayer == null) { - targetPlayer = targetPlayers.getOrDefault(playerId, null); + if (targetPlayerIds.containsKey(playerId)) { + targetPlayer = Grasscutter.getGameServer().getPlayerByUid(targetPlayerIds.get(playerId)); // We check every time in case the target goes offline after being targeted + if (targetPlayer == null) { + CommandHandler.sendMessage(player, Grasscutter.getLanguage().Player_not_found_or_offline); + return; + } + } else { + // If there's still no targetPlayer at this point, use local player + if (targetPlayer == null) { + targetPlayer = player; + } + } } // Check for permission. diff --git a/src/main/java/emu/grasscutter/command/commands/ClearCommand.java b/src/main/java/emu/grasscutter/command/commands/ClearCommand.java index 6abe46b1d..21ad38861 100644 --- a/src/main/java/emu/grasscutter/command/commands/ClearCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ClearCommand.java @@ -32,7 +32,7 @@ public final class ClearCommand implements CommandHandler { .filter(item -> item.getItemType() == ItemType.ITEM_WEAPON) .filter(item -> !item.isLocked() && !item.isEquipped()) .forEach(item -> playerInventory.removeItem(item, item.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_weapons.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_weapons.replace("{name}", targetPlayer.getNickname())); } case "art" -> { playerInventory.getItems().values().stream() @@ -40,7 +40,7 @@ public final class ClearCommand implements CommandHandler { .filter(item -> item.getLevel() == 1 && item.getExp() == 0) .filter(item -> !item.isLocked() && !item.isEquipped()) .forEach(item -> playerInventory.removeItem(item, item.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); } case "mat" -> { playerInventory.getItems().values().stream() @@ -48,7 +48,7 @@ public final class ClearCommand implements CommandHandler { .filter(item -> item.getLevel() == 1 && item.getExp() == 0) .filter(item -> !item.isLocked() && !item.isEquipped()) .forEach(item -> playerInventory.removeItem(item, item.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); } case "all" -> { playerInventory.getItems().values().stream() @@ -56,34 +56,34 @@ public final class ClearCommand implements CommandHandler { .filter(item1 -> item1.getLevel() == 1 && item1.getExp() == 0) .filter(item1 -> !item1.isLocked() && !item1.isEquipped()) .forEach(item1 -> playerInventory.removeItem(item1, item1.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); playerInventory.getItems().values().stream() .filter(item2 -> item2.getItemType() == ItemType.ITEM_MATERIAL) .filter(item2 -> !item2.isLocked() && !item2.isEquipped()) .forEach(item2 -> playerInventory.removeItem(item2, item2.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); playerInventory.getItems().values().stream() .filter(item3 -> item3.getItemType() == ItemType.ITEM_WEAPON) .filter(item3 -> item3.getLevel() == 1 && item3.getExp() == 0) .filter(item3 -> !item3.isLocked() && !item3.isEquipped()) .forEach(item3 -> playerInventory.removeItem(item3, item3.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_artifacts.replace("{name}", targetPlayer.getNickname())); playerInventory.getItems().values().stream() .filter(item4 -> item4.getItemType() == ItemType.ITEM_FURNITURE) .filter(item4 -> !item4.isLocked() && !item4.isEquipped()) .forEach(item4 -> playerInventory.removeItem(item4, item4.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_furniture.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_furniture.replace("{name}", targetPlayer.getNickname())); playerInventory.getItems().values().stream() .filter(item5 -> item5.getItemType() == ItemType.ITEM_DISPLAY) .filter(item5 -> !item5.isLocked() && !item5.isEquipped()) .forEach(item5 -> playerInventory.removeItem(item5, item5.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_displays.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_displays.replace("{name}", targetPlayer.getNickname())); playerInventory.getItems().values().stream() .filter(item6 -> item6.getItemType() == ItemType.ITEM_VIRTUAL) .filter(item6 -> !item6.isLocked() && !item6.isEquipped()) .forEach(item6 -> playerInventory.removeItem(item6, item6.getCount())); - sender.dropMessage(Grasscutter.getLanguage().Clear_virtuals.replace("{name}", targetPlayer.getNickname())); - sender.dropMessage(Grasscutter.getLanguage().Clear_everything.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_virtuals.replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_everything.replace("{name}", targetPlayer.getNickname())); } } } diff --git a/src/main/java/emu/grasscutter/command/commands/CoopCommand.java b/src/main/java/emu/grasscutter/command/commands/CoopCommand.java index f20d4aa01..fff548d95 100644 --- a/src/main/java/emu/grasscutter/command/commands/CoopCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/CoopCommand.java @@ -20,17 +20,21 @@ public final class CoopCommand implements CommandHandler { Player host = sender; switch (args.size()) { case 0: // Summon target to self + if (sender == null) { // Console doesn't have a self to summon to + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_usage); + return; + } break; case 1: // Summon target to argument try { - int hostId = Integer.parseInt(args.get(1)); - host = sender.getServer().getPlayerByUid(hostId); + int hostId = Integer.parseInt(args.get(0)); + host = Grasscutter.getGameServer().getPlayerByUid(hostId); if (host == null) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_is_offline); return; } break; - } catch (Exception e) { + } catch (NumberFormatException ignored) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId); return; } @@ -39,12 +43,12 @@ public final class CoopCommand implements CommandHandler { return; } - + // There's no target==host check but this just places them in multiplayer in their own world which seems fine. if (targetPlayer.isInMultiplayer()) { - sender.getServer().getMultiplayerManager().leaveCoop(targetPlayer); + targetPlayer.getServer().getMultiplayerManager().leaveCoop(targetPlayer); } - sender.getServer().getMultiplayerManager().applyEnterMp(targetPlayer, host.getUid()); - sender.getServer().getMultiplayerManager().applyEnterMpReply(host, targetPlayer.getUid(), true); + host.getServer().getMultiplayerManager().applyEnterMp(targetPlayer, host.getUid()); + targetPlayer.getServer().getMultiplayerManager().applyEnterMpReply(host, targetPlayer.getUid(), true); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_success.replace("{host}", host.getNickname()).replace("{target}", targetPlayer.getNickname())); } } diff --git a/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java index 9158b5e87..21352dba5 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java @@ -37,7 +37,7 @@ public final class GiveAllCommand implements CommandHandler { } break; default: // invalid - CommandHandler.sendMessage(null, Grasscutter.getLanguage().GiveAll_usage); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveAll_usage); return; } diff --git a/src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java index d2bd95922..4d123dc5a 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java @@ -52,7 +52,7 @@ public final class GiveArtifactCommand implements CommandHandler { int level = 1; try { int last = Integer.parseInt(args.get(args.size()-1)); - if (last > 0 && last < 21) { // Luckily appendPropIds aren't in the range of [1,20] + if (last > 0 && last < 22) { // Luckily appendPropIds aren't in the range of [1,21] level = last; args.remove(args.size()-1); } diff --git a/src/main/java/emu/grasscutter/command/commands/GodModeCommand.java b/src/main/java/emu/grasscutter/command/commands/GodModeCommand.java index 824fc73b5..73ec8b032 100644 --- a/src/main/java/emu/grasscutter/command/commands/GodModeCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GodModeCommand.java @@ -35,6 +35,6 @@ public final class GodModeCommand implements CommandHandler { } targetPlayer.setGodmode(enabled); - sender.dropMessage(Grasscutter.getLanguage().Godmode_status.replace("{status}", (enabled ? Grasscutter.getLanguage().Enabled : Grasscutter.getLanguage().Disabled)).replace("{name}", targetPlayer.getNickname())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Godmode_status.replace("{status}", (enabled ? Grasscutter.getLanguage().Enabled : Grasscutter.getLanguage().Disabled)).replace("{name}", targetPlayer.getNickname())); } } diff --git a/src/main/java/emu/grasscutter/command/commands/PositionCommand.java b/src/main/java/emu/grasscutter/command/commands/PositionCommand.java index 85f2d6bc4..0e015d178 100644 --- a/src/main/java/emu/grasscutter/command/commands/PositionCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/PositionCommand.java @@ -20,6 +20,6 @@ public final class PositionCommand implements CommandHandler { } Position pos = targetPlayer.getPos(); - sender.dropMessage(Grasscutter.getLanguage().Position_message.replace("{x}", Float.toString(pos.getX())).replace("{y}", Float.toString(pos.getY())).replace("{z}", Float.toString(pos.getZ())).replace("{id}", Integer.toString(sender.getSceneId()))); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Position_message.replace("{x}", Float.toString(pos.getX())).replace("{y}", Float.toString(pos.getY())).replace("{z}", Float.toString(pos.getZ())).replace("{id}", Integer.toString(targetPlayer.getSceneId()))); } } diff --git a/src/main/java/emu/grasscutter/command/commands/ResetConstCommand.java b/src/main/java/emu/grasscutter/command/commands/ResetConstCommand.java index 6d58f8328..52ed0a55e 100644 --- a/src/main/java/emu/grasscutter/command/commands/ResetConstCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ResetConstCommand.java @@ -23,7 +23,7 @@ public final class ResetConstCommand implements CommandHandler { if (args.size() > 0 && args.get(0).equalsIgnoreCase("all")) { targetPlayer.getAvatars().forEach(this::resetConstellation); - targetPlayer.dropMessage(Grasscutter.getLanguage().ResetConst_reset_all); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().ResetConst_reset_all); } else { EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity(); if (entity == null) { @@ -33,7 +33,7 @@ public final class ResetConstCommand implements CommandHandler { Avatar avatar = entity.getAvatar(); this.resetConstellation(avatar); - sender.dropMessage(Grasscutter.getLanguage().ResetConst_reset_all_done.replace("{name}", avatar.getAvatarData().getName())); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().ResetConst_reset_all_done.replace("{name}", avatar.getAvatarData().getName())); } } diff --git a/src/main/java/emu/grasscutter/command/commands/RestartCommand.java b/src/main/java/emu/grasscutter/command/commands/RestartCommand.java index 156a0cd93..e3b8b2747 100644 --- a/src/main/java/emu/grasscutter/command/commands/RestartCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/RestartCommand.java @@ -11,6 +11,9 @@ public final class RestartCommand implements CommandHandler { @Override public void execute(Player sender, Player targetPlayer, List args) { + if (sender == null) { + return; + } sender.getSession().close(); } } diff --git a/src/main/java/emu/grasscutter/command/commands/SetWorldLevelCommand.java b/src/main/java/emu/grasscutter/command/commands/SetWorldLevelCommand.java index 74880a16b..cd53237ca 100644 --- a/src/main/java/emu/grasscutter/command/commands/SetWorldLevelCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SetWorldLevelCommand.java @@ -27,7 +27,7 @@ public final class SetWorldLevelCommand implements CommandHandler { try { int level = Integer.parseInt(args.get(0)); if (level > 8 || level < 0) { - sender.dropMessage(Grasscutter.getLanguage().SetWorldLevel_world_level_must_between_0_and_8); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().SetWorldLevel_world_level_must_between_0_and_8); return; } @@ -35,7 +35,7 @@ public final class SetWorldLevelCommand implements CommandHandler { targetPlayer.getWorld().setWorldLevel(level); targetPlayer.setWorldLevel(level); - sender.dropMessage(Grasscutter.getLanguage().SetWorldLevel_set_world_level.replace("{level}", Integer.toString(level))); + CommandHandler.sendMessage(sender, Grasscutter.getLanguage().SetWorldLevel_set_world_level.replace("{level}", Integer.toString(level))); } catch (NumberFormatException ignored) { CommandHandler.sendMessage(null, Grasscutter.getLanguage().SetWorldLevel_invalid_world_level); } diff --git a/src/main/java/emu/grasscutter/command/commands/TalentCommand.java b/src/main/java/emu/grasscutter/command/commands/TalentCommand.java index 6d6535e9a..559006473 100644 --- a/src/main/java/emu/grasscutter/command/commands/TalentCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/TalentCommand.java @@ -56,7 +56,7 @@ public final class TalentCommand implements CommandHandler { return; } - EntityAvatar entity = sender.getTeamManager().getCurrentAvatarEntity(); + EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity(); Avatar avatar = entity.getAvatar(); String cmdSwitch = args.get(0); switch (cmdSwitch) { diff --git a/src/main/java/emu/grasscutter/command/commands/TeleportCommand.java b/src/main/java/emu/grasscutter/command/commands/TeleportCommand.java index f47ea73f6..f5a533bef 100644 --- a/src/main/java/emu/grasscutter/command/commands/TeleportCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/TeleportCommand.java @@ -46,8 +46,8 @@ public final class TeleportCommand implements CommandHandler { case 3: try { x = parseRelative(args.get(0), x); - y = parseRelative(args.get(0), y); - z = parseRelative(args.get(0), z); + y = parseRelative(args.get(1), y); + z = parseRelative(args.get(2), z); } catch (NumberFormatException ignored) { CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Teleport_invalid_position); } diff --git a/src/main/java/emu/grasscutter/command/commands/WeatherCommand.java b/src/main/java/emu/grasscutter/command/commands/WeatherCommand.java index c389eb428..eaf73e53a 100644 --- a/src/main/java/emu/grasscutter/command/commands/WeatherCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/WeatherCommand.java @@ -43,9 +43,9 @@ public final class WeatherCommand implements CommandHandler { ClimateType climate = ClimateType.getTypeByValue(climateId); - sender.getScene().setWeather(weatherId); - sender.getScene().setClimate(climate); - sender.getScene().broadcastPacket(new PacketSceneAreaWeatherNotify(sender)); + targetPlayer.getScene().setWeather(weatherId); + targetPlayer.getScene().setClimate(climate); + targetPlayer.getScene().broadcastPacket(new PacketSceneAreaWeatherNotify(targetPlayer)); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Weather_message.replace("{weatherId}", Integer.toString(weatherId)).replace("{climateId}", Integer.toString(climateId))); }