aboutsummaryrefslogtreecommitdiffhomepage
path: root/paper-server/patches/sources/net/minecraft/server/level/ServerEntity.java.patch
blob: f458422c506c24e88a3fe1ecde0f7970fabd90bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
--- a/net/minecraft/server/level/ServerEntity.java
+++ b/net/minecraft/server/level/ServerEntity.java
@@ -31,7 +31,6 @@
 import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
 import net.minecraft.network.protocol.game.VecDeltaCodec;
 import net.minecraft.network.syncher.SynchedEntityData;
-import net.minecraft.util.Mth;
 import net.minecraft.world.entity.Entity;
 import net.minecraft.world.entity.EquipmentSlot;
 import net.minecraft.world.entity.Leashable;
@@ -50,6 +49,13 @@
 import net.minecraft.world.phys.Vec3;
 import org.slf4j.Logger;
 
+// CraftBukkit start
+import net.minecraft.server.network.ServerPlayerConnection;
+import net.minecraft.util.Mth;
+import org.bukkit.entity.Player;
+import org.bukkit.event.player.PlayerVelocityEvent;
+// CraftBukkit end
+
 public class ServerEntity {
 
     private static final Logger LOGGER = LogUtils.getLogger();
@@ -69,18 +75,22 @@
     private Vec3 lastSentMovement;
     private int tickCount;
     private int teleportDelay;
-    private List<Entity> lastPassengers = Collections.emptyList();
+    private List<Entity> lastPassengers = com.google.common.collect.ImmutableList.of(); // Paper - optimize passenger checks
     private boolean wasRiding;
     private boolean wasOnGround;
     @Nullable
     private List<SynchedEntityData.DataValue<?>> trackedDataValues;
+    // CraftBukkit start
+    private final Set<ServerPlayerConnection> trackedPlayers;
 
-    public ServerEntity(ServerLevel world, Entity entity, int tickInterval, boolean alwaysUpdateVelocity, Consumer<Packet<?>> receiver) {
-        this.level = world;
-        this.broadcast = receiver;
+    public ServerEntity(ServerLevel worldserver, Entity entity, int i, boolean flag, Consumer<Packet<?>> consumer, Set<ServerPlayerConnection> trackedPlayers) {
+        this.trackedPlayers = trackedPlayers;
+        // CraftBukkit end
+        this.level = worldserver;
+        this.broadcast = consumer;
         this.entity = entity;
-        this.updateInterval = tickInterval;
-        this.trackDelta = alwaysUpdateVelocity;
+        this.updateInterval = i;
+        this.trackDelta = flag;
         this.positionCodec.setBase(entity.trackingPosition());
         this.lastSentMovement = entity.getDeltaMovement();
         this.lastSentYRot = Mth.packDegrees(entity.getYRot());
@@ -94,7 +104,7 @@
         List<Entity> list = this.entity.getPassengers();
 
         if (!list.equals(this.lastPassengers)) {
-            this.broadcast.accept(new ClientboundSetPassengersPacket(this.entity));
+            this.broadcastAndSend(new ClientboundSetPassengersPacket(this.entity)); // CraftBukkit
             ServerEntity.removedPassengers(list, this.lastPassengers).forEach((entity) -> {
                 if (entity instanceof ServerPlayer entityplayer) {
                     entityplayer.connection.teleport(entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(), entityplayer.getYRot(), entityplayer.getXRot());
@@ -106,19 +116,19 @@
 
         Entity entity = this.entity;
 
-        if (entity instanceof ItemFrame entityitemframe) {
-            if (this.tickCount % 10 == 0) {
+        if (!this.trackedPlayers.isEmpty() && entity instanceof ItemFrame entityitemframe) { // Paper - Perf: Only tick item frames if players can see it
+            if (true || this.tickCount % 10 == 0) { // CraftBukkit - Moved below, should always enter this block
                 ItemStack itemstack = entityitemframe.getItem();
 
-                if (itemstack.getItem() instanceof MapItem) {
-                    MapId mapid = (MapId) itemstack.get(DataComponents.MAP_ID);
+                if (this.level.paperConfig().maps.itemFrameCursorUpdateInterval > 0 && this.tickCount % this.level.paperConfig().maps.itemFrameCursorUpdateInterval == 0 && itemstack.getItem() instanceof MapItem) { // CraftBukkit - Moved this.tickCounter % 10 logic here so item frames do not enter the other blocks // Paper - Make item frame map cursor update interval configurable
+                    MapId mapid = entityitemframe.cachedMapId; // Paper - Perf: Cache map ids on item frames
                     MapItemSavedData worldmap = MapItem.getSavedData(mapid, this.level);
 
                     if (worldmap != null) {
-                        Iterator iterator = this.level.players().iterator();
+                        Iterator<ServerPlayerConnection> iterator = this.trackedPlayers.iterator(); // CraftBukkit
 
                         while (iterator.hasNext()) {
-                            ServerPlayer entityplayer = (ServerPlayer) iterator.next();
+                            ServerPlayer entityplayer = iterator.next().getPlayer(); // CraftBukkit
 
                             worldmap.tickCarriedBy(entityplayer, itemstack);
                             Packet<?> packet = worldmap.getUpdatePacket(mapid, entityplayer);
@@ -168,7 +178,13 @@
 
                     ++this.teleportDelay;
                     Vec3 vec3d = this.entity.trackingPosition();
-                    boolean flag1 = this.positionCodec.delta(vec3d).lengthSqr() >= 7.62939453125E-6D;
+                    // Paper start - reduce allocation of Vec3D here
+                    Vec3 base = this.positionCodec.base;
+                    double vec3d_dx = vec3d.x - base.x;
+                    double vec3d_dy = vec3d.y - base.y;
+                    double vec3d_dz = vec3d.z - base.z;
+                    boolean flag1 = (vec3d_dx * vec3d_dx + vec3d_dy * vec3d_dy + vec3d_dz * vec3d_dz) >= 7.62939453125E-6D;
+                    // Paper end - reduce allocation of Vec3D here
                     Packet<?> packet1 = null;
                     boolean flag2 = flag1 || this.tickCount % 60 == 0;
                     boolean flag3 = false;
@@ -248,6 +264,27 @@
 
         ++this.tickCount;
         if (this.entity.hurtMarked) {
+            // CraftBukkit start - Create PlayerVelocity event
+            boolean cancelled = false;
+
+            if (this.entity instanceof ServerPlayer) {
+                Player player = (Player) this.entity.getBukkitEntity();
+                org.bukkit.util.Vector velocity = player.getVelocity();
+
+                PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity.clone());
+                this.entity.level().getCraftServer().getPluginManager().callEvent(event);
+
+                if (event.isCancelled()) {
+                    cancelled = true;
+                } else if (!velocity.equals(event.getVelocity())) {
+                    player.setVelocity(event.getVelocity());
+                }
+            }
+
+            if (cancelled) {
+                return;
+            }
+            // CraftBukkit end
             this.entity.hurtMarked = false;
             this.broadcastAndSend(new ClientboundSetEntityMotionPacket(this.entity));
         }
@@ -298,7 +335,10 @@
 
     public void sendPairingData(ServerPlayer player, Consumer<Packet<ClientGamePacketListener>> sender) {
         if (this.entity.isRemoved()) {
-            ServerEntity.LOGGER.warn("Fetching packet for removed entity {}", this.entity);
+            // CraftBukkit start - Remove useless error spam, just return
+            // EntityTrackerEntry.LOGGER.warn("Fetching packet for removed entity {}", this.entity);
+            return;
+            // CraftBukkit end
         }
 
         Packet<ClientGamePacketListener> packet = this.entity.getAddEntityPacket(this);
@@ -313,6 +353,12 @@
         if (this.entity instanceof LivingEntity) {
             Collection<AttributeInstance> collection = ((LivingEntity) this.entity).getAttributes().getSyncableAttributes();
 
+            // CraftBukkit start - If sending own attributes send scaled health instead of current maximum health
+            if (this.entity.getId() == player.getId()) {
+                ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(collection, false);
+            }
+            // CraftBukkit end
+
             if (!collection.isEmpty()) {
                 sender.accept(new ClientboundUpdateAttributesPacket(this.entity.getId(), collection));
             }
@@ -342,8 +388,9 @@
             }
 
             if (!list.isEmpty()) {
-                sender.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list));
+                sender.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list, true)); // Paper - data sanitization
             }
+            ((LivingEntity) this.entity).detectEquipmentUpdatesPublic(); // CraftBukkit - SPIGOT-3789: sync again immediately after sending
         }
 
         if (!this.entity.getPassengers().isEmpty()) {
@@ -396,6 +443,11 @@
             Set<AttributeInstance> set = ((LivingEntity) this.entity).getAttributes().getAttributesToSync();
 
             if (!set.isEmpty()) {
+                // CraftBukkit start - Send scaled max health
+                if (this.entity instanceof ServerPlayer) {
+                    ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(set, false);
+                }
+                // CraftBukkit end
                 this.broadcastAndSend(new ClientboundUpdateAttributesPacket(this.entity.getId(), set));
             }