Add the new vanilla info object to Keyboard and Mouse related events.

This commit is contained in:
LexManos committed 2025-10-02 12:09:01 -07:00
1 parent b0ccf1a480
commit 58aa3b276c
5 files changed
+105 -56

No files matched your search

@@ -428,25 +428,25 @@ public class ForgeHooksClient {
}
public static boolean onScreenKeyPressed(Screen screen, KeyEvent info) {
return ForgeEventFactoryClient.onScreenKeyPressedPre(screen, info.key(), info.scancode(), info.modifiers())
return ForgeEventFactoryClient.onScreenKeyPressedPre(screen, info)
|| screen.keyPressed(info)
|| ForgeEventFactoryClient.onScreenKeyPressedPost(screen, info.key(), info.scancode(), info.modifiers());
|| ForgeEventFactoryClient.onScreenKeyPressedPost(screen, info);
}
public static boolean onScreenKeyReleased(Screen screen, KeyEvent info) {
return ForgeEventFactoryClient.onScreenKeyReleasedPre(screen, info.key(), info.scancode(), info.modifiers())
return ForgeEventFactoryClient.onScreenKeyReleasedPre(screen, info)
|| screen.keyReleased(info)
|| ForgeEventFactoryClient.onScreenKeyReleasedPost(screen, info.key(), info.scancode(), info.modifiers());
|| ForgeEventFactoryClient.onScreenKeyReleasedPost(screen, info);
}
public static boolean onScreenCharTyped(Screen screen, CharacterEvent info) {
return ForgeEventFactoryClient.onScreenCharTypedPre(screen, (char)info.codepoint(), info.modifiers())
return ForgeEventFactoryClient.onScreenCharTypedPre(screen, info)
|| screen.charTyped(info)
|| ForgeEventFactoryClient.onScreenCharTypedPost(screen, (char)info.codepoint(), info.modifiers());
|| ForgeEventFactoryClient.onScreenCharTypedPost(screen, info);
}
public static void onKeyInput(KeyEvent info, int action) {
InputEvent.Key.BUS.post(new InputEvent.Key(info.key(), info.scancode(), action, info.modifiers()));
InputEvent.Key.BUS.post(new InputEvent.Key(info, action));
}
public static boolean isNameplateInRenderDistance(Entity entity, double squareDistance) {
@@ -33,7 +33,10 @@ import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.client.input.CharacterEvent;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.client.input.MouseButtonInfo;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.SkullModelBase;
import net.minecraft.client.model.geom.EntityModelSet;
@@ -90,20 +93,20 @@ public final class ForgeEventFactoryClient {
}
public static boolean onScreenMouseClicked(Screen screen, double mouseX, double mouseY, MouseButtonEvent info, boolean repeate) {
var ret = ScreenEvent.MouseButtonPressed.Pre.BUS.post(new ScreenEvent.MouseButtonPressed.Pre(screen, mouseX, mouseY, info.button()));
var ret = ScreenEvent.MouseButtonPressed.Pre.BUS.post(new ScreenEvent.MouseButtonPressed.Pre(screen, mouseX, mouseY, info));
if (!ret)
ret = screen.mouseClicked(info, repeate);
var result = ScreenEvent.MouseButtonPressed.Post.BUS.fire(new ScreenEvent.MouseButtonPressed.Post(screen, mouseX, mouseY, info.button(), ret)).getResult();
var result = ScreenEvent.MouseButtonPressed.Post.BUS.fire(new ScreenEvent.MouseButtonPressed.Post(screen, mouseX, mouseY, info, ret, new Result.Holder())).getResult();
return result == Result.DEFAULT ? ret : result == Result.ALLOW;
}
public static boolean onMouseButtonPre(int button, int action, int mods) {
return InputEvent.MouseButton.Pre.BUS.post(new InputEvent.MouseButton.Pre(button, action, mods));
public static boolean onMouseButtonPre(MouseButtonInfo info, int action) {
return InputEvent.MouseButton.Pre.BUS.post(new InputEvent.MouseButton.Pre(info, action));
}
public static void onMouseButtonPost(int button, int action, int mods) {
InputEvent.MouseButton.Post.BUS.post(new InputEvent.MouseButton.Post(button, action, mods));
public static void onMouseButtonPost(MouseButtonInfo info, int action) {
InputEvent.MouseButton.Post.BUS.post(new InputEvent.MouseButton.Post(info, action));
}
public static boolean onScreenMouseScrollPre(Screen guiScreen, double mouseX, double mouseY, double deltaX, double deltaY) {
@@ -145,28 +148,28 @@ public final class ForgeEventFactoryClient {
PlayStreamingSourceEvent.BUS.post(new PlayStreamingSourceEvent(engine, sound, channel));
}
public static boolean onScreenKeyPressedPre(Screen screen, int keyCode, int scanCode, int modifiers) {
return ScreenEvent.KeyPressed.Pre.BUS.post(new ScreenEvent.KeyPressed.Pre(screen, keyCode, scanCode, modifiers));
public static boolean onScreenKeyPressedPre(Screen screen, KeyEvent info) {
return ScreenEvent.KeyPressed.Pre.BUS.post(new ScreenEvent.KeyPressed.Pre(screen, info));
}
public static boolean onScreenKeyPressedPost(Screen screen, int keyCode, int scanCode, int modifiers) {
return ScreenEvent.KeyPressed.Post.BUS.post(new ScreenEvent.KeyPressed.Post(screen, keyCode, scanCode, modifiers));
public static boolean onScreenKeyPressedPost(Screen screen, KeyEvent info) {
return ScreenEvent.KeyPressed.Post.BUS.post(new ScreenEvent.KeyPressed.Post(screen, info));
}
public static boolean onScreenKeyReleasedPre(Screen screen, int keyCode, int scanCode, int modifiers) {
return ScreenEvent.KeyReleased.Pre.BUS.post(new ScreenEvent.KeyReleased.Pre(screen, keyCode, scanCode, modifiers));
public static boolean onScreenKeyReleasedPre(Screen screen, KeyEvent info) {
return ScreenEvent.KeyReleased.Pre.BUS.post(new ScreenEvent.KeyReleased.Pre(screen, info));
}
public static boolean onScreenKeyReleasedPost(Screen screen, int keyCode, int scanCode, int modifiers) {
return ScreenEvent.KeyReleased.Post.BUS.post(new ScreenEvent.KeyReleased.Post(screen, keyCode, scanCode, modifiers));
public static boolean onScreenKeyReleasedPost(Screen screen, KeyEvent info) {
return ScreenEvent.KeyReleased.Post.BUS.post(new ScreenEvent.KeyReleased.Post(screen, info));
}
public static boolean onScreenCharTypedPre(Screen screen, char codePoint, int modifiers) {
return ScreenEvent.CharacterTyped.Pre.BUS.post(new ScreenEvent.CharacterTyped.Pre(screen, codePoint, modifiers));
public static boolean onScreenCharTypedPre(Screen screen, CharacterEvent info) {
return ScreenEvent.CharacterTyped.Pre.BUS.post(new ScreenEvent.CharacterTyped.Pre(screen, info));
}
public static boolean onScreenCharTypedPost(Screen screen, char codePoint, int modifiers) {
return ScreenEvent.CharacterTyped.Post.BUS.post(new ScreenEvent.CharacterTyped.Post(screen, codePoint, modifiers));
public static boolean onScreenCharTypedPost(Screen screen, CharacterEvent info) {
return ScreenEvent.CharacterTyped.Post.BUS.post(new ScreenEvent.CharacterTyped.Post(screen, info));
}
public static boolean onClickInputPickBlock(KeyMapping keyBinding) {
@@ -7,6 +7,8 @@ package net.minecraftforge.client.event;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.client.input.MouseButtonInfo;
import net.minecraft.world.InteractionHand;
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
import net.minecraftforge.eventbus.api.bus.EventBus;
@@ -37,13 +39,21 @@ public sealed interface InputEvent {
* @see Post
*/
sealed interface MouseButton extends InputEvent {
/**
* {@return the MouseButtonInfo object containing information about the mouse button state}
* @see MouseButtonInfo
*/
MouseButtonInfo getInfo();
/**
* {@return the mouse button's input code}
*
* @see GLFW mouse constants starting with 'GLFW_MOUSE_BUTTON_'
* @see <a href="https://www.glfw.org/docs/latest/group__buttons.html" target="_top">the online GLFW documentation</a>
*/
int getButton();
default int getButton() {
return getInfo().button();
}
/**
* {@return the mouse button's action}
@@ -64,7 +74,9 @@ public sealed interface InputEvent {
* @see GLFW#GLFW_KEY_NUM_LOCK NUM LOCK modifier key bit
* @see <a href="https://www.glfw.org/docs/latest/group__mods.html" target="_top">the online GLFW documentation</a>
*/
int getModifiers();
default int getModifiers() {
return getInfo().modifiers();
}
/**
* Fired when a mouse button is pressed/released, <b>before</b> being processed by vanilla.
@@ -75,7 +87,7 @@ public sealed interface InputEvent {
*
* @see <a href="https://www.glfw.org/docs/latest/input_guide.html#input_mouse_button" target="_top">the online GLFW documentation</a>
*/
record Pre(int getButton, int getAction, int getModifiers) implements Cancellable, MouseButton, RecordEvent {
record Pre(MouseButtonInfo getInfo, int getAction) implements Cancellable, MouseButton, RecordEvent {
public static final CancellableEventBus<Pre> BUS = CancellableEventBus.create(Pre.class);
@ApiStatus.Internal
@@ -89,7 +101,7 @@ public sealed interface InputEvent {
*
* @see <a href="https://www.glfw.org/docs/latest/input_guide.html#input_mouse_button" target="_top">the online GLFW documentation</a>
*/
record Post(int getButton, int getAction, int getModifiers) implements MouseButton, RecordEvent {
record Post(MouseButtonInfo getInfo, int getAction) implements MouseButton, RecordEvent {
public static final EventBus<Post> BUS = EventBus.create(Post.class);
@ApiStatus.Internal
@@ -135,12 +147,20 @@ public sealed interface InputEvent {
*
* <p>This event is fired only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
record Key(int getKey, int getScanCode, int getAction, int getModifiers) implements RecordEvent, InputEvent {
record Key(KeyEvent getInfo, int getAction) implements RecordEvent, InputEvent {
public static final EventBus<Key> BUS = EventBus.create(Key.class);
@ApiStatus.Internal
public Key {}
/**
* {@return the KeyEvent object containing information about the key being pressed/released}
* @see KeyEvent
*/
public KeyEvent getInfo() {
return getInfo;
}
/**
* {@return the {@code GLFW} (platform-agnostic) key code}
*
@@ -149,7 +169,7 @@ public sealed interface InputEvent {
* @see <a href="https://www.glfw.org/docs/latest/group__keys.html" target="_top">the online GLFW documentation</a>
*/
public int getKey() {
return getKey;
return getInfo().key();
}
/**
@@ -162,7 +182,7 @@ public sealed interface InputEvent {
* @see InputConstants#getKey(int, int)
*/
public int getScanCode() {
return getScanCode;
return getInfo().scancode();
}
/**
@@ -188,7 +208,7 @@ public sealed interface InputEvent {
* @see <a href="https://www.glfw.org/docs/latest/group__mods.html" target="_top">the online GLFW documentation</a>
*/
public int getModifiers() {
return getModifiers;
return getInfo().modifiers();
}
}
@@ -9,6 +9,9 @@ import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.input.CharacterEvent;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.HasResult;
import net.minecraftforge.common.util.Result;
@@ -322,13 +325,20 @@ public sealed interface ScreenEvent {
* @see MouseButtonPressed.Post
*/
sealed interface MouseButtonPressed extends MouseInput {
/**
* {@return the MouseButtonEvent object containing information about the mouse button being pressed}
*/
MouseButtonEvent getInfo();
/**
* {@return the mouse button's input code}
*
* @see GLFW mouse constants starting with 'GLFW_MOUSE_BUTTON_'
* @see <a href="https://www.glfw.org/docs/latest/group__buttons.html" target="_top">the online GLFW documentation</a>
*/
int getButton();
default int getButton() {
return getInfo().button();
}
/**
* Fired <b>before</b> the mouse click is handled by the screen.
@@ -340,7 +350,7 @@ public sealed interface ScreenEvent {
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
record Pre(Screen getScreen, double getMouseX, double getMouseY, int getButton)
record Pre(Screen getScreen, double getMouseX, double getMouseY, MouseButtonEvent getInfo)
implements Cancellable, MouseButtonPressed, RecordEvent {
public static final CancellableEventBus<Pre> BUS = CancellableEventBus.create(Pre.class);
@@ -367,15 +377,14 @@ public sealed interface ScreenEvent {
Screen getScreen,
double getMouseX,
double getMouseY,
int getButton,
MouseButtonEvent getInfo,
boolean wasHandled,
Result.Holder resultHolder
) implements HasResult.Record, MouseButtonPressed, RecordEvent {
public static final EventBus<Post> BUS = EventBus.create(Post.class);
@ApiStatus.Internal
public Post(Screen screen, double mouseX, double mouseY, int button, boolean handled) {
this(screen, mouseX, mouseY, button, handled, new Result.Holder());
public Post {
}
/**
@@ -600,6 +609,12 @@ public sealed interface ScreenEvent {
* @see <a href="https://www.glfw.org/docs/latest/input_guide.html#input_key" target="_top">the online GLFW documentation</a>
*/
sealed interface KeyInput extends ScreenEvent {
/**
* {@return the KeyEvent object containing information about the key being pressed/released}
* @see KeyEvent
*/
KeyEvent getInfo();
/**
* {@return the {@code GLFW} (platform-agnostic) key code}
*
@@ -607,7 +622,9 @@ public sealed interface ScreenEvent {
* @see GLFW key constants starting with {@code GLFW_KEY_}
* @see <a href="https://www.glfw.org/docs/latest/group__keys.html" target="_top">the online GLFW documentation</a>
*/
int getKeyCode();
default int getKeyCode() {
return getInfo().key();
}
/**
* {@return the platform-specific scan code}
@@ -618,7 +635,9 @@ public sealed interface ScreenEvent {
*
* @see InputConstants#getKey(int, int)
*/
int getScanCode();
default int getScanCode() {
return getInfo().scancode();
}
/**
* {@return a bit field representing the active modifier keys}
@@ -631,7 +650,9 @@ public sealed interface ScreenEvent {
* @see GLFW#GLFW_KEY_NUM_LOCK NUM LOCK modifier key bit
* @see <a href="https://www.glfw.org/docs/latest/group__mods.html" target="_top">the online GLFW documentation</a>
*/
int getModifiers();
default int getModifiers() {
return getInfo().modifiers();
}
}
/**
@@ -652,8 +673,7 @@ public sealed interface ScreenEvent {
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
record Pre(Screen getScreen, int getKeyCode, int getScanCode, int getModifiers)
implements Cancellable, KeyPressed, RecordEvent {
record Pre(Screen getScreen, KeyEvent getInfo) implements Cancellable, KeyPressed, RecordEvent {
public static final CancellableEventBus<Pre> BUS = CancellableEventBus.create(Pre.class);
@ApiStatus.Internal
@@ -670,8 +690,7 @@ public sealed interface ScreenEvent {
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
record Post(Screen getScreen, int getKeyCode, int getScanCode, int getModifiers)
implements Cancellable, KeyPressed, RecordEvent {
record Post(Screen getScreen, KeyEvent getInfo) implements Cancellable, KeyPressed, RecordEvent {
public static final CancellableEventBus<Post> BUS = CancellableEventBus.create(Post.class);
@ApiStatus.Internal
@@ -697,8 +716,7 @@ public sealed interface ScreenEvent {
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
record Pre(Screen getScreen, int getKeyCode, int getScanCode, int getModifiers)
implements Cancellable, KeyReleased, RecordEvent {
record Pre(Screen getScreen, KeyEvent getInfo) implements Cancellable, KeyReleased, RecordEvent {
public static final CancellableEventBus<Pre> BUS = CancellableEventBus.create(Pre.class);
@ApiStatus.Internal
@@ -715,8 +733,7 @@ public sealed interface ScreenEvent {
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
record Post(Screen getScreen, int getKeyCode, int getScanCode, int getModifiers)
implements Cancellable, KeyReleased, RecordEvent {
record Post(Screen getScreen, KeyEvent getInfo) implements Cancellable, KeyReleased, RecordEvent {
public static final CancellableEventBus<Post> BUS = CancellableEventBus.create(Post.class);
@ApiStatus.Internal
@@ -733,10 +750,18 @@ public sealed interface ScreenEvent {
* @see <a href="https://www.glfw.org/docs/latest/input_guide.html#input_char" target="_top">the online GLFW documentation</a>
*/
sealed interface CharacterTyped extends ScreenEvent {
/**
* {@return The CharacterEvent object containing information about the character being typed}
*/
CharacterEvent getInfo();
/**
* {@return the character code point}
*/
char getCodePoint();
default char getCodePoint() {
return (char)getInfo().codepoint();
}
/**
* {@return a bit field representing the active modifier keys}
@@ -749,7 +774,9 @@ public sealed interface ScreenEvent {
* @see GLFW#GLFW_KEY_NUM_LOCK NUM LOCK modifier key bit
* @see <a href="https://www.glfw.org/docs/latest/group__mods.html" target="_top">the online GLFW documentation</a>
*/
int getModifiers();
default int getModifiers() {
return getInfo().modifiers();
}
/**
* Fired <b>before</b> the character input is handled by the screen.
@@ -761,8 +788,7 @@ public sealed interface ScreenEvent {
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
record Pre(Screen getScreen, char getCodePoint, int getModifiers)
implements Cancellable, CharacterTyped, RecordEvent {
record Pre(Screen getScreen, CharacterEvent getInfo) implements Cancellable, CharacterTyped, RecordEvent {
public static final CancellableEventBus<Pre> BUS = CancellableEventBus.create(Pre.class);
@ApiStatus.Internal
@@ -779,7 +805,7 @@ public sealed interface ScreenEvent {
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
record Post(Screen getScreen, char getCodePoint, int getModifiers) implements CharacterTyped, RecordEvent {
record Post(Screen getScreen, CharacterEvent getInfo) implements CharacterTyped, RecordEvent {
public static final EventBus<Post> BUS = EventBus.create(Post.class);
@ApiStatus.Internal