From 0d5cef1a64847f941114bcc25d2aa4620a8cc5b3 Mon Sep 17 00:00:00 2001 From: 3944Realms Date: Mon, 16 Feb 2026 22:14:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=B3=A8=E5=86=8C=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E4=BA=8B=E4=BB=B6=EF=BC=8C=E6=8A=BD=E5=87=BA=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E7=9A=84CompatManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../lib39/api/event/RegisterCompatEvent.java | 69 -------------- .../lib39/core/compat/CompatManager.java | 89 +++++++++++++------ .../lib39/core/event/CommonEventHandler.java | 43 +-------- .../example/compat/Lib39CompatManager.java | 18 ++++ .../core/event/ExCommonEventHandler.java | 44 +++++++-- 6 files changed, 120 insertions(+), 145 deletions(-) delete mode 100644 src/main/java/top/r3944realms/lib39/api/event/RegisterCompatEvent.java create mode 100644 src/main/java/top/r3944realms/lib39/example/compat/Lib39CompatManager.java diff --git a/gradle.properties b/gradle.properties index b4216fd..afdcc7f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -33,7 +33,7 @@ mod_name=3944Realms 's Lib Mod # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=MIT # The mod version. See https://semver.org/ -mod_version=0.2.1 +mod_version=0.3.0 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/top/r3944realms/lib39/api/event/RegisterCompatEvent.java b/src/main/java/top/r3944realms/lib39/api/event/RegisterCompatEvent.java deleted file mode 100644 index f34b815..0000000 --- a/src/main/java/top/r3944realms/lib39/api/event/RegisterCompatEvent.java +++ /dev/null @@ -1,69 +0,0 @@ -package top.r3944realms.lib39.api.event; - -import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.eventbus.api.Event; -import net.minecraftforge.fml.event.IModBusEvent; -import top.r3944realms.lib39.core.compat.CompatManager; -import top.r3944realms.lib39.core.compat.ICompat; - -/** - * The type Register compat event. - */ -public class RegisterCompatEvent extends Event implements IModBusEvent { - /** - * The Compat manager. - */ - protected final CompatManager compatManager; - - /** - * Instantiates a new Register compat event. - * - * @param compatManager the compat manager - */ - public RegisterCompatEvent(CompatManager compatManager) { - this.compatManager = compatManager; - } - - /** - * Gets compat manager. - * - * @return the compat manager - */ - public CompatManager getCompatManager() { - return compatManager; - } - - /** - * Register compat. - * - * @param id the id - * @param compat the compat - */ -// 注册兼容模块 - public void registerCompat(ResourceLocation id, ICompat compat) { - compatManager.registerCompat(id, compat); - } - - /** - * Register compat. - * - * @param namespace the namespace - * @param path the path - * @param compat the compat - */ -// 注册兼容模块(简化版本) - public void registerCompat(String namespace, String path, ICompat compat) { - compatManager.registerCompat(namespace, path, compat); - } - - /** - * Unregister compat. - * - * @param id the id - */ -// 取消注册兼容模块 - public void unregisterCompat(ResourceLocation id) { - compatManager.unregisterCompat(id); - } - -} diff --git a/src/main/java/top/r3944realms/lib39/core/compat/CompatManager.java b/src/main/java/top/r3944realms/lib39/core/compat/CompatManager.java index cdd1240..cb1bcff 100644 --- a/src/main/java/top/r3944realms/lib39/core/compat/CompatManager.java +++ b/src/main/java/top/r3944realms/lib39/core/compat/CompatManager.java @@ -4,8 +4,12 @@ import net.minecraft.resources.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; +import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import top.r3944realms.lib39.Lib39; import java.util.*; @@ -16,23 +20,43 @@ import java.util.stream.Collectors; * The type Compat manager. */ @SuppressWarnings("unused") -public class CompatManager { - private final Map compats = new HashMap<>(); - private final IEventBus modEventBus, gameEventBus; +public abstract class CompatManager { + public ResourceLocation getId() { + return id; + } + protected final Logger logger; + protected final ResourceLocation id; + protected final Map compats = new HashMap<>(); + protected boolean initialized = false; + protected final List pendingTasks = new ArrayList<>(); + protected final IEventBus modEventBus, gameEventBus; // 存储事件监听器配置 - private final List listenerConfigs = new ArrayList<>(); + protected final List listenerConfigs = new ArrayList<>(); + protected void initialize() { + modEventBus.addListener(this::onConstructMod); + modEventBus.addListener(this::onFMLCommonSetup); + } - /** - * Instantiates a new Compat manager. - * - * @param modEventBus the mod event bus - * @param gameEventBus the game event bus - */ - public CompatManager(IEventBus modEventBus, IEventBus gameEventBus) { + private void onConstructMod(FMLConstructModEvent event) { + event.enqueueWork(() -> { + Lib39.LOGGER.info("[{}]: RegisterCompatEvent published during construct phase", this.getId()); + }); + } + private void onFMLCommonSetup(FMLCommonSetupEvent event) { + event.enqueueWork(() -> { + initializeAllCompat(); + Lib39.LOGGER.info("[{}]: CompatManager initialized with {} modules", this.getId(), getLoadedCompats().size()); + }); + } + + public CompatManager(ResourceLocation id, IEventBus modEventBus, IEventBus gameEventBus) { + this.id = id; this.modEventBus = modEventBus; this.gameEventBus = gameEventBus; + this.logger = LoggerFactory.getLogger(id.toString()); + initialize(); } /** @@ -42,13 +66,24 @@ public class CompatManager { * @param compat the compat */ public void registerCompat(ResourceLocation id, ICompat compat) { + if (initialized) { + // 已初始化,直接注册 + doRegisterCompat(id, compat); + } else { + // 未初始化,缓存起来 + pendingTasks.add(() -> doRegisterCompat(id, compat)); + logger.debug("Cached compat registration for: {}", id); + } + } + + private void doRegisterCompat(ResourceLocation id, ICompat compat) { if (compats.containsKey(id)) { - Lib39.LOGGER.warn("Compat with id {} is already registered!", id); + logger.warn("Compat with id {} is already registered!", id); return; } compats.put(id, compat); addListenerForCompat(id); - Lib39.LOGGER.debug("Registered compat: {}", id); + logger.debug("Registered compat: {}", id); } /** @@ -120,19 +155,23 @@ public class CompatManager { /** * 初始化所有兼容模块并应用事件监听器 */ - public void initializeAll() { - Lib39.LOGGER.info("Initializing {} compatibility modules", compats.size()); + public void initializeAllCompat() { + logger.info("Initializing {} compatibility modules", compats.size()); + + // 先处理所有缓存的注册 + pendingTasks.forEach(Runnable::run); + pendingTasks.clear(); // 1. 先初始化所有兼容模块 for (Map.Entry entry : compats.entrySet()) { try { entry.getValue().initialize(); - Lib39.LOGGER.info("Initialized compat: {}", entry.getKey()); + logger.info("Initialized compat: {}", entry.getKey()); } catch (Exception e) { - Lib39.LOGGER.error("Failed to initialize compat: {}", entry.getKey(), e); + logger.error("Failed to initialize compat: {}", entry.getKey(), e); } } - + initialized = true; // 2. 然后应用所有事件监听器 applyAllEventListeners(); } @@ -141,7 +180,7 @@ public class CompatManager { * 应用所有配置的事件监听器到对应的 ICompat 实例 */ private void applyAllEventListeners() { - Lib39.LOGGER.info("Applying {} event listener configurations", listenerConfigs.size()); + logger.info("Applying {} event listener configurations", listenerConfigs.size()); for (ListenerConfig config : listenerConfigs) { if (config.compatId == null) { @@ -207,11 +246,11 @@ public class CompatManager { } } - Lib39.LOGGER.debug("Applied {} listener to compat: {}", + logger.debug("Applied {} listener to compat: {}", getListenerTypeName(config), compat.id()); } catch (Exception e) { - Lib39.LOGGER.error("Failed to apply listener to compat: {}", compat.id(), e); + logger.error("Failed to apply listener to compat: {}", compat.id(), e); } } @@ -238,7 +277,7 @@ public class CompatManager { addListenerForCompat(compatId, null, bus); } - private static class ListenerConfig { + protected static class ListenerConfig { /** * The Compat id. */ @@ -282,12 +321,12 @@ public class CompatManager { * On load complete. */ public void onLoadComplete() { - Lib39.LOGGER.info("Calling onLoadComplete for {} compatibility modules", compats.size()); + logger.info("Calling onLoadComplete for {} compatibility modules", compats.size()); for (Map.Entry entry : compats.entrySet()) { try { entry.getValue().onLoadComplete(); } catch (Exception e) { - Lib39.LOGGER.error("Error in onLoadComplete for compat: {}", entry.getKey(), e); + logger.error("Error in onLoadComplete for compat: {}", entry.getKey(), e); } } } @@ -320,7 +359,7 @@ public class CompatManager { public void unregisterCompat(ResourceLocation id) { ICompat removed = compats.remove(id); if (removed != null) { - Lib39.LOGGER.debug("Unregistered compat: {}", id); + logger.debug("Unregistered compat: {}", id); } } diff --git a/src/main/java/top/r3944realms/lib39/core/event/CommonEventHandler.java b/src/main/java/top/r3944realms/lib39/core/event/CommonEventHandler.java index 5a6f50b..741338a 100644 --- a/src/main/java/top/r3944realms/lib39/core/event/CommonEventHandler.java +++ b/src/main/java/top/r3944realms/lib39/core/event/CommonEventHandler.java @@ -31,7 +31,6 @@ import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.RegistryObject; import top.r3944realms.lib39.Lib39; -import top.r3944realms.lib39.api.event.RegisterCompatEvent; import top.r3944realms.lib39.api.event.SyncManagerRegisterEvent; import top.r3944realms.lib39.base.command.Lib39HelpCommand; import top.r3944realms.lib39.base.datagen.Lib39BaseDataGenEvent; @@ -40,6 +39,7 @@ import top.r3944realms.lib39.content.register.Lib39Items; import top.r3944realms.lib39.core.compat.CompatManager; import top.r3944realms.lib39.core.sync.ISyncData; import top.r3944realms.lib39.core.sync.SyncData2Manager; +import top.r3944realms.lib39.example.compat.Lib39CompatManager; import top.r3944realms.lib39.util.GameProfileHelper; import java.util.ArrayList; @@ -225,40 +225,6 @@ public class CommonEventHandler { public static class Mod extends CommonEventHandler { private static final Map, ResourceKey[]> itemAddMap = new ConcurrentHashMap<>(); private static final Map, List>> tabToItemsMap = new ConcurrentHashMap<>(); - - /** - * Gets compat manager. - * - * @return the compat manager - */ - public static CompatManager getCompatManager() { - return compatManager; - } - - /** - * The Compat manager. - */ - static volatile CompatManager compatManager; - - - /** - * On construct mod. - * - * @param event the event - */ - @SubscribeEvent - public static void onConstructMod(FMLConstructModEvent event) { - event.enqueueWork(() -> { - IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); - IEventBus gameBus = MinecraftForge.EVENT_BUS; - compatManager = new CompatManager(modBus, gameBus); - - // 在Mod总线上发布事件 - modBus.post(new RegisterCompatEvent(compatManager)); - Lib39.LOGGER.info("RegisterCompatEvent published during construct phase"); - }); - } - /** * On fml common setup. * @@ -272,13 +238,6 @@ public class CommonEventHandler { } catch (ClassNotFoundException e) { throw new RuntimeException(e); } - - // 在这里初始化兼容模块,给其他模组足够的时间注册 - if (compatManager != null) { - compatManager.initializeAll(); - Lib39.LOGGER.info("CompatManager initialized with {} modules", - compatManager.getLoadedCompats().size()); - } }); } diff --git a/src/main/java/top/r3944realms/lib39/example/compat/Lib39CompatManager.java b/src/main/java/top/r3944realms/lib39/example/compat/Lib39CompatManager.java new file mode 100644 index 0000000..436d21b --- /dev/null +++ b/src/main/java/top/r3944realms/lib39/example/compat/Lib39CompatManager.java @@ -0,0 +1,18 @@ +package top.r3944realms.lib39.example.compat; + +import net.minecraftforge.eventbus.api.IEventBus; +import top.r3944realms.lib39.Lib39; +import top.r3944realms.lib39.core.compat.CompatManager; + +public class Lib39CompatManager extends CompatManager { + /** + * Instantiates a new Compat manager. + * + * @param path the path + * @param modEventBus the mod event bus + * @param gameEventBus the game event bus + */ + public Lib39CompatManager(String path, IEventBus modEventBus, IEventBus gameEventBus) { + super(Lib39.rl(path), modEventBus, gameEventBus); + } +} diff --git a/src/main/java/top/r3944realms/lib39/example/core/event/ExCommonEventHandler.java b/src/main/java/top/r3944realms/lib39/example/core/event/ExCommonEventHandler.java index 431f202..ffcff5b 100644 --- a/src/main/java/top/r3944realms/lib39/example/core/event/ExCommonEventHandler.java +++ b/src/main/java/top/r3944realms/lib39/example/core/event/ExCommonEventHandler.java @@ -1,14 +1,20 @@ package top.r3944realms.lib39.example.core.event; import net.minecraft.world.entity.Entity; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent; import net.minecraftforge.event.AttachCapabilitiesEvent; +import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; -import top.r3944realms.lib39.api.event.RegisterCompatEvent; +import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import top.r3944realms.lib39.api.event.SyncManagerRegisterEvent; +import top.r3944realms.lib39.core.compat.CompatManager; +import top.r3944realms.lib39.core.event.CommonEventHandler; import top.r3944realms.lib39.core.sync.CachedSyncManager; import top.r3944realms.lib39.example.compat.Lib39Compat; +import top.r3944realms.lib39.example.compat.Lib39CompatManager; import top.r3944realms.lib39.example.content.capability.AbstractedTestSyncData; import top.r3944realms.lib39.example.content.capability.ExCapabilityHandler; import top.r3944realms.lib39.example.content.capability.TestSyncData; @@ -64,17 +70,40 @@ public class ExCommonEventHandler { * The type Mod. */ public static class Mod extends ExCommonEventHandler { + public static final IEventBus EVENT_BUS = FMLJavaModLoadingContext.get().getModEventBus(); + /** + * Gets compat manager. + * + * @return the compat manager + */ + public static CompatManager getOrCreateCompatManager() { + if (compatManager == null) { + synchronized (CommonEventHandler.Mod.class) { + if (compatManager == null) { + compatManager = new Lib39CompatManager("compat", EVENT_BUS, MinecraftForge.EVENT_BUS); + } + } + } + return compatManager; + } /** - * On register compat. + * The Compat manager. + */ + static volatile CompatManager compatManager; + + + /** + * On construct mod. * * @param event the event */ @SubscribeEvent - public static void onRegisterCompat (RegisterCompatEvent event) { - event.registerCompat(Lib39Compat.ID, Lib39Compat.INSTANCE); + public static void onConstructMod(FMLConstructModEvent event) { + event.enqueueWork(() -> { + getOrCreateCompatManager(); + }); } - /** * On fml common setup. * @@ -82,9 +111,8 @@ public class ExCommonEventHandler { */ @SubscribeEvent public static void onFMLCommonSetup(FMLCommonSetupEvent event) { - event.enqueueWork(() -> { - - }); + event.enqueueWork(() -> ExCommonEventHandler.Mod.getOrCreateCompatManager() + .registerCompat(Lib39Compat.ID, Lib39Compat.INSTANCE)); } /**