From 0fd1e5c650bdc15e5e5159ecf05fe4d8069438de Mon Sep 17 00:00:00 2001
From: Cryptite <cryptite@gmail.com>
Date: Tue, 21 Sep 2021 18:17:33 -0500
Subject: [PATCH] Multiple Entries with Scoreboards

---
 .../ClientboundSetPlayerTeamPacket.java.patch | 14 +++++-
 .../server/ServerScoreboard.java.patch        | 48 ++++++++++++++++---
 .../craftbukkit/scoreboard/CraftTeam.java     | 37 ++++++++++++++
 3 files changed, 92 insertions(+), 7 deletions(-)

diff --git a/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java.patch b/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java.patch
index b0fb588588..1d4da793e7 100644
--- a/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java.patch
+++ b/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java.patch
@@ -1,6 +1,18 @@
 --- a/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java
 +++ b/net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java
-@@ -200,7 +200,7 @@
+@@ -58,6 +58,11 @@
+         );
+     }
+ 
++    // Paper start - Multiple Entries with Scoreboards
++    public static ClientboundSetPlayerTeamPacket createMultiplePlayerPacket(PlayerTeam team, Collection<String> players, ClientboundSetPlayerTeamPacket.Action operation) {
++        return new ClientboundSetPlayerTeamPacket(team.getName(), operation == ClientboundSetPlayerTeamPacket.Action.ADD ? 3 : 4, Optional.empty(), players);
++    }
++    // Paper end - Multiple Entries with Scoreboards
+     private ClientboundSetPlayerTeamPacket(RegistryFriendlyByteBuf buf) {
+         this.name = buf.readUtf();
+         this.method = buf.readByte();
+@@ -200,7 +205,7 @@
              ComponentSerialization.TRUSTED_STREAM_CODEC.encode(buf, this.displayName);
              buf.writeByte(this.options);
              buf.writeUtf(this.nametagVisibility);
diff --git a/paper-server/patches/sources/net/minecraft/server/ServerScoreboard.java.patch b/paper-server/patches/sources/net/minecraft/server/ServerScoreboard.java.patch
index 2cccb6d1cc..712e8444b1 100644
--- a/paper-server/patches/sources/net/minecraft/server/ServerScoreboard.java.patch
+++ b/paper-server/patches/sources/net/minecraft/server/ServerScoreboard.java.patch
@@ -54,7 +54,29 @@
              this.setDirty();
              return true;
          } else {
-@@ -109,7 +109,7 @@
+@@ -106,13 +106,43 @@
+         }
+     }
+ 
++    // Paper start - Multiple Entries with Scoreboards
++    public boolean addPlayersToTeam(java.util.Collection<String> 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 - Multiple Entries with Scoreboards
++
      @Override
      public void removePlayerFromTeam(String scoreHolderName, PlayerTeam team) {
          super.removePlayerFromTeam(scoreHolderName, team);
@@ -63,7 +85,21 @@
          this.setDirty();
      }
  
-@@ -123,7 +123,7 @@
++    // Paper start - Multiple Entries with Scoreboards
++    public void removePlayersFromTeam(java.util.Collection<String> players, PlayerTeam team) {
++        for (String playerName : players) {
++            super.removePlayerFromTeam(playerName, team);
++        }
++
++        this.broadcastAll(ClientboundSetPlayerTeamPacket.createMultiplePlayerPacket(team, players, ClientboundSetPlayerTeamPacket.Action.REMOVE));
++        this.setDirty();
++    }
++    // Paper end - Multiple Entries with Scoreboards
++
+     @Override
+     public void onObjectiveAdded(Objective objective) {
+         super.onObjectiveAdded(objective);
+@@ -123,7 +153,7 @@
      public void onObjectiveChanged(Objective objective) {
          super.onObjectiveChanged(objective);
          if (this.trackedObjectives.contains(objective)) {
@@ -72,7 +108,7 @@
          }
  
          this.setDirty();
-@@ -142,21 +142,21 @@
+@@ -142,21 +172,21 @@
      @Override
      public void onTeamAdded(PlayerTeam team) {
          super.onTeamAdded(team);
@@ -97,7 +133,7 @@
          this.setDirty();
      }
  
-@@ -207,6 +207,7 @@
+@@ -207,6 +237,7 @@
  
          while (iterator.hasNext()) {
              ServerPlayer entityplayer = (ServerPlayer) iterator.next();
@@ -105,7 +141,7 @@
              Iterator iterator1 = list.iterator();
  
              while (iterator1.hasNext()) {
-@@ -243,6 +244,7 @@
+@@ -243,6 +274,7 @@
  
          while (iterator.hasNext()) {
              ServerPlayer entityplayer = (ServerPlayer) iterator.next();
@@ -113,7 +149,7 @@
              Iterator iterator1 = list.iterator();
  
              while (iterator1.hasNext()) {
-@@ -287,6 +289,16 @@
+@@ -287,6 +319,16 @@
          return this.createData().load(nbt, registries);
      }
  
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java b/paper-server/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java
index 27219bf2f1..2b335c750c 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftTeam.java
@@ -229,6 +229,21 @@ final class CraftTeam extends CraftScoreboardComponent implements Team {
         scoreboard.board.addPlayerToTeam(entry, this.team);
     }
 
+    // Paper start - Multiple Entries with Scoreboards
+    @Override
+    public void addEntities(java.util.Collection<org.bukkit.entity.Entity> 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<String> entries) throws IllegalStateException, IllegalArgumentException {
+        Preconditions.checkArgument(entries != null, "Entries cannot be null");
+        CraftScoreboard scoreboard = this.checkState();
+
+        ((net.minecraft.server.ServerScoreboard) scoreboard.board).addPlayersToTeam(entries, this.team);
+    }
+    // Paper end - Multiple Entries with Scoreboards
+
     @Override
     public boolean removePlayer(OfflinePlayer player) {
         Preconditions.checkArgument(player != null, "OfflinePlayer cannot be null");
@@ -248,6 +263,28 @@ final class CraftTeam extends CraftScoreboardComponent implements Team {
         return true;
     }
 
+    // Paper start - Multiple Entries with Scoreboards
+    @Override
+    public boolean removeEntities(java.util.Collection<org.bukkit.entity.Entity> 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<String> entries) throws IllegalStateException, IllegalArgumentException {
+        Preconditions.checkArgument(entries != null, "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 - Multiple Entries with Scoreboards
+
     @Override
     public boolean hasPlayer(OfflinePlayer player) throws IllegalArgumentException, IllegalStateException {
         Preconditions.checkArgument(player != null, "OfflinePlayer cannot be null");