From 3d63d68ecb3b21279183e562a394b5cb3370a880 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Wed, 15 Apr 2020 17:56:07 -0700 Subject: [PATCH] Don't run entity collision code if not needed Will not run if: Max entity cramming is disabled and the max collisions per entity is less than or equal to 0. Entity#isPushable() returns false, meaning all entities will not be able to collide with this entity anyways. The entity's current team collision rule causes them to NEVER collide. Co-authored-by: Owen1212055 <23108066+Owen1212055@users.noreply.github.com> --- .../world/entity/LivingEntity.java.patch | 106 ++++++++++-------- 1 file changed, 61 insertions(+), 45 deletions(-) diff --git a/paper-server/patches/sources/net/minecraft/world/entity/LivingEntity.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/LivingEntity.java.patch index 07126a15d8..7def6de5fc 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/LivingEntity.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/LivingEntity.java.patch @@ -17,10 +17,11 @@ import net.minecraft.world.entity.projectile.AbstractArrow; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.item.AxeItem; -@@ -136,6 +137,30 @@ +@@ -135,6 +136,30 @@ + import net.minecraft.world.scores.PlayerTeam; import net.minecraft.world.scores.Scoreboard; import org.slf4j.Logger; - ++ +// CraftBukkit start +import java.util.ArrayList; +import java.util.HashSet; @@ -44,10 +45,9 @@ +import org.bukkit.event.entity.EntityTeleportEvent; +import org.bukkit.event.player.PlayerItemConsumeEvent; +// CraftBukkit end -+ + public abstract class LivingEntity extends Entity implements Attackable { - private static final Logger LOGGER = LogUtils.getLogger(); @@ -174,7 +199,7 @@ public static final float DEFAULT_BABY_SCALE = 0.5F; public static final String ATTRIBUTES_FIELD = "attributes"; @@ -162,19 +162,18 @@ } public void onEquipItem(EquipmentSlot slot, ItemStack oldStack, ItemStack newStack) { -- if (!this.level().isClientSide() && !this.isSpectator()) { -- boolean flag = newStack.isEmpty() && oldStack.isEmpty(); + // CraftBukkit start + this.onEquipItem(slot, oldStack, newStack, false); + } ++ ++ public void onEquipItem(EquipmentSlot enumitemslot, ItemStack itemstack, ItemStack itemstack1, boolean silent) { ++ // CraftBukkit end + if (!this.level().isClientSide() && !this.isSpectator()) { +- boolean flag = newStack.isEmpty() && oldStack.isEmpty(); ++ boolean flag = itemstack1.isEmpty() && itemstack.isEmpty(); - if (!flag && !ItemStack.isSameItemSameComponents(oldStack, newStack) && !this.firstTick) { - Equippable equippable = (Equippable) newStack.get(DataComponents.EQUIPPABLE); -+ public void onEquipItem(EquipmentSlot enumitemslot, ItemStack itemstack, ItemStack itemstack1, boolean silent) { -+ // CraftBukkit end -+ if (!this.level().isClientSide() && !this.isSpectator()) { -+ boolean flag = itemstack1.isEmpty() && itemstack.isEmpty(); -+ + if (!flag && !ItemStack.isSameItemSameComponents(itemstack, itemstack1) && !this.firstTick) { + Equippable equippable = (Equippable) itemstack1.get(DataComponents.EQUIPPABLE); @@ -459,8 +458,8 @@ public MobEffectInstance removeEffectNoUpdate(Holder effect) { - return (MobEffectInstance) this.activeEffects.remove(effect); + return this.removeEffectNoUpdate(effect, org.bukkit.event.entity.EntityPotionEffectEvent.Cause.UNKNOWN); -+ } -+ + } + + @Nullable + public MobEffectInstance removeEffectNoUpdate(Holder holder, EntityPotionEffectEvent.Cause cause) { + if (this.isTickingEffects) { @@ -479,8 +478,8 @@ + } + + return (MobEffectInstance) this.activeEffects.remove(holder); - } - ++ } ++ public boolean removeEffect(Holder effect) { - MobEffectInstance mobeffect = this.removeEffectNoUpdate(effect); + return this.removeEffect(effect, org.bukkit.event.entity.EntityPotionEffectEvent.Cause.UNKNOWN); @@ -967,12 +966,10 @@ } } -@@ -1681,8 +2052,22 @@ - - public LivingEntity.Fallsounds getFallSounds() { +@@ -1683,6 +2054,20 @@ return new LivingEntity.Fallsounds(SoundEvents.GENERIC_SMALL_FALL, SoundEvents.GENERIC_BIG_FALL); -+ } -+ + } + + // CraftBukkit start - Add delegate methods + public SoundEvent getHurtSound0(DamageSource damagesource) { + return this.getHurtSound(damagesource); @@ -980,8 +977,8 @@ + + public SoundEvent getDeathSound0() { + return this.getDeathSound(); - } - ++ } ++ + public SoundEvent getFallDamageSound0(int fallHeight) { + return this.getFallDamageSound(fallHeight); + } @@ -1232,27 +1229,26 @@ } public CombatTracker getCombatTracker() { -@@ -1935,9 +2487,19 @@ +@@ -1935,8 +2487,18 @@ } public final void setArrowCount(int stuckArrowCount) { - this.entityData.set(LivingEntity.DATA_ARROW_COUNT_ID, stuckArrowCount); + // CraftBukkit start + this.setArrowCount(stuckArrowCount, false); - } - ++ } ++ + public final void setArrowCount(int i, boolean flag) { + ArrowBodyCountChangeEvent event = CraftEventFactory.callArrowBodyCountChangeEvent(this, this.getArrowCount(), i, flag); + if (event.isCancelled()) { + return; + } + this.entityData.set(LivingEntity.DATA_ARROW_COUNT_ID, event.getNewAmount()); -+ } + } + // CraftBukkit end -+ + public final int getStingerCount() { return (Integer) this.entityData.get(LivingEntity.DATA_STINGER_COUNT_ID); - } @@ -1999,7 +2561,7 @@ this.playSound(soundeffect, this.getSoundVolume(), (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F); } @@ -1385,16 +1381,36 @@ this.setSharedFlag(7, false); return; } -@@ -3113,7 +3691,7 @@ +@@ -3113,12 +3691,26 @@ Level world = this.level(); if (!(world instanceof ServerLevel worldserver)) { - this.level().getEntities(EntityTypeTest.forClass(Player.class), this.getBoundingBox(), EntitySelector.pushableBy(this)).forEach(this::doPush); + this.level().getEntities(EntityTypeTest.forClass(net.minecraft.world.entity.player.Player.class), this.getBoundingBox(), EntitySelector.pushableBy(this)).forEach(this::doPush); } else { ++ // Paper start - don't run getEntities if we're not going to use its result ++ if (!this.isPushable()) { ++ return; ++ } ++ net.minecraft.world.scores.Team team = this.getTeam(); ++ if (team != null && team.getCollisionRule() == net.minecraft.world.scores.Team.CollisionRule.NEVER) { ++ return; ++ } ++ ++ int i = worldserver.getGameRules().getInt(GameRules.RULE_MAX_ENTITY_CRAMMING); ++ if (i <= 0 && this.level().paperConfig().collisions.maxEntityCollisions <= 0) { ++ return; ++ } ++ // Paper end - don't run getEntities if we're not going to use its result List list = this.level().getEntities((Entity) this, this.getBoundingBox(), EntitySelector.pushableBy(this)); -@@ -3138,10 +3716,12 @@ + if (!list.isEmpty()) { +- int i = worldserver.getGameRules().getInt(GameRules.RULE_MAX_ENTITY_CRAMMING); ++ // Paper - don't run getEntities if we're not going to use its result; moved up + + if (i > 0 && list.size() > i - 1 && this.random.nextInt(4) == 0) { + int j = 0; +@@ -3138,10 +3730,12 @@ } Iterator iterator1 = list.iterator(); @@ -1409,7 +1425,7 @@ this.doPush(entity1); } } -@@ -3190,9 +3770,15 @@ +@@ -3190,9 +3784,15 @@ @Override public void stopRiding() { @@ -1426,7 +1442,7 @@ if (entity != null && entity != this.getVehicle() && !this.level().isClientSide) { this.dismountVehicle(entity); } -@@ -3305,15 +3891,22 @@ +@@ -3305,15 +3905,22 @@ @Override public boolean isPickable() { @@ -1451,7 +1467,7 @@ public float getYHeadRot() { return this.yHeadRot; } -@@ -3342,7 +3935,7 @@ +@@ -3342,7 +3949,7 @@ } public final void setAbsorptionAmount(float absorptionAmount) { @@ -1460,7 +1476,7 @@ } protected void internalSetAbsorptionAmount(float absorptionAmount) { -@@ -3410,9 +4003,14 @@ +@@ -3410,9 +4017,14 @@ } public void startUsingItem(InteractionHand hand) { @@ -1476,7 +1492,7 @@ this.useItem = itemstack; this.useItemRemaining = itemstack.getUseDuration(this); if (!this.level().isClientSide) { -@@ -3483,13 +4081,49 @@ +@@ -3483,13 +4095,49 @@ this.releaseUsingItem(); } else { if (!this.useItem.isEmpty() && this.isUsingItem()) { @@ -1490,7 +1506,7 @@ + org.bukkit.inventory.EquipmentSlot hand = org.bukkit.craftbukkit.CraftEquipmentSlot.getHand(enumhand); + event = new PlayerItemConsumeEvent((Player) this.getBukkitEntity(), craftItem, hand); // Paper + this.level().getCraftServer().getPluginManager().callEvent(event); - ++ + if (event.isCancelled()) { + // Update client + Consumable consumable = this.useItem.get(DataComponents.CONSUMABLE); @@ -1501,7 +1517,7 @@ + entityPlayer.getBukkitEntity().updateScaledHealth(); + return; + } -+ + + itemstack = (craftItem.equals(event.getItem())) ? this.useItem.finishUsingItem(this.level(), this) : CraftItemStack.asNMSCopy(event.getItem()).finishUsingItem(this.level(), this); + } else { + itemstack = this.useItem.finishUsingItem(this.level(), this); @@ -1527,7 +1543,7 @@ } } -@@ -3544,12 +4178,69 @@ +@@ -3544,12 +4192,69 @@ if (this.isUsingItem() && !this.useItem.isEmpty()) { Item item = this.useItem.getItem(); @@ -1598,7 +1614,7 @@ public boolean isSuppressingSlidingDownLadder() { return this.isShiftKeyDown(); } -@@ -3568,12 +4259,18 @@ +@@ -3568,12 +4273,18 @@ } public boolean randomTeleport(double x, double y, double z, boolean particleEffects) { @@ -1619,7 +1635,7 @@ Level world = this.level(); if (world.hasChunkAt(blockposition)) { -@@ -3592,18 +4289,43 @@ +@@ -3592,18 +4303,43 @@ } if (flag2) { @@ -1667,7 +1683,7 @@ world.broadcastEntityEvent(this, (byte) 46); } -@@ -3613,7 +4335,7 @@ +@@ -3613,7 +4349,7 @@ entitycreature.getNavigation().stop(); } @@ -1676,7 +1692,7 @@ } } -@@ -3706,7 +4428,7 @@ +@@ -3706,7 +4442,7 @@ } public void stopSleeping() { @@ -1685,7 +1701,7 @@ Level world = this.level(); java.util.Objects.requireNonNull(world); -@@ -3718,9 +4440,9 @@ +@@ -3718,9 +4454,9 @@ this.level().setBlock(blockposition, (BlockState) iblockdata.setValue(BedBlock.OCCUPIED, false), 3); Vec3 vec3d = (Vec3) BedBlock.findStandUpPosition(this.getType(), this.level(), blockposition, enumdirection, this.getYRot()).orElseGet(() -> { @@ -1697,7 +1713,7 @@ }); Vec3 vec3d1 = Vec3.atBottomCenterOf(blockposition).subtract(vec3d).normalize(); float f = (float) Mth.wrapDegrees(Mth.atan2(vec3d1.z, vec3d1.x) * 57.2957763671875D - 90.0D); -@@ -3740,7 +4462,7 @@ +@@ -3740,7 +4476,7 @@ @Nullable public Direction getBedOrientation() { @@ -1706,7 +1722,7 @@ return blockposition != null ? BedBlock.getBedOrientation(this.level(), blockposition) : null; } -@@ -3905,7 +4627,7 @@ +@@ -3905,7 +4641,7 @@ public float maxUpStep() { float f = (float) this.getAttributeValue(Attributes.STEP_HEIGHT);