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

Commit a3f4cb5

Browse files
committed
Move InputEvent firing to a new class, InputEvents
1 parent c50deda commit a3f4cb5

File tree

4 files changed

+86
-26
lines changed

4 files changed

+86
-26
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: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
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;
26-
import org.spongepowered.asm.mixin.Shadow;
2723
import org.spongepowered.asm.mixin.injection.At;
2824
import org.spongepowered.asm.mixin.injection.At.Shift;
2925
import org.spongepowered.asm.mixin.injection.Inject;
@@ -32,36 +28,26 @@
3228

3329
import net.minecraft.client.Mouse;
3430

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();
31+
import net.patchworkmc.api.input.ForgeMouse;
32+
import net.patchworkmc.impl.event.input.InputEvents;
4733

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

5341
@Inject(method = "onMouseButton", at = @At(value = "FIELD", ordinal = 3, target = "Lnet/minecraft/client/Mouse;client:Lnet/minecraft/client/MinecraftClient;", shift = Shift.BEFORE), cancellable = true)
5442
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))) {
43+
if (InputEvents.onRawMouseClicked(button, action, mods)) {
5644
info.cancel();
5745
}
5846
}
5947

6048
@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)) {
49+
private void onMouseScroll(long window, double d, double e, CallbackInfo info, double scrollDelta, float i) {
50+
if (InputEvents.onMouseScroll((Mouse) (Object) this, scrollDelta)) {
6551
info.cancel();
6652
}
6753
}

0 commit comments

Comments
 (0)