From 87d8ef9f24d8c1e343b56b12e059ab1874dd6046 Mon Sep 17 00:00:00 2001 From: Tom Miller Date: Sun, 9 Jan 2022 23:08:55 -0600 Subject: [PATCH] Add/Remove multiple entries with Scoreboard Team (#6640) --- ...55-Multiple-Entries-with-Scoreboards.patch | 128 ++++++++++++++++++ ...47-Multiple-Entries-with-Scoreboards.patch | 126 +++++++++++++++++ 2 files changed, 254 insertions(+) create mode 100644 patches/api/0355-Multiple-Entries-with-Scoreboards.patch create mode 100644 patches/server/0847-Multiple-Entries-with-Scoreboards.patch diff --git a/patches/api/0355-Multiple-Entries-with-Scoreboards.patch b/patches/api/0355-Multiple-Entries-with-Scoreboards.patch new file mode 100644 index 0000000000..e1d867d054 --- /dev/null +++ b/patches/api/0355-Multiple-Entries-with-Scoreboards.patch @@ -0,0 +1,128 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Cryptite +Date: Tue, 21 Sep 2021 18:17:34 -0500 +Subject: [PATCH] Multiple Entries with Scoreboards + + +diff --git a/src/main/java/org/bukkit/scoreboard/Team.java b/src/main/java/org/bukkit/scoreboard/Team.java +index 2b93692204a74ea0def513a54ddf77a40c64d3d2..04c8cc55a2fe19c888aba842775cc66648dddb0c 100644 +--- a/src/main/java/org/bukkit/scoreboard/Team.java ++++ b/src/main/java/org/bukkit/scoreboard/Team.java +@@ -316,6 +316,60 @@ public interface Team { + */ + void addEntry(@NotNull String entry) throws IllegalStateException, IllegalArgumentException; + ++ // Paper start ++ /** ++ * This puts a collection of entities onto this team for the scoreboard which results in one ++ * packet for the updates rather than a packet-per-entity. ++ *

++ * Entities on other teams will be removed from their respective teams. ++ * ++ * @param entities the entities to add ++ * @throws IllegalArgumentException if entities are null ++ * @throws IllegalStateException if this team has been unregistered ++ */ ++ default void addEntities(@NotNull org.bukkit.entity.Entity @NotNull ...entities) { ++ this.addEntities(java.util.List.of(entities)); ++ } ++ ++ /** ++ * This puts a collection of entities onto this team for the scoreboard which results in one ++ * packet for the updates rather than a packet-per-entity. ++ *

++ * Entities on other teams will be removed from their respective teams. ++ * ++ * @param entities the entities to add ++ * @throws IllegalArgumentException if entities are null ++ * @throws IllegalStateException if this team has been unregistered ++ */ ++ void addEntities(@NotNull java.util.Collection entities) throws IllegalStateException, IllegalArgumentException; ++ ++ /** ++ * This puts a collection of entries onto this team for the scoreboard which results in one ++ * packet for the updates rather than a packet-per-entry. ++ *

++ * Entries on other teams will be removed from their respective teams. ++ * ++ * @param entries the entries to add ++ * @throws IllegalArgumentException if entries are null ++ * @throws IllegalStateException if this team has been unregistered ++ */ ++ default void addEntries(@NotNull String... entries) throws IllegalStateException, IllegalArgumentException { ++ this.addEntries(java.util.List.of(entries)); ++ } ++ ++ /** ++ * This puts a collection of entries onto this team for the scoreboard which results in one ++ * packet for the updates rather than a packet-per-entry. ++ *

++ * Entries on other teams will be removed from their respective teams. ++ * ++ * @param entries the entries to add ++ * @throws IllegalArgumentException if entries are null ++ * @throws IllegalStateException if this team has been unregistered ++ */ ++ void addEntries(@NotNull java.util.Collection entries) throws IllegalStateException, IllegalArgumentException; ++ // Paper end ++ + /** + * Removes the player from this team. + * +@@ -338,6 +392,56 @@ public interface Team { + */ + boolean removeEntry(@NotNull String entry) throws IllegalStateException, IllegalArgumentException; + ++ // Paper start ++ /** ++ * Removes a collection of entities from this team which results in one ++ * packet for the updates rather than a packet-per-entity. ++ * ++ * @param entities the entries to remove ++ * @return if any of the entities were a part of this team ++ * @throws IllegalArgumentException if entities is null ++ * @throws IllegalStateException if this team has been unregistered ++ */ ++ default boolean removeEntities(@NotNull org.bukkit.entity.Entity @NotNull ... entities) throws IllegalStateException, IllegalArgumentException { ++ return this.removeEntities(java.util.List.of(entities)); ++ } ++ ++ /** ++ * Removes a collection of entities from this team which results in one ++ * packet for the updates rather than a packet-per-entity. ++ * ++ * @param entities the entries to remove ++ * @return if any of the entities were a part of this team ++ * @throws IllegalArgumentException if entities is null ++ * @throws IllegalStateException if this team has been unregistered ++ */ ++ boolean removeEntities(@NotNull java.util.Collection entities) throws IllegalStateException, IllegalArgumentException; ++ ++ /** ++ * Removes a collection of entries from this team which results in one ++ * packet for the updates rather than a packet-per-entry. ++ * ++ * @param entries the entries to remove ++ * @return if any of the entries were a part of this team ++ * @throws IllegalArgumentException if entries is null ++ * @throws IllegalStateException if this team has been unregistered ++ */ ++ default boolean removeEntries(@NotNull String... entries) throws IllegalStateException, IllegalArgumentException { ++ return this.removeEntries(java.util.List.of(entries)); ++ } ++ ++ /** ++ * Removes a collection of entries from this team which results in one ++ * packet for the updates rather than a packet-per-entry. ++ * ++ * @param entries the entries to remove ++ * @return if any of the entries were a part of this team ++ * @throws IllegalArgumentException if entries is null ++ * @throws IllegalStateException if this team has been unregistered ++ */ ++ boolean removeEntries(@NotNull java.util.Collection entries) throws IllegalStateException, IllegalArgumentException; ++ // Paper end ++ + /** + * Unregisters this team from the Scoreboard + * diff --git a/patches/server/0847-Multiple-Entries-with-Scoreboards.patch b/patches/server/0847-Multiple-Entries-with-Scoreboards.patch new file mode 100644 index 0000000000..2f9e8e9118 --- /dev/null +++ b/patches/server/0847-Multiple-Entries-with-Scoreboards.patch @@ -0,0 +1,126 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Cryptite +Date: Tue, 21 Sep 2021 18:17:33 -0500 +Subject: [PATCH] Multiple Entries with Scoreboards + + +diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java +index 005a3058c51a41a39f050b1817e2079be93ad366..9c7c6751e81af9a013de4fe9e4d029b69a8534ca 100644 +--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java ++++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java +@@ -42,6 +42,12 @@ public class ClientboundSetPlayerTeamPacket implements Packet players, ClientboundSetPlayerTeamPacket.Action operation) { ++ return new ClientboundSetPlayerTeamPacket(team.getName(), operation == ClientboundSetPlayerTeamPacket.Action.ADD ? 3 : 4, Optional.empty(), players); ++ } ++ // Paper end ++ + public ClientboundSetPlayerTeamPacket(FriendlyByteBuf buf) { + this.name = buf.readUtf(); + this.method = buf.readByte(); +diff --git a/src/main/java/net/minecraft/server/ServerScoreboard.java b/src/main/java/net/minecraft/server/ServerScoreboard.java +index 610d312b9c8f6c8d1f102e8ba2fe9fc2cc3e98c5..3a4a0727ad44322e3ba85512cd077808dab080b7 100644 +--- a/src/main/java/net/minecraft/server/ServerScoreboard.java ++++ b/src/main/java/net/minecraft/server/ServerScoreboard.java +@@ -92,6 +92,25 @@ public class ServerScoreboard extends Scoreboard { + } + } + ++ // Paper start ++ public boolean addPlayersToTeam(java.util.Collection players, PlayerTeam team) { ++ boolean anyAdded = false; ++ for (String playerName : players) { ++ if (super.addPlayerToTeam(playerName, team)) { ++ anyAdded = true; ++ } ++ } ++ ++ if (anyAdded) { ++ this.broadcastAll(ClientboundSetPlayerTeamPacket.createMultiplePlayerPacket(team, players, ClientboundSetPlayerTeamPacket.Action.ADD)); ++ this.setDirty(); ++ return true; ++ } else { ++ return false; ++ } ++ } ++ // Paper end ++ + @Override + public void removePlayerFromTeam(String playerName, PlayerTeam team) { + super.removePlayerFromTeam(playerName, team); +@@ -99,6 +118,17 @@ public class ServerScoreboard extends Scoreboard { + this.setDirty(); + } + ++ // Paper start ++ public void removePlayersFromTeam(java.util.Collection players, PlayerTeam team) { ++ for (String playerName : players) { ++ super.removePlayerFromTeam(playerName, team); ++ } ++ ++ this.broadcastAll(ClientboundSetPlayerTeamPacket.createMultiplePlayerPacket(team, players, ClientboundSetPlayerTeamPacket.Action.REMOVE)); ++ this.setDirty(); ++ } ++ // Paper end ++ + @Override + public void onObjectiveAdded(Objective objective) { + super.onObjectiveAdded(objective); +diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java +index 47f2e8824fff51f4271e7aa61e233d57e3ca2942..be5e4c2f3c552e0438ed1cc660e411c41fd13df7 100644 +--- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java ++++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java +@@ -226,6 +226,21 @@ final class CraftTeam extends CraftScoreboardComponent implements Team { + scoreboard.board.addPlayerToTeam(entry, team); + } + ++ // Paper start ++ @Override ++ public void addEntities(java.util.Collection entities) throws IllegalStateException, IllegalArgumentException { ++ this.addEntries(entities.stream().map(entity -> ((org.bukkit.craftbukkit.entity.CraftEntity) entity).getHandle().getScoreboardName()).toList()); ++ } ++ ++ @Override ++ public void addEntries(java.util.Collection entries) throws IllegalStateException, IllegalArgumentException { ++ Validate.notNull(entries, "Entries cannot be null"); ++ CraftScoreboard scoreboard = this.checkState(); ++ ++ ((net.minecraft.server.ServerScoreboard) scoreboard.board).addPlayersToTeam(entries, this.team); ++ } ++ // Paper end ++ + @Override + public boolean removePlayer(OfflinePlayer player) throws IllegalStateException, IllegalArgumentException { + Validate.notNull(player, "OfflinePlayer cannot be null"); +@@ -245,6 +260,28 @@ final class CraftTeam extends CraftScoreboardComponent implements Team { + return true; + } + ++ // Paper start ++ @Override ++ public boolean removeEntities(java.util.Collection entities) throws IllegalStateException, IllegalArgumentException { ++ return this.removeEntries(entities.stream().map(entity -> ((org.bukkit.craftbukkit.entity.CraftEntity) entity).getHandle().getScoreboardName()).toList()); ++ } ++ ++ @Override ++ public boolean removeEntries(java.util.Collection entries) throws IllegalStateException, IllegalArgumentException { ++ Validate.notNull(entries, "Entry cannot be null"); ++ CraftScoreboard scoreboard = this.checkState(); ++ ++ for (String entry : entries) { ++ if (this.team.getPlayers().contains(entry)) { ++ ((net.minecraft.server.ServerScoreboard) scoreboard.board).removePlayersFromTeam(entries, this.team); ++ return true; ++ } ++ } ++ ++ return false; ++ } ++ // Paper end ++ + @Override + public boolean hasPlayer(OfflinePlayer player) throws IllegalArgumentException, IllegalStateException { + Validate.notNull(player, "OfflinePlayer cannot be null");