From f1509146f504707dfab3e9abac36038f94040b64 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Fri, 27 May 2022 23:39:11 +0700 Subject: universal events add mixinextras and keventbus --- .../java/cc/polyfrost/oneconfig/OneConfig.java | 7 +-- .../polyfrost/oneconfig/events/EventManager.java | 17 ++++++++ .../oneconfig/events/event/CancellableEvent.java | 5 +++ .../oneconfig/events/event/HudRenderEvent.java | 9 ++++ .../oneconfig/events/event/RenderEvent.java | 11 +++++ .../oneconfig/events/event/ScreenOpenEvent.java | 11 +++++ .../cc/polyfrost/oneconfig/events/event/Stage.java | 6 +++ .../oneconfig/events/event/TickEvent.java | 9 ++++ .../java/cc/polyfrost/oneconfig/hud/HudCore.java | 10 ++--- .../cc/polyfrost/oneconfig/lwjgl/BlurHandler.java | 23 +++++----- .../oneconfig/mixin/GuiIngameForgeMixin.java | 17 ++++++++ .../polyfrost/oneconfig/mixin/MinecraftMixin.java | 51 ++++++++++++++++++++++ .../oneconfig/plugin/OneConfigMixinPlugin.java | 2 + .../cc/polyfrost/oneconfig/utils/TickDelay.java | 25 ++++------- 14 files changed, 166 insertions(+), 37 deletions(-) create mode 100644 src/main/java/cc/polyfrost/oneconfig/events/EventManager.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/events/event/CancellableEvent.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/events/event/HudRenderEvent.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/events/event/RenderEvent.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/events/event/ScreenOpenEvent.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/events/event/Stage.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/events/event/TickEvent.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java (limited to 'src/main/java') diff --git a/src/main/java/cc/polyfrost/oneconfig/OneConfig.java b/src/main/java/cc/polyfrost/oneconfig/OneConfig.java index e612be7..0eb9ec3 100644 --- a/src/main/java/cc/polyfrost/oneconfig/OneConfig.java +++ b/src/main/java/cc/polyfrost/oneconfig/OneConfig.java @@ -5,16 +5,15 @@ import cc.polyfrost.oneconfig.config.OneConfigConfig; import cc.polyfrost.oneconfig.config.core.ConfigCore; import cc.polyfrost.oneconfig.config.data.Mod; import cc.polyfrost.oneconfig.config.data.ModType; +import cc.polyfrost.oneconfig.events.EventManager; import cc.polyfrost.oneconfig.hud.HudCore; import cc.polyfrost.oneconfig.lwjgl.BlurHandler; import cc.polyfrost.oneconfig.lwjgl.RenderManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.Images; import cc.polyfrost.oneconfig.test.TestConfig; -import cc.polyfrost.oneconfig.utils.InputUtils; import net.minecraft.launchwrapper.Launch; import net.minecraftforge.client.ClientCommandHandler; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.DummyModContainer; import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.ModContainer; @@ -51,9 +50,7 @@ public class OneConfig { BlurHandler.INSTANCE.load(); testConfig = new TestConfig(); ClientCommandHandler.instance.registerCommand(new OneConfigCommand()); - MinecraftForge.EVENT_BUS.register(this); - MinecraftForge.EVENT_BUS.register(new HudCore()); - MinecraftForge.EVENT_BUS.register(new InputUtils()); + EventManager.INSTANCE.getEventBus().register(new HudCore()); RenderManager.setupAndDraw((vg) -> { RenderManager.drawRoundedRect(vg, -100, -100, 50, 50, -1, 12f); RenderManager.drawString(vg, "OneConfig loading...", -100, -100, -1, 12f, Fonts.MEDIUM); diff --git a/src/main/java/cc/polyfrost/oneconfig/events/EventManager.java b/src/main/java/cc/polyfrost/oneconfig/events/EventManager.java new file mode 100644 index 0000000..956c9b6 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/EventManager.java @@ -0,0 +1,17 @@ +package cc.polyfrost.oneconfig.events; + +import cc.polyfrost.oneconfig.libs.eventbus.EventBus; +import cc.polyfrost.oneconfig.libs.eventbus.invokers.LMFInvoker; + +public final class EventManager { + private EventManager() { + + } + + public static final EventManager INSTANCE = new EventManager(); + private final EventBus eventBus = new EventBus(new LMFInvoker(), Throwable::printStackTrace); + + public EventBus getEventBus() { + return eventBus; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/CancellableEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/CancellableEvent.java new file mode 100644 index 0000000..2afa613 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/CancellableEvent.java @@ -0,0 +1,5 @@ +package cc.polyfrost.oneconfig.events.event; + +public class CancellableEvent { + public boolean isCancelled = false; +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/HudRenderEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/HudRenderEvent.java new file mode 100644 index 0000000..974508d --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/HudRenderEvent.java @@ -0,0 +1,9 @@ +package cc.polyfrost.oneconfig.events.event; + +public class HudRenderEvent { + public final float deltaTicks; + + public HudRenderEvent(float deltaTicks) { + this.deltaTicks = deltaTicks; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/RenderEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/RenderEvent.java new file mode 100644 index 0000000..aa305a1 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/RenderEvent.java @@ -0,0 +1,11 @@ +package cc.polyfrost.oneconfig.events.event; + +public class RenderEvent { + public final Stage stage; + public final float deltaTicks; + + public RenderEvent(Stage stage, float deltaTicks) { + this.stage = stage; + this.deltaTicks = deltaTicks; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/ScreenOpenEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/ScreenOpenEvent.java new file mode 100644 index 0000000..d3a5b66 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/ScreenOpenEvent.java @@ -0,0 +1,11 @@ +package cc.polyfrost.oneconfig.events.event; + +import net.minecraft.client.gui.GuiScreen; + +public class ScreenOpenEvent extends CancellableEvent { + public final GuiScreen screen; + + public ScreenOpenEvent(GuiScreen screen) { + this.screen = screen; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/Stage.java b/src/main/java/cc/polyfrost/oneconfig/events/event/Stage.java new file mode 100644 index 0000000..77c93b8 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/Stage.java @@ -0,0 +1,6 @@ +package cc.polyfrost.oneconfig.events.event; + +public enum Stage { + START, + END +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/TickEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/TickEvent.java new file mode 100644 index 0000000..5eb776c --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/TickEvent.java @@ -0,0 +1,9 @@ +package cc.polyfrost.oneconfig.events.event; + +public class TickEvent { + public final Stage stage; + + public TickEvent(Stage stage) { + this.stage = stage; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/HudCore.java b/src/main/java/cc/polyfrost/oneconfig/hud/HudCore.java index 87336e9..200d403 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/HudCore.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/HudCore.java @@ -1,8 +1,8 @@ package cc.polyfrost.oneconfig.hud; +import cc.polyfrost.oneconfig.events.event.HudRenderEvent; +import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; import cc.polyfrost.oneconfig.libs.universal.UResolution; -import net.minecraftforge.client.event.RenderGameOverlayEvent; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import java.util.ArrayList; @@ -10,9 +10,9 @@ public class HudCore { public static ArrayList huds = new ArrayList<>(); public static boolean editing = false; - @SubscribeEvent - public void onRender(RenderGameOverlayEvent.Post event) { - if (event.type != RenderGameOverlayEvent.ElementType.ALL || editing) return; + @Subscribe + public void onRender(HudRenderEvent event) { + if (editing) return; for (BasicHud hud : huds) { if (hud.enabled) hud.drawAll(hud.getXScaled(UResolution.getScaledWidth()), hud.getYScaled(UResolution.getScaledHeight()), hud.scale, true); diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java index 090121b..a769e12 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java @@ -1,6 +1,11 @@ package cc.polyfrost.oneconfig.lwjgl; +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.RenderEvent; +import cc.polyfrost.oneconfig.events.event.ScreenOpenEvent; +import cc.polyfrost.oneconfig.events.event.Stage; import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; import cc.polyfrost.oneconfig.libs.universal.UMinecraft; import cc.polyfrost.oneconfig.libs.universal.UScreen; import cc.polyfrost.oneconfig.mixin.ShaderGroupAccessor; @@ -9,10 +14,6 @@ import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.shader.Shader; import net.minecraft.client.shader.ShaderUniform; import net.minecraft.util.ResourceLocation; -import net.minecraftforge.client.event.GuiOpenEvent; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.TickEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -44,17 +45,17 @@ public class BlurHandler { * Simply initializes the blur mod so events are properly handled by forge. */ public void load() { - MinecraftForge.EVENT_BUS.register(this); + EventManager.INSTANCE.getEventBus().register(this); } - @SubscribeEvent - public void onGuiChange(GuiOpenEvent event) { - reloadBlur(event.gui); + @Subscribe + public void onGuiChange(ScreenOpenEvent event) { + reloadBlur(event.screen); } - @SubscribeEvent - public void onRenderTick(final TickEvent.RenderTickEvent event) { - if (event.phase != TickEvent.Phase.END) { + @Subscribe + public void onRenderTick(RenderEvent event) { + if (event.stage != Stage.END) { return; } diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java new file mode 100644 index 0000000..1774fdc --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java @@ -0,0 +1,17 @@ +package cc.polyfrost.oneconfig.mixin; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.HudRenderEvent; +import net.minecraftforge.client.GuiIngameForge; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(GuiIngameForge.class) +public class GuiIngameForgeMixin { + @Inject(method = "renderGameOverlay", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/GuiIngameForge;post(Lnet/minecraftforge/client/event/RenderGameOverlayEvent$ElementType;)V", shift = At.Shift.AFTER)) + private void onRenderGameOverlay(float partialTicks, CallbackInfo ci) { + EventManager.INSTANCE.getEventBus().post(new HudRenderEvent(partialTicks)); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java new file mode 100644 index 0000000..05d2819 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java @@ -0,0 +1,51 @@ +package cc.polyfrost.oneconfig.mixin; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.RenderEvent; +import cc.polyfrost.oneconfig.events.event.ScreenOpenEvent; +import cc.polyfrost.oneconfig.events.event.Stage; +import cc.polyfrost.oneconfig.events.event.TickEvent; +import cc.polyfrost.oneconfig.libs.mixinextras.injector.ModifyExpressionValue; +import net.minecraft.client.Minecraft; +import net.minecraft.util.Timer; +import net.minecraftforge.client.event.GuiOpenEvent; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(Minecraft.class) +public class MinecraftMixin { + @Shadow private Timer timer; + + @Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onRenderTickStart(F)V", shift = At.Shift.AFTER)) + private void onRenderTickStart(CallbackInfo ci) { + EventManager.INSTANCE.getEventBus().post(new RenderEvent(Stage.START, timer.renderPartialTicks)); + } + + @Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onRenderTickEnd(F)V", shift = At.Shift.AFTER)) + private void onRenderTickEnd(CallbackInfo ci) { + EventManager.INSTANCE.getEventBus().post(new RenderEvent(Stage.END, timer.renderPartialTicks)); + } + + @Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onPreClientTick()V", shift = At.Shift.AFTER)) + private void onClientTickStart(CallbackInfo ci) { + EventManager.INSTANCE.getEventBus().post(new TickEvent(Stage.START)); + } + + @Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onPostClientTick()V", shift = At.Shift.AFTER)) + private void onClientTickEnd(CallbackInfo ci) { + EventManager.INSTANCE.getEventBus().post(new TickEvent(Stage.END)); + } + + @ModifyExpressionValue(method = "displayGuiScreen", at = @At(value = "NEW", target = "Lnet/minecraftforge/client/event/GuiOpenEvent;(Lnet/minecraft/client/gui/GuiScreen;)V")) + private GuiOpenEvent onGuiOpenEvent(GuiOpenEvent screen) { + ScreenOpenEvent event = new ScreenOpenEvent(screen.gui); + EventManager.INSTANCE.getEventBus().post(event); + if (event.isCancelled) { + screen.setCanceled(true); + } + return screen; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/plugin/OneConfigMixinPlugin.java b/src/main/java/cc/polyfrost/oneconfig/plugin/OneConfigMixinPlugin.java index 951e942..f5cac15 100644 --- a/src/main/java/cc/polyfrost/oneconfig/plugin/OneConfigMixinPlugin.java +++ b/src/main/java/cc/polyfrost/oneconfig/plugin/OneConfigMixinPlugin.java @@ -1,5 +1,6 @@ package cc.polyfrost.oneconfig.plugin; +import cc.polyfrost.oneconfig.libs.mixinextras.MixinExtrasBootstrap; import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; @@ -12,6 +13,7 @@ public class OneConfigMixinPlugin implements IMixinConfigPlugin { @Override public void onLoad(String mixinPackage) { + MixinExtrasBootstrap.init(); try { Class.forName("gg.essential.vigilance.Vigilant"); isVigilance = true; diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java b/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java index 08f5fce..2fa2017 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java @@ -1,36 +1,29 @@ package cc.polyfrost.oneconfig.utils; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.TickEvent; +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.Stage; +import cc.polyfrost.oneconfig.events.event.TickEvent; +import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; public class TickDelay { private int delay; private final Runnable function; public TickDelay(Runnable functionName, int ticks) { - register(); + EventManager.INSTANCE.getEventBus().register(this); delay = ticks; function = functionName; } - @SubscribeEvent - public void onTick(TickEvent.ClientTickEvent event) { - if (event.phase == TickEvent.Phase.START) { + @Subscribe + public void onTick(TickEvent event) { + if (event.stage == Stage.START) { // Delay expired if (delay < 1) { function.run(); - destroy(); + EventManager.INSTANCE.getEventBus().unregister(this); } delay--; } } - - private void destroy() { - MinecraftForge.EVENT_BUS.unregister(this); - } - - private void register() { - MinecraftForge.EVENT_BUS.register(this); - } } -- cgit