From 6da6e9da0793777c40c90a427d3b76c5a265c59e Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 6 Sep 2013 13:19:51 -0400 Subject: [PATCH] Add a cancellable despawn event- allows mods to decide to prevent the despawning of certain otherwise normally despawnable mobs. --- .../event/ForgeEventFactory.java | 6 ++++++ .../event/entity/living/LivingSpawnEvent.java | 21 +++++++++++++++++++ .../minecraft/entity/EntityLiving.java.patch | 20 +++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/common/net/minecraftforge/event/ForgeEventFactory.java b/common/net/minecraftforge/event/ForgeEventFactory.java index 8158c59d9..f8b8dbec4 100644 --- a/common/net/minecraftforge/event/ForgeEventFactory.java +++ b/common/net/minecraftforge/event/ForgeEventFactory.java @@ -13,6 +13,7 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.Event.Result; import net.minecraftforge.event.entity.living.LivingPackSizeEvent; import net.minecraftforge.event.entity.living.LivingSpawnEvent; +import net.minecraftforge.event.entity.living.LivingSpawnEvent.AllowDespawn; import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent; import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; @@ -59,6 +60,11 @@ public class ForgeEventFactory return MinecraftForge.EVENT_BUS.post(new LivingSpawnEvent.SpecialSpawn(entity, world, x, y, z)); } + public static boolean canEntityDespawn(EntityLiving entity) + { + return !MinecraftForge.EVENT_BUS.post(new AllowDespawn(entity)); + } + public static List getPotentialSpawns(WorldServer world, EnumCreatureType type, int x, int y, int z, List oldList) { WorldEvent.PotentialSpawns event = new WorldEvent.PotentialSpawns(world, type, x, y, z, oldList); diff --git a/common/net/minecraftforge/event/entity/living/LivingSpawnEvent.java b/common/net/minecraftforge/event/entity/living/LivingSpawnEvent.java index 320b80a73..beb904d4a 100644 --- a/common/net/minecraftforge/event/entity/living/LivingSpawnEvent.java +++ b/common/net/minecraftforge/event/entity/living/LivingSpawnEvent.java @@ -47,4 +47,25 @@ public class LivingSpawnEvent extends LivingEvent super(entity, world, x, y, z); } } + + /** + * Fired if the mob may be allowed to despawn. Cancellation will prevent possible despawning. This is fired every tick for + * every despawnable entity. Be efficient in your handlers. + * + * Note: this is not fired if the mob is definitely going to otherwise despawn. It is fired to check if + * the mob can be allowed to despawn according to standard mob spawning rules. See {@link EntityLiving#despawnEntity} + * + * @author cpw + * + */ + @Cancelable + public static class AllowDespawn extends LivingSpawnEvent + { + + public AllowDespawn(EntityLiving entity) + { + super(entity, entity.worldObj, (float)entity.posX, (float)entity.posY, (float)entity.posZ); + } + + } } \ No newline at end of file diff --git a/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch b/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch index 970fa5915..1a7fd413d 100644 --- a/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch @@ -1,14 +1,17 @@ --- ../src_base/minecraft/net/minecraft/entity/EntityLiving.java +++ ../src_work/minecraft/net/minecraft/entity/EntityLiving.java -@@ -31,6 +31,7 @@ +@@ -31,6 +31,10 @@ import net.minecraft.util.MathHelper; import net.minecraft.world.World; import net.minecraft.world.WorldServer; +import net.minecraftforge.common.ForgeHooks; ++import net.minecraftforge.common.MinecraftForge; ++import net.minecraftforge.event.ForgeEventFactory; ++import net.minecraftforge.event.entity.living.LivingSpawnEvent.AllowDespawn; public abstract class EntityLiving extends EntityLivingBase { -@@ -141,6 +142,7 @@ +@@ -141,6 +145,7 @@ public void setAttackTarget(EntityLivingBase par1EntityLivingBase) { this.attackTarget = par1EntityLivingBase; @@ -16,7 +19,18 @@ } /** -@@ -726,8 +728,6 @@ +@@ -548,6 +553,10 @@ + protected void despawnEntity() + { + if (this.persistenceRequired) ++ { ++ this.entityAge = 0; ++ } ++ else if (this.canDespawn() && !ForgeEventFactory.canEntityDespawn(this)) + { + this.entityAge = 0; + } +@@ -726,8 +735,6 @@ return this.worldObj.checkNoEntityCollision(this.boundingBox) && this.worldObj.getCollidingBoundingBoxes(this, this.boundingBox).isEmpty() && !this.worldObj.isAnyLiquid(this.boundingBox); }