From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Sat, 11 Jul 2020 19:41:34 -0500
Subject: [PATCH] Entity lifespan


diff --git a/src/main/java/net/minecraft/server/network/PlayerConnection.java b/src/main/java/net/minecraft/server/network/PlayerConnection.java
index d309552f56f5ea695d57a95ed0a091f4c383ca34..77e7f3f33b13050ff383458ae294906c5ff77476 100644
--- a/src/main/java/net/minecraft/server/network/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/network/PlayerConnection.java
@@ -2457,6 +2457,7 @@ public class PlayerConnection implements PacketListenerPlayIn {
                     boolean triggerLeashUpdate = itemInHand != null && itemInHand.getItem() == Items.LEAD && entity instanceof EntityInsentient;
                     Item origItem = this.player.inventory.getItemInHand() == null ? null : this.player.inventory.getItemInHand().getItem();
                     PlayerInteractEntityEvent event;
+                    if (entity instanceof EntityInsentient) ((EntityInsentient) entity).ticksSinceLastInteraction = 0; // Purpur
                     if (packetplayinuseentity.b() == PacketPlayInUseEntity.EnumEntityUseAction.INTERACT) {
                         event = new PlayerInteractEntityEvent((Player) this.getPlayer(), entity.getBukkitEntity(), (packetplayinuseentity.c() == EnumHand.OFF_HAND) ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND);
                     } else {
diff --git a/src/main/java/net/minecraft/world/entity/EntityInsentient.java b/src/main/java/net/minecraft/world/entity/EntityInsentient.java
index 462bcb7cfe2daa27c10a7a0318bef9b739e586f4..c9136f1b54ff0620a621b703b4e7487f4a63b01d 100644
--- a/src/main/java/net/minecraft/world/entity/EntityInsentient.java
+++ b/src/main/java/net/minecraft/world/entity/EntityInsentient.java
@@ -124,7 +124,7 @@ public abstract class EntityInsentient extends EntityLiving {
     private NBTTagCompound by;
     private BlockPosition bz;
     private float bA;
-
+    public int ticksSinceLastInteraction; // Purpur
     public boolean aware = true; // CraftBukkit
 
     protected EntityInsentient(EntityTypes<? extends EntityInsentient> entitytypes, World world) {
@@ -276,6 +276,7 @@ public abstract class EntityInsentient extends EntityLiving {
                 entityliving = null;
             }
         }
+        if (entityliving instanceof EntityPlayer) this.ticksSinceLastInteraction = 0; // Purpur
         this.goalTarget = entityliving;
         return true;
         // CraftBukkit end
@@ -320,10 +321,35 @@ public abstract class EntityInsentient extends EntityLiving {
             this.m();
             this.F();
         }
-
+        incrementTicksSinceLastInteraction(); // Purpur
         this.world.getMethodProfiler().exit();
     }
 
+    // Purpur start
+    private void incrementTicksSinceLastInteraction() {
+        ++ticksSinceLastInteraction;
+        //if (hasRider()) {
+        //    ticksSinceLastInteraction = 0;
+        //    return;
+        //}
+        if (world.purpurConfig.entityLifeSpan <= 0) {
+            return; // feature disabled
+        }
+        if (!isTypeNotPersistent(0) || isPersistent() || isSpecialPersistence() || hasCustomName()) {
+            return; // mob persistent
+        }
+        if (ticksSinceLastInteraction > world.purpurConfig.entityLifeSpan) {
+            this.dead = true;
+        }
+    }
+
+    @Override
+    public boolean damageEntity(DamageSource damagesource, float f) {
+        if (damagesource.getEntity() instanceof EntityPlayer) this.ticksSinceLastInteraction = 0; // Purpur
+        return super.damageEntity(damagesource, f);
+    }
+    // Purpur end
+
     @Override
     protected void c(DamageSource damagesource) {
         this.m();
@@ -497,6 +523,7 @@ public abstract class EntityInsentient extends EntityLiving {
         }
 
         nbttagcompound.setBoolean("Bukkit.Aware", this.aware); // CraftBukkit
+        nbttagcompound.setInt("Purpur.ticksSinceLastInteraction", ticksSinceLastInteraction); // Purpur
     }
 
     @Override
@@ -567,6 +594,11 @@ public abstract class EntityInsentient extends EntityLiving {
             this.aware = nbttagcompound.getBoolean("Bukkit.Aware");
         }
         // CraftBukkit end
+        // Purpur start
+        if (nbttagcompound.hasKey("Purpur.ticksSinceLastInteraction")) {
+            ticksSinceLastInteraction = nbttagcompound.getInt("Purpur.ticksSinceLastInteraction");
+        }
+        // Purpur end
     }
 
     @Override
@@ -1625,7 +1657,7 @@ public abstract class EntityInsentient extends EntityLiving {
             this.a((EntityLiving) this, entity);
             this.z(entity);
         }
-
+        if (entity instanceof EntityPlayer) this.ticksSinceLastInteraction = 0; // Purpur
         return flag;
     }
 
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index a366742e8d11513b469ffb8dafd98a4c0d8ab30a..021dbb20793dd365c181c4d8a2ebc429af85778b 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -103,6 +103,11 @@ public class PurpurWorldConfig {
         }
     }
 
+    public int entityLifeSpan = 0;
+    private void entitySettings() {
+        entityLifeSpan = getInt("gameplay-mechanics.entity-lifespan", entityLifeSpan);
+    }
+
     public List<Item> itemImmuneToCactus = new ArrayList<>();
     public List<Item> itemImmuneToExplosion = new ArrayList<>();
     public List<Item> itemImmuneToFire = new ArrayList<>();
