Skip to content

Commit

Permalink
Add a cancellable despawn event- allows mods to decide to prevent the…
Browse files Browse the repository at this point in the history
… despawning

of certain otherwise normally despawnable mobs.
  • Loading branch information
cpw committed Sep 6, 2013
1 parent c63efa9 commit 6da6e9d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions common/net/minecraftforge/event/ForgeEventFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -59,6 +60,11 @@ public static boolean doSpecialSpawn(EntityLiving entity, World world, float x,
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,25 @@ public SpecialSpawn(EntityLiving entity, World world, float x, float y, float z)
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);
}

}
}
20 changes: 17 additions & 3 deletions patches/minecraft/net/minecraft/entity/EntityLiving.java.patch
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
--- ../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;
+ ForgeHooks.onLivingSetAttackTarget(this, par1EntityLivingBase);
}

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

Expand Down

0 comments on commit 6da6e9d

Please sign in to comment.