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

Commit e1cd10e

Browse files
authored
Add GuiOpenEvent (#83)
* Add GuiOpenEvent * Override isCancelable to return true instead of using the annotation * coderbot's fix * Make the code work in both reobf and dev env * GuiOpenEvent cancel marker * replace gui with screen in comments
1 parent 5bf9149 commit e1cd10e

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed
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-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.minecraftforge.client.event;
21+
22+
import net.minecraftforge.eventbus.api.Event;
23+
24+
import net.minecraft.client.gui.screen.Screen;
25+
26+
/**
27+
* This event is called before any Screen will open. If you don't want this to
28+
* happen, cancel the event. If you want to override this Screen, simply set the
29+
* Screen variable to your own Screen.
30+
*
31+
* @author jk-5, Rikka0w0
32+
*/
33+
public class GuiOpenEvent extends Event {
34+
private Screen screen;
35+
36+
public GuiOpenEvent(Screen screen) {
37+
this.setGui(screen);
38+
}
39+
40+
public Screen getGui() {
41+
return screen;
42+
}
43+
44+
public void setGui(Screen screen) {
45+
this.screen = screen;
46+
}
47+
48+
@Override
49+
public boolean isCancelable() {
50+
return true;
51+
}
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.gui;
21+
22+
import net.minecraft.client.gui.screen.Screen;
23+
import net.minecraft.text.LiteralText;
24+
25+
public final class GuiOpenEventCancelMarker extends Screen {
26+
public static final GuiOpenEventCancelMarker INSTANCE = new GuiOpenEventCancelMarker();
27+
28+
private GuiOpenEventCancelMarker() {
29+
super(new LiteralText("Patchwork GuiOpenEvent cancel marker"));
30+
}
31+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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.mixin.gui;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.Shadow;
24+
import org.spongepowered.asm.mixin.injection.At;
25+
import org.spongepowered.asm.mixin.injection.At.Shift;
26+
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
28+
import org.spongepowered.asm.mixin.injection.Redirect;
29+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
30+
import net.minecraftforge.client.event.GuiOpenEvent;
31+
import net.minecraftforge.common.MinecraftForge;
32+
33+
import net.minecraft.client.MinecraftClient;
34+
import net.minecraft.client.gui.screen.Screen;
35+
36+
import net.patchworkmc.impl.gui.GuiOpenEventCancelMarker;
37+
38+
@Mixin(MinecraftClient.class)
39+
public class MixinMinecraftClient {
40+
private static final String PATCHWORK_YARN_MTD_OPENSCREEN = "openScreen(Lnet/minecraft/client/gui/screen/Screen;)V";
41+
private static final String PATCHWORK_YARN_MTD_SCREEN_REMOVE = "net/minecraft/client/gui/screen/Screen.removed()V";
42+
43+
// net.minecraft.client.gui.TitleScreen --> net.minecraft.class_442
44+
private static final String PATCHWORK_YARN_CLS_TITLESCREEN = "classValue=net/minecraft/client/gui/screen/TitleScreen";
45+
private static final String PATCHWORK_REOBF_CLS_TITLESCREEN = "classValue=net/minecraft/class_442";
46+
47+
private static Screen patchwork_oldScreen;
48+
49+
@Shadow
50+
public Screen currentScreen;
51+
52+
@Redirect(method = PATCHWORK_YARN_MTD_OPENSCREEN, at = @At(value = "INVOKE", target = PATCHWORK_YARN_MTD_SCREEN_REMOVE))
53+
private void patchwork_suppressScreenRemoved(Screen screen) {
54+
// No-op (handled in patchwork_fireOpenEvent)
55+
}
56+
57+
/**
58+
* patchwork_yarn_fireOpenEvent and patchwork_yarn_cancelOpening use @(value = "CONSTANT", args = xxx),
59+
* the classname specified in "args" is not processed by obfuscator, so we need to have 2 @ModifyVariable
60+
* here to make it work in both
61+
* the development(method name PATCHWORK_YARN_CLS_xxx) and
62+
* the obfuscated environment(PATCHWORK_REOBF_CLS_xxx).
63+
*/
64+
@ModifyVariable(method = PATCHWORK_YARN_MTD_OPENSCREEN, at = @At(value = "CONSTANT", args = PATCHWORK_YARN_CLS_TITLESCREEN, shift = Shift.BY, by = -2), require = 0)
65+
public Screen patchwork_yarn_fireOpenEvent(Screen screen) {
66+
return patchwork_impl_fireOpenEvent(screen);
67+
}
68+
69+
@ModifyVariable(method = PATCHWORK_YARN_MTD_OPENSCREEN, at = @At(value = "CONSTANT", args = PATCHWORK_REOBF_CLS_TITLESCREEN, shift = Shift.BY, by = -2), require = 0)
70+
public Screen patchwork_reobf_fireOpenEvent(Screen screen) {
71+
return patchwork_impl_fireOpenEvent(screen);
72+
}
73+
74+
@Inject(method = PATCHWORK_YARN_MTD_OPENSCREEN, at = @At(value = "CONSTANT", args = PATCHWORK_YARN_CLS_TITLESCREEN), cancellable = true, require = 0)
75+
public void patchwork_yarn_cancelOpening(Screen screen, CallbackInfo callback) {
76+
patchwork_impl_cancelOpening(screen, callback);
77+
}
78+
79+
@Inject(method = PATCHWORK_YARN_MTD_OPENSCREEN, at = @At(value = "CONSTANT", args = PATCHWORK_REOBF_CLS_TITLESCREEN), cancellable = true, require = 0)
80+
public void patchwork_reobf_cancelOpening(Screen screen, CallbackInfo callback) {
81+
patchwork_impl_cancelOpening(screen, callback);
82+
}
83+
84+
/**
85+
* <p>Sets the argument Screen as the main (topmost visible) screen.</p>
86+
* <strong>WARNING</strong>: This method is not thread-safe. Opening Screens from a
87+
* thread other than the main thread may cause many different issues, including
88+
* the Screen being rendered before it has initialized (leading to unusual
89+
* crashes). If on a thread other than the main thread, use
90+
* {@link net.minecraft.client.MinecraftClient#executeTask}:
91+
*
92+
* <pre>
93+
* MinecraftClient.getInstance().executeTask(() -> MinecraftClient.getInstance().openScreen(screen));
94+
* </pre>
95+
*
96+
* @return the screen object which can be replaced during the GuiOpenEvent.
97+
*/
98+
private Screen patchwork_impl_fireOpenEvent(Screen screen) {
99+
// This is called just before: if (screen instanceof TitleScreen ... )
100+
// We need to save a copy of currentScreen, just in case if it gets changed during GuiOpenEvent
101+
patchwork_oldScreen = this.currentScreen;
102+
GuiOpenEvent event = new GuiOpenEvent(screen);
103+
104+
if (MinecraftForge.EVENT_BUS.post(event)) {
105+
return GuiOpenEventCancelMarker.INSTANCE;
106+
} else {
107+
return event.getGui();
108+
}
109+
}
110+
111+
private void patchwork_impl_cancelOpening(Screen screen, CallbackInfo callback) {
112+
if (screen == GuiOpenEventCancelMarker.INSTANCE) {
113+
patchwork_oldScreen = null;
114+
callback.cancel();
115+
return;
116+
}
117+
118+
if (patchwork_oldScreen != null && screen != patchwork_oldScreen) {
119+
patchwork_oldScreen.removed();
120+
}
121+
122+
patchwork_oldScreen = null;
123+
}
124+
}

patchwork-gui/src/main/resources/patchwork-gui.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"MixinAbstractInventoryScreen",
77
"MixinGameRenderer",
88
"MixinKeyboard",
9+
"MixinMinecraftClient",
910
"MixinMouse",
1011
"MixinScreen"
1112
],

0 commit comments

Comments
 (0)