diff --git a/src/main/java/emu/grasscutter/game/TeamInfo.java b/src/main/java/emu/grasscutter/game/TeamInfo.java index 304c06a66..24b8aa60d 100644 --- a/src/main/java/emu/grasscutter/game/TeamInfo.java +++ b/src/main/java/emu/grasscutter/game/TeamInfo.java @@ -60,13 +60,16 @@ public class TeamInfo { } public void copyFrom(TeamInfo team, int maxTeamSize) { - // Clear + // Clone avatar ids from team to copy from + List avatarIds = new ArrayList<>(team.getAvatars()); + + // Clear current avatar list first this.getAvatars().clear(); // Copy from team - int len = Math.min(team.getAvatars().size(), maxTeamSize); + int len = Math.min(avatarIds.size(), maxTeamSize); for (int i = 0; i < len; i++) { - int id = team.getAvatars().get(i); + int id = avatarIds.get(i); this.getAvatars().add(id); } } diff --git a/src/main/java/emu/grasscutter/game/TeamManager.java b/src/main/java/emu/grasscutter/game/TeamManager.java index bcbea7544..5649151a4 100644 --- a/src/main/java/emu/grasscutter/game/TeamManager.java +++ b/src/main/java/emu/grasscutter/game/TeamManager.java @@ -166,7 +166,7 @@ public class TeamManager { // Methods - public void updateTeamResonances() { + private void updateTeamResonances() { Int2IntOpenHashMap map = new Int2IntOpenHashMap(); this.getTeamResonances().clear(); @@ -196,7 +196,7 @@ public class TeamManager { } } - private void updateTeamEntities(GenshinPacket responsePacket) { + public void updateTeamEntities(GenshinPacket responsePacket) { // Sanity check - Should never happen if (this.getCurrentTeamInfo().getAvatars().size() <= 0) { return; @@ -241,7 +241,7 @@ public class TeamManager { // Set new selected character index if (prevSelectedAvatarIndex == -1) { // Previous selected avatar is not in the same spot, we will select the current one in the prev slot - prevSelectedAvatarIndex = Math.min(this.currentCharacterIndex, getCurrentTeamInfo().getAvatars().size() - 1); + prevSelectedAvatarIndex = Math.min(this.currentCharacterIndex, this.getActiveTeam().size() - 1); } this.currentCharacterIndex = prevSelectedAvatarIndex; diff --git a/src/main/java/emu/grasscutter/game/World.java b/src/main/java/emu/grasscutter/game/World.java index 2a5cb6b95..76187d1de 100644 --- a/src/main/java/emu/grasscutter/game/World.java +++ b/src/main/java/emu/grasscutter/game/World.java @@ -28,7 +28,6 @@ import emu.grasscutter.server.packet.send.PacketPlayerEnterSceneNotify; import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify; import emu.grasscutter.server.packet.send.PacketSceneEntityDisappearNotify; import emu.grasscutter.server.packet.send.PacketScenePlayerInfoNotify; -import emu.grasscutter.server.packet.send.PacketSceneTeamUpdateNotify; import emu.grasscutter.server.packet.send.PacketSyncScenePlayTeamEntityNotify; import emu.grasscutter.server.packet.send.PacketSyncTeamEntityNotify; import emu.grasscutter.server.packet.send.PacketWorldPlayerInfoNotify; @@ -169,7 +168,7 @@ public class World implements Iterable { player.setPeerId(this.getNextPeerId()); player.getTeamManager().setEntityId(getNextEntityId(EntityIdType.TEAM)); - // TODO Update team of all players + // Setup team avatars this.setupPlayerAvatars(player); // Info packet for other players @@ -212,18 +211,23 @@ public class World implements Iterable { private void updatePlayerInfos(GenshinPlayer paramPlayer) { for (GenshinPlayer player : getPlayers()) { - // Dont send packets if player is loading in + // Dont send packets if player is loading in and filter out joining player if (!player.hasSentAvatarDataNotify() || player.getSceneLoadState().getValue() < SceneLoadState.INIT.getValue() || player == paramPlayer) { continue; } + // Update team of all players since max players has been changed - Probably not the best way to do it + if (this.isMultiplayer()) { + player.getTeamManager().getMpTeam().copyFrom(player.getTeamManager().getMpTeam(), player.getTeamManager().getMaxTeamSize()); + player.getTeamManager().updateTeamEntities(null); + } + // World player info packets player.getSession().send(new PacketWorldPlayerInfoNotify(this)); player.getSession().send(new PacketScenePlayerInfoNotify(this)); player.getSession().send(new PacketWorldPlayerRTTNotify(this)); // Team packets - player.getSession().send(new PacketSceneTeamUpdateNotify(player)); player.getSession().send(new PacketSyncTeamEntityNotify(player)); player.getSession().send(new PacketSyncScenePlayTeamEntityNotify(player)); } @@ -283,6 +287,11 @@ public class World implements Iterable { EntityAvatar entity = new EntityAvatar(this, player.getAvatars().getAvatarById(avatarId)); player.getTeamManager().getActiveTeam().add(entity); } + + // Limit character index in case its out of bounds + if (player.getTeamManager().getCurrentCharacterIndex() >= player.getTeamManager().getActiveTeam().size() || player.getTeamManager().getCurrentCharacterIndex() < 0) { + player.getTeamManager().setCurrentCharacterIndex(player.getTeamManager().getCurrentCharacterIndex() - 1); + } } private void removePlayerAvatars(GenshinPlayer player) {