From b5b43a5a36269b1eed57393dd4270b1694c40706 Mon Sep 17 00:00:00 2001 From: bl4ckscor3 Date: Wed, 4 Sep 2019 00:15:48 +0200 Subject: [PATCH] Add spawn reason to spawn events (#6096) --- .../minecraft/entity/EntityType.java.patch | 2 +- .../spawner/WorldEntitySpawner.java.patch | 6 +++--- .../net/minecraftforge/common/ForgeHooks.java | 5 +++-- .../event/ForgeEventFactory.java | 10 +++++----- .../event/entity/living/LivingSpawnEvent.java | 19 +++++++++++++++++-- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/patches/minecraft/net/minecraft/entity/EntityType.java.patch b/patches/minecraft/net/minecraft/entity/EntityType.java.patch index bc2ce8e8c7..53c6d524d3 100644 --- a/patches/minecraft/net/minecraft/entity/EntityType.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityType.java.patch @@ -46,7 +46,7 @@ @Nullable public T func_220342_a(World p_220342_1_, @Nullable CompoundNBT p_220342_2_, @Nullable ITextComponent p_220342_3_, @Nullable PlayerEntity p_220342_4_, BlockPos p_220342_5_, SpawnReason p_220342_6_, boolean p_220342_7_, boolean p_220342_8_) { T t = this.func_220349_b(p_220342_1_, p_220342_2_, p_220342_3_, p_220342_4_, p_220342_5_, p_220342_6_, p_220342_7_, p_220342_8_); -+ if (t instanceof net.minecraft.entity.MobEntity && net.minecraftforge.event.ForgeEventFactory.doSpecialSpawn((net.minecraft.entity.MobEntity) t, p_220342_1_, p_220342_5_.func_177958_n(), p_220342_5_.func_177956_o(), p_220342_5_.func_177952_p(), null)) return null; ++ if (t instanceof net.minecraft.entity.MobEntity && net.minecraftforge.event.ForgeEventFactory.doSpecialSpawn((net.minecraft.entity.MobEntity) t, p_220342_1_, p_220342_5_.func_177958_n(), p_220342_5_.func_177956_o(), p_220342_5_.func_177952_p(), null, p_220342_6_)) return null; p_220342_1_.func_217376_c(t); return t; } diff --git a/patches/minecraft/net/minecraft/world/spawner/WorldEntitySpawner.java.patch b/patches/minecraft/net/minecraft/world/spawner/WorldEntitySpawner.java.patch index 419e688794..de40491b99 100644 --- a/patches/minecraft/net/minecraft/world/spawner/WorldEntitySpawner.java.patch +++ b/patches/minecraft/net/minecraft/world/spawner/WorldEntitySpawner.java.patch @@ -23,12 +23,12 @@ mobentity.func_70012_b((double)f, (double)k, (double)f1, p_222263_1_.field_73012_v.nextFloat() * 360.0F, 0.0F); - if (d0 > 16384.0D && mobentity.func_213397_c(d0) || !mobentity.func_213380_a(p_222263_1_, SpawnReason.NATURAL) || !mobentity.func_205019_a(p_222263_1_)) { -+ int canSpawn = net.minecraftforge.common.ForgeHooks.canEntitySpawn(mobentity, p_222263_1_, f, k, f1, null); ++ int canSpawn = net.minecraftforge.common.ForgeHooks.canEntitySpawn(mobentity, p_222263_1_, f, k, f1, null, SpawnReason.NATURAL); + if (canSpawn == -1 || (canSpawn == 0 && d0 > 16384.0D && (mobentity.func_213397_c(d0) || !mobentity.func_213380_a(p_222263_1_, SpawnReason.NATURAL) || !mobentity.func_205019_a(p_222263_1_)))) { break label115; } -+ if (!net.minecraftforge.event.ForgeEventFactory.doSpecialSpawn(mobentity, p_222263_1_, f, k, f1, null)) ++ if (!net.minecraftforge.event.ForgeEventFactory.doSpecialSpawn(mobentity, p_222263_1_, f, k, f1, null, SpawnReason.NATURAL)) ilivingentitydata = mobentity.func_213386_a(p_222263_1_, p_222263_1_.func_175649_E(new BlockPos(mobentity)), SpawnReason.NATURAL, ilivingentitydata, (CompoundNBT)null); ++i; ++j2; @@ -90,7 +90,7 @@ entity.func_70012_b(d0, (double)blockpos.func_177956_o(), d1, p_77191_4_.nextFloat() * 360.0F, 0.0F); if (entity instanceof MobEntity) { MobEntity mobentity = (MobEntity)entity; -+ if (net.minecraftforge.common.ForgeHooks.canEntitySpawn(mobentity, p_77191_0_, d0, blockpos.func_177956_o(), d1, null) == -1) continue; ++ if (net.minecraftforge.common.ForgeHooks.canEntitySpawn(mobentity, p_77191_0_, d0, blockpos.func_177956_o(), d1, null, SpawnReason.CHUNK_GENERATION) == -1) continue; if (mobentity.func_213380_a(p_77191_0_, SpawnReason.CHUNK_GENERATION) && mobentity.func_205019_a(p_77191_0_)) { ilivingentitydata = mobentity.func_213386_a(p_77191_0_, p_77191_0_.func_175649_E(new BlockPos(mobentity)), SpawnReason.CHUNK_GENERATION, ilivingentitydata, (CompoundNBT)null); p_77191_0_.func_217376_c(mobentity); diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index 4a60e5b100..dd8f76c584 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -52,6 +52,7 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.MobEntity; +import net.minecraft.entity.SpawnReason; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.item.minecart.ContainerMinecartEntity; @@ -1052,8 +1053,8 @@ public class ForgeHooks return event.getVanillaNoteId(); } - public static int canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, AbstractSpawner spawner) { - Result res = ForgeEventFactory.canEntitySpawn(entity, world, x, y, z, null); + public static int canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, AbstractSpawner spawner, SpawnReason spawnReason) { + Result res = ForgeEventFactory.canEntitySpawn(entity, world, x, y, z, null, spawnReason); return res == Result.DEFAULT ? 0 : res == Result.DENY ? -1 : 1; } diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index f47e845041..99bddd3653 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -175,27 +175,27 @@ public class ForgeEventFactory MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, stack, hand)); } - public static Result canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, AbstractSpawner spawner) + public static Result canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, AbstractSpawner spawner, SpawnReason spawnReason) { if (entity == null) return Result.DEFAULT; - LivingSpawnEvent.CheckSpawn event = new LivingSpawnEvent.CheckSpawn(entity, world, x, y, z, spawner); + LivingSpawnEvent.CheckSpawn event = new LivingSpawnEvent.CheckSpawn(entity, world, x, y, z, spawner, spawnReason); MinecraftForge.EVENT_BUS.post(event); return event.getResult(); } public static boolean canEntitySpawnSpawner(MobEntity entity, World world, float x, float y, float z, AbstractSpawner spawner) { - Result result = canEntitySpawn(entity, world, x, y, z, spawner); + Result result = canEntitySpawn(entity, world, x, y, z, spawner, SpawnReason.SPAWNER); if (result == Result.DEFAULT) return entity.canSpawn(world, SpawnReason.SPAWNER) || !entity.isNotColliding(world); // vanilla logic else return result == Result.ALLOW; } - public static boolean doSpecialSpawn(MobEntity entity, World world, float x, float y, float z, AbstractSpawner spawner) + public static boolean doSpecialSpawn(MobEntity entity, World world, float x, float y, float z, AbstractSpawner spawner, SpawnReason spawnReason) { - return MinecraftForge.EVENT_BUS.post(new LivingSpawnEvent.SpecialSpawn(entity, world, x, y, z, spawner)); + return MinecraftForge.EVENT_BUS.post(new LivingSpawnEvent.SpecialSpawn(entity, world, x, y, z, spawner, spawnReason)); } public static Result canEntityDespawn(MobEntity entity) diff --git a/src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java b/src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java index b514813f12..c3a56c0e0f 100644 --- a/src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java +++ b/src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java @@ -25,6 +25,7 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.ForgeEventFactory; import net.minecraftforge.eventbus.api.Event; import net.minecraft.entity.MobEntity; +import net.minecraft.entity.SpawnReason; import net.minecraft.world.IWorld; import net.minecraft.world.World; @@ -76,6 +77,7 @@ public class LivingSpawnEvent extends LivingEvent { @Nullable private final AbstractSpawner spawner; + private final SpawnReason spawnReason; /** * CheckSpawn is fired when an Entity is about to be spawned. @@ -87,10 +89,11 @@ public class LivingSpawnEvent extends LivingEvent * @param spawner position of the MobSpawner * null if it this spawn is coming from a WorldSpawner */ - public CheckSpawn(MobEntity entity, IWorld world, double x, double y, double z, @Nullable AbstractSpawner spawner) + public CheckSpawn(MobEntity entity, IWorld world, double x, double y, double z, @Nullable AbstractSpawner spawner, SpawnReason spawnReason) { super(entity, world, x, y, z); this.spawner = spawner; + this.spawnReason = spawnReason; } public boolean isSpawner() @@ -103,6 +106,11 @@ public class LivingSpawnEvent extends LivingEvent { return spawner; } + + public SpawnReason getSpawnReason() + { + return spawnReason; + } } /** @@ -123,14 +131,16 @@ public class LivingSpawnEvent extends LivingEvent { @Nullable private final AbstractSpawner spawner; + private final SpawnReason spawnReason; /** * @param spawner the position of a tileentity or approximate position of an entity that initiated the spawn if any */ - public SpecialSpawn(MobEntity entity, World world, double x, double y, double z, @Nullable AbstractSpawner spawner) + public SpecialSpawn(MobEntity entity, World world, double x, double y, double z, @Nullable AbstractSpawner spawner, SpawnReason spawnReason) { super(entity, world, x, y, z); this.spawner = spawner; + this.spawnReason = spawnReason; } @Nullable @@ -138,6 +148,11 @@ public class LivingSpawnEvent extends LivingEvent { return spawner; } + + public SpawnReason getSpawnReason() + { + return spawnReason; + } } /**