Command Targeting overhaul fixes

This commit is contained in:
AnimeGitB 2022-05-05 14:33:08 +09:30 committed by Melledy
parent d8b2e787e5
commit 37c2ee5e2c
13 changed files with 78 additions and 54 deletions

View File

@ -12,7 +12,7 @@ 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 final Map<String, Integer> targetPlayerIds = new HashMap<>();
private static final String consoleId = "console"; private static final String consoleId = "console";
public CommandMap() { public CommandMap() {
this(false); this(false);
@ -122,24 +122,29 @@ public final class CommandMap {
String targetUidStr = null; String targetUidStr = null;
if (label.startsWith("@")) { // @[UID] if (label.startsWith("@")) { // @[UID]
targetUidStr = label.substring(1); targetUidStr = label.substring(1);
} else if (label == "target") { // target [[@]UID] } else if (label.equalsIgnoreCase("target")) { // target [[@]UID]
if (args.size() > 0) {
targetUidStr = args.get(0); targetUidStr = args.get(0);
if (targetUidStr.startsWith("@")) { if (targetUidStr.startsWith("@")) {
targetUidStr = targetUidStr.substring(1); targetUidStr = targetUidStr.substring(1);
} }
} else {
targetUidStr = "";
} }
if (targetUidStr == "") { // Clears default targetPlayer }
targetPlayers.remove(playerId); if (targetUidStr != null) {
if (targetUidStr.equals("")) { // Clears default targetPlayer
targetPlayerIds.remove(playerId);
CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_cleared); CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_cleared);
return; return;
} else if (targetUidStr != null) { // Sets default targetPlayer to the UID given } else { // Sets default targetPlayer to the UID given
try { try {
int uid = Integer.parseInt(targetUidStr); int uid = Integer.parseInt(targetUidStr);
targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid); targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid);
if (targetPlayer == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(player, Grasscutter.getLanguage().Player_not_found_or_offline); CommandHandler.sendMessage(player, Grasscutter.getLanguage().Player_not_found_or_offline);
} else { } else {
targetPlayers.put(playerId, targetPlayer); targetPlayerIds.put(playerId, uid);
CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_set.replace("{uid}", targetUidStr)); CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_set.replace("{uid}", targetUidStr));
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -147,6 +152,7 @@ public final class CommandMap {
} }
return; return;
} }
}
// Get command handler. // Get command handler.
CommandHandler handler = this.commands.get(label); 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 there's still no targetPlayer at this point, use previously-set target
if (targetPlayer == null) { 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. // Check for permission.

View File

@ -32,7 +32,7 @@ public final class ClearCommand implements CommandHandler {
.filter(item -> item.getItemType() == ItemType.ITEM_WEAPON) .filter(item -> item.getItemType() == ItemType.ITEM_WEAPON)
.filter(item -> !item.isLocked() && !item.isEquipped()) .filter(item -> !item.isLocked() && !item.isEquipped())
.forEach(item -> playerInventory.removeItem(item, item.getCount())); .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" -> { case "art" -> {
playerInventory.getItems().values().stream() 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.getLevel() == 1 && item.getExp() == 0)
.filter(item -> !item.isLocked() && !item.isEquipped()) .filter(item -> !item.isLocked() && !item.isEquipped())
.forEach(item -> playerInventory.removeItem(item, item.getCount())); .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" -> { case "mat" -> {
playerInventory.getItems().values().stream() 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.getLevel() == 1 && item.getExp() == 0)
.filter(item -> !item.isLocked() && !item.isEquipped()) .filter(item -> !item.isLocked() && !item.isEquipped())
.forEach(item -> playerInventory.removeItem(item, item.getCount())); .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" -> { case "all" -> {
playerInventory.getItems().values().stream() 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.getLevel() == 1 && item1.getExp() == 0)
.filter(item1 -> !item1.isLocked() && !item1.isEquipped()) .filter(item1 -> !item1.isLocked() && !item1.isEquipped())
.forEach(item1 -> playerInventory.removeItem(item1, item1.getCount())); .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() playerInventory.getItems().values().stream()
.filter(item2 -> item2.getItemType() == ItemType.ITEM_MATERIAL) .filter(item2 -> item2.getItemType() == ItemType.ITEM_MATERIAL)
.filter(item2 -> !item2.isLocked() && !item2.isEquipped()) .filter(item2 -> !item2.isLocked() && !item2.isEquipped())
.forEach(item2 -> playerInventory.removeItem(item2, item2.getCount())); .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() playerInventory.getItems().values().stream()
.filter(item3 -> item3.getItemType() == ItemType.ITEM_WEAPON) .filter(item3 -> item3.getItemType() == ItemType.ITEM_WEAPON)
.filter(item3 -> item3.getLevel() == 1 && item3.getExp() == 0) .filter(item3 -> item3.getLevel() == 1 && item3.getExp() == 0)
.filter(item3 -> !item3.isLocked() && !item3.isEquipped()) .filter(item3 -> !item3.isLocked() && !item3.isEquipped())
.forEach(item3 -> playerInventory.removeItem(item3, item3.getCount())); .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() playerInventory.getItems().values().stream()
.filter(item4 -> item4.getItemType() == ItemType.ITEM_FURNITURE) .filter(item4 -> item4.getItemType() == ItemType.ITEM_FURNITURE)
.filter(item4 -> !item4.isLocked() && !item4.isEquipped()) .filter(item4 -> !item4.isLocked() && !item4.isEquipped())
.forEach(item4 -> playerInventory.removeItem(item4, item4.getCount())); .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() playerInventory.getItems().values().stream()
.filter(item5 -> item5.getItemType() == ItemType.ITEM_DISPLAY) .filter(item5 -> item5.getItemType() == ItemType.ITEM_DISPLAY)
.filter(item5 -> !item5.isLocked() && !item5.isEquipped()) .filter(item5 -> !item5.isLocked() && !item5.isEquipped())
.forEach(item5 -> playerInventory.removeItem(item5, item5.getCount())); .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() playerInventory.getItems().values().stream()
.filter(item6 -> item6.getItemType() == ItemType.ITEM_VIRTUAL) .filter(item6 -> item6.getItemType() == ItemType.ITEM_VIRTUAL)
.filter(item6 -> !item6.isLocked() && !item6.isEquipped()) .filter(item6 -> !item6.isLocked() && !item6.isEquipped())
.forEach(item6 -> playerInventory.removeItem(item6, item6.getCount())); .forEach(item6 -> playerInventory.removeItem(item6, item6.getCount()));
sender.dropMessage(Grasscutter.getLanguage().Clear_virtuals.replace("{name}", targetPlayer.getNickname())); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_virtuals.replace("{name}", targetPlayer.getNickname()));
sender.dropMessage(Grasscutter.getLanguage().Clear_everything.replace("{name}", targetPlayer.getNickname())); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_everything.replace("{name}", targetPlayer.getNickname()));
} }
} }
} }

View File

@ -20,17 +20,21 @@ public final class CoopCommand implements CommandHandler {
Player host = sender; Player host = sender;
switch (args.size()) { switch (args.size()) {
case 0: // Summon target to self 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; break;
case 1: // Summon target to argument case 1: // Summon target to argument
try { try {
int hostId = Integer.parseInt(args.get(1)); int hostId = Integer.parseInt(args.get(0));
host = sender.getServer().getPlayerByUid(hostId); host = Grasscutter.getGameServer().getPlayerByUid(hostId);
if (host == null) { if (host == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_is_offline); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_is_offline);
return; return;
} }
break; break;
} catch (Exception e) { } catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return; return;
} }
@ -39,12 +43,12 @@ public final class CoopCommand implements CommandHandler {
return; return;
} }
// There's no target==host check but this just places them in multiplayer in their own world which seems fine.
if (targetPlayer.isInMultiplayer()) { if (targetPlayer.isInMultiplayer()) {
sender.getServer().getMultiplayerManager().leaveCoop(targetPlayer); targetPlayer.getServer().getMultiplayerManager().leaveCoop(targetPlayer);
} }
sender.getServer().getMultiplayerManager().applyEnterMp(targetPlayer, host.getUid()); host.getServer().getMultiplayerManager().applyEnterMp(targetPlayer, host.getUid());
sender.getServer().getMultiplayerManager().applyEnterMpReply(host, targetPlayer.getUid(), true); targetPlayer.getServer().getMultiplayerManager().applyEnterMpReply(host, targetPlayer.getUid(), true);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_success.replace("{host}", host.getNickname()).replace("{target}", targetPlayer.getNickname())); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_success.replace("{host}", host.getNickname()).replace("{target}", targetPlayer.getNickname()));
} }
} }

View File

