From 7d7600f94dcbebc1735432ad3e68ba59d2618798 Mon Sep 17 00:00:00 2001 From: Kitlith Date: Sun, 26 Jul 2020 14:12:00 -0700 Subject: [PATCH] Dispatch to InputEvents from patchwork-god-classes (#130) * Move InputEvent firing to a new class, InputEvents * Dispatch from ForgeHooksClient to InputEvents * Fixup! mixin didn't actually implement interface. --- .../net/patchworkmc/api/input/ForgeMouse.java | 26 +++++++++ .../impl/event/input/InputEvents.java | 48 +++++++++++++++++ .../mixin/event/input/MixinKeyboard.java | 8 +-- .../mixin/event/input/MixinMouse.java | 54 +++++++++++-------- patchwork-god-classes/build.gradle | 1 + .../client/ForgeHooksClient.java | 18 +++++++ .../src/main/resources/fabric.mod.json | 1 + 7 files changed, 131 insertions(+), 25 deletions(-) create mode 100644 patchwork-events-input/src/main/java/net/patchworkmc/api/input/ForgeMouse.java create mode 100644 patchwork-events-input/src/main/java/net/patchworkmc/impl/event/input/InputEvents.java diff --git a/patchwork-events-input/src/main/java/net/patchworkmc/api/input/ForgeMouse.java b/patchwork-events-input/src/main/java/net/patchworkmc/api/input/ForgeMouse.java new file mode 100644 index 00000000..364e019f --- /dev/null +++ b/patchwork-events-input/src/main/java/net/patchworkmc/api/input/ForgeMouse.java @@ -0,0 +1,26 @@ +/* + * Minecraft Forge, Patchwork Project + * Copyright (c) 2016-2020, 2019-2020 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.patchworkmc.api.input; + +public interface ForgeMouse { + boolean isMiddleDown(); + double getXVelocity(); + double getYVelocity(); +} diff --git a/patchwork-events-input/src/main/java/net/patchworkmc/impl/event/input/InputEvents.java b/patchwork-events-input/src/main/java/net/patchworkmc/impl/event/input/InputEvents.java new file mode 100644 index 00000000..65a2951b --- /dev/null +++ b/patchwork-events-input/src/main/java/net/patchworkmc/impl/event/input/InputEvents.java @@ -0,0 +1,48 @@ +/* + * Minecraft Forge, Patchwork Project + * Copyright (c) 2016-2020, 2019-2020 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.patchworkmc.impl.event.input; + +import net.minecraftforge.client.event.InputEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.Event; + +import net.minecraft.client.Mouse; + +import net.patchworkmc.api.input.ForgeMouse; + +public class InputEvents { + public static void fireMouseInput(int button, int action, int mods) { + MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods)); + } + + public static void fireKeyInput(int key, int scanCode, int action, int modifiers) { + MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scanCode, action, modifiers)); + } + + public static boolean onMouseScroll(Mouse mouseHelper, double scrollDelta) { + final Event event = new InputEvent.MouseScrollEvent(scrollDelta, mouseHelper.wasLeftButtonClicked(), ((ForgeMouse) mouseHelper).isMiddleDown(), mouseHelper.wasRightButtonClicked(), mouseHelper.getX(), mouseHelper.getY()); + + return MinecraftForge.EVENT_BUS.post(event); + } + + public static boolean onRawMouseClicked(int button, int action, int mods) { + return MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods)); + } +} diff --git a/patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinKeyboard.java b/patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinKeyboard.java index 70d1d82b..0891efb3 100644 --- a/patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinKeyboard.java +++ b/patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinKeyboard.java @@ -19,8 +19,6 @@ package net.patchworkmc.mixin.event.input; -import net.minecraftforge.client.event.InputEvent; -import net.minecraftforge.common.MinecraftForge; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -30,15 +28,17 @@ import net.minecraft.client.Keyboard; import net.minecraft.client.MinecraftClient; +import net.patchworkmc.impl.event.input.InputEvents; + @Mixin(Keyboard.class) public abstract class MixinKeyboard { @Shadow MinecraftClient client; @Inject(method = "onKey", at = @At("RETURN")) - private void fireKeyInput(long window, int key, int scancode, int i, int j, CallbackInfo info) { + private void fireKeyInput(long window, int key, int scancode, int action, int modifiers, CallbackInfo info) { if (window == this.client.window.getHandle()) { - MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scancode, i, j)); + InputEvents.fireKeyInput(key, scancode, action, modifiers); } } } diff --git a/patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinMouse.java b/patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinMouse.java index 06827afb..37d3d483 100644 --- a/patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinMouse.java +++ b/patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinMouse.java @@ -19,9 +19,6 @@ package net.patchworkmc.mixin.event.input; -import net.minecraftforge.client.event.InputEvent; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.eventbus.api.Event; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -32,37 +29,52 @@ import net.minecraft.client.Mouse; -@Mixin(Mouse.class) -public abstract class MixinMouse { - @Shadow - boolean middleButtonClicked; - @Shadow - abstract boolean wasLeftButtonClicked(); - @Shadow - abstract boolean wasRightButtonClicked(); - @Shadow - abstract double getX(); - @Shadow - abstract double getY(); +import net.patchworkmc.api.input.ForgeMouse; +import net.patchworkmc.impl.event.input.InputEvents; +@Mixin(Mouse.class) +public abstract class MixinMouse implements ForgeMouse { @Inject(method = "onMouseButton", at = @At("RETURN"), cancellable = true) private void fireMouseInput(long window, int button, int action, int mods, CallbackInfo info) { - MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods)); + InputEvents.fireMouseInput(button, action, mods); } @Inject(method = "onMouseButton", at = @At(value = "FIELD", ordinal = 3, target = "Lnet/minecraft/client/Mouse;client:Lnet/minecraft/client/MinecraftClient;", shift = Shift.BEFORE), cancellable = true) private void onRawMouseClicked(long window, int button, int action, int mods, CallbackInfo info) { - if (MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods))) { + if (InputEvents.onRawMouseClicked(button, action, mods)) { info.cancel(); } } @Inject(method = "onMouseScroll", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isSpectator()Z", shift = Shift.BEFORE), cancellable = true) - private void onMouseScroll(long window, double d, double e, CallbackInfo info, double f, float i) { - final Event event = new InputEvent.MouseScrollEvent(f, wasLeftButtonClicked(), middleButtonClicked, wasRightButtonClicked(), getX(), getY()); - - if (MinecraftForge.EVENT_BUS.post(event)) { + private void onMouseScroll(long window, double d, double e, CallbackInfo info, double scrollDelta, float i) { + if (InputEvents.onMouseScroll((Mouse) (Object) this, scrollDelta)) { info.cancel(); } } + + // Methods added by forge + @Shadow + boolean middleButtonClicked; + + @Shadow + double cursorDeltaX; + + @Shadow + double cursorDeltaY; + + @Override + public boolean isMiddleDown() { + return middleButtonClicked; + } + + @Override + public double getXVelocity() { + return cursorDeltaX; + } + + @Override + public double getYVelocity() { + return cursorDeltaY; + } } diff --git a/patchwork-god-classes/build.gradle b/patchwork-god-classes/build.gradle index ad0f0d6a..1b5d076f 100644 --- a/patchwork-god-classes/build.gradle +++ b/patchwork-god-classes/build.gradle @@ -5,6 +5,7 @@ dependencies { compile project(path: ':patchwork-fml', configuration: 'dev') compile project(path: ':patchwork-capabilities', configuration: 'dev') compile project(path: ':patchwork-events-entity', configuration: 'dev') + compile project(path: ':patchwork-events-input', configuration: 'dev') compile project(path: ':patchwork-events-lifecycle', configuration: 'dev') compile project(path: ':patchwork-events-rendering', configuration: 'dev') compile project(path: ':patchwork-loot', configuration: 'dev') diff --git a/patchwork-god-classes/src/main/java/net/minecraftforge/client/ForgeHooksClient.java b/patchwork-god-classes/src/main/java/net/minecraftforge/client/ForgeHooksClient.java index 9a369e8e..2c48048d 100644 --- a/patchwork-god-classes/src/main/java/net/minecraftforge/client/ForgeHooksClient.java +++ b/patchwork-god-classes/src/main/java/net/minecraftforge/client/ForgeHooksClient.java @@ -25,7 +25,9 @@ import net.minecraft.client.color.item.ItemColors; import net.minecraft.client.texture.SpriteAtlasTexture; import net.minecraft.util.Identifier; +import net.minecraft.client.Mouse; +import net.patchworkmc.impl.event.input.InputEvents; import net.patchworkmc.impl.event.render.RenderEvents; /* @@ -33,6 +35,22 @@ * Do not keep implementation details here, methods should be thin wrappers around methods in other modules. */ public class ForgeHooksClient { + public static void fireMouseInput(int button, int action, int mods) { + InputEvents.fireMouseInput(button, action, mods); + } + + public static void fireKeyInput(int key, int scanCode, int action, int modifiers) { + InputEvents.fireKeyInput(key, scanCode, action, modifiers); + } + + public static boolean onMouseScroll(Mouse mouseHelper, double scrollDelta) { + return InputEvents.onMouseScroll(mouseHelper, scrollDelta); + } + + public static boolean onRawMouseClicked(int button, int action, int mods) { + return InputEvents.onRawMouseClicked(button, action, mods); + } + public static void onBlockColorsInit(BlockColors blockColors) { RenderEvents.onBlockColorsInit(blockColors); } diff --git a/patchwork-god-classes/src/main/resources/fabric.mod.json b/patchwork-god-classes/src/main/resources/fabric.mod.json index 388b9196..b14cf7e8 100644 --- a/patchwork-god-classes/src/main/resources/fabric.mod.json +++ b/patchwork-god-classes/src/main/resources/fabric.mod.json @@ -18,6 +18,7 @@ "fabricloader": ">=0.8.4", "patchwork-fml": "*", "patchwork-capabilities": "*", + "patchwork-events-input": "*", "patchwork-events-lifecycle": "*", "patchwork-events-rendering": "*", "patchwork-loot": "*"