Hostile mobs usually despawn once the player moves away from them, typically once they're 128 blocks away. But passive mobs won't despawn, not by default anyway. I want to spawn a cow that will dissapear once unloaded.
I thought I could do this by spawning a mob with "PersistenceRequired" set to false, but this doesn't seem to work:
List<ResourceLocation> entities = new ArrayList<ResourceLocation>(EntityList.ENTITY_EGGS.keySet());
ResourceLocation randomResource = entities.get(rand.nextInt(entities.size()));
Entity randomEntity = EntityList.createEntityByIDFromName(randomResource, entity.world);
if (!randomEntity.isNonBoss())
return;
NBTTagCompound nbt = new NBTTagCompound();
nbt.setBoolean("PersistenceRequired", false);
((EntityLiving) randomEntity).readEntityFromNBT(nbt);
randomEntity.setPositionAndRotation(entity.posX, entity.posY, entity.posZ, entity.rotationYaw, entity.rotationPitch);
entity.world.spawnEntity(randomEntity);
entity.setDead();