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

Commit 63d33f6

Browse files
authored
Merge pull request #22 from shedaniel/master
Implement GuiScreenEvents for patchwork-gui
2 parents 7aeb1e8 + b2160d2 commit 63d33f6

11 files changed

Lines changed: 1252 additions & 0 deletions

File tree

patchwork-gui/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
archivesBaseName = "patchwork-gui"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
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 com.patchworkmc.mixin.gui;
21+
22+
import net.minecraftforge.client.event.GuiScreenEvent;
23+
import net.minecraftforge.common.MinecraftForge;
24+
import org.spongepowered.asm.mixin.Mixin;
25+
import org.spongepowered.asm.mixin.Shadow;
26+
import org.spongepowered.asm.mixin.injection.At;
27+
import org.spongepowered.asm.mixin.injection.Inject;
28+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
29+
30+
import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
31+
import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen;
32+
import net.minecraft.container.Container;
33+
import net.minecraft.entity.player.PlayerInventory;
34+
import net.minecraft.text.Text;
35+
36+
@Mixin(AbstractInventoryScreen.class)
37+
public abstract class MixinAbstractInventoryScreen extends AbstractContainerScreen {
38+
@Shadow
39+
protected boolean offsetGuiForEffects;
40+
41+
public MixinAbstractInventoryScreen(Container container, PlayerInventory playerInventory, Text name) {
42+
super(container, playerInventory, name);
43+
}
44+
45+
@Inject(method = "method_2476", at = @At("RETURN"))
46+
private void potionShift(CallbackInfo info) {
47+
if (offsetGuiForEffects) {
48+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.PotionShiftEvent())) {
49+
this.left = (this.width - this.containerWidth) / 2;
50+
}
51+
}
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
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 com.patchworkmc.mixin.gui;
21+
22+
import net.minecraftforge.client.event.GuiScreenEvent;
23+
import net.minecraftforge.common.MinecraftForge;
24+
import org.spongepowered.asm.mixin.Final;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.Shadow;
27+
import org.spongepowered.asm.mixin.injection.At;
28+
import org.spongepowered.asm.mixin.injection.Inject;
29+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
30+
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
31+
32+
import net.minecraft.client.MinecraftClient;
33+
import net.minecraft.client.render.GameRenderer;
34+
35+
@Mixin(GameRenderer.class)
36+
public abstract class MixinGameRenderer {
37+
@Shadow
38+
@Final
39+
private MinecraftClient client;
40+
41+
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;render(IIF)V"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
42+
private void beforeRenderScreen(float tickDelta, long startTime, boolean fullRender, CallbackInfo info, int mouseX, int mouseY) {
43+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.DrawScreenEvent.Pre(client.currentScreen, mouseX, mouseY, tickDelta))) {
44+
info.cancel();
45+
}
46+
}
47+
48+
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;render(IIF)V", shift = At.Shift.BY, by = 2), locals = LocalCapture.CAPTURE_FAILHARD)
49+
private void afterRenderScreen(float tickDelta, long startTime, boolean fullRender, CallbackInfo info, int mouseX, int mouseY) {
50+
MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.DrawScreenEvent.Post(client.currentScreen, mouseX, mouseY, tickDelta));
51+
}
52+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
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 com.patchworkmc.mixin.gui;
21+
22+
import net.minecraftforge.client.event.GuiScreenEvent;
23+
import net.minecraftforge.common.MinecraftForge;
24+
import org.spongepowered.asm.mixin.Mixin;
25+
import org.spongepowered.asm.mixin.Shadow;
26+
import org.spongepowered.asm.mixin.injection.At;
27+
import org.spongepowered.asm.mixin.injection.Inject;
28+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
29+
30+
import net.minecraft.client.Keyboard;
31+
import net.minecraft.client.gui.Element;
32+
import net.minecraft.client.gui.ParentElement;
33+
import net.minecraft.client.gui.screen.Screen;
34+
35+
@Mixin(Keyboard.class)
36+
public abstract class MixinKeyboard {
37+
@Shadow
38+
private boolean repeatEvents;
39+
40+
@Inject(method = "method_1454", at = @At("HEAD"), cancellable = true)
41+
private void preKeyEvent(int i, boolean[] bls, ParentElement element, int key, int scanCode, int mods, CallbackInfo info) {
42+
if (i != 1 && (i != 2 || !this.repeatEvents)) {
43+
if (i == 0) {
44+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.KeyboardKeyReleasedEvent.Pre((Screen) element, key, scanCode, mods))) {
45+
bls[0] = true;
46+
info.cancel();
47+
}
48+
}
49+
} else {
50+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.KeyboardKeyPressedEvent.Pre((Screen) element, key, scanCode, mods))) {
51+
bls[0] = true;
52+
info.cancel();
53+
}
54+
}
55+
}
56+
57+
@Inject(method = "method_1454", at = @At("RETURN"))
58+
private void postKeyEvent(int i, boolean[] bls, ParentElement element, int key, int scanCode, int mods, CallbackInfo info) {
59+
if (bls[0]) {
60+
return;
61+
}
62+
63+
if (i != 1 && (i != 2 || !this.repeatEvents)) {
64+
if (i == 0) {
65+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.KeyboardKeyReleasedEvent.Post((Screen) element, key, scanCode, mods))) {
66+
bls[0] = true;
67+
}
68+
}
69+
} else {
70+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.KeyboardKeyPressedEvent.Post((Screen) element, key, scanCode, mods))) {
71+
bls[0] = true;
72+
}
73+
}
74+
}
75+
76+
@Inject(method = "method_1458", at = @At("HEAD"), cancellable = true)
77+
private static void charEvent(Element element, int character, int mods, CallbackInfo info) {
78+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.KeyboardCharTypedEvent.Pre((Screen) element, (char) character, mods))) {
79+
info.cancel();
80+
} else if (!element.charTyped((char) character, mods)) {
81+
MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.KeyboardCharTypedEvent.Post((Screen) element, (char) character, mods));
82+
}
83+
}
84+
85+
@Inject(method = "method_1473", at = @At("HEAD"), cancellable = true)
86+
private static void charEvent(Element element, char character, int mods, CallbackInfo info) {
87+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.KeyboardCharTypedEvent.Pre((Screen) element, character, mods))) {
88+
info.cancel();
89+
} else if (!element.charTyped(character, mods)) {
90+
MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.KeyboardCharTypedEvent.Post((Screen) element, character, mods));
91+
}
92+
}
93+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
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 com.patchworkmc.mixin.gui;
21+
22+
import net.minecraftforge.client.event.GuiScreenEvent;
23+
import net.minecraftforge.common.MinecraftForge;
24+
import org.spongepowered.asm.mixin.Final;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.Shadow;
27+
import org.spongepowered.asm.mixin.injection.At;
28+
import org.spongepowered.asm.mixin.injection.Inject;
29+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
30+
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
31+
32+
import net.minecraft.client.MinecraftClient;
33+
import net.minecraft.client.Mouse;
34+
import net.minecraft.client.gui.Element;
35+
import net.minecraft.client.gui.screen.Screen;
36+
37+
@Mixin(Mouse.class)
38+
public abstract class MixinMouse {
39+
@Shadow
40+
@Final
41+
private MinecraftClient client;
42+
43+
@Shadow
44+
private int activeButton;
45+
46+
@Shadow
47+
private boolean middleButtonClicked;
48+
49+
@Shadow
50+
private double cursorDeltaY;
51+
52+
@Shadow
53+
private double cursorDeltaX;
54+
55+
@Inject(method = "method_1611", at = @At("HEAD"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
56+
public void preMouseClicked(boolean[] handled, double mouseX, double mouseY, int button, CallbackInfo info) {
57+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseClickedEvent.Pre(client.currentScreen, mouseX, mouseY, button))) {
58+
handled[0] = true;
59+
info.cancel();
60+
}
61+
}
62+
63+
@Inject(method = "method_1611", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
64+
private void postMouseClicked(boolean[] handled, double mouseX, double mouseY, int button, CallbackInfo info) {
65+
if (handled[0]) {
66+
return;
67+
}
68+
69+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseClickedEvent.Post(client.currentScreen, mouseX, mouseY, button))) {
70+
handled[0] = true;
71+
info.cancel();
72+
}
73+
}
74+
75+
@Inject(method = "method_1605", at = @At("HEAD"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
76+
private void preMouseReleased(boolean[] handled, double mouseX, double mouseY, int button, CallbackInfo info) {
77+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseReleasedEvent.Pre(client.currentScreen, mouseX, mouseY, button))) {
78+
handled[0] = true;
79+
info.cancel();
80+
}
81+
}
82+
83+
@Inject(method = "method_1605", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD)
84+
private void postMouseReleased(boolean[] handled, double mouseX, double mouseY, int button, CallbackInfo info) {
85+
if (handled[0]) {
86+
return;
87+
}
88+
89+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseReleasedEvent.Post(client.currentScreen, mouseX, mouseY, button))) {
90+
handled[0] = true;
91+
info.cancel();
92+
}
93+
}
94+
95+
@Inject(method = "method_1602", at = @At("HEAD"), cancellable = true)
96+
private void preMouseDragged(Element element, double mouseX, double mouseY, double deltaX, double deltaY, CallbackInfo info) {
97+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseDragEvent.Pre((Screen) element, mouseX, mouseY, activeButton, deltaX, deltaY))) {
98+
info.cancel();
99+
}
100+
}
101+
102+
@Inject(method = "method_1602", at = @At("RETURN"))
103+
private void postMouseDragged(Element element, double mouseX, double mouseY, double deltaX, double deltaY, CallbackInfo info) {
104+
MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseDragEvent.Post((Screen) element, mouseX, mouseY, activeButton, deltaX, deltaY));
105+
}
106+
107+
@Inject(method = "onMouseScroll", at = @At(value = "INVOKE",
108+
target = "Lnet/minecraft/client/gui/screen/Screen;mouseScrolled(DDD)Z",
109+
ordinal = 0), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
110+
private void preMouseScrolled(long window, double xOffset, double yOffset, CallbackInfo info, double amount, double mouseX, double mouseY) {
111+
if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseScrollEvent.Pre(client.currentScreen, mouseX, mouseY, amount))) {
112+
info.cancel();
113+
}
114+
}
115+
116+
@Inject(method = "onMouseScroll", at = @At(value = "INVOKE",
117+
target = "Lnet/minecraft/client/gui/screen/Screen;mouseScrolled(DDD)Z",
118+
ordinal = 0, shift = At.Shift.BY, by = 2), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
119+
private void postMouseScrolled(long window, double xOffset, double yOffset, CallbackInfo info, double amount, double mouseX, double mouseY) {
120+
MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseScrollEvent.Post(client.currentScreen, mouseX, mouseY, amount));
121+
}
122+
123+
public boolean isMiddleDown() {
124+
return this.middleButtonClicked;
125+
}
126+
127+
public double getXVelocity() {
128+
return this.cursorDeltaX;
129+
}
130+
131+
public double getYVelocity() {
132+
return this.cursorDeltaY;
133+
}
134+
}

0 commit comments

Comments
 (0)