From 58278dd8b458ea29f28026f627c62817cd042b60 Mon Sep 17 00:00:00 2001 From: Kneelawk Date: Wed, 17 Apr 2024 19:20:16 -0700 Subject: [PATCH] Move events to platforms --- .../api/event/BeginFrameCallback.java | 15 +++++++++++++ .../EndClientResourceReloadCallback.java | 21 +++++++++++++++++++ .../event/ReloadLevelRendererCallback.java | 17 +++++++++++++++ .../api/event/RenderStageCallback.java | 15 +++++++++++++ .../flywheel/impl/mixin/MinecraftMixin.java | 15 ++++++------- .../flywheel/api/event/BeginFrameEvent.java | 0 .../event/EndClientResourceReloadEvent.java | 0 .../api/event/ReloadLevelRendererEvent.java | 0 .../flywheel/api/event/RenderStageEvent.java | 0 9 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 fabric/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameCallback.java create mode 100644 fabric/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadCallback.java create mode 100644 fabric/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererCallback.java create mode 100644 fabric/src/main/java/com/jozufozu/flywheel/api/event/RenderStageCallback.java rename {common => forge}/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameEvent.java (100%) rename {common => forge}/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadEvent.java (100%) rename {common => forge}/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererEvent.java (100%) rename {common => forge}/src/main/java/com/jozufozu/flywheel/api/event/RenderStageEvent.java (100%) diff --git a/fabric/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameCallback.java b/fabric/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameCallback.java new file mode 100644 index 000000000..47d16fd95 --- /dev/null +++ b/fabric/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameCallback.java @@ -0,0 +1,15 @@ +package com.jozufozu.flywheel.api.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +@FunctionalInterface +public interface BeginFrameCallback { + Event EVENT = EventFactory.createArrayBacked(BeginFrameCallback.class, callbacks -> context -> { + for (BeginFrameCallback callback : callbacks) { + callback.onBeginFrame(context); + } + }); + + void onBeginFrame(RenderContext context); +} diff --git a/fabric/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadCallback.java b/fabric/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadCallback.java new file mode 100644 index 000000000..d6f1a8644 --- /dev/null +++ b/fabric/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadCallback.java @@ -0,0 +1,21 @@ +package com.jozufozu.flywheel.api.event; + +import java.util.Optional; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.client.Minecraft; +import net.minecraft.server.packs.resources.ResourceManager; + +@FunctionalInterface +public interface EndClientResourceReloadCallback { + Event EVENT = EventFactory.createArrayBacked(EndClientResourceReloadCallback.class, + callbacks -> (minecraft, resourceManager, initialReload, error) -> { + for (EndClientResourceReloadCallback callback : callbacks) { + callback.onEndClientResourceReload(minecraft, resourceManager, initialReload, error); + } + }); + + void onEndClientResourceReload(Minecraft minecraft, ResourceManager resourceManager, boolean initialReload, + Optional error); +} diff --git a/fabric/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererCallback.java b/fabric/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererCallback.java new file mode 100644 index 000000000..ca16d46a3 --- /dev/null +++ b/fabric/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererCallback.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.api.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.client.multiplayer.ClientLevel; + +@FunctionalInterface +public interface ReloadLevelRendererCallback { + Event EVENT = + EventFactory.createArrayBacked(ReloadLevelRendererCallback.class, callbacks -> level -> { + for (ReloadLevelRendererCallback callback : callbacks) { + callback.onReloadLevelRenderer(level); + } + }); + + void onReloadLevelRenderer(ClientLevel level); +} diff --git a/fabric/src/main/java/com/jozufozu/flywheel/api/event/RenderStageCallback.java b/fabric/src/main/java/com/jozufozu/flywheel/api/event/RenderStageCallback.java new file mode 100644 index 000000000..27170fda5 --- /dev/null +++ b/fabric/src/main/java/com/jozufozu/flywheel/api/event/RenderStageCallback.java @@ -0,0 +1,15 @@ +package com.jozufozu.flywheel.api.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +public interface RenderStageCallback { + Event EVENT = + EventFactory.createArrayBacked(RenderStageCallback.class, callbacks -> (context, stage) -> { + for (RenderStageCallback callback : callbacks) { + callback.onRenderStage(context, stage); + } + }); + + void onRenderStage(RenderContext context, RenderStage stage); +} diff --git a/fabric/src/main/java/com/jozufozu/flywheel/impl/mixin/MinecraftMixin.java b/fabric/src/main/java/com/jozufozu/flywheel/impl/mixin/MinecraftMixin.java index f76827547..c1ec8c809 100644 --- a/fabric/src/main/java/com/jozufozu/flywheel/impl/mixin/MinecraftMixin.java +++ b/fabric/src/main/java/com/jozufozu/flywheel/impl/mixin/MinecraftMixin.java @@ -3,10 +3,6 @@ package com.jozufozu.flywheel.impl.mixin; import java.util.Optional; import java.util.concurrent.CompletableFuture; -import com.jozufozu.flywheel.api.event.EndClientResourceReloadEvent; - -import net.minecraftforge.fml.ModLoader; - import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -14,6 +10,8 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import com.jozufozu.flywheel.api.event.EndClientResourceReloadCallback; + import net.minecraft.client.Minecraft; import net.minecraft.server.packs.resources.ReloadableResourceManager; @@ -25,11 +23,14 @@ abstract class MinecraftMixin { @Inject(method = "method_24040", at = @At("HEAD")) private void flywheel$onEndInitialResourceReload(Optional error, CallbackInfo ci) { - ModLoader.get().postEvent(new EndClientResourceReloadEvent((Minecraft) (Object) this, resourceManager, true, error)); + EndClientResourceReloadCallback.EVENT.invoker() + .onEndClientResourceReload((Minecraft) (Object) this, resourceManager, true, error); } @Inject(method = "method_24228", at = @At("HEAD")) - private void flywheel$onEndManualResourceReload(boolean recovery, CompletableFuture future, Optional error, CallbackInfo ci) { - ModLoader.get().postEvent(new EndClientResourceReloadEvent((Minecraft) (Object) this, resourceManager, false, error)); + private void flywheel$onEndManualResourceReload(boolean recovery, CompletableFuture future, + Optional error, CallbackInfo ci) { + EndClientResourceReloadCallback.EVENT.invoker() + .onEndClientResourceReload((Minecraft) (Object) this, resourceManager, false, error); } } diff --git a/common/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameEvent.java b/forge/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameEvent.java similarity index 100% rename from common/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameEvent.java rename to forge/src/main/java/com/jozufozu/flywheel/api/event/BeginFrameEvent.java diff --git a/common/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadEvent.java b/forge/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadEvent.java similarity index 100% rename from common/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadEvent.java rename to forge/src/main/java/com/jozufozu/flywheel/api/event/EndClientResourceReloadEvent.java diff --git a/common/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererEvent.java b/forge/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererEvent.java similarity index 100% rename from common/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererEvent.java rename to forge/src/main/java/com/jozufozu/flywheel/api/event/ReloadLevelRendererEvent.java diff --git a/common/src/main/java/com/jozufozu/flywheel/api/event/RenderStageEvent.java b/forge/src/main/java/com/jozufozu/flywheel/api/event/RenderStageEvent.java similarity index 100% rename from common/src/main/java/com/jozufozu/flywheel/api/event/RenderStageEvent.java rename to forge/src/main/java/com/jozufozu/flywheel/api/event/RenderStageEvent.java