From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: William Blake Galbreath Date: Sat, 11 Jul 2020 19:41:34 -0500 Subject: [PATCH] Entity lifespan diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java index e1c2ac7e9578db319f582cdd9cbe0084d43e34f0..cb36cc5248a7166fa8d43cabb6ada371c1026aaa 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -2823,6 +2823,7 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic } if (entity.distanceToSqr(this.player.getEyePosition()) < ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE) { + if (entity instanceof Mob mob) mob.ticksSinceLastInteraction = 0; // Purpur packet.dispatch(new ServerboundInteractPacket.Handler() { private void performInteraction(InteractionHand enumhand, ServerGamePacketListenerImpl.EntityInteraction playerconnection_a, PlayerInteractEntityEvent event) { // CraftBukkit ItemStack itemstack = ServerGamePacketListenerImpl.this.player.getItemInHand(enumhand); diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java index b6f2fbe5a2577696d62e001a3c07b52b07a33c74..d1eb52081277a51610abd83810ea96dfed2ea54b 100644 --- a/src/main/java/net/minecraft/world/entity/Mob.java +++ b/src/main/java/net/minecraft/world/entity/Mob.java @@ -134,6 +134,7 @@ public abstract class Mob extends LivingEntity { private BlockPos restrictCenter; private float restrictRadius; + public int ticksSinceLastInteraction; // Purpur public boolean aware = true; // CraftBukkit protected Mob(EntityType type, Level world) { @@ -288,6 +289,7 @@ public abstract class Mob extends LivingEntity { entityliving = null; } } + if (entityliving instanceof ServerPlayer) this.ticksSinceLastInteraction = 0; // Purpur this.target = entityliving; return true; // CraftBukkit end @@ -334,9 +336,29 @@ public abstract class Mob extends LivingEntity { this.playAmbientSound(); } + incrementTicksSinceLastInteraction(); // Purpur this.level.getProfiler().pop(); } + // Purpur start + private void incrementTicksSinceLastInteraction() { + ++this.ticksSinceLastInteraction; + if (getRider() != null) { + this.ticksSinceLastInteraction = 0; + return; + } + if (this.level.purpurConfig.entityLifeSpan <= 0) { + return; // feature disabled + } + if (!this.removeWhenFarAway(0) || isPersistenceRequired() || requiresCustomPersistence() || hasCustomName()) { + return; // mob persistent + } + if (this.ticksSinceLastInteraction > this.level.purpurConfig.entityLifeSpan) { + this.discard(); + } + } + // Purpur end + @Override protected void playHurtSound(DamageSource source) { this.resetAmbientSoundTime(); @@ -526,6 +548,7 @@ public abstract class Mob extends LivingEntity { } nbt.putBoolean("Bukkit.Aware", this.aware); // CraftBukkit + nbt.putInt("Purpur.ticksSinceLastInteraction", this.ticksSinceLastInteraction); // Purpur } @Override @@ -596,6 +619,11 @@ public abstract class Mob extends LivingEntity { this.aware = nbt.getBoolean("Bukkit.Aware"); } // CraftBukkit end + // Purpur start + if (nbt.contains("Purpur.ticksSinceLastInteraction")) { + this.ticksSinceLastInteraction = nbt.getInt("Purpur.ticksSinceLastInteraction"); + } + // Purpur end } @Override @@ -1660,6 +1688,7 @@ public abstract class Mob extends LivingEntity { this.setLastHurtMob(target); } + if (target instanceof ServerPlayer) this.ticksSinceLastInteraction = 0; // Purpur return flag; } diff --git a/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java b/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java index 7a01302e0ce1a89f2107cbb110ae4819daf118d2..a34e1932fdd32a517b5e627cecbfa4062607f8b7 100644 --- a/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java +++ b/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java @@ -128,6 +128,11 @@ public class PurpurWorldConfig { elytraDamagePerTridentBoost = getInt("gameplay-mechanics.elytra.damage-per-boost.trident", elytraDamagePerTridentBoost); } + public int entityLifeSpan = 0; + private void entitySettings() { + entityLifeSpan = getInt("gameplay-mechanics.entity-lifespan", entityLifeSpan); + } + public List itemImmuneToCactus = new ArrayList<>(); public List itemImmuneToExplosion = new ArrayList<>(); public List itemImmuneToFire = new ArrayList<>();