Add spawn reason to spawn events (#6096)
This commit is contained in:
parent
c09fe57031
commit
b5b43a5a36
5 changed files with 29 additions and 13 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue