Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 7d7600f

Browse files
authored
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.
1 parent 5ce6d98 commit 7d7600f

File tree

7 files changed

+131
-25
lines changed

7 files changed

+131
-25
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.api.input;
21+
22+
public interface ForgeMouse {
23+
boolean isMiddleDown();
24+
double getXVelocity();
25+
double getYVelocity();
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.impl.event.input;
21+
22+
import net.minecraftforge.client.event.InputEvent;
23+
import net.minecraftforge.common.MinecraftForge;
24+
import net.minecraftforge.eventbus.api.Event;
25+
26+
import net.minecraft.client.Mouse;
27+
28+
import net.patchworkmc.api.input.ForgeMouse;
29+
30+
public class InputEvents {
31+
public static void fireMouseInput(int button, int action, int mods) {
32+
MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods));
33+
}
34+
35+
public static void fireKeyInput(int key, int scanCode, int action, int modifiers) {
36+
MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scanCode, action, modifiers));
37+
}
38+
39+
public static boolean onMouseScroll(Mouse mouseHelper, double scrollDelta) {
40+
final Event event = new InputEvent.MouseScrollEvent(scrollDelta, mouseHelper.wasLeftButtonClicked(), ((ForgeMouse) mouseHelper).isMiddleDown(), mouseHelper.wasRightButtonClicked(), mouseHelper.getX(), mouseHelper.getY());
41+
42+
return MinecraftForge.EVENT_BUS.post(event);
43+
}
44+
45+
public static boolean onRawMouseClicked(int button, int action, int mods) {
46+
return MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods));
47+
}
48+
}

patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinKeyboard.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package net.patchworkmc.mixin.event.input;
2121

22-
import net.minecraftforge.client.event.InputEvent;
23-
import net.minecraftforge.common.MinecraftForge;
2422
import org.spongepowered.asm.mixin.Mixin;
2523
import org.spongepowered.asm.mixin.Shadow;
2624
import org.spongepowered.asm.mixin.injection.At;
@@ -30,15 +28,17 @@
3028
import net.minecraft.client.Keyboard;
3129
import net.minecraft.client.MinecraftClient;
3230

31+
import net.patchworkmc.impl.event.input.InputEvents;
32+
3333
@Mixin(Keyboard.class)
3434
public abstract class MixinKeyboard {
3535
@Shadow
3636
MinecraftClient client;
3737

3838
@Inject(method = "onKey", at = @At("RETURN"))
39-
private void fireKeyInput(long window, int key, int scancode, int i, int j, CallbackInfo info) {
39+
private void fireKeyInput(long window, int key, int scancode, int action, int modifiers, CallbackInfo info) {
4040
if (window == this.client.window.getHandle()) {
41-
MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scancode, i, j));
41+
InputEvents.fireKeyInput(key, scancode, action, modifiers);
4242
}
4343
}
4444
}

patchwork-events-input/src/main/java/net/patchworkmc/mixin/event/input/MixinMouse.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
package net.patchworkmc.mixin.event.input;
2121

22-
import net.minecraftforge.client.event.InputEvent;
23-
import net.minecraftforge.common.MinecraftForge;
24-
import net.minecraftforge.eventbus.api.Event;
2522
import org.spongepowered.asm.mixin.Mixin;
2623
import org.spongepowered.asm.mixin.Shadow;
2724
import org.spongepowered.asm.mixin.injection.At;
@@ -32,37 +29,52 @@
3229

3330
import net.minecraft.client.Mouse;
3431

35-
@Mixin(Mouse.class)
36-
public abstract class MixinMouse {
37-
@Shadow
38-
boolean middleButtonClicked;
39-
@Shadow
40-
abstract boolean wasLeftButtonClicked();
41-
@Shadow
42-
abstract boolean wasRightButtonClicked();
43-
@Shadow
44-
abstract double getX();
45-
@Shadow
46-
abstract double getY();
32+
import net.patchworkmc.api.input.ForgeMouse;
33+
import net.patchworkmc.impl.event.input.InputEvents;
4734

35+
@Mixin(Mouse.class)
36+
public abstract class MixinMouse implements ForgeMouse {
4837
@Inject(method = "onMouseButton", at = @At("RETURN"), cancellable = true)
4938
private void fireMouseInput(long window, int button, int action, int mods, CallbackInfo info) {
50-
MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods));
39+
InputEvents.fireMouseInput(button, action, mods);
5140
}
5241

