Commit 880d1770 authored by ProxyismGH's avatar ProxyismGH Committed by Melledy
Browse files

TeamManager avatar add refactor.

parent 6a9018e3
...@@ -57,7 +57,8 @@ public final class GiveAllCommand implements CommandHandler { ...@@ -57,7 +57,8 @@ public final class GiveAllCommand implements CommandHandler {
} }
// This will handle stats and talents // This will handle stats and talents
avatar.recalcStats(); avatar.recalcStats();
player.addAvatar(avatar); // Don't try to add each avatar to the current team
player.addAvatar(avatar, false);
} }
//some test items //some test items
......
...@@ -809,7 +809,7 @@ public class Player { ...@@ -809,7 +809,7 @@ public class Player {
this.hasSentAvatarDataNotify = hasSentAvatarDataNotify; this.hasSentAvatarDataNotify = hasSentAvatarDataNotify;
} }
public void addAvatar(Avatar avatar) { public void addAvatar(Avatar avatar, boolean addToCurrentTeam) {
boolean result = getAvatars().addAvatar(avatar); boolean result = getAvatars().addAvatar(avatar);
if (result) { if (result) {
...@@ -820,14 +820,22 @@ public class Player { ...@@ -820,14 +820,22 @@ public class Player {
if (hasSentAvatarDataNotify()) { if (hasSentAvatarDataNotify()) {
// Recalc stats // Recalc stats
avatar.recalcStats(); avatar.recalcStats();
// Packet // Packet, show notice on left if the avatar will be added to the team
sendPacket(new PacketAvatarAddNotify(avatar, false)); sendPacket(new PacketAvatarAddNotify(avatar, addToCurrentTeam && this.getTeamManager().canAddAvatarToCurrentTeam()));
if (addToCurrentTeam) {
// If space in team, add
this.getTeamManager().addAvatarToCurrentTeam(avatar);
}
} }
} else { } else {
// Failed adding avatar // Failed adding avatar
} }
} }
public void addAvatar(Avatar avatar) {
addAvatar(avatar, true);
}
public void addFlycloak(int flycloakId) { public void addFlycloak(int flycloakId) {
this.getFlyCloakList().add(flycloakId); this.getFlyCloakList().add(flycloakId);
this.sendPacket(new PacketAvatarGainFlycloakNotify(flycloakId)); this.sendPacket(new PacketAvatarGainFlycloakNotify(flycloakId));
......
...@@ -44,7 +44,7 @@ public class TeamInfo { ...@@ -44,7 +44,7 @@ public class TeamInfo {
} }
public boolean addAvatar(Avatar avatar) { public boolean addAvatar(Avatar avatar) {
if (size() >= GAME_OPTIONS.avatarLimits.singlePlayerTeam || contains(avatar)) { if (contains(avatar)) {
return false; return false;
} }
......
...@@ -104,6 +104,20 @@ public class TeamManager { ...@@ -104,6 +104,20 @@ public class TeamManager {
this.mpTeam = mpTeam; 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() { public int getCurrentTeamId() {
// Starts from 1 // Starts from 1
return currentTeamIndex; return currentTeamIndex;
...@@ -186,6 +200,107 @@ public class TeamManager { ...@@ -186,6 +200,107 @@ public class TeamManager {
// Methods // 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<Avatar> 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<Avatar> 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() { private void updateTeamResonances() {
Int2IntOpenHashMap map = new Int2IntOpenHashMap(); Int2IntOpenHashMap map = new Int2IntOpenHashMap();
...@@ -315,19 +430,7 @@ public class TeamManager { ...@@ -315,19 +430,7 @@ public class TeamManager {
// Clear current team info and add avatars from our new team // Clear current team info and add avatars from our new team
teamInfo.getAvatars().clear(); teamInfo.getAvatars().clear();
for (Avatar avatar : newTeam) { this.addAvatarsToTeam(teamInfo, 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));
}
} }
public void setupMpTeam(List<Long> list) { public void setupMpTeam(List<Long> list) {
...@@ -351,12 +454,7 @@ public class TeamManager { ...@@ -351,12 +454,7 @@ public class TeamManager {
// Clear current team info and add avatars from our new team // Clear current team info and add avatars from our new team
teamInfo.getAvatars().clear(); teamInfo.getAvatars().clear();
for (Avatar avatar : newTeam) { this.addAvatarsToTeam(teamInfo, newTeam);
teamInfo.addAvatar(avatar);
}
// Packet
this.updateTeamEntities(new PacketChangeMpTeamAvatarRsp(getPlayer(), teamInfo));
} }
public void setupTemporaryTeam(List<List<Long>> guidList) { public void setupTemporaryTeam(List<List<Long>> guidList) {
......
...@@ -65,7 +65,8 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler { ...@@ -65,7 +65,8 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler {
if (player.getAvatars().getAvatarCount() == 0) { if (player.getAvatars().getAvatarCount() == 0) {
Avatar mainCharacter = new Avatar(avatarId); Avatar mainCharacter = new Avatar(avatarId);
mainCharacter.setSkillDepotData(GameData.getAvatarSkillDepotDataMap().get(startingSkillDepot)); mainCharacter.setSkillDepotData(GameData.getAvatarSkillDepotDataMap().get(startingSkillDepot));
player.addAvatar(mainCharacter); // Manually handle adding to team
player.addAvatar(mainCharacter, false);
player.setMainCharacterId(avatarId); player.setMainCharacterId(avatarId);
player.setHeadImage(avatarId); player.setHeadImage(avatarId);
player.getTeamManager().getCurrentSinglePlayerTeamInfo().getAvatars().add(mainCharacter.getAvatarId()); player.getTeamManager().getCurrentSinglePlayerTeamInfo().getAvatars().add(mainCharacter.getAvatarId());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment