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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
--- a/net/minecraft/server/level/ServerEntity.java
+++ b/net/minecraft/server/level/ServerEntity.java
@@ -40,6 +41,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();
private static final int TOLERANCE_LEVEL_ROTATION = 1;
@@ -63,10 +72,16 @@
private boolean wasOnGround;
@Nullable
private List<SynchedEntityData.DataValue<?>> trackedDataValues;
+ // CraftBukkit start
+ private final Set<ServerPlayerConnection> trackedPlayers;
- public ServerEntity(ServerLevel level, Entity entity, int updateInterval, boolean trackDelta, Consumer<Packet<?>> broadcast) {
- this.level = level;
- this.broadcast = broadcast;
+ public ServerEntity(ServerLevel worldserver, Entity entity, int i, boolean flag, Consumer<Packet<?>> consumer, Set<ServerPlayerConnection> trackedPlayers) {
+ this.trackedPlayers = trackedPlayers;
+ // CraftBukkit end
+ this.ap = Vec3.ZERO;
+ this.lastPassengers = Collections.emptyList();
+ this.level = worldserver;
+ this.broadcast = consumer;
this.entity = entity;
this.updateInterval = updateInterval;
this.trackDelta = trackDelta;
@@ -79,32 +94,45 @@
}
public void sendChanges() {
- List<Entity> passengers = this.entity.getPassengers();
- if (!passengers.equals(this.lastPassengers)) {
- this.broadcast.accept(new ClientboundSetPassengersPacket(this.entity));
- removedPassengers(passengers, this.lastPassengers)
- .forEach(
- entity -> {
- if (entity instanceof ServerPlayer serverPlayer1) {
- serverPlayer1.connection
- .teleport(serverPlayer1.getX(), serverPlayer1.getY(), serverPlayer1.getZ(), serverPlayer1.getYRot(), serverPlayer1.getXRot());
- }
- }
- );
- this.lastPassengers = passengers;
+ List<Entity> list = this.entity.getPassengers();
+
+ if (!list.equals(this.lastPassengers)) {
+ this.broadcastAndSend(new ClientboundSetPassengersPacket(this.entity)); // CraftBukkit
+ removedPassengers(list, this.lastPassengers).forEach((entity) -> {
+ if (entity instanceof ServerPlayer) {
+ ServerPlayer entityplayer = (ServerPlayer) entity;
+
+ entityplayer.connection.teleport(entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(), entityplayer.getYRot(), entityplayer.getXRot());
+ }
+
+ });
+ this.lastPassengers = list;
}
- if (this.entity instanceof ItemFrame itemFrame && this.tickCount % 10 == 0) {
- ItemStack item = itemFrame.getItem();
- if (item.getItem() instanceof MapItem) {
- Integer mapId = MapItem.getMapId(item);
- MapItemSavedData savedData = MapItem.getSavedData(mapId, this.level);
- if (savedData != null) {
- for (ServerPlayer serverPlayer : this.level.players()) {
- savedData.tickCarriedBy(serverPlayer, item);
- Packet<?> updatePacket = savedData.getUpdatePacket(mapId, serverPlayer);
- if (updatePacket != null) {
- serverPlayer.connection.send(updatePacket);
+ Entity entity = this.entity;
+
+ if (entity instanceof ItemFrame) {
+ ItemFrame entityitemframe = (ItemFrame) entity;
+
+ if (true || this.tickCount % 10 == 0) { // CraftBukkit - Moved below, should always enter this block
+ ItemStack itemstack = entityitemframe.getItem();
+
+ if (this.tickCount % 10 == 0 && itemstack.getItem() instanceof MapItem) { // CraftBukkit - Moved this.tickCounter % 10 logic here so item frames do not enter the other blocks
+ Integer integer = MapItem.getMapId(itemstack);
+ MapItemSavedData worldmap = MapItem.getSavedData(integer, this.level);
+
+ if (worldmap != null) {
+ Iterator<ServerPlayerConnection> iterator = this.trackedPlayers.iterator(); // CraftBukkit
+
+ while (iterator.hasNext()) {
+ ServerPlayer entityplayer = iterator.next().getPlayer(); // CraftBukkit
+
+ worldmap.tickCarriedBy(entityplayer, itemstack);
+ Packet<?> packet = worldmap.getUpdatePacket(integer, entityplayer);
+
+ if (packet != null) {
+ entityplayer.connection.send(packet);
+ }
}
}
}
@@ -206,7 +238,27 @@
this.tickCount++;
if (this.entity.hurtMarked) {
- this.broadcastAndSend(new ClientboundSetEntityMotionPacket(this.entity));
+ // 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) {
+ this.broadcastAndSend(new ClientboundSetEntityMotionPacket(this.entity));
+ }
+ // CraftBukkit end
this.entity.hurtMarked = false;
}
}
@@ -229,7 +286,10 @@
public void sendPairingData(ServerPlayer player, Consumer<Packet<ClientGamePacketListener>> consumer) {
if (this.entity.isRemoved()) {
- 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> addEntityPacket = this.entity.getAddEntityPacket();
@@ -241,10 +303,13 @@
boolean flag = this.trackDelta;
if (this.entity instanceof LivingEntity) {
- Collection<AttributeInstance> syncableAttributes = ((LivingEntity)this.entity).getAttributes().getSyncableAttributes();
- if (!syncableAttributes.isEmpty()) {
- consumer.accept(new ClientboundUpdateAttributesPacket(this.entity.getId(), syncableAttributes));
+ 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 (((LivingEntity)this.entity).isFallFlying()) {
flag = true;
@@ -269,8 +342,15 @@
if (!list.isEmpty()) {
consumer.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list));
}
+ ((LivingEntity) this.entity).detectEquipmentUpdatesPublic(); // CraftBukkit - SPIGOT-3789: sync again immediately after sending
}
+ // CraftBukkit start - MC-109346: Fix for nonsensical head yaw
+ if (this.entity instanceof ServerPlayer) {
+ consumer.accept(new ClientboundRotateHeadPacket(this.entity, (byte) Mth.floor(this.entity.getYHeadRot() * 256.0F / 360.0F)));
+ }
+ // CraftBukkit end
+
if (!this.entity.getPassengers().isEmpty()) {
consumer.accept(new ClientboundSetPassengersPacket(this.entity));
}
@@ -293,9 +381,15 @@
}
if (this.entity instanceof LivingEntity) {
- Set<AttributeInstance> dirtyAttributes = ((LivingEntity)this.entity).getAttributes().getDirtyAttributes();
- if (!dirtyAttributes.isEmpty()) {
- this.broadcastAndSend(new ClientboundUpdateAttributesPacket(this.entity.getId(), dirtyAttributes));
+ Set<AttributeInstance> set = ((LivingEntity) this.entity).getAttributes().getDirtyAttributes();
+
+ 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));
}
dirtyAttributes.clear();
|