修改兼容初始化方式,手动初始化

This commit is contained in:
叁玖领域 2026-02-17 09:24:09 +08:00
parent 70ce25d158
commit 903585d281
5 changed files with 34 additions and 31 deletions

View File

@ -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

View File

@ -34,15 +34,8 @@ public abstract class CompatManager {
// 存储事件监听器配置
protected final List<ListenerConfig> 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<ResourceLocation, ICompat> 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);
}
}
}
}

View File

@ -9,7 +9,8 @@ import java.util.concurrent.Callable;
* The interface Compat.
*/
public interface ICompat {
void setInitialize(boolean initialize);
boolean isInitialized();
/**
* Id resource location.
*

View File

@ -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;

View File

@ -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.