diff --git a/src/main/java/dev/anvilcraft/lib/AnvilLib.java b/src/main/java/dev/anvilcraft/lib/AnvilLib.java index 33399e2..9984fe0 100644 --- a/src/main/java/dev/anvilcraft/lib/AnvilLib.java +++ b/src/main/java/dev/anvilcraft/lib/AnvilLib.java @@ -3,8 +3,10 @@ import com.electronwill.nightconfig.core.UnmodifiableConfig; import dev.anvilcraft.lib.integration.AnvilLibIntegrations; +import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.ModList; import net.neoforged.fml.common.Mod; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.neoforgespi.language.IModInfo; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -22,7 +24,7 @@ public class AnvilLib { public static final String MOD_NAME = "AnvilLib"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME); - public AnvilLib() { + public AnvilLib(IEventBus modEventBus) { for (IModInfo mod : ModList.get().getMods()) { Map modProperties = mod.getModProperties(); for (Map.Entry entry : modProperties.entrySet()) { @@ -36,13 +38,18 @@ public AnvilLib() { } } AnvilLibIntegrations.apply(); + modEventBus.addListener(AnvilLib::clientSetup); + } + + public static void clientSetup(FMLClientSetupEvent event) { + AnvilLibIntegrations.applyClient(); } private static void loadIntegrations(@NotNull UnmodifiableConfig integrations) { for (UnmodifiableConfig.Entry entry2 : integrations.entrySet()) { String modid = entry2.getKey(); Object value = entry2.getValue(); - List classes = Collections.synchronizedList(new ArrayList<>()); + List classes = AnvilLibIntegrations.INTEGRATIONS.getOrDefault(modid, Collections.synchronizedList(new ArrayList<>())); if (value instanceof String string) { classes.add(string); } else if (value instanceof List list) { diff --git a/src/main/java/dev/anvilcraft/lib/integration/AnvilLibIntegrations.java b/src/main/java/dev/anvilcraft/lib/integration/AnvilLibIntegrations.java index 2cec61f..9b859bb 100644 --- a/src/main/java/dev/anvilcraft/lib/integration/AnvilLibIntegrations.java +++ b/src/main/java/dev/anvilcraft/lib/integration/AnvilLibIntegrations.java @@ -25,6 +25,18 @@ public static void apply() { } } + public static void applyClient() { + for (Map.Entry> entry : AnvilLibIntegrations.INTEGRATIONS.entrySet()) { + String modid = entry.getKey(); + if (AnvilLib.isLoaded(modid)) { + AnvilLib.LOGGER.info("{}'s client integrations is loading...", modid); + List classes = entry.getValue(); + classes.forEach(AnvilLibIntegrations::applyClient); + AnvilLib.LOGGER.info("{}'s client integrations is loaded!", modid); + } + } + } + private static void apply(String name) { try { Class clazz = Class.forName(name); @@ -36,4 +48,16 @@ private static void apply(String name) { AnvilLib.LOGGER.error(ex.getMessage(), ex); } } + + private static void applyClient(String name) { + try { + Class clazz = Class.forName(name); + if (!Integration.class.isAssignableFrom(clazz)) return; + Class integrationClass = clazz.asSubclass(Integration.class); + Integration integration = integrationClass.getDeclaredConstructor().newInstance(); + integration.applyClient(); + } catch (Exception ex) { + AnvilLib.LOGGER.error(ex.getMessage(), ex); + } + } } diff --git a/src/main/java/dev/anvilcraft/lib/integration/Integration.java b/src/main/java/dev/anvilcraft/lib/integration/Integration.java index c84a040..de41d12 100644 --- a/src/main/java/dev/anvilcraft/lib/integration/Integration.java +++ b/src/main/java/dev/anvilcraft/lib/integration/Integration.java @@ -4,5 +4,9 @@ * 集成 */ public interface Integration { - void apply(); + default void apply() { + } + + default void applyClient() { + } }