2024-12-11 22:26:55 +01:00
--- 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;
2022-02-28 16:00:00 +01:00
import org.slf4j.Logger;
2019-04-23 06:00:30 +02:00
2014-11-25 22:32:16 +01:00
+// CraftBukkit start
2021-06-11 07:00:00 +02:00
+import net.minecraft.server.network.ServerPlayerConnection;
2024-12-11 22:26:55 +01:00
+import net.minecraft.util.Mth;
2014-11-25 22:32:16 +01:00
+import org.bukkit.entity.Player;
+import org.bukkit.event.player.PlayerVelocityEvent;
+// CraftBukkit end
2019-04-23 06:00:30 +02:00
+
2024-12-11 22:26:55 +01:00
public class ServerEntity {
2017-01-04 00:50:02 +01:00
2022-02-28 16:00:00 +01:00
private static final Logger LOGGER = LogUtils.getLogger();
2021-08-08 06:52:54 +02:00
@@ -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;
2021-06-11 07:00:00 +02:00
private boolean wasOnGround;
2022-12-07 17:00:00 +01:00
@Nullable
2024-12-11 22:26:55 +01:00
private List<SynchedEntityData.DataValue<?>> trackedDataValues;
2019-04-23 04:00:00 +02:00
+ // CraftBukkit start
2021-06-11 07:00:00 +02:00
+ private final Set<ServerPlayerConnection> trackedPlayers;
2019-04-23 04:00:00 +02:00
2024-12-11 22:26:55 +01:00
- 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) {
2019-04-23 04:00:00 +02:00
+ this.trackedPlayers = trackedPlayers;
+ // CraftBukkit end
2024-12-11 22:26:55 +01:00
+ this.level = worldserver;
+ this.broadcast = consumer;
2024-06-13 17:05:00 +02:00
this.entity = entity;
2024-12-11 22:26:55 +01:00
- 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());
2024-10-22 17:15:00 +02:00
@@ -94,7 +104,7 @@
2023-03-14 17:30:00 +01:00
List<Entity> list = this.entity.getPassengers();
2022-06-10 02:39:19 +02:00
if (!list.equals(this.lastPassengers)) {
2024-12-11 22:26:55 +01:00
- 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) {
2024-04-23 17:15:00 +02:00
entityplayer.connection.teleport(entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(), entityplayer.getYRot(), entityplayer.getXRot());
2023-06-19 22:45:53 +02:00
@@ -106,19 +116,19 @@
2024-04-23 17:15:00 +02:00
Entity entity = this.entity;
2021-06-11 07:00:00 +02:00
2023-06-19 22:45:53 +02:00
- if (entity instanceof ItemFrame entityitemframe) {
2022-06-07 18:00:00 +02:00
- if (this.tickCount % 10 == 0) {
2023-06-19 22:45:53 +02:00
+ if (!this.trackedPlayers.isEmpty() && entity instanceof ItemFrame entityitemframe) { // Paper - Perf: Only tick item frames if players can see it
2022-06-07 18:00:00 +02:00
+ if (true || this.tickCount % 10 == 0) { // CraftBukkit - Moved below, should always enter this block
ItemStack itemstack = entityitemframe.getItem();
2014-11-25 22:32:16 +01:00
2024-12-11 22:26:55 +01:00
- if (itemstack.getItem() instanceof MapItem) {
2023-08-07 12:58:28 +02:00
- MapId mapid = (MapId) itemstack.get(DataComponents.MAP_ID);
2021-08-13 01:14:38 +02:00
+ 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
2023-08-07 12:58:28 +02:00
+ MapId mapid = entityitemframe.cachedMapId; // Paper - Perf: Cache map ids on item frames
2024-12-11 22:26:55 +01:00
MapItemSavedData worldmap = MapItem.getSavedData(mapid, this.level);
2022-06-07 18:00:00 +02:00
if (worldmap != null) {
- Iterator iterator = this.level.players().iterator();
+ Iterator<ServerPlayerConnection> iterator = this.trackedPlayers.iterator(); // CraftBukkit
while (iterator.hasNext()) {
2024-12-11 22:26:55 +01:00
- ServerPlayer entityplayer = (ServerPlayer) iterator.next();
+ ServerPlayer entityplayer = iterator.next().getPlayer(); // CraftBukkit
2022-06-07 18:00:00 +02:00
worldmap.tickCarriedBy(entityplayer, itemstack);
2024-04-23 17:15:00 +02:00
Packet<?> packet = worldmap.getUpdatePacket(mapid, entityplayer);
2020-04-27 09:04:16 +02:00
@@ -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 @@
2014-11-25 22:32:16 +01:00
2021-06-11 07:00:00 +02:00
++this.tickCount;
if (this.entity.hurtMarked) {
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - Create PlayerVelocity event
+ boolean cancelled = false;
+
2024-12-11 22:26:55 +01:00
+ if (this.entity instanceof ServerPlayer) {
2021-06-11 07:00:00 +02:00
+ Player player = (Player) this.entity.getBukkitEntity();
2014-11-25 22:32:16 +01:00
+ org.bukkit.util.Vector velocity = player.getVelocity();
+
2015-07-20 21:42:30 +02:00
+ PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity.clone());
2023-06-07 17:30:00 +02:00
+ this.entity.level().getCraftServer().getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
+ if (event.isCancelled()) {
+ cancelled = true;
+ } else if (!velocity.equals(event.getVelocity())) {
2015-07-20 21:42:30 +02:00
+ player.setVelocity(event.getVelocity());
2014-11-25 22:32:16 +01:00
+ }
+ }
+
2024-06-13 17:05:00 +02:00
+ if (cancelled) {
+ return;
2014-11-25 22:32:16 +01:00
+ }
+ // CraftBukkit end
2024-06-13 17:05:00 +02:00
this.entity.hurtMarked = false;
2024-12-11 22:26:55 +01:00
this.broadcastAndSend(new ClientboundSetEntityMotionPacket(this.entity));
2024-06-13 17:05:00 +02:00
}
2020-04-27 09:04:16 +02:00
@@ -298,7 +335,10 @@
2019-04-23 04:00:00 +02:00
2024-12-11 22:26:55 +01:00
public void sendPairingData(ServerPlayer player, Consumer<Packet<ClientGamePacketListener>> sender) {
2021-06-11 07:00:00 +02:00
if (this.entity.isRemoved()) {
2024-12-11 22:26:55 +01:00
- ServerEntity.LOGGER.warn("Fetching packet for removed entity {}", this.entity);
2019-04-23 04:00:00 +02:00
+ // CraftBukkit start - Remove useless error spam, just return
2021-06-11 07:00:00 +02:00
+ // EntityTrackerEntry.LOGGER.warn("Fetching packet for removed entity {}", this.entity);
2019-04-23 04:00:00 +02:00
+ return;
+ // CraftBukkit end
}
2024-12-11 22:26:55 +01:00
Packet<ClientGamePacketListener> packet = this.entity.getAddEntityPacket(this);
2020-04-27 09:04:16 +02:00
@@ -313,6 +353,12 @@
2024-12-11 22:26:55 +01:00
if (this.entity instanceof LivingEntity) {
Collection<AttributeInstance> collection = ((LivingEntity) this.entity).getAttributes().getSyncableAttributes();
2019-04-23 04:00:00 +02:00
+ // CraftBukkit start - If sending own attributes send scaled health instead of current maximum health
2024-12-11 22:26:55 +01:00
+ if (this.entity.getId() == player.getId()) {
+ ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(collection, false);
2019-04-23 04:00:00 +02:00
+ }
+ // CraftBukkit end
+
if (!collection.isEmpty()) {
2024-12-11 22:26:55 +01:00
sender.accept(new ClientboundUpdateAttributesPacket(this.entity.getId(), collection));
2019-04-23 04:00:00 +02:00
}
2020-04-27 09:04:16 +02:00
@@ -344,6 +390,7 @@
2021-01-27 04:24:20 +01:00
if (!list.isEmpty()) {
2024-12-11 22:26:55 +01:00
sender.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list));
2019-04-23 04:00:00 +02:00
}
2024-12-11 22:26:55 +01:00
+ ((LivingEntity) this.entity).detectEquipmentUpdatesPublic(); // CraftBukkit - SPIGOT-3789: sync again immediately after sending
2023-09-21 18:57:13 +02:00
}
2023-06-07 17:30:00 +02:00
if (!this.entity.getPassengers().isEmpty()) {
2020-04-27 09:04:16 +02:00
@@ -396,6 +443,11 @@
2024-12-11 22:26:55 +01:00
Set<AttributeInstance> set = ((LivingEntity) this.entity).getAttributes().getAttributesToSync();
2014-11-25 22:32:16 +01:00
if (!set.isEmpty()) {
+ // CraftBukkit start - Send scaled max health
2024-12-11 22:26:55 +01:00
+ if (this.entity instanceof ServerPlayer) {
+ ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(set, false);
2014-11-25 22:32:16 +01:00
+ }
+ // CraftBukkit end
2024-12-11 22:26:55 +01:00
this.broadcastAndSend(new ClientboundUpdateAttributesPacket(this.entity.getId(), set));
2014-11-25 22:32:16 +01:00
}