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.
This commit is contained in:
Christian 2013-09-06 16:05:29 -04:00
parent ff655de27e
commit d9ca67e162
3 changed files with 31 additions and 15 deletions

View File

@ -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)

View File

@ -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 <em>if</em> 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);

View File

@ -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);
}