|
| 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 | +} |
0 commit comments