Class MobSpawnEvent.FinalizeSpawn
- Enclosing class:
- MobSpawnEvent
Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag) is called.This allows mods to control mob initialization.
In vanilla code, this event is injected by a transformer and not via patch, so calls cannot be traced via call hierarchy (it is not source-visible).
Canceling this event will result in Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag) not being called, and the returned value always being null, instead of propagating the SpawnGroupData.
The entity will still be spawned. If you want to prevent the spawn, use setSpawnCancelled(boolean), which will cause Forge to prevent the spawn.
This event is fired on MinecraftForge.EVENT_BUS, and is only fired on the logical server.
- See Also:
- API Note:
- Callers do not need to check if the entity's spawn was cancelled, as the spawn will be blocked by Forge.
-
Nested Class Summary
Nested classes/interfaces inherited from class net.minecraftforge.event.entity.living.MobSpawnEvent
MobSpawnEvent.AllowDespawn, MobSpawnEvent.FinalizeSpawn, MobSpawnEvent.PositionCheck, MobSpawnEvent.SpawnPlacementCheckNested classes/interfaces inherited from class net.minecraftforge.event.entity.EntityEvent
EntityEvent.EnteringSection, EntityEvent.EntityConstructingNested classes/interfaces inherited from class net.minecraftforge.eventbus.api.Event
net.minecraftforge.eventbus.api.Event.HasResult, net.minecraftforge.eventbus.api.Event.Result -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate DifficultyInstanceprivate @Nullable SpawnGroupDataprivate final @Nullable BaseSpawnerprivate @Nullable CompoundTagprivate final MobSpawnType -
Constructor Summary
ConstructorsConstructorDescriptionFinalizeSpawn(Mob entity, ServerLevelAccessor level, double x, double y, double z, DifficultyInstance difficulty, MobSpawnType spawnType, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag spawnTag, @Nullable BaseSpawner spawner) -
Method Summary
Modifier and TypeMethodDescriptionRetrieves theDifficultyInstancefor the chunk where the mob is about to be spawned.@Nullable SpawnGroupDataRetrieves theSpawnGroupDatafor this entity.@Nullable BaseSpawnerRetrieves the underlyingBaseSpawnerinstance if this mob was created by a Mob Spawner of some form.@Nullable CompoundTagThis is the NBT data the entity was loaded from, if applicable.Retrieves the type of mob spawn that happened (the event that caused the spawn).booleanReturns the current spawn cancellation status, which can be changed viasetSpawnCancelled(boolean).voidSets the difficulty instance for this event, which will be propagated toMob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag)unless cancelled.voidsetSpawnCancelled(boolean cancel) This method can be used to cancel the spawn of this mob.voidsetSpawnData(@Nullable SpawnGroupData data) Sets the spawn data for this entity.voidsetSpawnTag(@Nullable CompoundTag tag) Sets the spawn data for this event, which will be propagated toMob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag)unless cancelled.Methods inherited from class net.minecraftforge.event.entity.living.MobSpawnEvent
getEntity, getLevel, getX, getY, getZMethods inherited from class net.minecraftforge.eventbus.api.Event
getListenerList, getPhase, getResult, hasResult, isCancelable, isCanceled, setCanceled, setPhase, setResult
-
Field Details
-
spawnType
-
spawner
-
difficulty
-
spawnData
-
spawnTag
-
-
Constructor Details
-
FinalizeSpawn
@Internal public FinalizeSpawn(Mob entity, ServerLevelAccessor level, double x, double y, double z, DifficultyInstance difficulty, MobSpawnType spawnType, @Nullable @Nullable SpawnGroupData spawnData, @Nullable @Nullable CompoundTag spawnTag, @Nullable @Nullable BaseSpawner spawner) - API Note:
- Do not construct directly. Access via
ForgeEventFactory.onFinalizeSpawn(net.minecraft.world.entity.Mob, net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag)/ForgeEventFactory.onFinalizeSpawnSpawner(net.minecraft.world.entity.Mob, net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag, net.minecraft.world.level.BaseSpawner).
-
-
Method Details
-
getDifficulty
Retrieves theDifficultyInstancefor the chunk where the mob is about to be spawned.- Returns:
- The local difficulty instance
-
setDifficulty
Sets the difficulty instance for this event, which will be propagated toMob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag)unless cancelled. The difficulty instance controls how likely certain random effects are to occur, or if certain mob abilities are enabled.- Parameters:
inst- The new difficulty instance.
-
getSpawnType
Retrieves the type of mob spawn that happened (the event that caused the spawn). The enum names are self-explanatory.- Returns:
- The mob spawn type.
- See Also:
-
getSpawnData
Retrieves theSpawnGroupDatafor this entity. When spawning mobs in a loop, this group data is used for the entire group and impacts future spawns. This is how entities like horses ensure that the whole group spawns as a single variant. How this is used varies on a per-entity basis.- Returns:
- The spawn group data.
-
setSpawnData
Sets the spawn data for this entity. If this event is cancelled, this value is not used, sinceMob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag)will not be called.- Parameters:
data- The new spawn data- See Also:
-
getSpawnTag
This is the NBT data the entity was loaded from, if applicable. It is unknown if the entity has already been loaded from this data, or if it will be loaded later. Callers should not modify this data. If you need to change the data, you can create a copy, modify it, and set it viasetSpawnTag(net.minecraft.nbt.CompoundTag)- Returns:
- The spawn data this entity was or will be loaded from, if any.
-
setSpawnTag
Sets the spawn data for this event, which will be propagated toMob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag)unless cancelled. The only vanilla mob known to use this tag for anything in finalize is tropical fish for setting the variant when spawned via bucket.- Parameters:
tag- The new spawn tag
-
getSpawner
Retrieves the underlyingBaseSpawnerinstance if this mob was created by a Mob Spawner of some form. This is always null unlessgetSpawnType()isMobSpawnType.SPAWNER, and may still be null even then.- Returns:
- The BaseSpawner responsible for triggering the spawn, or null if none is available.
-
setSpawnCancelled
public void setSpawnCancelled(boolean cancel) This method can be used to cancel the spawn of this mob.This method must be used if you want to block the spawn, as canceling the event only blocks the call to
Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag).Note that if the spawn is cancelled, but the event is not, then
Mob.finalizeSpawn(net.minecraft.world.level.ServerLevelAccessor, net.minecraft.world.DifficultyInstance, net.minecraft.world.entity.MobSpawnType, net.minecraft.world.entity.SpawnGroupData, net.minecraft.nbt.CompoundTag)will still be called, but the entity will not be spawned. Usually that has no side effects, but callers should be aware.- Parameters:
cancel- If the spawn should be cancelled (or not).
-
isSpawnCancelled
public boolean isSpawnCancelled()Returns the current spawn cancellation status, which can be changed viasetSpawnCancelled(boolean).- Returns:
- If this mob's spawn is cancelled or not.
- Implementation Note:
- This is enforced in
ForgeInternalHandler.builtinMobSpawnBlocker(net.minecraftforge.event.entity.EntityJoinLevelEvent)and a patch inWorldGenRegion#addEntity
-