Add a cancellable despawn event- allows mods to decide to prevent the despawning

of certain otherwise normally despawnable mobs.
This commit is contained in:
Christian 2013-09-06 13:19:51 -04:00
parent c63efa917d
commit 6da6e9da07
3 changed files with 44 additions and 3 deletions

View File

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

View File

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

View File

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