From 76829bd792eaf4d4846f760342cb01ddfd8bb715 Mon Sep 17 00:00:00 2001 From: Kitlith Date: Fri, 10 Jul 2020 23:13:30 -0700 Subject: [PATCH 1/3] Move InputEvent firing to a new class, InputEvents --- .../net/patchworkmc/api/input/ForgeMouse.java | 26 ++++++++++ .../impl/event/input/InputEvents.java | 48 +++++++++++++++++++ .../mixin/event/input/MixinKeyboard.java | 8 ++-- .../mixin/event/input/MixinMouse.java | 30 ++++-------- 4 files changed, 86 insertions(+), 26 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..e75e77a1 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,11 +19,7 @@ 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; import org.spongepowered.asm.mixin.injection.At.Shift; import org.spongepowered.asm.mixin.injection.Inject; @@ -32,36 +28,26 @@ 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(); } } From 9cdbf2a748d6eee12dce5afbdab6260ccf5fe768 Mon Sep 17 00:00:00 2001 From: Kitlith Date: Wed, 15 Jul 2020 12:45:21 -0700 Subject: [PATCH 2/3] Dispatch from ForgeHooksClient to InputEvents --- patchwork-god-classes/build.gradle | 1 + .../client/ForgeHooksClient.java | 19 +++++++++++++++++++ .../src/main/resources/fabric.mod.json | 1 + 3 files changed, 21 insertions(+) diff --git a/patchwork-god-classes/build.gradle b/patchwork-god-classes/build.gradle index 445a1e9b..bdf9ab4a 100644 --- a/patchwork-god-classes/build.gradle +++ b/patchwork-god-classes/build.gradle @@ -4,5 +4,6 @@ version = getSubprojectVersion(project, "0.1.0") dependencies { compile project(path: ':patchwork-fml', configuration: 'dev') compile project(path: ':patchwork-capabilities', configuration: 'dev') + compile project(path: ':patchwork-events-input', configuration: 'dev') compile project(path: ':patchwork-events-lifecycle', 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 ef6b02e5..be3b6099 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 @@ -19,9 +19,28 @@ package net.minecraftforge.client; +import net.minecraft.client.Mouse; + +import net.patchworkmc.impl.event.input.InputEvents; + /* * Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules. * 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); + } } diff --git a/patchwork-god-classes/src/main/resources/fabric.mod.json b/patchwork-god-classes/src/main/resources/fabric.mod.json index 102bb0d7..a7286881 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": "*" }, "custom": { From d38f7daa76ac33ee08a4b7fdf58ff682410dcd5f Mon Sep 17 00:00:00 2001 From: Kitlith Date: Thu, 16 Jul 2020 18:30:37 -0700 Subject: [PATCH 3/3] Fixup! mixin didn't actually implement interface. --- .../mixin/event/input/MixinMouse.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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 e75e77a1..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 @@ -20,6 +20,7 @@ package net.patchworkmc.mixin.event.input; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At.Shift; import org.spongepowered.asm.mixin.injection.Inject; @@ -51,4 +52,29 @@ private void onMouseScroll(long window, double d, double e, CallbackInfo info, d 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; + } }