Make more events records

This commit is contained in:
Paint_Ninja committed 2025-06-20 18:22:53 +01:00
1 parent 6915753b5a
commit 219abfed22
22 files changed
+188 -522

No files matched your search

@@ -13,7 +13,7 @@ import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.eventbus.api.event.RecordEvent;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
import net.minecraftforge.fml.LogicalSide;
@@ -30,69 +30,27 @@ import org.jetbrains.annotations.ApiStatus;
* This event (and items stacking on others in general) is fired on both {@linkplain LogicalSide sides}, but only on {@linkplain LogicalSide#CLIENT the client} in the creative menu.
* Practically, that means that listeners of this event should require the player to be in survival mode if using capabilities that are not synced.
* <p>
* This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
* This event is {@linkplain Cancellable cancellable}.
* If the event is cancelled, the container's logic halts, the carried item and the slot will not be swapped, and handling is assumed to have been done by the mod.
* This also means that the two vanilla checks described above will not be called.
*
* @param getCarriedItem The stack being carried by the mouse, which may be empty
* @param getStackedOnItem The stack currently in the slot being clicked on, which may be empty
* @param getSlot The slot being clicked on
* @param getClickAction The click action being used. By default, {@linkplain ClickAction#PRIMARY} corresponds to left-click, and {@linkplain ClickAction#SECONDARY} is right-click.
* @param getPlayer The player doing the item swap attempt
* @param getCarriedSlotAccess A fake slot allowing the listener to see and change what item is being carried
*/
public final class ItemStackedOnOtherEvent extends MutableEvent implements Cancellable {
public record ItemStackedOnOtherEvent(
ItemStack getCarriedItem,
ItemStack getStackedOnItem,
Slot getSlot,
ClickAction getClickAction,
Player getPlayer,
SlotAccess getCarriedSlotAccess
) implements Cancellable, RecordEvent {
public static final CancellableEventBus<ItemStackedOnOtherEvent> BUS = CancellableEventBus.create(ItemStackedOnOtherEvent.class);
private final ItemStack carriedItem;
private final ItemStack stackedOnItem;
private final Slot slot;
private final ClickAction action;
private final Player player;
private final SlotAccess carriedSlotAccess;
@ApiStatus.Internal
public ItemStackedOnOtherEvent(ItemStack carriedItem, ItemStack stackedOnItem, Slot slot, ClickAction action, Player player, SlotAccess carriedSlotAccess) {
this.carriedItem = carriedItem;
this.stackedOnItem = stackedOnItem;
this.slot = slot;
this.action = action;
this.player = player;
this.carriedSlotAccess = carriedSlotAccess;
}
/**
* {@return the stack being carried by the mouse} This may be empty!
*/
public ItemStack getCarriedItem() {
return carriedItem;
}
/**
* {@return the stack currently in the slot being clicked on} This may be empty!
*/
public ItemStack getStackedOnItem() {
return stackedOnItem;
}
/**
* {@return the slot being clicked on}
*/
public Slot getSlot() {
return slot;
}
/**
* {@return the click action being used} By default {@linkplain ClickAction#PRIMARY} corresponds to left-click, and {@linkplain ClickAction#SECONDARY} is right-click.
*/
public ClickAction getClickAction() {
return action;
}
/**
* {@return the player doing the item swap attempt}
*/
public Player getPlayer() {
return player;
}
/**
* {@return a fake slot allowing the listener to see and change what item is being carried}
*/
public SlotAccess getCarriedSlotAccess() {
return carriedSlotAccess;
}
public ItemStackedOnOtherEvent {}
}
@@ -8,8 +8,9 @@ package net.minecraftforge.event;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import org.jetbrains.annotations.Nullable;
import net.minecraftforge.eventbus.api.event.RecordEvent;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.List;
@@ -17,41 +18,19 @@ import java.util.List;
* Fires when a player joins the server or when the reload command is ran,
* before tags and crafting recipes are sent to the client. Send datapack data
* to clients when this event fires.
*
* @param getPlayerList The server's player list to get a view of all players.
* @param getPlayer The player to sync datapacks to. Null when syncing for all players, such as when the reload command runs.
*/
public final class OnDatapackSyncEvent extends MutableEvent {
@NullMarked
public record OnDatapackSyncEvent(PlayerList getPlayerList, @Nullable ServerPlayer getPlayer) implements RecordEvent {
public static final EventBus<OnDatapackSyncEvent> BUS = EventBus.create(OnDatapackSyncEvent.class);
private final PlayerList playerList;
@Nullable
private final ServerPlayer player;
public OnDatapackSyncEvent(PlayerList playerList, @Nullable ServerPlayer player) {
this.playerList = playerList;
this.player = player;
}
/**
* @return The server's player list to get a view of all players.
*/
public PlayerList getPlayerList() {
return this.playerList;
}
/**
* @return The player to sync datapacks to. Null when syncing for all players,
* such as when the reload command runs.
*/
@Nullable
public ServerPlayer getPlayer() {
return this.player;
}
/**
* @return A list of players that should receive data during this event, which is the specified player (if not null)
* or all players otherwise.
*/
public List<ServerPlayer> getPlayers() {
return this.player == null ? this.playerList.getPlayers() : List.of(this.player);
return getPlayer == null ? getPlayerList.getPlayers() : List.of(this.getPlayer);
}
}
@@ -8,50 +8,24 @@ package net.minecraftforge.event;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.server.ReloadableServerResources;
import net.minecraftforge.common.MinecraftForge;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.eventbus.api.event.RecordEvent;
/**
* Commands are rebuilt whenever {@link ReloadableServerResources} is recreated.
* You can use this event to register your commands whenever the {@link Commands} class in constructed.
*
* The event is fired on the {@link MinecraftForge#EVENT_BUS}
* @param getDispatcher The command dispatcher for registering commands to be executed on the client
* @param getCommandSelection The environment the command is being registered for
* @param getBuildContext The context to build the commands for
*/
public final class RegisterCommandsEvent extends MutableEvent {
public record RegisterCommandsEvent(
CommandDispatcher<CommandSourceStack> getDispatcher,
Commands.CommandSelection getCommandSelection,
CommandBuildContext getBuildContext
) implements RecordEvent {
public static final EventBus<RegisterCommandsEvent> BUS = EventBus.create(RegisterCommandsEvent.class);
private final CommandDispatcher<CommandSourceStack> dispatcher;
private final Commands.CommandSelection environment;
private final CommandBuildContext context;
public RegisterCommandsEvent(CommandDispatcher<CommandSourceStack> dispatcher, Commands.CommandSelection environment, CommandBuildContext context) {
this.dispatcher = dispatcher;
this.environment = environment;
this.context = context;
}
/**
* {@return the command dispatcher for registering commands to be executed on the client}
*/
public CommandDispatcher<CommandSourceStack> getDispatcher() {
return dispatcher;
}
/**
* {@return the environment the command is being registered for}
*/
public Commands.CommandSelection getCommandSelection() {
return environment;
}
/**
* {@return the context to build the commands for}
*/
public CommandBuildContext getBuildContext() {
return context;
}
}
@@ -9,9 +9,8 @@ import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
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.Nullable;
@@ -20,58 +19,26 @@ import org.jetbrains.annotations.Nullable;
* <br>
* This allows for listening to Vanilla's events in a more structured and global way that is not tied to needing a block entity listener. <br>
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}. <br>
* <br>
* Cancel this event to prevent Vanilla from posting the {@link GameEvent} to all nearby {@link net.minecraft.world.level.gameevent.GameEventListener GameEventListeners}.
*
* @param getLevel The level the Vanilla {@link GameEvent} occurred in
* @param getVanillaEvent The Vanilla event
* @param getEventPosition The position the event took place at
* @param getContext The context of the Vanilla event
**/
public final class VanillaGameEvent extends MutableEvent implements Cancellable {
public record VanillaGameEvent(
Level getLevel,
GameEvent getVanillaEvent,
Vec3 getEventPosition,
GameEvent.Context getContext
) implements Cancellable, RecordEvent {
public static final CancellableEventBus<VanillaGameEvent> BUS = CancellableEventBus.create(VanillaGameEvent.class);
private final Level level;
private final GameEvent vanillaEvent;
private final Vec3 position;
private final GameEvent.Context context;
public VanillaGameEvent(Level level, GameEvent vanillaEvent, Vec3 position, GameEvent.Context context) {
this.level = level;
this.vanillaEvent = vanillaEvent;
this.position = position;
this.context = context;
}
/**
* @return The level the Vanilla {@link GameEvent} occurred.
*/
public Level getLevel() {
return level;
}
/**
* @return The entity that was the source or "cause" of the {@link GameEvent}.
*/
@Nullable
public Entity getCause() {
return context.sourceEntity();
}
/**
* @return The Vanilla event.
*/
public GameEvent getVanillaEvent() {
return vanillaEvent;
}
/**
* @return The position the event took place at.
*/
public Vec3 getEventPosition() {
return position;
}
/**
* @return the context of the vanilla event
*/
public GameEvent.Context getContext() {
return context;
return getContext.sourceEntity();
}
}
@@ -11,40 +11,28 @@ import java.util.Set;
import net.minecraft.network.Connection;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.eventbus.api.bus.EventBus;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.eventbus.api.event.RecordEvent;
/**
* Fired when the channel registration (see minecraft custom channel documentation) changes.
* <br>
* It seems plausible that this will fire multiple times for the same state, depending on what the server is doing.
* It just directly dispatches upon receipt.
*
* @param getType The type of change, either {@link Type#REGISTER} or {@link Type#UNREGISTER}
*/
public final class ChannelRegistrationChangeEvent extends MutableEvent {
public record ChannelRegistrationChangeEvent(
Connection getSource,
Type getType,
Set<ResourceLocation> getChannels
) implements RecordEvent {
public static final EventBus<ChannelRegistrationChangeEvent> BUS = EventBus.create(ChannelRegistrationChangeEvent.class);
public enum Type {
REGISTER, UNREGISTER
}
private final Connection source;
private final Type changeType;
private final Set<ResourceLocation> channels;
public ChannelRegistrationChangeEvent(Connection source, Type changeType, Set<ResourceLocation> channels) {
this.source = source;
this.changeType = changeType;
this.channels = Collections.unmodifiableSet(channels);
}
public Type getType() {
return changeType;
}
public Connection getSource() {
return source;
}
public Set<ResourceLocation> getChannels() {
return channels;
public ChannelRegistrationChangeEvent {
getChannels = Collections.unmodifiableSet(getChannels);
}
}
@@ -10,43 +10,20 @@ import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.ai.village.VillageSiege;
import net.minecraft.world.level.Level;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.event.MutableEvent;
import net.minecraftforge.eventbus.api.event.RecordEvent;
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
/**
* VillageSiegeEvent is fired just before a zombie siege finds a successful location in
* {@code VillageSiege#tryToSetupSiege(ServerLevel)}, to give mods the chance to stop the siege.<br>
* <br>
* Canceling this event stops the siege.<br>
* Cancelling this event stops the siege.<br>
*/
public final class VillageSiegeEvent extends MutableEvent implements Cancellable {
public record VillageSiegeEvent(
VillageSiege getSiege,
Level getLevel,
Player getPlayer,
Vec3 getAttemptedSpawnPos
) implements Cancellable, RecordEvent {
public static final CancellableEventBus<VillageSiegeEvent> BUS = CancellableEventBus.create(VillageSiegeEvent.class);
private final VillageSiege siege;
private final Level level;
private final Player player;
private final Vec3 attemptedSpawnPos;
public VillageSiegeEvent(VillageSiege siege, Level level, Player player, Vec3 attemptedSpawnPos) {
this.siege = siege;
this.level = level;
this.player = player;
this.attemptedSpawnPos = attemptedSpawnPos;
}
public VillageSiege getSiege() {
return siege;
}
public Level getLevel() {
return level;
}
public Player getPlayer() {
return player;
}
public Vec3 getAttemptedSpawnPos() {
return attemptedSpawnPos;
}
}