From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: wling-art Date: Sat, 17 May 2025 08:25:33 +0800 Subject: [PATCH] Optimize isEyeInFluid diff --git a/net/minecraft/server/level/ServerPlayer.java b/net/minecraft/server/level/ServerPlayer.java index 896e50276e4e70f694562f0626dc53d3ecd21258..58b0eb299f8750e167145f4cba6aba99b6198aa5 100644 --- a/net/minecraft/server/level/ServerPlayer.java +++ b/net/minecraft/server/level/ServerPlayer.java @@ -1915,7 +1915,7 @@ public class ServerPlayer extends Player implements ca.spottedleaf.moonrise.patc this.awardStat(Stats.SWIM_ONE_CM, rounded); this.causeFoodExhaustion(this.level().spigotConfig.swimMultiplier * (float) rounded * 0.01F, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.SWIM); // CraftBukkit - EntityExhaustionEvent // Spigot } - } else if (this.isEyeInFluid(FluidTags.WATER)) { + } else if (this.isEyeInWater()) { // Leaf - Optimize isEyeInFluid int rounded = Math.round((float)Math.sqrt(dx * dx + dy * dy + dz * dz) * 100.0F); if (rounded > 0) { this.awardStat(Stats.WALK_UNDER_WATER_ONE_CM, rounded); diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java index b694c7c1837fd4facb16d6bcf93245da9d18a56d..48c2aeee828c1b79d8d6c5f5fd88fae1ef4befc8 100644 --- a/net/minecraft/world/entity/Entity.java +++ b/net/minecraft/world/entity/Entity.java @@ -288,7 +288,14 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess public boolean wasTouchingWater; protected Object2DoubleMap> fluidHeight = new Object2DoubleArrayMap<>(2); protected boolean wasEyeInWater; - private final Set> fluidOnEyes = new HashSet<>(); + // Leaf start - Optimize isEyeInFluid + // Remove original field since plugin should not direct access to it, and able to + // expose potential incompatibility asap. + // In paper api, if plugins have custom conditions, then It's more reasonable to + // use isEyeInFluid and their own conditions. + //private final Set> fluidOnEyes = null; + private int isInWaterOrLava; + // Leaf end - Optimize isEyeInFluid public int invulnerableTime; protected boolean firstTick = true; protected final SynchedEntityData entityData; @@ -2038,8 +2045,8 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess } private void updateFluidOnEyes() { - this.wasEyeInWater = this.isEyeInFluid(FluidTags.WATER); - this.fluidOnEyes.clear(); + this.wasEyeInWater = this.isInWaterOrLava == 1; // Leaf - Optimize isEyeInFluid + this.isInWaterOrLava = 0; // Leaf - Optimize isEyeInFluid - reset cache double eyeY = this.getEyeY(); if (!( this.getVehicle() instanceof AbstractBoat abstractBoat @@ -2051,7 +2058,9 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess FluidState fluidState = this.level().getFluidState(blockPos); double d = blockPos.getY() + fluidState.getHeight(this.level(), blockPos); if (d > eyeY) { - fluidState.getTags().forEach(this.fluidOnEyes::add); + // Leaf start - Optimize isEyeInFluid + this.isInWaterOrLava = fluidState.isEmpty() ? 0 : fluidState.is(FluidTags.WATER) ? 1 : 2; + // Leaf end - Optimize isEyeInFluid } } } @@ -2131,9 +2140,25 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess } } + // Leaf start - Optimize isEyeInFluid public boolean isEyeInFluid(TagKey fluidTag) { - return this.fluidOnEyes.contains(fluidTag); + if (isInWaterOrLava == 0) { + return false; + } + if (fluidTag == FluidTags.WATER && isInWaterOrLava == 1) { + return true; + } + return fluidTag == FluidTags.LAVA && isInWaterOrLava == 2; + } + + public boolean isEyeInWater() { + return isInWaterOrLava == 1; + } + + public boolean isEyeInLava() { + return isInWaterOrLava == 2; } + // Leaf end - Optimize isEyeInFluid public boolean isInLava() { return !this.firstTick && this.fluidHeight.getDouble(FluidTags.LAVA) > 0.0; diff --git a/net/minecraft/world/entity/ExperienceOrb.java b/net/minecraft/world/entity/ExperienceOrb.java index c8354d46ed909090f7c15f396863bf7d73afcefa..3ee788b172240ccf38cb31385dff13364ccc4142 100644 --- a/net/minecraft/world/entity/ExperienceOrb.java +++ b/net/minecraft/world/entity/ExperienceOrb.java @@ -153,7 +153,7 @@ public class ExperienceOrb extends Entity { } else { super.tick(); boolean flag = !this.level().noCollision(this.getBoundingBox()); - if (this.isEyeInFluid(FluidTags.WATER)) { + if (this.isEyeInWater()) { // Leaf - Optimize isEyeInFluid this.setUnderwaterMovement(); } else if (!flag) { this.applyGravity(); diff --git a/net/minecraft/world/entity/LivingEntity.java b/net/minecraft/world/entity/LivingEntity.java index 68c03f5d12978f7da12a892bfc31600e755f1202..d2e0fc60575a52b8daef07f00df8991ec5462379 100644 --- a/net/minecraft/world/entity/LivingEntity.java +++ b/net/minecraft/world/entity/LivingEntity.java @@ -463,7 +463,7 @@ public abstract class LivingEntity extends Entity implements Attackable, Waypoin } } - if (this.isEyeInFluid(FluidTags.WATER) + if (this.isEyeInWater() // Leaf - Optimize isEyeInFluid && !serverLevel1.getBlockState(BlockPos.containing(this.getX(), this.getEyeY(), this.getZ())).is(Blocks.BUBBLE_COLUMN)) { boolean flag1 = !this.canBreatheUnderwater() && !MobEffectUtil.hasWaterBreathing(this) diff --git a/net/minecraft/world/entity/animal/AbstractFish.java b/net/minecraft/world/entity/animal/AbstractFish.java index 0002e39e2670ad92849ccc0aada163b174fe1ec2..36b787f5cf4f30589d8b78f86b58553895a1ae51 100644 --- a/net/minecraft/world/entity/animal/AbstractFish.java +++ b/net/minecraft/world/entity/animal/AbstractFish.java @@ -182,7 +182,7 @@ public abstract class AbstractFish extends WaterAnimal implements Bucketable { @Override public void vanillaTick() { // Purpur - Ridables - if (this.fish.isEyeInFluid(FluidTags.WATER)) { + if (this.fish.isEyeInWater()) { // Leaf - Optimize isEyeInFluid this.fish.setDeltaMovement(this.fish.getDeltaMovement().add(0.0, 0.005, 0.0)); } diff --git a/net/minecraft/world/entity/animal/horse/SkeletonHorse.java b/net/minecraft/world/entity/animal/horse/SkeletonHorse.java index f6ab6ecc10486694d77905239a82bda4dec94936..8550a8758b1b26e0ddd9ed7f6ae0f167e23d96fe 100644 --- a/net/minecraft/world/entity/animal/horse/SkeletonHorse.java +++ b/net/minecraft/world/entity/animal/horse/SkeletonHorse.java @@ -111,7 +111,7 @@ public class SkeletonHorse extends AbstractHorse { @Override public SoundEvent getAmbientSound() { - return this.isEyeInFluid(FluidTags.WATER) ? SoundEvents.SKELETON_HORSE_AMBIENT_WATER : SoundEvents.SKELETON_HORSE_AMBIENT; + return this.isEyeInWater() ? SoundEvents.SKELETON_HORSE_AMBIENT_WATER : SoundEvents.SKELETON_HORSE_AMBIENT; // Leaf - Optimize isEyeInFluid } @Override diff --git a/net/minecraft/world/entity/monster/Strider.java b/net/minecraft/world/entity/monster/Strider.java index e1717b5c854aa81fdd7b7e715d7c3498d9f86072..31cace20dfd61bd591f501d840a190799808926f 100644 --- a/net/minecraft/world/entity/monster/Strider.java +++ b/net/minecraft/world/entity/monster/Strider.java @@ -391,7 +391,7 @@ public class Strider extends Animal implements ItemSteerable { @Override protected boolean canAddPassenger(Entity passenger) { - return !this.isVehicle() && !this.isEyeInFluid(FluidTags.LAVA); + return !this.isVehicle() && !this.isEyeInLava(); // Leaf - Optimize isEyeInFluid } @Override diff --git a/net/minecraft/world/entity/monster/Witch.java b/net/minecraft/world/entity/monster/Witch.java index 4b253ae8149f5d9505c5140a00a96d8c8850b1c4..bfb0e3381abb93bea1079fb0d476cf856fca442e 100644 --- a/net/minecraft/world/entity/monster/Witch.java +++ b/net/minecraft/world/entity/monster/Witch.java @@ -178,7 +178,7 @@ public class Witch extends Raider implements RangedAttackMob { } } else { Holder holder = null; - if (this.random.nextFloat() < 0.15F && this.isEyeInFluid(FluidTags.WATER) && !this.hasEffect(MobEffects.WATER_BREATHING)) { + if (this.random.nextFloat() < 0.15F && this.isEyeInWater() && !this.hasEffect(MobEffects.WATER_BREATHING)) { // Leaf - Optimize isEyeInFluid holder = Potions.WATER_BREATHING; } else if (this.random.nextFloat() < 0.15F && (this.isOnFire() || this.getLastDamageSource() != null && this.getLastDamageSource().is(DamageTypeTags.IS_FIRE)) diff --git a/net/minecraft/world/entity/monster/Zombie.java b/net/minecraft/world/entity/monster/Zombie.java index 202dca0a106a1610d8e58b7e9ba0c612998dd868..c27aa33e667abd89c10708f64b172efdf2f07a60 100644 --- a/net/minecraft/world/entity/monster/Zombie.java +++ b/net/minecraft/world/entity/monster/Zombie.java @@ -286,7 +286,7 @@ public class Zombie extends Monster { this.doUnderWaterConversion(); } } else if (this.convertsInWater()) { - if (this.isEyeInFluid(FluidTags.WATER)) { + if (this.isEyeInWater()) { // Leaf - Optimize isEyeInFluid this.inWaterTime++; if (this.inWaterTime >= 600) { this.startUnderWaterConversion(300); diff --git a/net/minecraft/world/entity/player/Player.java b/net/minecraft/world/entity/player/Player.java index 2da9d6a2c12f490bda2cd0b9fb83fa35749f8761..108ef0759e4060fc02d517175731f413c7e2532f 100644 --- a/net/minecraft/world/entity/player/Player.java +++ b/net/minecraft/world/entity/player/Player.java @@ -398,7 +398,7 @@ public abstract class Player extends LivingEntity { this.lastItemInMainHand = mainHandItem.copy(); } - if (!this.isEyeInFluid(FluidTags.WATER) && this.isEquipped(Items.TURTLE_HELMET)) { + if (!this.isEyeInWater() && this.isEquipped(Items.TURTLE_HELMET)) { // Leaf - Optimize isEyeInFluid this.turtleHelmetTick(); } @@ -438,8 +438,7 @@ public abstract class Player extends LivingEntity { } protected boolean updateIsUnderwater() { - this.wasUnderwater = this.isEyeInFluid(FluidTags.WATER); - return this.wasUnderwater; + return this.wasUnderwater = this.isEyeInWater(); // Leaf - Optimize isEyeInFluid } @Override @@ -845,7 +844,7 @@ public abstract class Player extends LivingEntity { } destroySpeed *= (float)this.getAttributeValue(Attributes.BLOCK_BREAK_SPEED); - if (this.isEyeInFluid(FluidTags.WATER)) { + if (this.isEyeInWater()) { // Leaf - Optimize isEyeInFluid destroySpeed *= (float)this.getAttribute(Attributes.SUBMERGED_MINING_SPEED).getValue(); } diff --git a/net/minecraft/world/entity/vehicle/AbstractBoat.java b/net/minecraft/world/entity/vehicle/AbstractBoat.java index d947801b616af5b5dcdcc8bb70b36f97d6a69fdd..678badf7622b81e94c973bed2082fbfafa596b24 100644 --- a/net/minecraft/world/entity/vehicle/AbstractBoat.java +++ b/net/minecraft/world/entity/vehicle/AbstractBoat.java @@ -793,7 +793,7 @@ public abstract class AbstractBoat extends VehicleEntity implements Leashable { @Override protected boolean canAddPassenger(Entity passenger) { - return this.getPassengers().size() < this.getMaxPassengers() && !this.isEyeInFluid(FluidTags.WATER); + return this.getPassengers().size() < this.getMaxPassengers() && !this.isEyeInWater(); // Leaf - Optimize isEyeInFluid } protected int getMaxPassengers() {