Cleanup Team class

This commit is contained in:
themode 2021-01-14 04:33:23 +01:00
parent 7e8bc5f7d8
commit 238ea649ab

View File

@ -11,6 +11,7 @@ import net.minestom.server.network.packet.server.play.TeamsPacket;
import net.minestom.server.network.packet.server.play.TeamsPacket.CollisionRule; import net.minestom.server.network.packet.server.play.TeamsPacket.CollisionRule;
import net.minestom.server.network.packet.server.play.TeamsPacket.NameTagVisibility; import net.minestom.server.network.packet.server.play.TeamsPacket.NameTagVisibility;
import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
@ -64,17 +65,12 @@ public class Team {
*/ */
private JsonMessage suffix; private JsonMessage suffix;
/**
* Identifiers for the entities in this team
*/
private String[] entities;
/** /**
* Default constructor to creates a team. * Default constructor to creates a team.
* *
* @param teamName The registry name for the team * @param teamName The registry name for the team
*/ */
protected Team(String teamName) { protected Team(@NotNull String teamName) {
this.teamName = teamName; this.teamName = teamName;
this.teamDisplayName = ColoredText.of(""); this.teamDisplayName = ColoredText.of("");
@ -86,7 +82,6 @@ public class Team {
this.prefix = ColoredText.of(""); this.prefix = ColoredText.of("");
this.suffix = ColoredText.of(""); this.suffix = ColoredText.of("");
this.entities = new String[0];
this.members = new CopyOnWriteArraySet<>(); this.members = new CopyOnWriteArraySet<>();
} }
@ -97,12 +92,7 @@ public class Team {
* *
* @param member The member to be added * @param member The member to be added
*/ */
public void addMember(String member) { public void addMember(@NotNull String member) {
final String[] entitiesCache = new String[this.entities.length + 1];
System.arraycopy(this.entities, 0, entitiesCache, 0, this.entities.length);
entitiesCache[this.entities.length] = member;
this.entities = entitiesCache;
// Adds a new member to the team // Adds a new member to the team
this.members.add(member); this.members.add(member);
@ -110,7 +100,7 @@ public class Team {
final TeamsPacket addPlayerPacket = new TeamsPacket(); final TeamsPacket addPlayerPacket = new TeamsPacket();
addPlayerPacket.teamName = this.teamName; addPlayerPacket.teamName = this.teamName;
addPlayerPacket.action = TeamsPacket.Action.ADD_PLAYERS_TEAM; addPlayerPacket.action = TeamsPacket.Action.ADD_PLAYERS_TEAM;
addPlayerPacket.entities = new String[]{member}; addPlayerPacket.entities = members.toArray(new String[0]);
// Sends to all online players the add player packet // Sends to all online players the add player packet
PacketUtils.sendGroupedPacket(CONNECTION_MANAGER.getOnlinePlayers(), addPlayerPacket); PacketUtils.sendGroupedPacket(CONNECTION_MANAGER.getOnlinePlayers(), addPlayerPacket);
@ -121,24 +111,18 @@ public class Team {
* *
* @param member The member to be removed * @param member The member to be removed
*/ */
public void removeMember(String member) { public void removeMember(@NotNull String member) {
// Removes the member from the team
this.members.remove(member);
// Initializes remove player packet // Initializes remove player packet
final TeamsPacket removePlayerPacket = new TeamsPacket(); final TeamsPacket removePlayerPacket = new TeamsPacket();
removePlayerPacket.teamName = this.teamName; removePlayerPacket.teamName = this.teamName;
removePlayerPacket.action = TeamsPacket.Action.REMOVE_PLAYERS_TEAM; removePlayerPacket.action = TeamsPacket.Action.REMOVE_PLAYERS_TEAM;
removePlayerPacket.entities = new String[]{member}; removePlayerPacket.entities = members.toArray(new String[0]);
// Sends to all online player teh remove player packet // Sends to all online player teh remove player packet
PacketUtils.sendGroupedPacket(CONNECTION_MANAGER.getOnlinePlayers(), removePlayerPacket); PacketUtils.sendGroupedPacket(CONNECTION_MANAGER.getOnlinePlayers(), removePlayerPacket);
// Removes the player from the
this.members.remove(member);
final String[] entitiesCache = new String[this.entities.length - 1];
int count = 0;
for (String teamMember : this.members) {
entitiesCache[count++] = teamMember;
}
this.entities = entitiesCache;
} }
/** /**
@ -168,8 +152,9 @@ public class Team {
* <b>Warning:</b> This is only changed on the <b>server side</b>. * <b>Warning:</b> This is only changed on the <b>server side</b>.
* *
* @param visibility The new tag visibility * @param visibility The new tag visibility
* @see #updateNameTagVisibility(NameTagVisibility)
*/ */
public void setNameTagVisibility(NameTagVisibility visibility) { public void setNameTagVisibility(@NotNull NameTagVisibility visibility) {
this.nameTagVisibility = visibility; this.nameTagVisibility = visibility;
} }
@ -178,7 +163,7 @@ public class Team {
* *
* @param nameTagVisibility The new tag visibility * @param nameTagVisibility The new tag visibility
*/ */
public void updateNameTagVisibility(NameTagVisibility nameTagVisibility) { public void updateNameTagVisibility(@NotNull NameTagVisibility nameTagVisibility) {
this.setNameTagVisibility(nameTagVisibility); this.setNameTagVisibility(nameTagVisibility);
sendUpdatePacket(); sendUpdatePacket();
} }
@ -189,8 +174,9 @@ public class Team {
* <b>Warning:</b> This is only changed on the <b>server side</b>. * <b>Warning:</b> This is only changed on the <b>server side</b>.
* *
* @param rule The new rule * @param rule The new rule
* @see #updateCollisionRule(CollisionRule)
*/ */
public void setCollisionRule(CollisionRule rule) { public void setCollisionRule(@NotNull CollisionRule rule) {
this.collisionRule = rule; this.collisionRule = rule;
} }
@ -199,7 +185,7 @@ public class Team {
* *
* @param collisionRule The new collision rule * @param collisionRule The new collision rule
*/ */
public void updateCollisionRule(CollisionRule collisionRule) { public void updateCollisionRule(@NotNull CollisionRule collisionRule) {
this.setCollisionRule(collisionRule); this.setCollisionRule(collisionRule);
sendUpdatePacket(); sendUpdatePacket();
} }
@ -210,8 +196,9 @@ public class Team {
* <b>Warning:</b> This is only changed on the <b>server side</b>. * <b>Warning:</b> This is only changed on the <b>server side</b>.
* *
* @param color The new team color * @param color The new team color
* @see #updateTeamColor(ChatColor)
*/ */
public void setTeamColor(ChatColor color) { public void setTeamColor(@NotNull ChatColor color) {
this.teamColor = color; this.teamColor = color;
} }
@ -220,7 +207,7 @@ public class Team {
* *
* @param teamColor The new team color * @param teamColor The new team color
*/ */
public void updateTeamColor(ChatColor teamColor) { public void updateTeamColor(@NotNull ChatColor teamColor) {
this.setTeamColor(teamColor); this.setTeamColor(teamColor);
sendUpdatePacket(); sendUpdatePacket();
} }
@ -302,6 +289,7 @@ public class Team {
* *
* @return the packet to add the team * @return the packet to add the team
*/ */
@NotNull
public TeamsPacket createTeamsCreationPacket() { public TeamsPacket createTeamsCreationPacket() {
TeamsPacket teamsCreationPacket = new TeamsPacket(); TeamsPacket teamsCreationPacket = new TeamsPacket();
teamsCreationPacket.teamName = teamName; teamsCreationPacket.teamName = teamName;
@ -313,7 +301,7 @@ public class Team {
teamsCreationPacket.teamColor = this.teamColor.getId(); teamsCreationPacket.teamColor = this.teamColor.getId();
teamsCreationPacket.teamPrefix = this.prefix; teamsCreationPacket.teamPrefix = this.prefix;
teamsCreationPacket.teamSuffix = this.suffix; teamsCreationPacket.teamSuffix = this.suffix;
teamsCreationPacket.entities = this.entities; teamsCreationPacket.entities = this.members.toArray(new String[0]);
return teamsCreationPacket; return teamsCreationPacket;
} }
@ -323,6 +311,7 @@ public class Team {
* *
* @return the packet to remove the team * @return the packet to remove the team
*/ */
@NotNull
public TeamsPacket createTeamDestructionPacket() { public TeamsPacket createTeamDestructionPacket() {
TeamsPacket teamsPacket = new TeamsPacket(); TeamsPacket teamsPacket = new TeamsPacket();
teamsPacket.teamName = teamName; teamsPacket.teamName = teamName;
@ -335,6 +324,7 @@ public class Team {
* *
* @return an unmodifiable {@link Set} of registered players * @return an unmodifiable {@link Set} of registered players
*/ */
@NotNull
public Set<String> getMembers() { public Set<String> getMembers() {
return Collections.unmodifiableSet(members); return Collections.unmodifiableSet(members);
} }
@ -362,6 +352,7 @@ public class Team {
* *
* @return the tag visibility * @return the tag visibility
*/ */
@NotNull
public NameTagVisibility getNameTagVisibility() { public NameTagVisibility getNameTagVisibility() {
return nameTagVisibility; return nameTagVisibility;
} }
@ -371,6 +362,7 @@ public class Team {
* *
* @return the collision rule * @return the collision rule
*/ */
@NotNull
public CollisionRule getCollisionRule() { public CollisionRule getCollisionRule() {
return collisionRule; return collisionRule;
} }
@ -380,6 +372,7 @@ public class Team {
* *
* @return the team color * @return the team color
*/ */
@NotNull
public ChatColor getTeamColor() { public ChatColor getTeamColor() {
return teamColor; return teamColor;
} }
@ -402,10 +395,6 @@ public class Team {
return suffix; return suffix;
} }
public String[] getEntities() {
return entities;
}
/** /**
* Sends an {@link TeamsPacket.Action#UPDATE_TEAM_INFO} packet. * Sends an {@link TeamsPacket.Action#UPDATE_TEAM_INFO} packet.
*/ */