Improve consistency of team packets (#1556)

This commit is contained in:
iam 2022-12-07 05:13:12 -05:00 committed by GitHub
parent ee7ac62b75
commit 7361bf0825
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 8 deletions

View File

@ -170,7 +170,7 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo
}
}
public record AddEntitiesToTeamAction(Collection<String> entities) implements Action {
public record AddEntitiesToTeamAction(@NotNull Collection<@NotNull String> entities) implements Action {
public AddEntitiesToTeamAction {
entities = List.copyOf(entities);
}
@ -190,7 +190,7 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo
}
}
public record RemoveEntitiesToTeamAction(@NotNull List<@NotNull String> entities) implements Action {
public record RemoveEntitiesToTeamAction(@NotNull Collection<@NotNull String> entities) implements Action {
public RemoveEntitiesToTeamAction {
entities = List.copyOf(entities);
}

View File

@ -103,17 +103,30 @@ public class Team implements PacketGroupingAudience {
/**
* Adds a member to the {@link Team}.
* <br>
* This member can be a {@link Player} or an {@link LivingEntity}.
* This member collection can contain {@link Player} or {@link LivingEntity}.
* For players use their username, for entities use their UUID
*
* @param member The member to be added
*/
public void addMember(@NotNull String member) {
addMembers(List.of(member));
}
/**
* Adds a members to the {@link Team}.
* <br>
* This member collection can contain {@link Player} or {@link LivingEntity}.
* For players use their username, for entities use their UUID
*
* @param toAdd The members to be added
*/
public void addMembers(@NotNull Collection<@NotNull String> toAdd) {
// Adds a new member to the team
this.members.add(member);
this.members.addAll(toAdd);
// Initializes add player packet
final TeamsPacket addPlayerPacket = new TeamsPacket(teamName,
new TeamsPacket.AddEntitiesToTeamAction(members));
new TeamsPacket.AddEntitiesToTeamAction(toAdd));
// Sends to all online players the add player packet
PacketUtils.broadcastPacket(addPlayerPacket);
@ -123,18 +136,33 @@ public class Team implements PacketGroupingAudience {
/**
* Removes a member from the {@link Team}.
* <br>
* This member collection can contain {@link Player} or {@link LivingEntity}.
* For players use their username, for entities use their UUID
*
* @param member The member to be removed
*/
public void removeMember(@NotNull String member) {
removeMembers(List.of(member));
}
/**
* Removes members from the {@link Team}.
* <br>
* This member collection can contain {@link Player} or {@link LivingEntity}.
* For players use their username, for entities use their UUID
*
* @param toRemove The members to be removed
*/
public void removeMembers(@NotNull Collection<@NotNull String> toRemove) {
// Initializes remove player packet
final TeamsPacket removePlayerPacket = new TeamsPacket(teamName,
new TeamsPacket.RemoveEntitiesToTeamAction(List.of(member)));
// Sends to all online player teh remove player packet
new TeamsPacket.RemoveEntitiesToTeamAction(toRemove));
// Sends to all online player the remove player packet
PacketUtils.broadcastPacket(removePlayerPacket);
// Removes the member from the team
this.members.remove(member);
this.members.removeAll(toRemove);
// invalidate player members
this.isPlayerMembersUpToDate = false;