Fix some behavior of trial avatars

investigation is required on the exceptions thrown
This commit is contained in:
KingRainbow44 2023-04-29 02:01:00 -04:00
parent d5f75e1889
commit 391903ac0e
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
3 changed files with 36 additions and 22 deletions

View File

@ -514,36 +514,49 @@ public final class TeamManager extends BasePlayerDataManager {
*/ */
public void removeTrialAvatarTeam(List<Integer> trialAvatarIds) { public void removeTrialAvatarTeam(List<Integer> trialAvatarIds) {
var player = this.getPlayer(); var player = this.getPlayer();
var scene = player.getScene();
// Disable the trial team. // Disable the trial team.
this.usingTrialTeam = false; this.usingTrialTeam = false;
this.trialAvatarTeam = new TeamInfo(); this.trialAvatarTeam = new TeamInfo();
// Remove the avatars from the team. // Remove the avatars from the team.
this.getActiveTeam().forEach(avatarEntity -> scene
.removeEntity(avatarEntity, VisionTypeOuterClass.VisionType.VISION_TYPE_REMOVE));
if (trialAvatarIds.size() == 4) {
this.getActiveTeam().clear();
this.getTrialAvatars().clear();
} else {
trialAvatarIds.forEach( trialAvatarIds.forEach(
trialAvatarId -> { trialAvatarId -> {
this.getActiveTeam()
.forEach(
x ->
player
.getScene()
.removeEntity(x, VisionTypeOuterClass.VisionType.VISION_TYPE_REMOVE));
this.getActiveTeam().removeIf(x -> x.getAvatar().getTrialAvatarId() == trialAvatarId); this.getActiveTeam().removeIf(x -> x.getAvatar().getTrialAvatarId() == trialAvatarId);
this.getTrialAvatars().values().removeIf(x -> x.getTrialAvatarId() == trialAvatarId); this.getTrialAvatars().values().removeIf(x -> x.getTrialAvatarId() == trialAvatarId);
}); });
}
// Re-add the avatars to the team. // Re-add the avatars to the team.
if (trialAvatarIds.size() == 4) {
// Restores all avatars from the player's avatar storage.
this.getCurrentTeamInfo().getAvatars().forEach(avatarId ->
this.getActiveTeam().add(new EntityAvatar(
scene, player.getAvatars().getAvatarById(avatarId)
)));
} else {
var index = 0; 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()) { for (var avatar : this.getCurrentTeamInfo().getAvatars()) {
if (this.getActiveTeam().stream() if (this.getActiveTeam().stream()
.map(entity -> entity.getAvatar().getAvatarId()) .map(entity -> entity.getAvatar().getAvatarId())
.toList() .toList()
.contains(avatar)) return; .contains(avatar)) continue;
this.getActiveTeam() this.getActiveTeam()
.add( .add(
index++, index++,
new EntityAvatar(player.getScene(), player.getAvatars().getAvatarById(avatar))); new EntityAvatar(scene, player.getAvatars().getAvatarById(avatar)));
}
} }
this.unsetTrialAvatarTeam(); this.unsetTrialAvatarTeam();
@ -1046,7 +1059,7 @@ public final class TeamManager extends BasePlayerDataManager {
this.removeTrialAvatar( this.removeTrialAvatar(
this.getActiveTeam().stream() this.getActiveTeam().stream()
.map(EntityAvatar::getAvatar) .map(EntityAvatar::getAvatar)
.map(Avatar::getAvatarId) .map(Avatar::getTrialAvatarId)
.toList()); .toList());
} }
@ -1068,11 +1081,11 @@ public final class TeamManager extends BasePlayerDataManager {
public void removeTrialAvatar(List<Integer> trialAvatarIds) { public void removeTrialAvatar(List<Integer> trialAvatarIds) {
if (!this.isUsingTrialTeam()) throw new RuntimeException("Player is not using a trial team."); if (!this.isUsingTrialTeam()) throw new RuntimeException("Player is not using a trial team.");
this.removeTrialAvatarTeam(trialAvatarIds);
this.getPlayer() this.getPlayer()
.sendPacket( .sendPacket(
new PacketAvatarDelNotify( new PacketAvatarDelNotify(
trialAvatarIds.stream().map(this::getTrialAvatarGuid).toList())); trialAvatarIds.stream().map(this::getTrialAvatarGuid).toList()));
this.removeTrialAvatarTeam(trialAvatarIds);
// Update the team. // Update the team.
if (trialAvatarIds.size() == 1) this.getPlayer().sendPacket(new PacketAvatarTeamUpdateNotify()); if (trialAvatarIds.size() == 1) this.getPlayer().sendPacket(new PacketAvatarTeamUpdateNotify());

View File

@ -18,9 +18,9 @@ public class ExecGrantTrialAvatar extends QuestExecHandler {
.addTrialAvatar(Integer.parseInt(paramStr[0]), quest.getMainQuestId()); .addTrialAvatar(Integer.parseInt(paramStr[0]), quest.getMainQuestId());
Grasscutter.getLogger() Grasscutter.getLogger()
.debug("Added trial avatar to team for quest {}", quest.getSubQuestId()); .debug("Added trial avatar to team for quest {}", quest.getSubQuestId());
return true; return true;
} catch (RuntimeException ignored) { } catch (RuntimeException exception) {
exception.printStackTrace();
return false; return false;
} }
} }

View File

@ -16,7 +16,8 @@ public class ExecRemoveTrialAvatar extends QuestExecHandler {
Grasscutter.getLogger() Grasscutter.getLogger()
.debug("Removed trial avatar from team for quest {}", quest.getSubQuestId()); .debug("Removed trial avatar from team for quest {}", quest.getSubQuestId());
return true; return true;
} catch (RuntimeException ignored) { } catch (RuntimeException exception) {
exception.printStackTrace();
return false; return false;
} }
} }