修改为模组总线避免部分事件无法被正确提取加载

This commit is contained in:
叁玖领域 2026-02-16 20:28:32 +08:00
parent 0a94440168
commit cf14cd705e
5 changed files with 62 additions and 25 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. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=MIT mod_license=MIT
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=0.1.9 mod_version=0.2.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # 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. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View File

@ -9,7 +9,7 @@ import top.r3944realms.lib39.core.compat.ICompat;
/** /**
* The type Register compat event. * The type Register compat event.
*/ */
public class RegisterCompatEvent extends Event { public class RegisterCompatEvent extends Event implements IModBusEvent {
/** /**
* The Compat manager. * The Compat manager.
*/ */

View File

@ -27,6 +27,7 @@ import net.minecraftforge.event.level.LevelEvent;
import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.RegistryObject; import net.minecraftforge.registries.RegistryObject;
import top.r3944realms.lib39.Lib39; import top.r3944realms.lib39.Lib39;
@ -72,7 +73,6 @@ public class CommonEventHandler {
*/ */
static volatile SyncData2Manager syncData2Manager; static volatile SyncData2Manager syncData2Manager;
private static boolean isSync2MInitialized = false; private static boolean isSync2MInitialized = false;
private static boolean isCompatInitialized = false;
/** /**
* Gets sync data 2 manager. * Gets sync data 2 manager.
@ -102,11 +102,7 @@ public class CommonEventHandler {
sl = serverLevel; sl = serverLevel;
Lib39.LOGGER.info("SyncData2Manager initialized on Sever load"); Lib39.LOGGER.info("SyncData2Manager initialized on Sever load");
} }
if (!isCompatInitialized) {
MinecraftForge.EVENT_BUS.post(new RegisterCompatEvent(Mod.compatManager));
Mod.compatManager.initializeAll();
isCompatInitialized = true;
}
} }
} else if (level instanceof ClientLevel clientLevel) { } else if (level instanceof ClientLevel clientLevel) {
synchronized (Game.class) { synchronized (Game.class) {
@ -116,11 +112,6 @@ public class CommonEventHandler {
MinecraftForge.EVENT_BUS.post(new SyncManagerRegisterEvent(syncData2Manager)); MinecraftForge.EVENT_BUS.post(new SyncManagerRegisterEvent(syncData2Manager));
Lib39.LOGGER.info("SyncData2Manager initialized on Client load"); Lib39.LOGGER.info("SyncData2Manager initialized on Client load");
} }
if (!isCompatInitialized) {
MinecraftForge.EVENT_BUS.post(new RegisterCompatEvent(Mod.compatManager));
Mod.compatManager.initializeAll();
isCompatInitialized = true;
}
} }
} }
@ -249,13 +240,23 @@ public class CommonEventHandler {
*/ */
static volatile CompatManager compatManager; static volatile CompatManager compatManager;
/**
* On fml common setup.
*
* @param event the event
*/
@SubscribeEvent @SubscribeEvent
public static void onFMLCommonSetup (FMLCommonSetupEvent event) { 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));
compatManager.initializeAll();
Lib39.LOGGER.info("RegisterCompatEvent published during construct phase");
});
}
@SubscribeEvent
public static void onFMLCommonSetup(FMLCommonSetupEvent event) {
event.enqueueWork(() -> { event.enqueueWork(() -> {
try { try {
Class.forName("top.r3944realms.lib39.base.command.Lib39CommandHelpManager"); Class.forName("top.r3944realms.lib39.base.command.Lib39CommandHelpManager");
@ -263,12 +264,6 @@ public class CommonEventHandler {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}); });
event.enqueueWork(() -> {
IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus();
IEventBus gameBus = MinecraftForge.EVENT_BUS;
compatManager = new CompatManager(modBus, gameBus);
});
} }
/** /**

View File

@ -0,0 +1,36 @@
package top.r3944realms.lib39.example.compat;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.lib39.Lib39;
import top.r3944realms.lib39.core.compat.ICompat;
public class Lib39Compat implements ICompat {
public static Lib39Compat INSTANCE = new Lib39Compat();
public static ResourceLocation ID = Lib39.rl("lib39");
@Override
public ResourceLocation id() {
return ID;
}
@Override
public boolean isModLoaded() {
return ModList.get().isLoaded("lib39");
}
@Override
public void initialize() {
}
@Override
public void addCommonModListener(@NotNull IEventBus modBus) {
modBus.addListener(this::onSetUp);
}
private void onSetUp (@NotNull FMLCommonSetupEvent event) {
event.enqueueWork(() -> Lib39.LOGGER.info("Loading Lib39 Compat"));
}
}

View File

@ -5,8 +5,10 @@ import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent;
import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import top.r3944realms.lib39.api.event.RegisterCompatEvent;
import top.r3944realms.lib39.api.event.SyncManagerRegisterEvent; import top.r3944realms.lib39.api.event.SyncManagerRegisterEvent;
import top.r3944realms.lib39.core.sync.CachedSyncManager; import top.r3944realms.lib39.core.sync.CachedSyncManager;
import top.r3944realms.lib39.example.compat.Lib39Compat;
import top.r3944realms.lib39.example.content.capability.AbstractedTestSyncData; import top.r3944realms.lib39.example.content.capability.AbstractedTestSyncData;
import top.r3944realms.lib39.example.content.capability.ExCapabilityHandler; import top.r3944realms.lib39.example.content.capability.ExCapabilityHandler;
import top.r3944realms.lib39.example.content.capability.TestSyncData; import top.r3944realms.lib39.example.content.capability.TestSyncData;
@ -63,6 +65,10 @@ public class ExCommonEventHandler {
*/ */
public static class Mod extends ExCommonEventHandler { public static class Mod extends ExCommonEventHandler {
@SubscribeEvent
public static void onRegisterCompat (RegisterCompatEvent event) {
event.registerCompat(Lib39Compat.ID, Lib39Compat.INSTANCE);
}
/** /**
* On fml common setup. * On fml common setup.
* *