From 93b129e69944a95cb528ec9a82d64c72b9798f3c Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 5 Mar 2022 17:37:04 -0600 Subject: [PATCH] Update the DependencyHandler to support circular references --- core | 2 +- .../lod/fabric/{Main.java => FabricMain.java} | 31 +++++++++++++------ .../lod/fabric/mixins/MixinMinecraft.java | 4 +-- .../mixins/events/MixinBlockUpdate.java | 4 +-- .../mixins/events/MixinClientLevel.java | 4 +-- .../fabric/mixins/events/MixinMinecraft.java | 6 ++-- .../mixins/events/MixinServerLevel.java | 4 +-- ...ySetup.java => FabricDependencySetup.java} | 11 ++++++- .../java/com/seibel/lod/forge/ForgeMain.java | 13 +++++--- .../forge/wrappers/ForgeDependencySetup.java | 10 +++++- 10 files changed, 62 insertions(+), 27 deletions(-) rename fabric/src/main/java/com/seibel/lod/fabric/{Main.java => FabricMain.java} (85%) rename fabric/src/main/java/com/seibel/lod/fabric/wrappers/{DependencySetup.java => FabricDependencySetup.java} (62%) diff --git a/core b/core index 2149da59d..58392a8ac 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 2149da59df12c2aa55a69c3e93bfcbc19e2f26ae +Subproject commit 58392a8ac13837f67ca3d10ff25e3f6aa949a703 diff --git a/fabric/src/main/java/com/seibel/lod/fabric/Main.java b/fabric/src/main/java/com/seibel/lod/fabric/FabricMain.java similarity index 85% rename from fabric/src/main/java/com/seibel/lod/fabric/Main.java rename to fabric/src/main/java/com/seibel/lod/fabric/FabricMain.java index a3b45a640..f39d6f41c 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/Main.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/FabricMain.java @@ -23,6 +23,7 @@ import com.seibel.lod.common.LodCommonMain; import com.seibel.lod.core.ModInfo; import com.seibel.lod.core.api.ApiShared; import com.seibel.lod.core.api.ClientApi; +import com.seibel.lod.core.handlers.dependencyInjection.DependencyHandler; import com.seibel.lod.core.handlers.dependencyInjection.ModAccessorHandler; import com.seibel.lod.core.handlers.dependencyInjection.SingletonHandler; import com.seibel.lod.core.wrapperInterfaces.modAccessor.IModChecker; @@ -31,7 +32,7 @@ import com.seibel.lod.core.wrapperInterfaces.modAccessor.ISodiumAccessor; import com.seibel.lod.fabric.wrappers.modAccessor.ModChecker; import com.seibel.lod.fabric.wrappers.modAccessor.OptifineAccessor; import com.seibel.lod.fabric.wrappers.modAccessor.SodiumAccessor; -import com.seibel.lod.fabric.wrappers.DependencySetup; +import com.seibel.lod.fabric.wrappers.FabricDependencySetup; import net.fabricmc.api.ClientModInitializer; @@ -44,7 +45,7 @@ import net.fabricmc.api.ClientModInitializer; * @author Ran * @version 12-1-2021 */ -public class Main implements ClientModInitializer +public class FabricMain implements ClientModInitializer { // This is a client mod so it should implement ClientModInitializer and in fabric.mod.json it should have "environment": "client" // Once it works on servers change the implement to ModInitializer and in fabric.mod.json it should be "environment": "*" @@ -63,25 +64,37 @@ public class Main implements ClientModInitializer public static void init() { LodCommonMain.initConfig(); LodCommonMain.startup(null, false); - DependencySetup.createInitialBindings(); - SingletonHandler.bind(IModChecker.class, ModChecker.INSTANCE); + ApiShared.LOGGER.info(ModInfo.READABLE_NAME + ", Version: " + ModInfo.VERSION); - - // Check if this works - client_proxy = new ClientProxy(); - client_proxy.registerEvents(); + + + // make sure the dependencies are set up before the mod needs them + FabricDependencySetup.createInitialBindings(); + FabricDependencySetup.finishBinding(); + + // mod dependencies if (SingletonHandler.get(IModChecker.class).isModLoaded("sodium")) { ModAccessorHandler.bind(ISodiumAccessor.class, new SodiumAccessor()); } if (SingletonHandler.get(IModChecker.class).isModLoaded("optifine")) { ModAccessorHandler.bind(IOptifineAccessor.class, new OptifineAccessor()); } + + ModAccessorHandler.finishBinding(); + + + // Check if this works + client_proxy = new ClientProxy(); + client_proxy.registerEvents(); } public static void initServer() { LodCommonMain.initConfig(); LodCommonMain.startup(null, true); - DependencySetup.createInitialBindings(); + ApiShared.LOGGER.info(ModInfo.READABLE_NAME + ", Version: " + ModInfo.VERSION); + + FabricDependencySetup.createInitialBindings(); + FabricDependencySetup.finishBinding(); } } diff --git a/fabric/src/main/java/com/seibel/lod/fabric/mixins/MixinMinecraft.java b/fabric/src/main/java/com/seibel/lod/fabric/mixins/MixinMinecraft.java index 7e4279abd..c661cffea 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/mixins/MixinMinecraft.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/mixins/MixinMinecraft.java @@ -1,6 +1,6 @@ package com.seibel.lod.fabric.mixins; -import com.seibel.lod.fabric.Main; +import com.seibel.lod.fabric.FabricMain; import net.minecraft.client.Minecraft; import net.minecraft.client.main.GameConfig; import org.spongepowered.asm.mixin.Mixin; @@ -16,6 +16,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; public class MixinMinecraft { @Inject(method = "", at = @At("TAIL")) private void startMod(GameConfig gameConfig, CallbackInfo ci) { - Main.init(); + FabricMain.init(); } } diff --git a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinBlockUpdate.java b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinBlockUpdate.java index 3652bd34b..a5531de82 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinBlockUpdate.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinBlockUpdate.java @@ -1,6 +1,6 @@ package com.seibel.lod.fabric.mixins.events; -import com.seibel.lod.fabric.Main; +import com.seibel.lod.fabric.FabricMain; import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; import net.minecraft.network.protocol.game.ClientGamePacketListener; @@ -23,6 +23,6 @@ public abstract class MixinBlockUpdate { @Inject(method = "handle(Lnet/minecraft/network/protocol/game/ClientGamePacketListener;)V", at = @At("TAIL")) private void onBlockUpdate(ClientGamePacketListener clientGamePacketListener, CallbackInfo ci) { - Main.client_proxy.blockChangeEvent(Minecraft.getInstance().player.clientLevel, this.getPos()); + FabricMain.client_proxy.blockChangeEvent(Minecraft.getInstance().player.clientLevel, this.getPos()); } } \ No newline at end of file diff --git a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinClientLevel.java b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinClientLevel.java index 24856cf04..5fc20babc 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinClientLevel.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinClientLevel.java @@ -1,6 +1,6 @@ package com.seibel.lod.fabric.mixins.events; -import com.seibel.lod.fabric.Main; +import com.seibel.lod.fabric.FabricMain; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.client.renderer.LevelRenderer; @@ -23,6 +23,6 @@ import java.util.function.Supplier; public class MixinClientLevel { @Inject(method = "", at = @At("TAIL")) private void loadWorldEvent(ClientPacketListener clientPacketListener, ClientLevel.ClientLevelData clientLevelData, ResourceKey resourceKey, DimensionType dimensionType, int i, Supplier supplier, LevelRenderer levelRenderer, boolean bl, long l, CallbackInfo ci) { - Main.client_proxy.worldLoadEvent((ClientLevel) (Object) this); + FabricMain.client_proxy.worldLoadEvent((ClientLevel) (Object) this); } } diff --git a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinMinecraft.java b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinMinecraft.java index cfaa4f413..bad25ffb7 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinMinecraft.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinMinecraft.java @@ -1,6 +1,6 @@ package com.seibel.lod.fabric.mixins.events; -import com.seibel.lod.fabric.Main; +import com.seibel.lod.fabric.FabricMain; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.multiplayer.ClientLevel; @@ -23,11 +23,11 @@ public class MixinMinecraft { @Inject(method = "setLevel", at = @At("HEAD")) private void unloadWorldEvent_sL(ClientLevel clientLevel, CallbackInfo ci) { - if (level != null) Main.client_proxy.worldUnloadEvent(level); + if (level != null) FabricMain.client_proxy.worldUnloadEvent(level); } @Inject(method = "clearLevel(Lnet/minecraft/client/gui/screens/Screen;)V", at = @At("HEAD")) private void unloadWorldEvent_cL(Screen screen, CallbackInfo ci) { - if (this.level != null) Main.client_proxy.worldUnloadEvent(this.level); + if (this.level != null) FabricMain.client_proxy.worldUnloadEvent(this.level); } } diff --git a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinServerLevel.java b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinServerLevel.java index becdb177d..e1fdfc8cb 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinServerLevel.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/mixins/events/MixinServerLevel.java @@ -1,6 +1,6 @@ package com.seibel.lod.fabric.mixins.events; -import com.seibel.lod.fabric.Main; +import com.seibel.lod.fabric.FabricMain; import net.minecraft.server.level.ServerLevel; import net.minecraft.util.ProgressListener; import org.spongepowered.asm.mixin.Mixin; @@ -16,6 +16,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; public class MixinServerLevel { @Inject(method = "save", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerChunkCache;save(Z)V", shift = At.Shift.AFTER)) private void saveWorldEvent(ProgressListener progressListener, boolean bl, boolean bl2, CallbackInfo ci) { - Main.client_proxy.worldSaveEvent(); + FabricMain.client_proxy.worldSaveEvent(); } } diff --git a/fabric/src/main/java/com/seibel/lod/fabric/wrappers/DependencySetup.java b/fabric/src/main/java/com/seibel/lod/fabric/wrappers/FabricDependencySetup.java similarity index 62% rename from fabric/src/main/java/com/seibel/lod/fabric/wrappers/DependencySetup.java rename to fabric/src/main/java/com/seibel/lod/fabric/wrappers/FabricDependencySetup.java index 0c83b354f..c1e922a8d 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/wrappers/DependencySetup.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/wrappers/FabricDependencySetup.java @@ -1,7 +1,10 @@ package com.seibel.lod.fabric.wrappers; +import com.seibel.lod.core.handlers.dependencyInjection.DependencyHandler; import com.seibel.lod.core.handlers.dependencyInjection.SingletonHandler; import com.seibel.lod.core.wrapperInterfaces.config.ILodConfigWrapperSingleton; +import com.seibel.lod.core.wrapperInterfaces.modAccessor.IModChecker; +import com.seibel.lod.fabric.wrappers.modAccessor.ModChecker; import com.seibel.lod.common.wrappers.config.LodConfigWrapperSingleton; /** @@ -14,10 +17,16 @@ import com.seibel.lod.common.wrappers.config.LodConfigWrapperSingleton; * @author Ran * @version 12-1-2021 */ -public class DependencySetup +public class FabricDependencySetup { public static void createInitialBindings() { SingletonHandler.bind(ILodConfigWrapperSingleton.class, LodConfigWrapperSingleton.INSTANCE);; + SingletonHandler.bind(IModChecker.class, ModChecker.INSTANCE); + } + + public static void finishBinding() + { + SingletonHandler.finishBinding(); } } diff --git a/forge/src/main/java/com/seibel/lod/forge/ForgeMain.java b/forge/src/main/java/com/seibel/lod/forge/ForgeMain.java index 558f9830e..89c93afc5 100644 --- a/forge/src/main/java/com/seibel/lod/forge/ForgeMain.java +++ b/forge/src/main/java/com/seibel/lod/forge/ForgeMain.java @@ -67,16 +67,21 @@ public class ForgeMain implements LodForgeMethodCaller private void init(final FMLCommonSetupEvent event) { - // make sure the dependencies are set up before the mod needs them + ApiShared.LOGGER.info("Distant Horizons initializing..."); + LodCommonMain.initConfig(); LodCommonMain.startup(this, !FMLLoader.getDist().isClient()); + + // make sure the dependencies are set up before the mod needs them ForgeDependencySetup.createInitialBindings(); - ApiShared.LOGGER.info("Distant Horizons initializing..."); - - SingletonHandler.bind(IModChecker.class, ModChecker.INSTANCE); + ForgeDependencySetup.finishBinding(); + + // mod dependencies if (ReflectionHandler.instance.optifinePresent()) { ModAccessorHandler.bind(IOptifineAccessor.class, new OptifineAccessor()); } + + ModAccessorHandler.finishBinding(); } diff --git a/forge/src/main/java/com/seibel/lod/forge/wrappers/ForgeDependencySetup.java b/forge/src/main/java/com/seibel/lod/forge/wrappers/ForgeDependencySetup.java index cb419dbb9..d5ae27a40 100644 --- a/forge/src/main/java/com/seibel/lod/forge/wrappers/ForgeDependencySetup.java +++ b/forge/src/main/java/com/seibel/lod/forge/wrappers/ForgeDependencySetup.java @@ -3,6 +3,8 @@ package com.seibel.lod.forge.wrappers; import com.seibel.lod.common.wrappers.config.LodConfigWrapperSingleton; import com.seibel.lod.core.handlers.dependencyInjection.SingletonHandler; import com.seibel.lod.core.wrapperInterfaces.config.ILodConfigWrapperSingleton; +import com.seibel.lod.core.wrapperInterfaces.modAccessor.IModChecker; +import com.seibel.lod.forge.wrappers.modAccessor.ModChecker; /** * Binds all necessary dependencies so we @@ -12,12 +14,18 @@ import com.seibel.lod.core.wrapperInterfaces.config.ILodConfigWrapperSingleton; * * @author James Seibel * @author Ran - * @version 12-1-2021 + * @version 3-5-2022 */ public class ForgeDependencySetup { public static void createInitialBindings() { SingletonHandler.bind(ILodConfigWrapperSingleton.class, LodConfigWrapperSingleton.INSTANCE); + SingletonHandler.bind(IModChecker.class, ModChecker.INSTANCE); + } + + public static void finishBinding() + { + SingletonHandler.finishBinding(); } }