1.21.6 - New EventBus 7

A full rewrite and redesign of the library that offers much better performance, new features and fixes all previous design flaws
See the migration guide here: https://gist.github.com/PaintNinja/ad82c224aecee25efac1ea3e2cf19b91

Co-authored-by: Paint_Ninja <[email protected]>
This commit is contained in:
LexManosandPaint_Ninja committed 2025-06-18 02:53:58 -07:00
1 parent a02f3f9841
commit 5e8942e0a7
642 files changed
+7258 -6187

No files matched your search

@@ -11,8 +11,8 @@ import net.minecraft.world.level.LevelSimulatedReader;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.feature.treedecorators.TreeDecorator;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import org.jetbrains.annotations.ApiStatus;
/**
@@ -24,7 +24,9 @@ import org.jetbrains.annotations.ApiStatus;
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}
* only on the {@linkplain net.minecraftforge.fml.LogicalSide#SERVER logical server}.
*/
public final class AlterGroundEvent extends Event {
public final class AlterGroundEvent extends MutableEvent {
public static final EventBus<AlterGroundEvent> BUS = EventBus.create(AlterGroundEvent.class);
private final LevelSimulatedReader level;
private final RandomSource random;
private final BlockPos pos;
@@ -27,14 +27,22 @@ import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.ToolAction;
import net.minecraftforge.common.ToolActions;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.common.util.HasResult;
import net.minecraftforge.common.util.Result;
import com.google.common.collect.ImmutableList;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.InheritableEvent;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.eventbus.api.event.RecordEvent;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class BlockEvent extends Event {
public class BlockEvent extends MutableEvent implements InheritableEvent {
public static final EventBus<BlockEvent> BUS = EventBus.create(BlockEvent.class);
private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("forge.debugBlockEvent", "false"));
private final LevelAccessor level;
@@ -61,17 +69,20 @@ public class BlockEvent extends Event {
/**
* Event that is fired when an Block is about to be broken by a player
* Canceling this event will prevent the Block from being broken.
* Setting the result to {@link Result#DENY} will prevent the Block from being broken.
*/
@Cancelable
public static final class BreakEvent extends BlockEvent {
public static final class BreakEvent extends BlockEvent implements Cancellable, HasResult {
public static final CancellableEventBus<BreakEvent> BUS = CancellableEventBus.create(BreakEvent.class);
/** Reference to the Player who broke the block. If no player is available, use a EntityFakePlayer */
private final Player player;
private int exp;
private Result result;
public BreakEvent(Level level, BlockPos pos, BlockState state, Player player) {
public BreakEvent(Level level, BlockPos pos, BlockState state, Player player, Result result) {
super(level, pos, state);
this.player = player;
this.result = result;
if (state == null || !ForgeHooks.isCorrectToolForDrops(state, player)) { // Handle empty block or player unable to break block scenario
this.exp = 0;
@@ -91,10 +102,10 @@ public class BlockEvent extends Event {
/**
* Get the experience dropped by the block after the event has processed
*
* @return The experience to drop or 0 if the event was canceled
* @return The experience to drop or 0 if the event was denied
*/
public int getExpToDrop() {
return this.isCanceled() ? 0 : exp;
return this.getResult().isDenied() ? 0 : exp;
}
/**
@@ -105,6 +116,16 @@ public class BlockEvent extends Event {
public void setExpToDrop(int exp) {
this.exp = exp;
}
@Override
public Result getResult() {
return this.result;
}
@Override
public void setResult(Result result) {
this.result = result;
}
}
/**
@@ -112,8 +133,9 @@ public class BlockEvent extends Event {
*
* If a Block Place event is cancelled, the block will not be placed.
*/
@Cancelable
public static sealed class EntityPlaceEvent extends BlockEvent {
public static sealed class EntityPlaceEvent extends BlockEvent implements Cancellable {
public static final CancellableEventBus<EntityPlaceEvent> BUS = CancellableEventBus.create(EntityPlaceEvent.class);
private final Entity entity;
private final BlockSnapshot blockSnapshot;
private final BlockState placedBlock;
@@ -145,8 +167,9 @@ public class BlockEvent extends Event {
* the placed block would exist if the placement only affected a single
* block.
*/
@Cancelable
public static final class EntityMultiPlaceEvent extends EntityPlaceEvent {
public static final class EntityMultiPlaceEvent extends EntityPlaceEvent implements Cancellable {
public static final CancellableEventBus<EntityMultiPlaceEvent> BUS = CancellableEventBus.create(EntityMultiPlaceEvent.class);
private final List<BlockSnapshot> blockSnapshots;
public EntityMultiPlaceEvent(@NotNull List<BlockSnapshot> blockSnapshots, @NotNull BlockState placedAgainst, @Nullable Entity entity) {
@@ -173,8 +196,9 @@ public class BlockEvent extends Event {
* a way for mods to detect physics updates, in the same way a BUD switch
* does. This event is only called on the server.
*/
@Cancelable
public static final class NeighborNotifyEvent extends BlockEvent {
public static final class NeighborNotifyEvent extends BlockEvent implements Cancellable {
public static final CancellableEventBus<NeighborNotifyEvent> BUS = CancellableEventBus.create(NeighborNotifyEvent.class);
private final EnumSet<Direction> notifiedSides;
private final boolean forceRedstoneUpdate;
@@ -208,28 +232,11 @@ public class BlockEvent extends Event {
* usually doesn't do that (like lava), and a result of DENY prevents creation
* even if the liquid usually does do that (like water).
*/
@HasResult
public static final class CreateFluidSourceEvent extends Event {
private final Level level;
private final BlockPos pos;
private final BlockState state;
public record CreateFluidSourceEvent(Level getLevel, BlockPos getPos, BlockState getState, Result.Holder resultHolder) implements RecordEvent, HasResult.Record {
public static final EventBus<CreateFluidSourceEvent> BUS = EventBus.create(CreateFluidSourceEvent.class);
public CreateFluidSourceEvent(Level level, BlockPos pos, BlockState state) {
this.level = level;
this.pos = pos;
this.state = state;
}
public Level getLevel() {
return level;
}
public BlockPos getPos() {
return pos;
}
public BlockState getState() {
return state;
this(level, pos, state, new Result.Holder());
}
}
@@ -241,8 +248,9 @@ public class BlockEvent extends Event {
* {@link #getState()} will return the block that was originally going to be placed.
* {@link #getPos()} will return the position of the block to be changed.
*/
@Cancelable
public static final class FluidPlaceBlockEvent extends BlockEvent {
public static final class FluidPlaceBlockEvent extends BlockEvent implements Cancellable {
public static final CancellableEventBus<FluidPlaceBlockEvent> BUS = CancellableEventBus.create(FluidPlaceBlockEvent.class);
private final BlockPos liquidPos;
private BlockState newState;
private final BlockState origState;
@@ -285,6 +293,8 @@ public class BlockEvent extends Event {
*
*/
public static sealed class CropGrowEvent extends BlockEvent {
public static final EventBus<CropGrowEvent> BUS = EventBus.create(CropGrowEvent.class);
public CropGrowEvent(Level level, BlockPos pos, BlockState state) {
super(level, pos, state);
}
@@ -300,11 +310,24 @@ public class BlockEvent extends Event {
* This event is not {@link Cancelable}.<br>
* <br>
*/
@HasResult
public static final class Pre extends CropGrowEvent {
public static final class Pre extends CropGrowEvent implements HasResult {
public static final EventBus<Pre> BUS = EventBus.create(Pre.class);
private Result result = Result.DEFAULT;
public Pre(Level level, BlockPos pos, BlockState state) {
super(level, pos, state);
}
@Override
public Result getResult() {
return result;
}
@Override
public void setResult(Result result) {
this.result = result;
}
}
/**
@@ -317,6 +340,8 @@ public class BlockEvent extends Event {
* This event does not have a result. {@link HasResult}<br>
*/
public static final class Post extends CropGrowEvent {
public static final EventBus<Post> BUS = EventBus.create(Post.class);
private final BlockState originalState;
public Post(Level level, BlockPos pos, BlockState original, BlockState state) {
@@ -334,8 +359,9 @@ public class BlockEvent extends Event {
* Fired when when farmland gets trampled
* This event is {@link Cancelable}
*/
@Cancelable
public static final class FarmlandTrampleEvent extends BlockEvent {
public static final class FarmlandTrampleEvent extends BlockEvent implements Cancellable {
public static final CancellableEventBus<FarmlandTrampleEvent> BUS = CancellableEventBus.create(FarmlandTrampleEvent.class);
private final Entity entity;
private final double fallDistance;
@@ -360,8 +386,9 @@ public class BlockEvent extends Event {
*
* If cancelled, the portal will not be spawned.
*/
@Cancelable
public static final class PortalSpawnEvent extends BlockEvent {
public static final class PortalSpawnEvent extends BlockEvent implements Cancellable {
public static final CancellableEventBus<PortalSpawnEvent> BUS = CancellableEventBus.create(PortalSpawnEvent.class);
private final PortalShape size;
public PortalSpawnEvent(LevelAccessor level, BlockPos pos, BlockState state, PortalShape size) {
@@ -384,8 +411,9 @@ public class BlockEvent extends Event {
* This event is {@link Cancelable}. If canceled, this will prevent the tool
* from changing the block's state.
*/
@Cancelable
public static final class BlockToolModificationEvent extends BlockEvent {
public static final class BlockToolModificationEvent extends BlockEvent implements Cancellable {
public static final CancellableEventBus<BlockToolModificationEvent> BUS = CancellableEventBus.create(BlockToolModificationEvent.class);
private final UseOnContext context;
private final ToolAction toolAction;
private final boolean simulate;
@@ -13,8 +13,7 @@ import net.minecraft.world.level.chunk.status.ChunkType;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.storage.SerializableChunkData;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.bus.EventBus;
/**
* ChunkDataEvent is fired when an event involving chunk data occurs.<br>
@@ -26,6 +25,8 @@ import net.minecraftforge.eventbus.api.Event;
* All children of this event are fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public sealed class ChunkDataEvent extends ChunkEvent {
public static final EventBus<ChunkDataEvent> BUS = EventBus.create(ChunkDataEvent.class);
private final SerializableChunkData data;
public ChunkDataEvent(ChunkAccess chunk, SerializableChunkData data) {
@@ -46,14 +47,10 @@ public sealed class ChunkDataEvent extends ChunkEvent {
* ChunkDataEvent.Load is fired when vanilla Minecraft attempts to load Chunk data.<br>
* This event is fired during chunk loading in
* {@link ChunkSerializer#read(ServerLevel, PoiManager, ChunkPos, SerializableChunkData)} which means it is async, so be careful.<br>
* <br>
* This event is not {@link Cancelable}.<br>
* <br>
* This event does not have a result. {@link HasResult} <br>
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public static final class Load extends ChunkDataEvent {
public static final EventBus<ChunkDataEvent.Load> BUS = EventBus.create(ChunkDataEvent.Load.class);
private final ChunkType status;
public Load(ChunkAccess chunk, SerializableChunkData data, ChunkType status) {
@@ -70,14 +67,10 @@ public sealed class ChunkDataEvent extends ChunkEvent {
* ChunkDataEvent.Save is fired when vanilla Minecraft attempts to save Chunk data.<br>
* This event is fired during chunk saving in
* {@code ChunkMap#save(ChunkAccess)}. <br>
* <br>
* This event is not {@link Cancelable}.<br>
* <br>
* This event does not have a result. {@link HasResult} <br>
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public static final class Save extends ChunkDataEvent {
public static final EventBus<ChunkDataEvent.Save> BUS = EventBus.create(ChunkDataEvent.Save.class);
public Save(ChunkAccess chunk, LevelAccessor world, SerializableChunkData data) {
super(chunk, world, data);
}
@@ -9,8 +9,7 @@ import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.bus.EventBus;
import org.jetbrains.annotations.ApiStatus;
/**
@@ -23,6 +22,8 @@ import org.jetbrains.annotations.ApiStatus;
* All children of this event are fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public class ChunkEvent extends LevelEvent {
public static final EventBus<ChunkEvent> BUS = EventBus.create(ChunkEvent.class);
private final ChunkAccess chunk;
public ChunkEvent(ChunkAccess chunk) {
@@ -53,6 +54,8 @@ public class ChunkEvent extends LevelEvent {
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public static final class Load extends ChunkEvent {
public static final EventBus<ChunkEvent.Load> BUS = EventBus.create(ChunkEvent.Load.class);
private final boolean newChunk;
@ApiStatus.Internal
@@ -85,14 +88,16 @@ public class ChunkEvent extends LevelEvent {
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public static final class Unload extends ChunkEvent {
public static final EventBus<ChunkEvent.Unload> BUS = EventBus.create(ChunkEvent.Unload.class);
public Unload(ChunkAccess chunk) {
super(chunk);
}
}
/**
* ChunkEvent.LightingCalculated is fired when MinecraftForge flags that lighting is correct in a chunk.<br>
* This event is fired during light propagation in ThreadedLevelLightEngine.CompletableFuture(), specifically upon setting
* This event is fired during light propagation in ThreadedLevelLightEngine.CompletableFuture(), specifically upon setting
* the ChunkAccess isLightCorrect to true.<br>
* <br>
* The game test for this event is lighting_event_test in net.minecraftforge.debug.chunk<br>
@@ -104,6 +109,8 @@ public class ChunkEvent extends LevelEvent {
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public static final class LightingCalculated extends ChunkEvent {
public static final EventBus<ChunkEvent.LightingCalculated> BUS = EventBus.create(ChunkEvent.LightingCalculated.class);
public LightingCalculated(ChunkAccess chunk) {
super(chunk);
}
@@ -8,8 +8,8 @@ package net.minecraftforge.event.level;
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.server.level.ServerLevel;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.fml.LogicalSide;
import org.jetbrains.annotations.Nullable;
@@ -29,8 +29,9 @@ import org.jetbrains.annotations.Nullable;
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}
* only on the {@linkplain LogicalSide#SERVER logical server}.
**/
public final class ChunkTicketLevelUpdatedEvent extends Event
{
public final class ChunkTicketLevelUpdatedEvent extends MutableEvent {
public static final EventBus<ChunkTicketLevelUpdatedEvent> BUS = EventBus.create(ChunkTicketLevelUpdatedEvent.class);
private final ServerLevel level;
private final long chunkPos;
private final int oldTicketLevel;
@@ -38,8 +39,7 @@ public final class ChunkTicketLevelUpdatedEvent extends Event
@Nullable
private final ChunkHolder chunkHolder;
public ChunkTicketLevelUpdatedEvent(ServerLevel level, long chunkPos, int oldTicketLevel, int newTicketLevel, @Nullable ChunkHolder chunkHolder)
{
public ChunkTicketLevelUpdatedEvent(ServerLevel level, long chunkPos, int oldTicketLevel, int newTicketLevel, @Nullable ChunkHolder chunkHolder) {
this.level = level;
this.chunkPos = chunkPos;
this.oldTicketLevel = oldTicketLevel;
@@ -50,32 +50,28 @@ public final class ChunkTicketLevelUpdatedEvent extends Event
/**
* {@return the server level containing the chunk}
*/
public ServerLevel getLevel()
{
public ServerLevel getLevel() {
return this.level;
}
/**
* {@return the long representation of the chunk position the ticket level changed for}
*/
public long getChunkPos()
{
public long getChunkPos() {
return this.chunkPos;
}
/**
* {@return the previous ticket level the chunk had}
*/
public int getOldTicketLevel()
{
public int getOldTicketLevel() {
return this.oldTicketLevel;
}
/**
* {@return the new ticket level for the chunk}
*/
public int getNewTicketLevel()
{
public int getNewTicketLevel() {
return this.newTicketLevel;
}
@@ -83,8 +79,7 @@ public final class ChunkTicketLevelUpdatedEvent extends Event
* {@return chunk that had its ticket level updated}
*/
@Nullable
public ChunkHolder getChunkHolder()
{
public ChunkHolder getChunkHolder() {
return this.chunkHolder;
}
}
@@ -10,8 +10,9 @@ import net.minecraft.world.level.ChunkPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.InheritableEvent;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.fml.LogicalSide;
/**
@@ -25,7 +26,9 @@ import net.minecraftforge.fml.LogicalSide;
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}
* only on the {@linkplain LogicalSide#SERVER logical server}.
**/
public sealed class ChunkWatchEvent extends Event {
public sealed class ChunkWatchEvent extends MutableEvent implements InheritableEvent {
public static final EventBus<ChunkWatchEvent> BUS = EventBus.create(ChunkWatchEvent.class);
private final ServerLevel level;
private final ServerPlayer player;
private final ChunkPos pos;
@@ -68,6 +71,8 @@ public sealed class ChunkWatchEvent extends Event {
* only on the {@linkplain LogicalSide#SERVER logical server}.
**/
public static final class Watch extends ChunkWatchEvent {
public static final EventBus<Watch> BUS = EventBus.create(Watch.class);
private final LevelChunk chunk;
public Watch(ServerPlayer player, LevelChunk chunk, ServerLevel level) {
@@ -89,6 +94,8 @@ public sealed class ChunkWatchEvent extends Event {
* only on the {@linkplain LogicalSide#SERVER logical server}.
**/
public static final class UnWatch extends ChunkWatchEvent {
public static final EventBus<UnWatch> BUS = EventBus.create(UnWatch.class);
public UnWatch(ServerPlayer player, ChunkPos pos, ServerLevel level) {
super(player, pos, level);
}
@@ -8,12 +8,15 @@ package net.minecraftforge.event.level;
import java.util.List;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.InheritableEvent;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
/** ExplosionEvent triggers when an explosion happens in the level.<br>
* <br>
@@ -25,7 +28,9 @@ import net.minecraft.world.level.Level;
* Children do not use {@link HasResult}.<br>
* Children of this event are fired on the {@link MinecraftForge#EVENT_BUS}.<br>
*/
public sealed class ExplosionEvent extends Event {
public sealed class ExplosionEvent extends MutableEvent implements InheritableEvent {
public static final EventBus<ExplosionEvent> BUS = EventBus.create(ExplosionEvent.class);
private final Level level;
private final Explosion explosion;
@@ -48,8 +53,9 @@ public sealed class ExplosionEvent extends Event {
* This event does not use {@link HasResult}.<br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
*/
@Cancelable
public static final class Start extends ExplosionEvent {
public static final class Start extends ExplosionEvent implements Cancellable {
public static final CancellableEventBus<Start> BUS = CancellableEventBus.create(Start.class);
public Start(Level level, Explosion explosion) {
super(level, explosion);
}
@@ -62,6 +68,8 @@ public sealed class ExplosionEvent extends Event {
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
*/
public static final class Detonate extends ExplosionEvent {
public static final EventBus<Detonate> BUS = EventBus.create(Detonate.class);
private final List<BlockPos> blocks;
private final List<Entity> entityList;
@@ -27,8 +27,11 @@ import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.storage.ServerLevelData;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.InheritableEvent;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
import net.minecraftforge.fml.LogicalSide;
import org.jetbrains.annotations.UnmodifiableView;
@@ -37,7 +40,9 @@ import org.jetbrains.annotations.UnmodifiableView;
* <p>
* All children of this event are fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus}.
*/
public class LevelEvent extends Event {
public class LevelEvent extends MutableEvent implements InheritableEvent {
public static final EventBus<LevelEvent> BUS = EventBus.create(LevelEvent.class);
private final LevelAccessor level;
public LevelEvent(LevelAccessor level) {
@@ -62,6 +67,8 @@ public class LevelEvent extends Event {
* on both logical sides.
**/
public static final class Load extends LevelEvent {
public static final EventBus<Load> BUS = EventBus.create(Load.class);
public Load(LevelAccessor level) { super(level); }
}
@@ -78,6 +85,8 @@ public class LevelEvent extends Event {
* on both logical sides.
**/
public static final class Unload extends LevelEvent {
public static final EventBus<Unload> BUS = EventBus.create(Unload.class);
public Unload(LevelAccessor level) { super(level); }
}
@@ -92,6 +101,8 @@ public class LevelEvent extends Event {
* only on the {@linkplain LogicalSide#SERVER logical server}.
**/
public static final class Save extends LevelEvent {
public static final EventBus<Save> BUS = EventBus.create(Save.class);
public Save(LevelAccessor level) { super(level); }
}
@@ -107,8 +118,9 @@ public class LevelEvent extends Event {
*
* @see ServerLevelData#isInitialized()
*/
@Cancelable
public static final class CreateSpawnPosition extends LevelEvent {
public static final class CreateSpawnPosition extends LevelEvent implements Cancellable {
public static final CancellableEventBus<CreateSpawnPosition> BUS = CancellableEventBus.create(CreateSpawnPosition.class);
private final ServerLevelData settings;
public CreateSpawnPosition(LevelAccessor level, ServerLevelData settings) {
@@ -132,8 +144,9 @@ public class LevelEvent extends Event {
* <p>This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
* Canceling the event will result in an empty list, meaning no entity will be spawned.</p>
*/
@Cancelable
public static final class PotentialSpawns extends LevelEvent {
public static final class PotentialSpawns extends LevelEvent implements Cancellable {
public static final CancellableEventBus<PotentialSpawns> BUS = CancellableEventBus.create(PotentialSpawns.class);
private final MobCategory mobcategory;
private final BlockPos pos;
private final List<Weighted<MobSpawnSettings.SpawnerData>> list;
@@ -9,15 +9,18 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraftforge.eventbus.api.Cancelable;
import com.google.common.base.Preconditions;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
/**
* Base class for Noteblock Events
*
*/
public sealed class NoteBlockEvent extends BlockEvent {
public sealed abstract class NoteBlockEvent extends BlockEvent {
public static final EventBus<NoteBlockEvent> BUS = EventBus.create(NoteBlockEvent.class);
private int noteId;
protected NoteBlockEvent(Level world, BlockPos pos, BlockState state, int note) {
@@ -64,8 +67,9 @@ public sealed class NoteBlockEvent extends BlockEvent {
* Fired when a Noteblock plays it's note. You can override the note and instrument
* Canceling this event will stop the note from playing.
*/
@Cancelable
public static final class Play extends NoteBlockEvent {
public static final class Play extends NoteBlockEvent implements Cancellable {
public static final CancellableEventBus<Play> BUS = CancellableEventBus.create(Play.class);
private NoteBlockInstrument instrument;
public Play(Level world, BlockPos pos, BlockState state, int note, NoteBlockInstrument instrument) {
@@ -86,8 +90,9 @@ public sealed class NoteBlockEvent extends BlockEvent {
* Fired when a Noteblock is changed. You can adjust the note it will change to via {@link #setNote(Note, Octave)}.
* Canceling this event will not change the note and also stop the Noteblock from playing it's note.
*/
@Cancelable
public static final class Change extends NoteBlockEvent {
public static final class Change extends NoteBlockEvent implements Cancellable {
public static final CancellableEventBus<Change> BUS = CancellableEventBus.create(Change.class);
private final Note oldNote;
private final Octave oldOctave;
@@ -9,14 +9,16 @@ import net.minecraft.world.level.block.piston.PistonStructureResolver;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
import org.jetbrains.annotations.Nullable;
/**
* Base piston event, use {@link PistonEvent.Post} and {@link PistonEvent.Pre}
*/
public abstract sealed class PistonEvent extends BlockEvent
{
public abstract sealed class PistonEvent extends BlockEvent {
public static final EventBus<PistonEvent> BUS = EventBus.create(PistonEvent.class);
private final Direction direction;
private final PistonMoveType moveType;
@@ -25,8 +27,7 @@ public abstract sealed class PistonEvent extends BlockEvent
* @param pos - The position of the piston
* @param direction - The move direction of the piston
*/
public PistonEvent(Level world, BlockPos pos, Direction direction, PistonMoveType moveType)
{
public PistonEvent(Level world, BlockPos pos, Direction direction, PistonMoveType moveType) {
super(world, pos, world.getBlockState(pos));
this.direction = direction;
this.moveType = moveType;
@@ -35,24 +36,21 @@ public abstract sealed class PistonEvent extends BlockEvent
/**
* @return The direction of the piston block
*/
public Direction getDirection()
{
public Direction getDirection() {
return this.direction;
}
/**
* Helper method that gets the piston position offset by its facing
*/
public BlockPos getFaceOffsetPos()
{
public BlockPos getFaceOffsetPos() {
return this.getPos().relative(direction);
}
/**
* @return The movement type of the piston (extension, retraction)
*/
public PistonMoveType getPistonMoveType()
{
public PistonMoveType getPistonMoveType() {
return moveType;
}
@@ -60,9 +58,8 @@ public abstract sealed class PistonEvent extends BlockEvent
* @return A piston structure helper for this movement. Returns null if the world stored is not a {@link Level}
*/
@Nullable
public PistonStructureResolver getStructureHelper()
{
if(this.getLevel() instanceof Level) {
public PistonStructureResolver getStructureHelper() {
if (this.getLevel() instanceof Level) {
return new PistonStructureResolver((Level) this.getLevel(), this.getPos(), this.getDirection(), this.getPistonMoveType().isExtend);
} else {
return null;
@@ -72,33 +69,29 @@ public abstract sealed class PistonEvent extends BlockEvent
/**
* Fires after the piston has moved and set surrounding states. This will not fire if {@link PistonEvent.Pre} is cancelled.
*/
public static final class Post extends PistonEvent
{
public static final class Post extends PistonEvent {
public static final EventBus<Post> BUS = EventBus.create(Post.class);
public Post(Level world, BlockPos pos, Direction direction, PistonMoveType moveType)
{
public Post(Level world, BlockPos pos, Direction direction, PistonMoveType moveType) {
super(world, pos, direction, moveType);
}
}
/**
* Fires before the piston has updated block states. Cancellation prevents movement.
*/
@Cancelable
public static final class Pre extends PistonEvent
{
public static final class Pre extends PistonEvent implements Cancellable {
public static final CancellableEventBus<Pre> BUS = CancellableEventBus.create(Pre.class);
public Pre(Level world, BlockPos pos, Direction direction, PistonMoveType moveType)
{
public Pre(Level world, BlockPos pos, Direction direction, PistonMoveType moveType) {
super(world, pos, direction, moveType);
}
}
public static enum PistonMoveType
{
EXTEND(true), RETRACT(false);
public enum PistonMoveType {
EXTEND(true),
RETRACT(false);
public final boolean isExtend;
@@ -107,5 +100,4 @@ public abstract sealed class PistonEvent extends BlockEvent
this.isExtend = isExtend;
}
}
}
@@ -13,8 +13,9 @@ import net.minecraft.util.RandomSource;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event.HasResult;
import net.minecraftforge.common.util.HasResult;
import net.minecraftforge.common.util.Result;
import net.minecraftforge.eventbus.api.bus.EventBus;
import org.jetbrains.annotations.Nullable;
/**
@@ -29,13 +30,15 @@ import org.jetbrains.annotations.Nullable;
* only on the {@linkplain net.minecraftforge.fml.LogicalSide#SERVER logical server}.
*/
// TODO: Rename to BlockFeatureGrowEvent in 1.20
@HasResult
@Deprecated(forRemoval = true, since = "1.21.1") // Dont remove, rename
public final class SaplingGrowTreeEvent extends LevelEvent {
public final class SaplingGrowTreeEvent extends LevelEvent implements HasResult {
public static final EventBus<SaplingGrowTreeEvent> BUS = EventBus.create(SaplingGrowTreeEvent.class);
private final RandomSource randomSource;
private final BlockPos pos;
@Nullable
private Holder<ConfiguredFeature<?, ?>> feature;
private Result result = Result.DEFAULT;
public SaplingGrowTreeEvent(LevelAccessor level, RandomSource randomSource, BlockPos pos, @Nullable Holder<ConfiguredFeature<?, ?>> feature) {
super(level);
@@ -79,4 +82,14 @@ public final class SaplingGrowTreeEvent extends LevelEvent {
public void setFeature(ResourceKey<ConfiguredFeature<?, ?>> featureKey) {
this.feature = this.getLevel().registryAccess().lookupOrThrow(Registries.CONFIGURED_FEATURE).get(featureKey).orElse(null);
}
@Override
public Result getResult() {
return result;
}
@Override
public void setResult(Result result) {
this.result = result;
}
}
@@ -6,19 +6,20 @@
package net.minecraftforge.event.level;
import net.minecraft.server.level.ServerLevel;
import net.minecraftforge.eventbus.api.bus.EventBus;
/**
* This event is fired when all players are asleep and the time should be set to day.<br>
*
* setWakeUpTime(wakeUpTime) sets a new time that will be added to the dayTime.<br>
*/
public final class SleepFinishedTimeEvent extends LevelEvent
{
public final class SleepFinishedTimeEvent extends LevelEvent {
public static final EventBus<SleepFinishedTimeEvent> BUS = EventBus.create(SleepFinishedTimeEvent.class);
private long newTime;
private final long minTime;
public SleepFinishedTimeEvent(ServerLevel level, long newTime, long minTime)
{
public SleepFinishedTimeEvent(ServerLevel level, long newTime, long minTime) {
super(level);
this.newTime = newTime;
this.minTime = minTime;
@@ -27,8 +28,7 @@ public final class SleepFinishedTimeEvent extends LevelEvent
/**
* @return the new time
*/
public long getNewTime()
{
public long getNewTime() {
return newTime;
}
@@ -37,10 +37,10 @@ public final class SleepFinishedTimeEvent extends LevelEvent
* @param newTimeIn The new time at wakeup
* @return {@code false} if newTimeIn was lower than current time
*/
public boolean setTimeAddition(long newTimeIn)
{
public boolean setTimeAddition(long newTimeIn) {
if (minTime > newTimeIn)
return false;
this.newTime = newTimeIn;
return true;
}