mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-16 17:05:20 +01:00
TeamManager avatar add refactor.
This commit is contained in:
@@ -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<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() {
|
||||
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<Long> 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<List<Long>> guidList) {
|
||||
|
||||
Reference in New Issue
Block a user