@ -37,7 +37,7 @@ public final class GiveAllCommand implements CommandHandler {
} }
break; break;
default: // invalid default: // invalid
CommandHandler.sendMessage(null, Grasscutter.getLanguage().GiveAll_usage); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveAll_usage);
return; return;
} }

View File

@ -52,7 +52,7 @@ public final class GiveArtifactCommand implements CommandHandler {
int level = 1; int level = 1;
try { try {
int last = Integer.parseInt(args.get(args.size()-1)); 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; level = last;
args.remove(args.size()-1); args.remove(args.size()-1);
} }

View File

@ -35,6 +35,6 @@ public final class GodModeCommand implements CommandHandler {
} }
targetPlayer.setGodmode(enabled); 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()));
} }
} }

View File

@ -20,6 +20,6 @@ public final class PositionCommand implements CommandHandler {
} }
Position pos = targetPlayer.getPos(); 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())));
} }
} }

View File

@ -23,7 +23,7 @@ public final class ResetConstCommand implements CommandHandler {
if (args.size() > 0 && args.get(0).equalsIgnoreCase("all")) { if (args.size() > 0 && args.get(0).equalsIgnoreCase("all")) {
targetPlayer.getAvatars().forEach(this::resetConstellation); targetPlayer.getAvatars().forEach(this::resetConstellation);
targetPlayer.dropMessage(Grasscutter.getLanguage().ResetConst_reset_all); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().ResetConst_reset_all);
} else { } else {
EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity(); EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity();
if (entity == null) { if (entity == null) {
@ -33,7 +33,7 @@ public final class ResetConstCommand implements CommandHandler {
Avatar avatar = entity.getAvatar(); Avatar avatar = entity.getAvatar();
this.resetConstellation(avatar); 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()));
} }
} }

View File

@ -11,6 +11,9 @@ public final class RestartCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, Player targetPlayer, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
return;
}
sender.getSession().close(); sender.getSession().close();
} }
} }

View File

@ -27,7 +27,7 @@ public final class SetWorldLevelCommand implements CommandHandler {
try { try {
int level = Integer.parseInt(args.get(0)); int level = Integer.parseInt(args.get(0));
if (level > 8 || level < 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; return;
} }
@ -35,7 +35,7 @@ public final class SetWorldLevelCommand implements CommandHandler {
targetPlayer.getWorld().setWorldLevel(level); targetPlayer.getWorld().setWorldLevel(level);
targetPlayer.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) { } catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().SetWorldLevel_invalid_world_level); CommandHandler.sendMessage(null, Grasscutter.getLanguage().SetWorldLevel_invalid_world_level);
} }

View File

@ -56,7 +56,7 @@ public final class TalentCommand implements CommandHandler {
return; return;
} }
EntityAvatar entity = sender.getTeamManager().getCurrentAvatarEntity(); EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity();
Avatar avatar = entity.getAvatar(); Avatar avatar = entity.getAvatar();
String cmdSwitch = args.get(0); String cmdSwitch = args.get(0);
switch (cmdSwitch) { switch (cmdSwitch) {

View File

@ -46,8 +46,8 @@ public final class TeleportCommand implements CommandHandler {
case 3: case 3:
try { try {
x = parseRelative(args.get(0), x); x = parseRelative(args.get(0), x);
y = parseRelative(args.get(0), y); y = parseRelative(args.get(1), y);
z = parseRelative(args.get(0), z); z = parseRelative(args.get(2), z);
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Teleport_invalid_position); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Teleport_invalid_position);
} }

View File

@ -43,9 +43,9 @@ public final class WeatherCommand implements CommandHandler {
ClimateType climate = ClimateType.getTypeByValue(climateId); ClimateType climate = ClimateType.getTypeByValue(climateId);
sender.getScene().setWeather(weatherId); targetPlayer.getScene().setWeather(weatherId);
sender.getScene().setClimate(climate); targetPlayer.getScene().setClimate(climate);
sender.getScene().broadcastPacket(new PacketSceneAreaWeatherNotify(sender)); targetPlayer.getScene().broadcastPacket(new PacketSceneAreaWeatherNotify(targetPlayer));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Weather_message.replace("{weatherId}", Integer.toString(weatherId)).replace("{climateId}", Integer.toString(climateId))); CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Weather_message.replace("{weatherId}", Integer.toString(weatherId)).replace("{climateId}", Integer.toString(climateId)));
} }