From ed3186a2b22c705315e568515b2e205a4df5855e Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 29 Apr 2023 21:21:58 -0400 Subject: [PATCH] Fix trial avatar removal it is still *sort of* broken when combined with early leaving a domain --- .../grasscutter/game/player/TeamManager.java | 18 +++++++++++------- .../game/quest/exec/ExecRemoveTrialAvatar.java | 4 ++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/player/TeamManager.java b/src/main/java/emu/grasscutter/game/player/TeamManager.java index 1d5e774e1..e10bcc69e 100644 --- a/src/main/java/emu/grasscutter/game/player/TeamManager.java +++ b/src/main/java/emu/grasscutter/game/player/TeamManager.java @@ -64,7 +64,7 @@ public final class TeamManager extends BasePlayerDataManager { public TeamManager() { this.mpTeam = new TeamInfo(); - this.avatars = new ArrayList<>(); + this.avatars = Collections.synchronizedList(new ArrayList<>()); this.gadgets = new HashSet<>(); this.teamResonances = new IntOpenHashSet(); this.teamResonancesConfig = new IntOpenHashSet(); @@ -551,10 +551,13 @@ public final class TeamManager extends BasePlayerDataManager { scene, player.getAvatars().getAvatarById(avatarId) ))); } else { - var index = 0; // Restores all avatars from the player's avatar storage. // If the avatar is already in the team, it will not be added. - for (var avatar : this.getCurrentTeamInfo().getAvatars()) { + // TODO: Fix order in which avatars are added. + // Currently, they are added from last to first. + var avatars = this.getCurrentTeamInfo().getAvatars(); + for (var index = 0; index < avatars.size(); index++) { + var avatar = avatars.get(index); if (this.getActiveTeam().stream() .map(entity -> entity.getAvatar().getAvatarId()) .toList() @@ -564,10 +567,7 @@ public final class TeamManager extends BasePlayerDataManager { var avatarData = player.getAvatars().getAvatarById(avatar); if (avatarData == null) continue; - this.getActiveTeam() - .add( - index++, - new EntityAvatar(scene, avatarData)); + this.getActiveTeam().add(index, new EntityAvatar(scene, avatarData)); } } @@ -1091,6 +1091,10 @@ public final class TeamManager extends BasePlayerDataManager { * @param trialAvatarIds List of trial avatar IDs. */ public void removeTrialAvatar(List trialAvatarIds) { + // Check if the player is using a trial team. + if (!this.isUsingTrialTeam()) + throw new IllegalStateException("Player is not using trial team."); + this.getPlayer() .sendPacket( new PacketAvatarDelNotify( diff --git a/src/main/java/emu/grasscutter/game/quest/exec/ExecRemoveTrialAvatar.java b/src/main/java/emu/grasscutter/game/quest/exec/ExecRemoveTrialAvatar.java index c2d07b2e2..38f25ed6a 100644 --- a/src/main/java/emu/grasscutter/game/quest/exec/ExecRemoveTrialAvatar.java +++ b/src/main/java/emu/grasscutter/game/quest/exec/ExecRemoveTrialAvatar.java @@ -16,6 +16,10 @@ public class ExecRemoveTrialAvatar extends QuestExecHandler { Grasscutter.getLogger() .debug("Removed trial avatar from team for quest {}", quest.getSubQuestId()); return true; + } catch (IllegalStateException ignored) { + // The player does not have any trial avatars equipped. + Grasscutter.getLogger().warn("Attempted to remove trial avatars from player with none equipped."); + return true; } catch (RuntimeException exception) { exception.printStackTrace(); return false;