From 903585d28142fadb35ad6524a5e60b2d0db7eff5 Mon Sep 17 00:00:00 2001 From: 3944Realms Date: Tue, 17 Feb 2026 09:24:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=BC=E5=AE=B9=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E6=96=B9=E5=BC=8F=EF=BC=8C=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../lib39/core/compat/CompatManager.java | 33 +++++++++---------- .../lib39/core/compat/ICompat.java | 3 +- .../lib39/example/compat/Lib39Compat.java | 12 +++++++ .../core/event/ExCommonEventHandler.java | 15 +++------ 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/gradle.properties b/gradle.properties index c906f33..c0b5d81 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.3.1 +mod_version=0.3.3 # 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/core/compat/CompatManager.java b/src/main/java/top/r3944realms/lib39/core/compat/CompatManager.java index cdd2763..9ed16c7 100644 --- a/src/main/java/top/r3944realms/lib39/core/compat/CompatManager.java +++ b/src/main/java/top/r3944realms/lib39/core/compat/CompatManager.java @@ -34,15 +34,8 @@ public abstract class CompatManager { // 存储事件监听器配置 protected final List listenerConfigs = new ArrayList<>(); - protected void initialize() { - modEventBus.addListener(this::onConstructMod); - } - - private void onConstructMod(FMLConstructModEvent event) { - event.enqueueWork(() -> { - initializeAllCompat(); - Lib39.LOGGER.info("[{}]: CompatManager initialized with {} modules", this.getId(), getLoadedCompats().size()); - }); + public void initialize() { + initializeAllCompat(); } public CompatManager(ResourceLocation id, IEventBus modEventBus, IEventBus gameEventBus) { @@ -50,7 +43,6 @@ public abstract class CompatManager { this.modEventBus = modEventBus; this.gameEventBus = gameEventBus; this.logger = LoggerFactory.getLogger(id.toString()); - initialize(); } /** @@ -149,7 +141,7 @@ public abstract class CompatManager { /** * 初始化所有兼容模块并应用事件监听器 */ - public void initializeAllCompat() { + protected synchronized void initializeAllCompat() { logger.info("Initializing {} compatibility modules", compats.size()); // 先处理所有缓存的注册 @@ -158,11 +150,14 @@ public abstract class CompatManager { // 初始化所有兼容模块 for (Map.Entry entry : compats.entrySet()) { - try { - entry.getValue().initialize(); - logger.info("Initialized compat: {}", entry.getKey()); - } catch (Exception e) { - logger.error("Failed to initialize compat: {}", entry.getKey(), e); + if (!entry.getValue().isInitialized()) { + try { + entry.getValue().initialize(); + entry.getValue().setInitialize(true); + logger.info("Initialized compat: {}", entry.getKey()); + } catch (Exception e) { + logger.error("Failed to initialize compat: {}", entry.getKey(), e); + } } } initialized = true; @@ -192,8 +187,10 @@ public abstract class CompatManager { */ private void applyListenerToAllCompats(ListenerConfig config) { for (ICompat compat : compats.values()) { - if (config.shouldApply(compat)) { - applyListenerToCompat(compat, config); + if(!compat.isInitialized()) { + if (config.shouldApply(compat)) { + applyListenerToCompat(compat, config); + } } } } diff --git a/src/main/java/top/r3944realms/lib39/core/compat/ICompat.java b/src/main/java/top/r3944realms/lib39/core/compat/ICompat.java index 467bfa6..750fbe8 100644 --- a/src/main/java/top/r3944realms/lib39/core/compat/ICompat.java +++ b/src/main/java/top/r3944realms/lib39/core/compat/ICompat.java @@ -9,7 +9,8 @@ import java.util.concurrent.Callable; * The interface Compat. */ public interface ICompat { - + void setInitialize(boolean initialize); + boolean isInitialized(); /** * Id resource location. * diff --git a/src/main/java/top/r3944realms/lib39/example/compat/Lib39Compat.java b/src/main/java/top/r3944realms/lib39/example/compat/Lib39Compat.java index f0c1415..05fb3e9 100644 --- a/src/main/java/top/r3944realms/lib39/example/compat/Lib39Compat.java +++ b/src/main/java/top/r3944realms/lib39/example/compat/Lib39Compat.java @@ -12,6 +12,7 @@ import top.r3944realms.lib39.core.compat.ICompat; * The type Lib 39 compat. */ public class Lib39Compat implements ICompat { + boolean initialized = false; /** * The constant INSTANCE. */ @@ -20,6 +21,17 @@ public class Lib39Compat implements ICompat { * The constant ID. */ public static ResourceLocation ID = Lib39.rl("lib39"); + + @Override + public void setInitialize(boolean initialize) { + this.initialized = initialize; + } + + @Override + public boolean isInitialized() { + return initialized; + } + @Override public ResourceLocation id() { return ID; 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 ffcff5b..b49d674 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 @@ -101,19 +101,12 @@ public class ExCommonEventHandler { @SubscribeEvent public static void onConstructMod(FMLConstructModEvent event) { event.enqueueWork(() -> { - getOrCreateCompatManager(); + CompatManager orCreateCompatManager = Mod.getOrCreateCompatManager(); + orCreateCompatManager + .registerCompat(Lib39Compat.ID, Lib39Compat.INSTANCE); + orCreateCompatManager.initialize(); }); } - /** - * On fml common setup. - * - * @param event the event - */ - @SubscribeEvent - public static void onFMLCommonSetup(FMLCommonSetupEvent event) { - event.enqueueWork(() -> ExCommonEventHandler.Mod.getOrCreateCompatManager() - .registerCompat(Lib39Compat.ID, Lib39Compat.INSTANCE)); - } /** * Register capability.