5342
@Inject(method = "onMouseButton", at = @At(value = "FIELD", ordinal = 3, target = "Lnet/minecraft/client/Mouse;client:Lnet/minecraft/client/MinecraftClient;", shift = Shift.BEFORE), cancellable = true)
5443
private void onRawMouseClicked(long window, int button, int action, int mods, CallbackInfo info) {
55-
if (MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods))) {
44+
if (InputEvents.onRawMouseClicked(button, action, mods)) {
5645
info.cancel();
5746
}
5847
}
5948

6049
@Inject(method = "onMouseScroll", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isSpectator()Z", shift = Shift.BEFORE), cancellable = true)
61-
private void onMouseScroll(long window, double d, double e, CallbackInfo info, double f, float i) {
62-
final Event event = new InputEvent.MouseScrollEvent(f, wasLeftButtonClicked(), middleButtonClicked, wasRightButtonClicked(), getX(), getY());
63-
64-
if (MinecraftForge.EVENT_BUS.post(event)) {
50+
private void onMouseScroll(long window, double d, double e, CallbackInfo info, double scrollDelta, float i) {
51+
if (InputEvents.onMouseScroll((Mouse) (Object) this, scrollDelta)) {
6552
info.cancel();
6653
}
6754
}
55+
56+
// Methods added by forge
57+
@Shadow
58+
boolean middleButtonClicked;
59+
60+
@Shadow
61+
double cursorDeltaX;
62+
63+
@Shadow
64+
double cursorDeltaY;
65+
66+
@Override
67+
public boolean isMiddleDown() {
68+
return middleButtonClicked;
69+
}
70+
71+
@Override
72+
public double getXVelocity() {
73+
return cursorDeltaX;
74+
}
75+
76+
@Override
77+
public double getYVelocity() {
78+
return cursorDeltaY;
79+
}
6880
}

patchwork-god-classes/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dependencies {
55
compile project(path: ':patchwork-fml', configuration: 'dev')
66
compile project(path: ':patchwork-capabilities', configuration: 'dev')
77
compile project(path: ':patchwork-events-entity', configuration: 'dev')
8+
compile project(path: ':patchwork-events-input', configuration: 'dev')
89
compile project(path: ':patchwork-events-lifecycle', configuration: 'dev')
910
compile project(path: ':patchwork-events-rendering', configuration: 'dev')
1011
compile project(path: ':patchwork-loot', configuration: 'dev')

patchwork-god-classes/src/main/java/net/minecraftforge/client/ForgeHooksClient.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,32 @@
2525
import net.minecraft.client.color.item.ItemColors;
2626
import net.minecraft.client.texture.SpriteAtlasTexture;
2727
import net.minecraft.util.Identifier;
28+
import net.minecraft.client.Mouse;
2829

30+
import net.patchworkmc.impl.event.input.InputEvents;
2931
import net.patchworkmc.impl.event.render.RenderEvents;
3032

3133
/*
3234
* Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules.
3335
* Do not keep implementation details here, methods should be thin wrappers around methods in other modules.
3436
*/
3537
public class ForgeHooksClient {
38+
public static void fireMouseInput(int button, int action, int mods) {
39+
InputEvents.fireMouseInput(button, action, mods);
40+
}
41+
42+
public static void fireKeyInput(int key, int scanCode, int action, int modifiers) {
43+
InputEvents.fireKeyInput(key, scanCode, action, modifiers);
44+
}
45+
46+
public static boolean onMouseScroll(Mouse mouseHelper, double scrollDelta) {
47+
return InputEvents.onMouseScroll(mouseHelper, scrollDelta);
48+
}
49+
50+
public static boolean onRawMouseClicked(int button, int action, int mods) {
51+
return InputEvents.onRawMouseClicked(button, action, mods);
52+
}
53+
3654
public static void onBlockColorsInit(BlockColors blockColors) {
3755
RenderEvents.onBlockColorsInit(blockColors);
3856
}

patchwork-god-classes/src/main/resources/fabric.mod.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"fabricloader": ">=0.8.4",
1919
"patchwork-fml": "*",
2020
"patchwork-capabilities": "*",
21+
"patchwork-events-input": "*",
2122
"patchwork-events-lifecycle": "*",
2223
"patchwork-events-rendering": "*",
2324
"patchwork-loot": "*"

0 commit comments

Comments
 (0)