mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
[1.19.4] Revamp and fix spawn events (#9133)
This commit is contained in:
1 parent
b33efc0727
commit
2815e69407
18 files changed
+558
-320
No files matched your search
@@ -1093,11 +1093,6 @@ public class ForgeHooks
|
||||
return event.getVanillaNoteId();
|
||||
}
|
||||
|
||||
public static int canEntitySpawn(Mob entity, LevelAccessor world, double x, double y, double z, BaseSpawner spawner, MobSpawnType spawnReason) {
|
||||
Result res = ForgeEventFactory.canEntitySpawn(entity, world, x, y, z, null, spawnReason);
|
||||
return res == Result.DEFAULT ? 0 : res == Result.DENY ? -1 : 1;
|
||||
}
|
||||
|
||||
public static boolean hasNoElements(Ingredient ingredient)
|
||||
{
|
||||
ItemStack[] items = ingredient.getItems();
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package net.minecraftforge.common;
|
||||
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.item.ItemEntity;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
@@ -135,5 +136,14 @@ public class ForgeInternalHandler
|
||||
event.addListener(TierSortingRegistry.getReloadListener());
|
||||
event.addListener(CreativeModeTabRegistry.getReloadListener());
|
||||
}
|
||||
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public void builtinMobSpawnBlocker(EntityJoinLevelEvent event)
|
||||
{
|
||||
if(event.getEntity() instanceof Mob mob && mob.isSpawnCancelled())
|
||||
{
|
||||
event.setCanceled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import net.minecraft.server.players.PlayerList;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.util.random.WeightedRandomList;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.DifficultyInstance;
|
||||
import net.minecraft.world.entity.MobCategory;
|
||||
import net.minecraft.world.entity.projectile.Projectile;
|
||||
import net.minecraft.world.entity.projectile.ThrownEnderpearl;
|
||||
@@ -38,6 +39,7 @@ import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.Pose;
|
||||
import net.minecraft.world.entity.SpawnGroupData;
|
||||
import net.minecraft.world.entity.EntityDimensions;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
@@ -61,6 +63,7 @@ import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
@@ -73,6 +76,7 @@ import net.minecraft.world.level.Explosion;
|
||||
import net.minecraft.world.level.GameRules;
|
||||
import net.minecraft.world.level.LevelAccessor;
|
||||
import net.minecraft.world.level.LevelReader;
|
||||
import net.minecraft.world.level.ServerLevelAccessor;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.storage.ServerLevelData;
|
||||
@@ -99,8 +103,8 @@ import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingExperienceDropEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingHealEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingPackSizeEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingSpawnEvent.AllowDespawn;
|
||||
import net.minecraftforge.event.entity.living.MobSpawnEvent;
|
||||
import net.minecraftforge.event.entity.living.MobSpawnEvent.AllowDespawn;
|
||||
import net.minecraftforge.event.entity.living.ZombieEvent.SummonAidEvent;
|
||||
import net.minecraftforge.event.entity.player.AdvancementEvent.AdvancementEarnEvent;
|
||||
import net.minecraftforge.event.entity.player.AdvancementEvent.AdvancementProgressEvent;
|
||||
@@ -185,23 +189,46 @@ public class ForgeEventFactory
|
||||
MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, stack, hand));
|
||||
}
|
||||
|
||||
public static Result canEntitySpawn(Mob entity, LevelAccessor level, double x, double y, double z, BaseSpawner spawner, MobSpawnType spawnReason)
|
||||
/**
|
||||
* Vanilla calls to {@link Mob#finalizeSpawn} are replaced with calls to this method via coremod.<br>
|
||||
* Mods should call this method in place of calling {@link Mob#finalizeSpawn}. Super calls (from within overrides) should not be wrapped.
|
||||
* <p>
|
||||
* Returns the SpawnGroupData from this event, or null if it was canceled.
|
||||
* @see MobSpawnEvent.FinalizeSpawn
|
||||
* @see Mob#finalizeSpawn(ServerLevelAccessor, DifficultyInstance, MobSpawnType, SpawnGroupData, CompoundTag)
|
||||
* @implNote Changes to the signature of this method must be reflected in the method redirector coremod.
|
||||
*/
|
||||
@Nullable
|
||||
public static SpawnGroupData onFinalizeSpawn(Mob mob, ServerLevelAccessor level, DifficultyInstance difficulty, MobSpawnType spawnType, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag spawnTag)
|
||||
{
|
||||
if (entity == null)
|
||||
return Result.DEFAULT;
|
||||
LivingSpawnEvent.CheckSpawn event = new LivingSpawnEvent.CheckSpawn(entity, level, x, y, z, spawner, spawnReason);
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
return event.getResult();
|
||||
var event = new MobSpawnEvent.FinalizeSpawn(mob, level, mob.getX(), mob.getY(), mob.getZ(), difficulty, spawnType, spawnData, spawnTag, null);
|
||||
boolean cancel = MinecraftForge.EVENT_BUS.post(event);
|
||||
|
||||
if (!cancel)
|
||||
{
|
||||
mob.finalizeSpawn(level, event.getDifficulty(), event.getSpawnType(), event.getSpawnData(), event.getSpawnTag());
|
||||
}
|
||||
|
||||
return cancel ? null : event.getSpawnData();
|
||||
}
|
||||
|
||||
public static boolean doSpecialSpawn(Mob entity, LevelAccessor level, float x, float y, float z, BaseSpawner spawner, MobSpawnType spawnReason)
|
||||
/**
|
||||
* Returns the FinalizeSpawn event instance, or null if it was canceled.<br>
|
||||
* This is separate since mob spawners perform special finalizeSpawn handling when NBT data is present, but we still want to fire the event.<br>
|
||||
* This overload is also the only way to pass through a {@link BaseSpawner} instance.
|
||||
* @see MobSpawnEvent.FinalizeSpawn
|
||||
*/
|
||||
@Nullable
|
||||
public static MobSpawnEvent.FinalizeSpawn onFinalizeSpawnSpawner(Mob mob, ServerLevelAccessor level, DifficultyInstance difficulty, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag spawnTag, BaseSpawner spawner)
|
||||
{
|
||||
return MinecraftForge.EVENT_BUS.post(new LivingSpawnEvent.SpecialSpawn(entity, level, x, y, z, spawner, spawnReason));
|
||||
var event = new MobSpawnEvent.FinalizeSpawn(mob, level, mob.getX(), mob.getY(), mob.getZ(), difficulty, MobSpawnType.SPAWNER, spawnData, spawnTag, spawner);
|
||||
boolean cancel = MinecraftForge.EVENT_BUS.post(event);
|
||||
return cancel ? null : event;
|
||||
}
|
||||
|
||||
public static Result canEntityDespawn(Mob entity)
|
||||
public static Result canEntityDespawn(Mob entity, ServerLevelAccessor level)
|
||||
{
|
||||
AllowDespawn event = new AllowDespawn(entity);
|
||||
AllowDespawn event = new AllowDespawn(entity, level);
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
return event.getResult();
|
||||
}
|
||||
|
||||
@@ -1,186 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.event.entity.living;
|
||||
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.MobSpawnType;
|
||||
import net.minecraft.world.level.BaseSpawner;
|
||||
import net.minecraft.world.level.LevelAccessor;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.api.Cancelable;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* This event is fired whenever a {@link Mob} should do something spawn-related.
|
||||
* <p>
|
||||
* This event is not {@linkplain Cancelable cancellable} and does not {@linkplain HasResult have a result}.
|
||||
* <p>
|
||||
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.
|
||||
*/
|
||||
public class LivingSpawnEvent extends LivingEvent
|
||||
{
|
||||
private final LevelAccessor level;
|
||||
private final Mob mob;
|
||||
private final double x;
|
||||
private final double y;
|
||||
private final double z;
|
||||
|
||||
public LivingSpawnEvent(Mob mob, LevelAccessor level, double x, double y, double z)
|
||||
{
|
||||
super(mob);
|
||||
this.mob = mob;
|
||||
this.level = level;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the mob that is causing a spawn action}
|
||||
*/
|
||||
@Override
|
||||
public Mob getEntity()
|
||||
{
|
||||
return this.mob;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the level relating to the mob spawn action}
|
||||
*/
|
||||
public LevelAccessor getLevel()
|
||||
{
|
||||
return this.level;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the x-coordinate relating to the mob spawn action}
|
||||
*/
|
||||
public double getX()
|
||||
{
|
||||
return this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the y-coordinate relating to the mob spawn action}
|
||||
*/
|
||||
public double getY()
|
||||
{
|
||||
return this.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the z-coordinate relating to the mob spawn action}
|
||||
*/
|
||||
public double getZ()
|
||||
{
|
||||
return this.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is fired before a {@link Mob} spawns.
|
||||
* <p>
|
||||
* This event is not {@linkplain Cancelable cancellable}, but does {@linkplain HasResult have a result}.
|
||||
* {@linkplain Result#DEFAULT DEFAULT} indicates that default spawn rules should be used.
|
||||
* {@linkplain Result#ALLOW ALLOW} indicates that the mob should spawn regardless of default spawn rules.
|
||||
* {@linkplain Result#DENY DENY} indicates that the mob should not spawn.
|
||||
* <p>
|
||||
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.
|
||||
*/
|
||||
@HasResult
|
||||
public static class CheckSpawn extends LivingSpawnEvent
|
||||
{
|
||||
@Nullable
|
||||
private final BaseSpawner spawner;
|
||||
private final MobSpawnType spawnReason;
|
||||
|
||||
public CheckSpawn(Mob mob, LevelAccessor level, double x, double y, double z, @Nullable BaseSpawner spawner, MobSpawnType spawnReason)
|
||||
{
|
||||
super(mob, level, x, y, z);
|
||||
this.spawner = spawner;
|
||||
this.spawnReason = spawnReason;
|
||||
}
|
||||
|
||||
public boolean isSpawner()
|
||||
{
|
||||
return spawner != null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BaseSpawner getSpawner()
|
||||
{
|
||||
return spawner;
|
||||
}
|
||||
|
||||
public MobSpawnType getSpawnReason()
|
||||
{
|
||||
return spawnReason;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is fired whenever a {@link Mob} is set to be spawned, to allow for mod-specific initializers.
|
||||
* <p>
|
||||
* This event is {@linkplain Cancelable cancellable} and does not {@linkplain HasResult have a result}.
|
||||
* If the event is canceled, the mob will not spawn.
|
||||
* <p>
|
||||
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.
|
||||
*/
|
||||
@Cancelable
|
||||
public static class SpecialSpawn extends LivingSpawnEvent
|
||||
{
|
||||
@Nullable
|
||||
private final BaseSpawner spawner;
|
||||
private final MobSpawnType spawnReason;
|
||||
|
||||
public SpecialSpawn(Mob entity, LevelAccessor level, double x, double y, double z, @Nullable BaseSpawner spawner, MobSpawnType spawnReason)
|
||||
{
|
||||
super(entity, level, x, y, z);
|
||||
this.spawner = spawner;
|
||||
this.spawnReason = spawnReason;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BaseSpawner getSpawner()
|
||||
{
|
||||
return spawner;
|
||||
}
|
||||
|
||||
public MobSpawnType getSpawnReason()
|
||||
{
|
||||
return spawnReason;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is fired for a {@link Mob} that can despawn <i>each mob tick</i>.
|
||||
* This event only fires if a mob can be allowed to despawn and will not
|
||||
* otherwise fire if a despawn is certain.
|
||||
* <p>
|
||||
* This event is not {@linkplain Cancelable cancellable}, but does {@linkplain HasResult have a result}.
|
||||
* {@link Result#DEFAULT} indicates that default despawn mechanics should be used.
|
||||
* {@link Result#ALLOW} indicates that the mob should forcefully despawn.
|
||||
* {@link Result#DENY} indicates that the mob should forcefully stay spawned.
|
||||
* <p>
|
||||
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.
|
||||
*
|
||||
* @see LivingEntity#checkDespawn()
|
||||
* @author cpw
|
||||
*/
|
||||
@HasResult
|
||||
public static class AllowDespawn extends LivingSpawnEvent
|
||||
{
|
||||
public AllowDespawn(Mob mob)
|
||||
{
|
||||
super(mob, mob.level, mob.getX(), mob.getY(), mob.getZ());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.event.entity.living;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.DifficultyInstance;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.world.entity.MobSpawnType;
|
||||
import net.minecraft.world.entity.SpawnGroupData;
|
||||
import net.minecraft.world.level.BaseSpawner;
|
||||
import net.minecraft.world.level.ServerLevelAccessor;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.ForgeEventFactory;
|
||||
import net.minecraftforge.event.entity.EntityEvent;
|
||||
import net.minecraftforge.eventbus.api.Cancelable;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
|
||||
/**
|
||||
* The subclasses of this event are fired whenever a mob performs a spawning-related action.
|
||||
*/
|
||||
public abstract class MobSpawnEvent extends EntityEvent
|
||||
{
|
||||
private final ServerLevelAccessor level;
|
||||
private final double x;
|
||||
private final double y;
|
||||
private final double z;
|
||||
|
||||
/**
|
||||
* @apiNote Do not construct directly. Access via {@link ForgeEventFactory#onFinalizeSpawn}.
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
public MobSpawnEvent(Mob mob, ServerLevelAccessor level, double x, double y, double z)
|
||||
{
|
||||
super(mob);
|
||||
this.level = level;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mob getEntity()
|
||||
{
|
||||
return (Mob) super.getEntity();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The level relating to the mob spawn action
|
||||
*/
|
||||
public ServerLevelAccessor getLevel()
|
||||
{
|
||||
return this.level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The x-coordinate relating to the mob spawn action
|
||||
*/
|
||||
public double getX()
|
||||
{
|
||||
return this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The y-coordinate relating to the mob spawn action
|
||||
*/
|
||||
public double getY()
|
||||
{
|
||||
return this.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The z-coordinate relating to the mob spawn action
|
||||
*/
|
||||
public double getZ()
|
||||
{
|
||||
return this.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is fired before {@link Mob#finalizeSpawn(ServerLevelAccessor, DifficultyInstance, MobSpawnType, SpawnGroupData, CompoundTag)} is called.<br>
|
||||
* This allows mods to control mob initialization.<br>
|
||||
* In vanilla code, this event is injected by a transformer and not via patch, so calls cannot be traced via call hierarchy.
|
||||
* <p>
|
||||
* Canceling this event will result in {@link Mob#finalizeSpawn} not being called, and the returned value always being null, instead of propagating the SpawnGroupData.<br>
|
||||
* The entity will still be spawned. If you want to prevent the spawn, use {@link FinalizeSpawn#cancelSpawn()}
|
||||
* <p>
|
||||
* This event is fired on {@link MinecraftForge#EVENT_BUS}, and is only fired on the logical server.
|
||||
*/
|
||||
@Cancelable
|
||||
public static class FinalizeSpawn extends MobSpawnEvent
|
||||
{
|
||||
private final MobSpawnType spawnType;
|
||||
@Nullable private final BaseSpawner spawner;
|
||||
|
||||
private DifficultyInstance difficulty;
|
||||
@Nullable private SpawnGroupData spawnData;
|
||||
@Nullable private CompoundTag spawnTag;
|
||||
|
||||
public FinalizeSpawn(Mob entity, ServerLevelAccessor level, double x, double y, double z, DifficultyInstance difficulty, MobSpawnType spawnType, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag spawnTag, @Nullable BaseSpawner spawner)
|
||||
{
|
||||
super(entity, level, x, y, z);
|
||||
this.difficulty = difficulty;
|
||||
this.spawnType = spawnType;
|
||||
this.spawnData = spawnData;
|
||||
this.spawnTag = spawnTag;
|
||||
this.spawner = spawner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the {@link DifficultyInstance} for the chunk where the mob is about to be spawned.
|
||||
* @return The local difficulty instance
|
||||
*/
|
||||
public DifficultyInstance getDifficulty()
|
||||
{
|
||||
return this.difficulty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the difficulty instance for this event, which will be propagated to {@link Mob#finalizeSpawn} unless cancelled.
|
||||
* The difficulty instance controls how likely certain random effects are to occur, or if certain mob abilities are enabled.
|
||||
* @param inst The new difficulty instance.
|
||||
*/
|
||||
public void setDifficulty(DifficultyInstance inst)
|
||||
{
|
||||
this.difficulty = inst;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the type of mob spawn that happened (the event that caused the spawn). The enum names are self-explanatory.
|
||||
* @return The mob spawn type.
|
||||
* @see MobSpawnType
|
||||
*/
|
||||
public MobSpawnType getSpawnType()
|
||||
{
|
||||
return this.spawnType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the {@link SpawnGroupData} for 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.
|
||||
* @return The spawn group data.
|
||||
*/
|
||||
@Nullable
|
||||
public SpawnGroupData getSpawnData()
|
||||
{
|
||||
return this.spawnData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the spawn data for this entity. If this event is cancelled, this value is not used, since {@link Mob#finalizeSpawn} will not be called.
|
||||
* @param data The new spawn data
|
||||
* @see FinalizeSpawn#getSpawnData
|
||||
*/
|
||||
public void setSpawnData(@Nullable SpawnGroupData data)
|
||||
{
|
||||
this.spawnData = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 via {@link FinalizeSpawn#setSpawnTag}
|
||||
* @return The spawn data this entity was or will be loaded from, if any.
|
||||
*/
|
||||
@Nullable
|
||||
public CompoundTag getSpawnTag()
|
||||
{
|
||||
return this.spawnTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the spawn data for this event, which will be propagated to {@link Mob#finalizeSpawn} 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.
|
||||
* @param tag The new spawn tag
|
||||
*/
|
||||
public void setSpawnTag(@Nullable CompoundTag tag)
|
||||
{
|
||||
this.spawnTag = tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the underlying {@link BaseSpawner} instance if this mob was created by a Mob Spawner of some form.
|
||||
* This is always null unless {@link #getSpawnType()} is {@link MobSpawnType#SPAWNER}, and may still be null even then.
|
||||
* @return The BaseSpawner responsible for triggering the spawn, or null if none is available.
|
||||
*/
|
||||
@Nullable
|
||||
public BaseSpawner getSpawner()
|
||||
{
|
||||
return spawner;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method can be used to cancel the spawn of this mob.<p>
|
||||
* This method must be used if you want to block the spawn, as canceling the event only blocks the call to {@link Mob#finalizeSpawn}.<p>
|
||||
* Note that if the spawn is cancelled, but the event is not, then {@link Mob#finalizeSpawn} will still be called, but the entity will not be spawned.
|
||||
* Usually that has no side effects, but callers should be aware.
|
||||
* @param cancel If the spawn should be cancelled (or not).
|
||||
*/
|
||||
public void setSpawnCancelled(boolean cancel) {
|
||||
this.getEntity().setSpawnCancelled(cancel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current spawn cancellation status, which can be changed via {@link FinalizeSpawn#setSpawnCancelled(boolean)}.
|
||||
* @return If this mob's spawn is cancelled or not.
|
||||
*/
|
||||
public boolean isSpawnCancelled() {
|
||||
return this.getEntity().isSpawnCancelled();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is fired from {@link Mob#checkDespawn()}.<br>
|
||||
* It fires once per tick per mob that is attempting to despawn.<br>
|
||||
* It is not fired if the mob is persistent (meaning it may not despawn).<br>
|
||||
* <p>
|
||||
* This event is not {@linkplain Cancelable cancellable}, but does {@linkplain HasResult have a result}.
|
||||
* {@link Result#DEFAULT} indicates that default despawn mechanics should be used.
|
||||
* {@link Result#ALLOW} indicates that the mob should forcefully despawn.
|
||||
* {@link Result#DENY} indicates that the mob should forcefully stay spawned.
|
||||
* <p>
|
||||
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.
|
||||
*
|
||||
* @see LivingEntity#checkDespawn()
|
||||
* @author cpw
|
||||
*/
|
||||
@HasResult
|
||||
public static class AllowDespawn extends MobSpawnEvent
|
||||
{
|
||||
public AllowDespawn(Mob mob, ServerLevelAccessor level)
|
||||
{
|
||||
super(mob, level, mob.getX(), mob.getY(), mob.getZ());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user