Skip to content

Commit 87d8ef9

Browse files
authored
Add/Remove multiple entries with Scoreboard Team (#6640)
1 parent 2be9beb commit 87d8ef9

File tree

2 files changed

+254
-0
lines changed

2 files changed

+254
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Cryptite <cryptite@gmail.com>
3+
Date: Tue, 21 Sep 2021 18:17:34 -0500
4+
Subject: [PATCH] Multiple Entries with Scoreboards
5+
6+
7+
diff --git a/src/main/java/org/bukkit/scoreboard/Team.java b/src/main/java/org/bukkit/scoreboard/Team.java
8+
index 2b93692204a74ea0def513a54ddf77a40c64d3d2..04c8cc55a2fe19c888aba842775cc66648dddb0c 100644
9+
--- a/src/main/java/org/bukkit/scoreboard/Team.java
10+
+++ b/src/main/java/org/bukkit/scoreboard/Team.java
11+
@@ -316,6 +316,60 @@ public interface Team {
12+
*/
13+
void addEntry(@NotNull String entry) throws IllegalStateException, IllegalArgumentException;
14+
15+
+ // Paper start
16+
+ /**
17+
+ * This puts a collection of entities onto this team for the scoreboard which results in one
18+
+ * packet for the updates rather than a packet-per-entity.
19+
+ * <p>
20+
+ * Entities on other teams will be removed from their respective teams.
21+
+ *
22+
+ * @param entities the entities to add
23+
+ * @throws IllegalArgumentException if entities are null
24+
+ * @throws IllegalStateException if this team has been unregistered
25+
+ */
26+
+ default void addEntities(@NotNull org.bukkit.entity.Entity @NotNull ...entities) {
27+
+ this.addEntities(java.util.List.of(entities));
28+
+ }
29+
+
30+
+ /**
31+
+ * This puts a collection of entities onto this team for the scoreboard which results in one
32+
+ * packet for the updates rather than a packet-per-entity.
33+
+ * <p>
34+
+ * Entities on other teams will be removed from their respective teams.
35+
+ *
36+
+ * @param entities the entities to add
37+
+ * @throws IllegalArgumentException if entities are null
38+
+ * @throws IllegalStateException if this team has been unregistered
39+
+ */
40+
+ void addEntities(@NotNull java.util.Collection<org.bukkit.entity.Entity> entities) throws IllegalStateException, IllegalArgumentException;
41+
+
42+
+ /**
43+
+ * This puts a collection of entries onto this team for the scoreboard which results in one
44+
+ * packet for the updates rather than a packet-per-entry.
45+
+ * <p>
46+
+ * Entries on other teams will be removed from their respective teams.
47+
+ *
48+
+ * @param entries the entries to add
49+
+ * @throws IllegalArgumentException if entries are null
50+
+ * @throws IllegalStateException if this team has been unregistered
51+
+ */
52+
+ default void addEntries(@NotNull String... entries) throws IllegalStateException, IllegalArgumentException {
53+
+ this.addEntries(java.util.List.of(entries));
54+
+ }
55+
+
56+
+ /**
57+
+ * This puts a collection of entries onto this team for the scoreboard which results in one
58+
+ * packet for the updates rather than a packet-per-entry.
59+
+ * <p>
60+
+ * Entries on other teams will be removed from their respective teams.
61+
+ *
62+
+ * @param entries the entries to add
63+
+ * @throws IllegalArgumentException if entries are null
64+
+ * @throws IllegalStateException if this team has been unregistered
65+
+ */
66+
+ void addEntries(@NotNull java.util.Collection<String> entries) throws IllegalStateException, IllegalArgumentException;
67+
+ // Paper end
68+
+
69+
/**
70+
* Removes the player from this team.
71+
*
72+
@@ -338,6 +392,56 @@ public interface Team {
73+
*/
74+
boolean removeEntry(@NotNull String entry) throws IllegalStateException, IllegalArgumentException;
75+
76+
+ // Paper start
77+
+ /**
78+
+ * Removes a collection of entities from this team which results in one
79+
+ * packet for the updates rather than a packet-per-entity.
80+
+ *
81+
+ * @param entities the entries to remove
82+
+ * @return if any of the entities were a part of this team
83+
+ * @throws IllegalArgumentException if entities is null
84+
+ * @throws IllegalStateException if this team has been unregistered
85+
+ */
86+
+ default boolean removeEntities(@NotNull org.bukkit.entity.Entity @NotNull ... entities) throws IllegalStateException, IllegalArgumentException {
87+
+ return this.removeEntities(java.util.List.of(entities));
88+
+ }
89+
+
90+
+ /**
91+
+ * Removes a collection of entities from this team which results in one
92+
+ * packet for the updates rather than a packet-per-entity.
93+
+ *
94+
+ * @param entities the entries to remove
95+
+ * @return if any of the entities were a part of this team
96+
+ * @throws IllegalArgumentException if entities is null
97+
+ * @throws IllegalStateException if this team has been unregistered
98+
+ */
99+
+ boolean removeEntities(@NotNull java.util.Collection<org.bukkit.entity.Entity> entities) throws IllegalStateException, IllegalArgumentException;
100+
+
101+
+ /**
102+
+ * Removes a collection of entries from this team which results in one
103+
+ * packet for the updates rather than a packet-per-entry.
104+
+ *
105+
+ * @param entries the entries to remove
106+
+ * @return if any of the entries were a part of this team
107+
+ * @throws IllegalArgumentException if entries is null
108+
+ * @throws IllegalStateException if this team has been unregistered
109+
+ */
110+
+ default boolean removeEntries(@NotNull String... entries) throws IllegalStateException, IllegalArgumentException {
111+
+ return this.removeEntries(java.util.List.of(entries));
112+
+ }
113+
+
114+
+ /**
115+
+ * Removes a collection of entries from this team which results in one
116+
+ * packet for the updates rather than a packet-per-entry.
117+
+ *
118+
+ * @param entries the entries to remove
119+
+ * @return if any of the entries were a part of this team
120+
+ * @throws IllegalArgumentException if entries is null
121+
+ * @throws IllegalStateException if this team has been unregistered
122+
+ */
123+
+ boolean removeEntries(@NotNull java.util.Collection<String> entries) throws IllegalStateException, IllegalArgumentException;
124+
+ // Paper end
125+
+
126+
/**
127+
* Unregisters this team from the Scoreboard
128+
*
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Cryptite <cryptite@gmail.com>
3+
Date: Tue, 21 Sep 2021 18:17:33 -0500
4+
Subject: [PATCH] Multiple Entries with Scoreboards
5+
6+
7+
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java
8+
index 005a3058c51a41a39f050b1817e2079be93ad366..9c7c6751e81af9a013de4fe9e4d029b69a8534ca 100644
9+
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java
10+
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java
11+
@@ -42,6 +42,12 @@ public class ClientboundSetPlayerTeamPacket implements Packet<ClientGamePacketLi
12+
return new ClientboundSetPlayerTeamPacket(team.getName(), operation == ClientboundSetPlayerTeamPacket.Action.ADD ? 3 : 4, Optional.empty(), ImmutableList.of(playerName));
13+
}
14+
15+
+ // Paper start
16+
+ public static ClientboundSetPlayerTeamPacket createMultiplePlayerPacket(PlayerTeam team, Collection<String> players, ClientboundSetPlayerTeamPacket.Action operation) {
17+
+ return new ClientboundSetPlayerTeamPacket(team.getName(), operation == ClientboundSetPlayerTeamPacket.Action.ADD ? 3 : 4, Optional.empty(), players);
18+
+ }
19+
+ // Paper end
20+
+
21+
public ClientboundSetPlayerTeamPacket(FriendlyByteBuf buf) {
22+
this.name = buf.readUtf();
23+
this.method = buf.readByte();
24+
diff --git a/src/main/java/net/minecraft/server/ServerScoreboard.java b/src/main/java/net/minecraft/server/ServerScoreboard.java
25+
index 610d312b9c8f6c8d1f102e8ba2fe9fc2cc3e98c5..3a4a0727ad44322e3ba85512cd077808dab080b7 100644
26+
--- a/src/main/java/net/minecraft/server/ServerScoreboard.java
27+
+++ b/src/main/java/net/minecraft/server/ServerScoreboard.java
28+
@@ -92,6 +92,25 @@ public class ServerScoreboard extends Scoreboard {
29+
}
30+
}
31+
32+
+ // Paper start
33+
+ public boolean addPlayersToTeam(java.util.Collection<String> players, PlayerTeam team) {
34+
+ boolean anyAdded = false;
35+
+ for (String playerName : players) {
36+
+ if (super.addPlayerToTeam(playerName, team)) {
37+
+ anyAdded = true;
38+
+ }
39+
+ }
40+
+
41+
+ if (anyAdded) {
42+
+ this.broadcastAll(ClientboundSetPlayerTeamPacket.createMultiplePlayerPacket(team, players, ClientboundSetPlayerTeamPacket.Action.ADD));
43+
+ this.setDirty();
44+
+ return true;
45+
+ } else {
46+
+ return false;
47+
+ }
48+
+ }
49+
+ // Paper end
50+
+
51+
@Override
52+
public void removePlayerFromTeam(String playerName, PlayerTeam team) {
53+
super.removePlayerFromTeam(playerName, team);
54+
@@ -99,6 +118,17 @@ public class ServerScoreboard extends Scoreboard {
55+
this.setDirty();
56+
}
57+
58+
+ // Paper start
59+
+ public void removePlayersFromTeam(java.util.Collection<String> players, PlayerTeam team) {
60+
+ for (String playerName : players) {
61+
+ super.removePlayerFromTeam(playerName, team);
62+
+ }
63+
+
64+
+ this.broadcastAll(ClientboundSetPlayerTeamPacket.createMultiplePlayerPacket(team, players, ClientboundSetPlayerTeamPacket.Action.REMOVE));
65+
+ this.setDirty();
66+
+ }
67+
+ // Paper end
68+
+
69+
@Override
70+
public void onObjectiveAdded(Objective objective) {
71+
super.onObjectiveAdded(objective);
72+
diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java
73+
index 47f2e8824fff51f4271e7aa61e233d57e3ca2942..be5e4c2f3c552e0438ed1cc660e411c41fd13df7 100644
74+
--- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java
75+
+++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java
76+
@@ -226,6 +226,21 @@ final class CraftTeam extends CraftScoreboardComponent implements Team {
77+
scoreboard.board.addPlayerToTeam(entry, team);
78+
}
79+
80+
+ // Paper start
81+
+ @Override
82+
+ public void addEntities(java.util.Collection<org.bukkit.entity.Entity> entities) throws IllegalStateException, IllegalArgumentException {
83+
+ this.addEntries(entities.stream().map(entity -> ((org.bukkit.craftbukkit.entity.CraftEntity) entity).getHandle().getScoreboardName()).toList());
84+
+ }
85+
+
86+
+ @Override
87+
+ public void addEntries(java.util.Collection<String> entries) throws IllegalStateException, IllegalArgumentException {
88+
+ Validate.notNull(entries, "Entries cannot be null");
89+
+ CraftScoreboard scoreboard = this.checkState();
90+
+
91+
+ ((net.minecraft.server.ServerScoreboard) scoreboard.board).addPlayersToTeam(entries, this.team);
92+
+ }
93+
+ // Paper end
94+
+
95+
@Override
96+
public boolean removePlayer(OfflinePlayer player) throws IllegalStateException, IllegalArgumentException {
97+
Validate.notNull(player, "OfflinePlayer cannot be null");
98+
@@ -245,6 +260,28 @@ final class CraftTeam extends CraftScoreboardComponent implements Team {
99+
return true;
100+
}
101+
102+
+ // Paper start
103+
+ @Override
104+
+ public boolean removeEntities(java.util.Collection<org.bukkit.entity.Entity> entities) throws IllegalStateException, IllegalArgumentException {
105+
+ return this.removeEntries(entities.stream().map(entity -> ((org.bukkit.craftbukkit.entity.CraftEntity) entity).getHandle().getScoreboardName()).toList());
106+
+ }
107+
+
108+
+ @Override
109+
+ public boolean removeEntries(java.util.Collection<String> entries) throws IllegalStateException, IllegalArgumentException {
110+
+ Validate.notNull(entries, "Entry cannot be null");
111+
+ CraftScoreboard scoreboard = this.checkState();
112+
+
113+
+ for (String entry : entries) {
114+
+ if (this.team.getPlayers().contains(entry)) {
115+
+ ((net.minecraft.server.ServerScoreboard) scoreboard.board).removePlayersFromTeam(entries, this.team);
116+
+ return true;
117+
+ }
118+
+ }
119+
+
120+
+ return false;
121+
+ }
122+
+ // Paper end
123+
+
124+
@Override
125+
public boolean hasPlayer(OfflinePlayer player) throws IllegalArgumentException, IllegalStateException {
126+
Validate.notNull(player, "OfflinePlayer cannot be null");

0 commit comments

Comments
 (0)