From 8dcb99bcfb19a96bab9af459c7d4b03629724201 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Sat, 2 Aug 2014 17:41:39 +1000 Subject: [PATCH] Fixed slime spawning in the Bayou. Closes #296 --- .../common/entities/EntityGlob.java | 3 +- .../common/eventhandler/BOPEventHandlers.java | 2 + .../entity/SlimeSpawnEventHandler.java | 59 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/main/java/biomesoplenty/common/eventhandler/entity/SlimeSpawnEventHandler.java diff --git a/src/main/java/biomesoplenty/common/entities/EntityGlob.java b/src/main/java/biomesoplenty/common/entities/EntityGlob.java index 3f5fb7fe9..77318a97d 100644 --- a/src/main/java/biomesoplenty/common/entities/EntityGlob.java +++ b/src/main/java/biomesoplenty/common/entities/EntityGlob.java @@ -13,6 +13,7 @@ import net.minecraft.world.EnumDifficulty; import net.minecraft.world.World; import net.minecraft.world.biome.BiomeGenBase; import net.minecraft.world.chunk.Chunk; +import net.minecraftforge.common.BiomeDictionary; import biomesoplenty.BiomesOPlenty; import biomesoplenty.api.BOPItemHelper; import biomesoplenty.api.content.BOPCItems; @@ -300,7 +301,7 @@ public class EntityGlob extends EntityLiving implements IMob { BiomeGenBase biomegenbase = worldObj.getBiomeGenForCoords(MathHelper.floor_double(posX), MathHelper.floor_double(posZ)); - if (biomegenbase == BiomeGenBase.swampland && posY > 50.0D && posY < 70.0D && rand.nextFloat() < 0.5F && rand.nextFloat() < this.worldObj.getCurrentMoonPhaseFactor() && worldObj.getBlockLightValue(MathHelper.floor_double(posX), MathHelper.floor_double(posY), MathHelper.floor_double(posZ)) <= rand.nextInt(8)) + if (BiomeDictionary.isBiomeOfType(biomegenbase, BiomeDictionary.Type.SWAMP) && posY > 50.0D && posY < 70.0D && rand.nextFloat() < 0.5F && rand.nextFloat() < this.worldObj.getCurrentMoonPhaseFactor() && worldObj.getBlockLightValue(MathHelper.floor_double(posX), MathHelper.floor_double(posY), MathHelper.floor_double(posZ)) <= rand.nextInt(8)) return super.getCanSpawnHere(); if (rand.nextInt(10) == 0 && chunk.getRandomWithSeed(987234911L).nextInt(10) == 0 && posY < 40.0D) diff --git a/src/main/java/biomesoplenty/common/eventhandler/BOPEventHandlers.java b/src/main/java/biomesoplenty/common/eventhandler/BOPEventHandlers.java index 7e5b36a53..c4b20da59 100644 --- a/src/main/java/biomesoplenty/common/eventhandler/BOPEventHandlers.java +++ b/src/main/java/biomesoplenty/common/eventhandler/BOPEventHandlers.java @@ -8,6 +8,7 @@ import biomesoplenty.common.eventhandler.client.gui.MainMenuEventHandler; import biomesoplenty.common.eventhandler.client.gui.StartupWarningEventHandler; import biomesoplenty.common.eventhandler.entity.DyeEventHandler; import biomesoplenty.common.eventhandler.entity.FlippersEventHandler; +import biomesoplenty.common.eventhandler.entity.SlimeSpawnEventHandler; import biomesoplenty.common.eventhandler.entity.TemptEventHandler; import biomesoplenty.common.eventhandler.misc.BonemealEventHandler; import biomesoplenty.common.eventhandler.misc.BucketEventHandler; @@ -46,6 +47,7 @@ public class BOPEventHandlers private static void registerEntityEventHandlers() { + MinecraftForge.EVENT_BUS.register(new SlimeSpawnEventHandler()); MinecraftForge.EVENT_BUS.register(new DyeEventHandler()); MinecraftForge.EVENT_BUS.register(new FlippersEventHandler()); MinecraftForge.EVENT_BUS.register(new TemptEventHandler()); diff --git a/src/main/java/biomesoplenty/common/eventhandler/entity/SlimeSpawnEventHandler.java b/src/main/java/biomesoplenty/common/eventhandler/entity/SlimeSpawnEventHandler.java new file mode 100644 index 000000000..8f59d2ed7 --- /dev/null +++ b/src/main/java/biomesoplenty/common/eventhandler/entity/SlimeSpawnEventHandler.java @@ -0,0 +1,59 @@ +package biomesoplenty.common.eventhandler.entity; + +import net.minecraft.entity.EntityLiving; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.monster.EntitySlime; +import net.minecraft.util.MathHelper; +import net.minecraft.world.EnumDifficulty; +import net.minecraft.world.World; +import net.minecraft.world.biome.BiomeGenBase; +import net.minecraftforge.common.BiomeDictionary; +import net.minecraftforge.event.entity.living.LivingSpawnEvent; +import cpw.mods.fml.common.eventhandler.Event.Result; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; + +/** + * A temporary (hopefully) measure to allow slimes to spawn + * in biomes other than the swampland + */ +public class SlimeSpawnEventHandler +{ + @SubscribeEvent + public void onCheckEntitySpawn(LivingSpawnEvent.CheckSpawn event) + { + World world = event.world; + EntityLivingBase entity = event.entityLiving; + + if (event.entityLiving instanceof EntitySlime) + { + BiomeGenBase biome = world.getBiomeGenForCoords(MathHelper.floor_double(entity.posX), MathHelper.floor_double(entity.posZ)); + + if (BiomeDictionary.isBiomeOfType(biome, BiomeDictionary.Type.SWAMP)) + { + if (canSlimeSpawn((EntitySlime)entity)) event.setResult(Result.ALLOW); + } + } + } + + private boolean canSlimeSpawn(EntitySlime slime) + { + if (!slime.worldObj.getWorldInfo().getTerrainType().handleSlimeSpawnReduction(slime.getRNG(), slime.worldObj)) + { + if (slime.getSlimeSize() == 1 || slime.worldObj.difficultySetting != EnumDifficulty.PEACEFUL) + { + if (slime.posY > 50.0D && slime.posY < 70.0D && slime.getRNG().nextFloat() < 0.5F && slime.getRNG().nextFloat() < slime.worldObj.getCurrentMoonPhaseFactor() && slime.worldObj.getBlockLightValue(MathHelper.floor_double(slime.posX), MathHelper.floor_double(slime.posY), MathHelper.floor_double(slime.posZ)) <= slime.getRNG().nextInt(8)) + { + return getCanSpawnHere(slime); + } + + } + } + + return false; + } + + private boolean getCanSpawnHere(EntityLiving entity) + { + return entity.worldObj.checkNoEntityCollision(entity.boundingBox) && entity.worldObj.getCollidingBoundingBoxes(entity, entity.boundingBox).isEmpty() && !entity.worldObj.isAnyLiquid(entity.boundingBox); + } +}