From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: lukas81298 <lukas81298@gmail.com>
Date: Sun, 13 Dec 2020 13:42:55 +0100
Subject: [PATCH] do not create unnecessary copies of passenger list

1.17: Mojang removed the copy of the passenger list from getPassengers, no longer needed

diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPassengersPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPassengersPacket.java
index a6ecb82d14ccab5d8229689a2a6cb67c579b1f71..cded79352dff0978e0d633eae9d9020b4dec1d4b 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPassengersPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetPassengersPacket.java
@@ -15,7 +15,7 @@ public class ClientboundSetPassengersPacket implements Packet<ClientGamePacketLi
 
     public ClientboundSetPassengersPacket(Entity entity) {
         this.vehicle = entity.getId();
-        List<Entity> list = entity.getPassengers();
+        List<Entity> list = entity.passengers; // Paper - do not create a copy of the list
 
         this.passengers = new int[list.size()];
 
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
index f4dd30c8b3326db72d3b3068ee2291de6f15de7c..c17e827a976f509c8294df65335f12139cd36a9f 100644
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
@@ -2312,7 +2312,7 @@ Sections go from 0..16. Now whenever a section is not empty, it can potentially
                 list.add(entity);
             }
 
-            if (!entity.getPassengers().isEmpty()) {
+            if (!entity.passengers.isEmpty()) { // Paper - do not copy list
                 list1.add(entity);
             }
         }
diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java
index 75e2274578c2c28de3d786372df0b4102337a2cc..e703233db7879c73378b3a06b2e89f7fcea97979 100644
--- a/src/main/java/net/minecraft/server/level/ServerEntity.java
+++ b/src/main/java/net/minecraft/server/level/ServerEntity.java
@@ -102,10 +102,10 @@ public class ServerEntity {
 
     public final void tick() { this.sendChanges(); } // Paper - OBFHELPER
     public void sendChanges() {
-        List<Entity> list = this.entity.getPassengers();
+        List<Entity> list = this.entity.passengers; // Paper - do not copy list
 
         if (!list.equals(this.lastPassengers)) {
-            this.lastPassengers = list;
+            this.lastPassengers = com.google.common.collect.ImmutableList.copyOf(list); // Paper - only copy list if something has changed
             this.broadcastAndSend(new ClientboundSetPassengersPacket(this.entity)); // CraftBukkit
         }
 
@@ -375,7 +375,7 @@ public class ServerEntity {
             }
         }
 
-        if (!this.entity.getPassengers().isEmpty()) {
+        if (!this.entity.passengers.isEmpty()) { // Paper - do not create copy of list
             consumer.accept(new ClientboundSetPassengersPacket(this.entity));
         }
 
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index ec30f886585d407fbd122e05107ebca44895c585..d055b362459e5b4658aa220e16118ee6174c0de4 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -2233,7 +2233,7 @@ public abstract class Entity implements Nameable, CommandSource, net.minecraft.s
     }
 
     protected boolean canAddPassenger(Entity passenger) {
-        return this.getPassengers().size() < 1;
+        return this.passengers.size() < 1; // Paper - do not copy list
     }
 
     public final float getCollisionBorderSize() { return getPickRadius(); } // Paper - OBFHELPER
@@ -2329,7 +2329,7 @@ public abstract class Entity implements Nameable, CommandSource, net.minecraft.s
     }
 
     public boolean isVehicle() {
-        return !this.getPassengers().isEmpty();
+        return !this.passengers.isEmpty(); // Paper - do not copy list
     }
 
     public boolean rideableUnderWater() {
@@ -3141,7 +3141,7 @@ public abstract class Entity implements Nameable, CommandSource, net.minecraft.s
     }
 
     public boolean hasPassenger(Entity passenger) {
-        Iterator iterator = this.getPassengers().iterator();
+        Iterator iterator = this.passengers.iterator(); // Paper - do not copy list
 
         Entity entity1;
 
@@ -3157,7 +3157,7 @@ public abstract class Entity implements Nameable, CommandSource, net.minecraft.s
     }
 
     public boolean hasPassenger(Class<? extends Entity> clazz) {
-        Iterator iterator = this.getPassengers().iterator();
+        Iterator iterator = this.passengers.iterator(); // Paper - do not copy list
 
         Entity entity;
 
@@ -3174,7 +3174,7 @@ public abstract class Entity implements Nameable, CommandSource, net.minecraft.s
 
     public Collection<Entity> getIndirectPassengers() {
         Set<Entity> set = Sets.newHashSet();
-        Iterator iterator = this.getPassengers().iterator();
+        Iterator iterator = this.passengers.iterator(); // Paper - do not copy list
 
         while (iterator.hasNext()) {
             Entity entity = (Entity) iterator.next();
@@ -3200,7 +3200,7 @@ public abstract class Entity implements Nameable, CommandSource, net.minecraft.s
     private void fillIndirectPassengers(boolean playersOnly, Set<Entity> output) {
         Entity entity;
 
-        for (Iterator iterator = this.getPassengers().iterator(); iterator.hasNext(); entity.fillIndirectPassengers(playersOnly, output)) {
+        for (Iterator iterator = this.passengers.iterator(); iterator.hasNext(); entity.fillIndirectPassengers(playersOnly, output)) { // Paper - do not copy list
             entity = (Entity) iterator.next();
             if (!playersOnly || ServerPlayer.class.isAssignableFrom(entity.getClass())) {
                 output.add(entity);
diff --git a/src/main/java/net/minecraft/world/entity/ai/goal/RunAroundLikeCrazyGoal.java b/src/main/java/net/minecraft/world/entity/ai/goal/RunAroundLikeCrazyGoal.java
index 3d919e878908e19d598d70011c44cf980676f4f8..debf53a8bf6f062a237160a7b7e0a251a9756ef6 100644
--- a/src/main/java/net/minecraft/world/entity/ai/goal/RunAroundLikeCrazyGoal.java
+++ b/src/main/java/net/minecraft/world/entity/ai/goal/RunAroundLikeCrazyGoal.java
@@ -52,7 +52,7 @@ public class RunAroundLikeCrazyGoal extends Goal {
     @Override
     public void tick() {
         if (!this.horse.isTamed() && this.horse.getRandom().nextInt(50) == 0) {
-            Entity entity = (Entity) this.horse.getPassengers().get(0);
+            Entity entity = this.horse.passengers.isEmpty() ? null : this.horse.passengers.get(0); // Paper - do not copy list, fixed array out of bounds exception as well
 
             if (entity == null) {
                 return;
diff --git a/src/main/java/net/minecraft/world/entity/animal/Pig.java b/src/main/java/net/minecraft/world/entity/animal/Pig.java
index e512a38ccbba93266f0234e3b2fcf7f62693039b..7a60c0b2c301e8cb768c39ad20f273a5921428cb 100644
--- a/src/main/java/net/minecraft/world/entity/animal/Pig.java
+++ b/src/main/java/net/minecraft/world/entity/animal/Pig.java
@@ -85,7 +85,7 @@ public class Pig extends Animal implements ItemSteerable, Saddleable {
     @Nullable
     @Override
     public Entity getControllingPassenger() {
-        return this.getPassengers().isEmpty() ? null : (Entity) this.getPassengers().get(0);
+        return this.passengers.isEmpty() ? null : (Entity) this.passengers.get(0); // Paper - do not copy list
     }
 
     @Override
diff --git a/src/main/java/net/minecraft/world/entity/animal/horse/AbstractHorse.java b/src/main/java/net/minecraft/world/entity/animal/horse/AbstractHorse.java
index b298bcfb665b1036cd21445cec1518069eb08f06..5901e92a749af50166c517bda575d541554756f5 100644
--- a/src/main/java/net/minecraft/world/entity/animal/horse/AbstractHorse.java
+++ b/src/main/java/net/minecraft/world/entity/animal/horse/AbstractHorse.java
@@ -971,7 +971,7 @@ public abstract class AbstractHorse extends Animal implements ContainerListener,
     @Nullable
     @Override
     public Entity getControllingPassenger() {
-        return this.getPassengers().isEmpty() ? null : (Entity) this.getPassengers().get(0);
+        return this.passengers.isEmpty() ? null : (Entity) this.passengers.get(0); // Paper - do not copy list
     }
 
     @Nullable
diff --git a/src/main/java/net/minecraft/world/entity/monster/Ravager.java b/src/main/java/net/minecraft/world/entity/monster/Ravager.java
index e50d72c98f2ee3cd3349d2df9a0cdc47b733f7cd..ccc9d941b28ee090436a5958e1b48589d48d9d6f 100644
--- a/src/main/java/net/minecraft/world/entity/monster/Ravager.java
+++ b/src/main/java/net/minecraft/world/entity/monster/Ravager.java
@@ -134,7 +134,7 @@ public class Ravager extends Raider {
     @Nullable
     @Override
     public Entity getControllingPassenger() {
-        return this.getPassengers().isEmpty() ? null : (Entity) this.getPassengers().get(0);
+        return this.passengers.isEmpty() ? null : (Entity) this.passengers.get(0); // Paper - do not copy list
     }
 
     @Override
diff --git a/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecart.java b/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecart.java
index 25df3ef6b96bec39847a732394af8eccdb4d5d45..23421c4964c67a963a55ce08595c8de112a2ba6e 100644
--- a/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecart.java
+++ b/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecart.java
@@ -560,7 +560,7 @@ public abstract class AbstractMinecart extends Entity {
 
         vec3d1 = new Vec3(d8 * d4 / d6, vec3d1.y, d8 * d5 / d6);
         this.setDeltaMovement(vec3d1);
-        Entity entity = this.getPassengers().isEmpty() ? null : (Entity) this.getPassengers().get(0);
+        Entity entity = this.passengers.isEmpty() ? null : (Entity) this.passengers.get(0); // Paper - do not copy list
 
         if (entity instanceof Player) {
             Vec3 vec3d2 = entity.getDeltaMovement();
diff --git a/src/main/java/net/minecraft/world/entity/vehicle/Boat.java b/src/main/java/net/minecraft/world/entity/vehicle/Boat.java
index e7ac3bff190c899397d6576fabbf4966878ea7e5..37f0e359ec858eebfa15d01f23a9ce0103816c8b 100644
--- a/src/main/java/net/minecraft/world/entity/vehicle/Boat.java
+++ b/src/main/java/net/minecraft/world/entity/vehicle/Boat.java
@@ -316,7 +316,7 @@ public class Boat extends Entity {
         super.tick();
         this.tickLerp();
         if (this.isControlledByLocalInstance()) {
-            if (this.getPassengers().isEmpty() || !(this.getPassengers().get(0) instanceof Player)) {
+            if (this.passengers.isEmpty() || !(this.passengers.get(0) instanceof Player)) { // Paper - do not copy list
                 this.setPaddleState(false, false);
             }
 
@@ -379,7 +379,7 @@ public class Boat extends Entity {
                 Entity entity = (Entity) list.get(j);
 
                 if (!entity.hasPassenger(this)) {
-                    if (flag && this.getPassengers().size() < 2 && !entity.isPassenger() && entity.getBbWidth() < this.getBbWidth() && entity instanceof LivingEntity && !(entity instanceof WaterAnimal) && !(entity instanceof Player)) {
+                    if (flag && this.passengers.size() < 2 && !entity.isPassenger() && entity.getBbWidth() < this.getBbWidth() && entity instanceof LivingEntity && !(entity instanceof WaterAnimal) && !(entity instanceof Player)) { // Paper - do not copy passenger list
                         entity.startRiding(this);
                     } else {
                         this.push(entity);
@@ -726,8 +726,8 @@ public class Boat extends Entity {
             float f = 0.0F;
             float f1 = (float) ((this.removed ? 0.009999999776482582D : this.getPassengersRidingOffset()) + passenger.getMyRidingOffset());
 
-            if (this.getPassengers().size() > 1) {
-                int i = this.getPassengers().indexOf(passenger);
+            if (this.passengers.size() > 1) { // Paper - do not copy list
+                int i = this.passengers.indexOf(passenger); // Paper - do not copy list
 
                 if (i == 0) {
                     f = 0.2F;
@@ -746,7 +746,7 @@ public class Boat extends Entity {
             passenger.yRot += this.deltaRotation;
             passenger.setYHeadRot(passenger.getYHeadRot() + this.deltaRotation);
             this.clampRotation(passenger);
-            if (passenger instanceof Animal && this.getPassengers().size() > 1) {
+            if (passenger instanceof Animal && this.passengers.size() > 1) { // Paper - do not copy list
                 int j = passenger.getId() % 2 == 0 ? 90 : 270;
 
                 passenger.setYBodyRot(((Animal) passenger).yBodyRot + (float) j);
@@ -906,13 +906,13 @@ public class Boat extends Entity {
 
     @Override
     protected boolean canAddPassenger(Entity passenger) {
-        return this.getPassengers().size() < 2 && !this.isEyeInFluid((Tag) FluidTags.WATER);
+        return this.passengers.size() < 2 && !this.isEyeInFluid((Tag) FluidTags.WATER); // Paper - do not copy list
     }
 
     @Nullable
     @Override
     public Entity getControllingPassenger() {
-        List<Entity> list = this.getPassengers();
+        List<Entity> list = this.passengers; // Paper - do not copy list
 
         return list.isEmpty() ? null : (Entity) list.get(0);
     }
