添加获取了ServerLevel的获取方法

This commit is contained in:
叁玖领域 2025-10-15 19:40:04 +08:00
parent 0521a1b2f0
commit 000ed81d13
2 changed files with 11 additions and 3 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.0.13 mod_version=0.0.14
# 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

@ -26,8 +26,13 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class CommonHandler { public class CommonHandler {
@SuppressWarnings("unused")
@net.minecraftforge.fml.common.Mod.EventBusSubscriber(modid = Lib39.MOD_ID, bus = net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus.FORGE) @net.minecraftforge.fml.common.Mod.EventBusSubscriber(modid = Lib39.MOD_ID, bus = net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus.FORGE)
public static class Game extends CommonHandler { public static class Game extends CommonHandler {
private static ServerLevel sl;
public ServerLevel getServerLevel() {
return sl;
}
static volatile SyncData2Manager syncData2Manager; static volatile SyncData2Manager syncData2Manager;
private static boolean isInitialized = false; private static boolean isInitialized = false;
public static SyncData2Manager getSyncData2Manager() { public static SyncData2Manager getSyncData2Manager() {
@ -39,20 +44,22 @@ public class CommonHandler {
if (event.getLevel().isClientSide() || !(event.getLevel() instanceof ServerLevel serverLevel)) return; if (event.getLevel().isClientSide() || !(event.getLevel() instanceof ServerLevel serverLevel)) return;
// 只处理主世界避免多次初始化 // 只处理主世界避免多次初始化
if (!serverLevel.dimension().equals(Level.OVERWORLD)) return; if (!serverLevel.dimension().equals(Level.OVERWORLD)) return;
synchronized (Game.class) { synchronized (Game.class) {
if (!isInitialized) { if (!isInitialized) {
syncData2Manager = new SyncData2Manager(); syncData2Manager = new SyncData2Manager();
MinecraftForge.EVENT_BUS.post(new SyncManagerRegisterEvent(syncData2Manager)); MinecraftForge.EVENT_BUS.post(new SyncManagerRegisterEvent(syncData2Manager));
isInitialized = true; isInitialized = true;
sl = serverLevel;
Lib39.LOGGER.info("SyncData2Manager initialized on world load"); Lib39.LOGGER.info("SyncData2Manager initialized on world load");
} }
} }
} }
@SubscribeEvent @SubscribeEvent
public static void onWorldLoad(LevelEvent.Unload event) { public static void onWorldUnload(LevelEvent.Unload event) {
if (event.getLevel().isClientSide() || !(event.getLevel() instanceof ServerLevel serverLevel)) return; if (event.getLevel().isClientSide() || !(event.getLevel() instanceof ServerLevel serverLevel)) return;
if (!serverLevel.dimension().equals(Level.OVERWORLD)) return; if (!serverLevel.dimension().equals(Level.OVERWORLD)) return;
sl = null;
isInitialized = false; isInitialized = false;
} }
@ -88,6 +95,7 @@ public class CommonHandler {
} }
} }
} }
@SuppressWarnings("unused")
@net.minecraftforge.fml.common.Mod.EventBusSubscriber(modid = Lib39.MOD_ID, bus = net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus.MOD) @net.minecraftforge.fml.common.Mod.EventBusSubscriber(modid = Lib39.MOD_ID, bus = net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus.MOD)
public static class Mod extends CommonHandler { public static class Mod extends CommonHandler {
private static final Map<RegistryObject<Block>, ResourceKey<CreativeModeTab>[]> itemAddMap = new ConcurrentHashMap<>(); private static final Map<RegistryObject<Block>, ResourceKey<CreativeModeTab>[]> itemAddMap = new ConcurrentHashMap<>();