From fc687a3bde77b932b0295ed72b9ee677cad749ed Mon Sep 17 00:00:00 2001 From: ProxyismGH <106286648+ProxyismGH@users.noreply.github.com> Date: Thu, 26 May 2022 18:38:52 -0700 Subject: [PATCH] TeamManager avatar add refactor. --- .../command/commands/GiveAllCommand.java | 3 +- .../emu/grasscutter/game/player/Player.java | 14 +- .../emu/grasscutter/game/player/TeamInfo.java | 2 +- .../grasscutter/game/player/TeamManager.java | 138 +++++++++++++++--- .../recv/HandlerSetPlayerBornDataReq.java | 3 +- 5 files changed, 134 insertions(+), 26 deletions(-) diff --git a/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java index 7350d1b3c..5af409e3b 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java @@ -57,7 +57,8 @@ public final class GiveAllCommand implements CommandHandler { } // This will handle stats and talents avatar.recalcStats(); - player.addAvatar(avatar); + // Don't try to add each avatar to the current team + player.addAvatar(avatar, false); } //some test items diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 1eac62faa..6e9b77eb6 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -809,7 +809,7 @@ public class Player { this.hasSentAvatarDataNotify = hasSentAvatarDataNotify; } - public void addAvatar(Avatar avatar) { + public void addAvatar(Avatar avatar, boolean addToCurrentTeam) { boolean result = getAvatars().addAvatar(avatar); if (result) { @@ -820,14 +820,22 @@ public class Player { if (hasSentAvatarDataNotify()) { // Recalc stats avatar.recalcStats(); - // Packet - sendPacket(new PacketAvatarAddNotify(avatar, false)); + // Packet, show notice on left if the avatar will be added to the team + sendPacket(new PacketAvatarAddNotify(avatar, addToCurrentTeam && this.getTeamManager().canAddAvatarToCurrentTeam())); + if (addToCurrentTeam) { + // If space in team, add + this.getTeamManager().addAvatarToCurrentTeam(avatar); + } } } else { // Failed adding avatar } } + public void addAvatar(Avatar avatar) { + addAvatar(avatar, true); + } + public void addFlycloak(int flycloakId) { this.getFlyCloakList().add(flycloakId); this.sendPacket(new PacketAvatarGainFlycloakNotify(flycloakId)); diff --git a/src/main/java/emu/grasscutter/game/player/TeamInfo.java b/src/main/java/emu/grasscutter/game/player/TeamInfo.java index 7d1232e50..89383e486 100644 --- a/src/main/java/emu/grasscutter/game/player/TeamInfo.java +++ b/src/main/java/emu/grasscutter/game/player/TeamInfo.java @@ -44,7 +44,7 @@ public class TeamInfo { } public boolean addAvatar(Avatar avatar) { - if (size() >= GAME_OPTIONS.avatarLimits.singlePlayerTeam || contains(avatar)) { + if (contains(avatar)) { return false; } diff --git a/src/main/java/emu/grasscutter/game/player/TeamManager.java b/src/main/java/emu/grasscutter/game/player/TeamManager.java index be1f2507d..d5d7e1881 100644 --- a/src/main/java/emu/grasscutter/game/player/TeamManager.java +++ b/src/main/java/emu/grasscutter/game/player/TeamManager.java @@ -104,6 +104,20 @@ public class TeamManager { this.mpTeam = mpTeam; } + /** + * Search through all teams and if the team matches, return that index. + * Otherwise, return -1. + * No match could mean that the team does not currently belong to the player. + */ + public int getTeamId(TeamInfo team) { + for (int i = 1; i <= this.teams.size(); i++) { + if (this.teams.get(i).equals(team)) { + return i; + } + } + return -1; + } + public int getCurrentTeamId() { // Starts from 1 return currentTeamIndex; @@ -185,7 +199,108 @@ public class TeamManager { } // Methods - + + /** + * Returns true if there is space to add the number of avatars to the team. + */ + public boolean canAddAvatarsToTeam(TeamInfo team, int avatars) { + return team.size() + avatars <= getMaxTeamSize(); + } + + /** + * Returns true if there is space to add to the team. + */ + public boolean canAddAvatarToTeam(TeamInfo team) { + return canAddAvatarsToTeam(team, 1); + } + + /** + * Returns true if there is space to add the number of avatars to the current team. + * If the current team is temporary, returns false. + */ + public boolean canAddAvatarsToCurrentTeam(int avatars) { + if (this.useTemporarilyTeamIndex != -1){ + return false; + } + return canAddAvatarsToTeam(this.getCurrentTeamInfo(), avatars); + } + + /** + * Returns true if there is space to add to the current team. + * If the current team is temporary, returns false. + */ + public boolean canAddAvatarToCurrentTeam() { + return canAddAvatarsToCurrentTeam(1); + } + + /** + * Try to add the collection of avatars to the team. + * Returns true if all were successfully added. + * If some can not be added, returns false and does not add any. + */ + public boolean addAvatarsToTeam(TeamInfo team, Collection avatars) { + if (!canAddAvatarsToTeam(team, avatars.size())) { + return false; + } + + // Convert avatars into a collection of avatar IDs, then add + team.getAvatars().addAll(avatars.stream().map(a -> a.getAvatarId()).toList()); + + // Update team + if (this.getPlayer().isInMultiplayer()) { + if (team.equals(this.getMpTeam())) { + // MP team Packet + this.updateTeamEntities(new PacketChangeMpTeamAvatarRsp(getPlayer(), team)); + } + } else { + // SP team update packet + getPlayer().sendPacket(new PacketAvatarTeamUpdateNotify(getPlayer())); + + int teamId = this.getTeamId(team); + if (teamId != -1) { + // This is one of the player's teams + // Update entites + if (teamId == this.getCurrentTeamId()) { + this.updateTeamEntities(new PacketSetUpAvatarTeamRsp(getPlayer(), teamId, team)); + } else { + getPlayer().sendPacket(new PacketSetUpAvatarTeamRsp(getPlayer(), teamId, team)); + } + } + } + + return true; + } + + /** + * Try to add an avatar to a team. + * Returns true if successful. + */ + public boolean addAvatarToTeam(TeamInfo team, Avatar avatar){ + return addAvatarsToTeam(team, Collections.singleton(avatar)); + } + + /** + * Try to add the collection of avatars to the current team. + * Will not modify a temporary team. + * Returns true if all were successfully added. + * If some can not be added, returns false and does not add any. + */ + public boolean addAvatarsToCurrentTeam(Collection avatars) { + if (this.useTemporarilyTeamIndex != -1){ + return false; + } + return addAvatarsToTeam(this.getCurrentTeamInfo(), avatars); + } + + /** + * Try to add an avatar to the current team. + * Will not modify a temporary team. + * Returns true if successful. + */ + public boolean addAvatarToCurrentTeam(Avatar avatar) { + return addAvatarsToCurrentTeam(Collections.singleton(avatar)); + } + private void updateTeamResonances() { Int2IntOpenHashMap map = new Int2IntOpenHashMap(); @@ -315,19 +430,7 @@ public class TeamManager { // Clear current team info and add avatars from our new team teamInfo.getAvatars().clear(); - for (Avatar avatar : newTeam) { - teamInfo.addAvatar(avatar); - } - - // Update packet - getPlayer().sendPacket(new PacketAvatarTeamUpdateNotify(getPlayer())); - - // Update entites - if (teamId == this.getCurrentTeamId()) { - this.updateTeamEntities(new PacketSetUpAvatarTeamRsp(getPlayer(), teamId, teamInfo)); - } else { - getPlayer().sendPacket(new PacketSetUpAvatarTeamRsp(getPlayer(), teamId, teamInfo)); - } + this.addAvatarsToTeam(teamInfo, newTeam); } public void setupMpTeam(List list) { @@ -351,12 +454,7 @@ public class TeamManager { // Clear current team info and add avatars from our new team teamInfo.getAvatars().clear(); - for (Avatar avatar : newTeam) { - teamInfo.addAvatar(avatar); - } - - // Packet - this.updateTeamEntities(new PacketChangeMpTeamAvatarRsp(getPlayer(), teamInfo)); + this.addAvatarsToTeam(teamInfo, newTeam); } public void setupTemporaryTeam(List> guidList) { diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java index ec591c91f..064481680 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java @@ -65,7 +65,8 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler { if (player.getAvatars().getAvatarCount() == 0) { Avatar mainCharacter = new Avatar(avatarId); mainCharacter.setSkillDepotData(GameData.getAvatarSkillDepotDataMap().get(startingSkillDepot)); - player.addAvatar(mainCharacter); + // Manually handle adding to team + player.addAvatar(mainCharacter, false); player.setMainCharacterId(avatarId); player.setHeadImage(avatarId); player.getTeamManager().getCurrentSinglePlayerTeamInfo().getAvatars().add(mainCharacter.getAvatarId());