From d9ca67e162f22cf85b364731dd281c9ef51334e4 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 6 Sep 2013 16:05:29 -0400 Subject: [PATCH] Change from Cancelable to using a Result. This means you can force despawn mobs you don't want around anymore. Also, deferred check to once every 20 ticks. May tune it down further or make it a config if this event is a lag issue. --- .../event/ForgeEventFactory.java | 6 ++-- .../event/entity/living/LivingSpawnEvent.java | 12 ++++---- .../minecraft/entity/EntityLiving.java.patch | 28 +++++++++++++------ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/common/net/minecraftforge/event/ForgeEventFactory.java b/common/net/minecraftforge/event/ForgeEventFactory.java index f8b8dbec4..e53808aec 100644 --- a/common/net/minecraftforge/event/ForgeEventFactory.java +++ b/common/net/minecraftforge/event/ForgeEventFactory.java @@ -60,9 +60,11 @@ public class ForgeEventFactory return MinecraftForge.EVENT_BUS.post(new LivingSpawnEvent.SpecialSpawn(entity, world, x, y, z)); } - public static boolean canEntityDespawn(EntityLiving entity) + public static Result canEntityDespawn(EntityLiving entity) { - return !MinecraftForge.EVENT_BUS.post(new AllowDespawn(entity)); + AllowDespawn event = new AllowDespawn(entity); + MinecraftForge.EVENT_BUS.post(event); + return event.getResult(); } public static List getPotentialSpawns(WorldServer world, EnumCreatureType type, int x, int y, int z, List oldList) diff --git a/common/net/minecraftforge/event/entity/living/LivingSpawnEvent.java b/common/net/minecraftforge/event/entity/living/LivingSpawnEvent.java index beb904d4a..cd1cfb7c0 100644 --- a/common/net/minecraftforge/event/entity/living/LivingSpawnEvent.java +++ b/common/net/minecraftforge/event/entity/living/LivingSpawnEvent.java @@ -49,19 +49,21 @@ public class LivingSpawnEvent extends LivingEvent } /** - * 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. + * Fired each tick for despawnable mobs to allow control over despawning. + * {@link Result#DEFAULT} will pass the mob on to vanilla despawn mechanics. + * {@link Result#ALLOW} will force the mob to despawn. + * {@link Result#DENY} will force the mob to remain. + * 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} + * the mob can be allowed to despawn. See {@link EntityLiving#despawnEntity} * * @author cpw * */ - @Cancelable + @HasResult public static class AllowDespawn extends LivingSpawnEvent { - public AllowDespawn(EntityLiving entity) { super(entity, entity.worldObj, (float)entity.posX, (float)entity.posY, (float)entity.posZ); diff --git a/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch b/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch index 1a7fd413d..858bb3270 100644 --- a/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch @@ -1,17 +1,18 @@ --- ../src_base/minecraft/net/minecraft/entity/EntityLiving.java +++ ../src_work/minecraft/net/minecraft/entity/EntityLiving.java -@@ -31,6 +31,10 @@ +@@ -31,6 +31,11 @@ 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.Event.Result; +import net.minecraftforge.event.ForgeEventFactory; +import net.minecraftforge.event.entity.living.LivingSpawnEvent.AllowDespawn; public abstract class EntityLiving extends EntityLivingBase { -@@ -141,6 +145,7 @@ +@@ -141,6 +146,7 @@ public void setAttackTarget(EntityLivingBase par1EntityLivingBase) { this.attackTarget = par1EntityLivingBase; @@ -19,18 +20,29 @@ } /** -@@ -548,6 +553,10 @@ +@@ -547,9 +553,21 @@ + */ protected void despawnEntity() { ++ Result result = null; if (this.persistenceRequired) -+ { -+ this.entityAge = 0; -+ } -+ else if (this.canDespawn() && !ForgeEventFactory.canEntityDespawn(this)) { this.entityAge = 0; ++ } ++ else if (this.entityAge % 20 == 1 && (result = ForgeEventFactory.canEntityDespawn(this)) != Result.DEFAULT) ++ { ++ if (result == Result.DENY) ++ { ++ this.entityAge = 0; ++ } ++ else ++ { ++ this.setDead(); ++ } } -@@ -726,8 +735,6 @@ + else + { +@@ -726,8 +744,6 @@ return this.worldObj.checkNoEntityCollision(this.boundingBox) && this.worldObj.getCollidingBoundingBoxes(this, this.boundingBox).isEmpty() && !this.worldObj.isAnyLiquid(this.boundingBox); }