优化注册类,明确类别

This commit is contained in:
C-H716 2025-09-10 13:12:34 +08:00
parent c4af701e70
commit d95b8ff049
3 changed files with 55 additions and 34 deletions

View File

@ -1,28 +1,24 @@
package com.extendedae_plus;
import appeng.menu.locator.MenuLocators;
import com.extendedae_plus.client.ClientProxy;
import com.extendedae_plus.config.ModConfigs;
import com.extendedae_plus.init.ModBlockEntities;
import com.extendedae_plus.init.ModBlocks;
import com.extendedae_plus.init.ModCreativeTabs;
import com.extendedae_plus.init.ModItems;
import com.extendedae_plus.init.ModMenuTypes;
import com.extendedae_plus.init.*;
import com.extendedae_plus.menu.locator.CuriosItemLocator;
import com.extendedae_plus.network.ModNetwork;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ModelEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraft.resources.ResourceLocation;
import com.extendedae_plus.client.ClientProxy;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
/**
* ExtendedAE Plus 主mod类
@ -36,20 +32,23 @@ public class ExtendedAEPlus {
public ExtendedAEPlus() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
// 在客户端尽早注册内置模型保证首次资源加载前映射已建立仿照 AE2 AppEngClient 构造期注册
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> ClientProxy::init);
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> ClientProxy::initBuiltInModels);
// 注册mod初始化事件
modEventBus.addListener(this::commonSetup);
// 注册方块与方块实体
ModBlocks.BLOCKS.register(modEventBus);
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);
ModItems.ITEMS.register(modEventBus);
// 在注册阶段将创造模式标签页放入注册表
ModCreativeTabs.TABS.register(modEventBus);
ModMenuTypes.MENUS.register(modEventBus);
// 注册到Forge事件总线
MinecraftForge.EVENT_BUS.register(this);
@ -57,9 +56,9 @@ public class ExtendedAEPlus {
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ModConfigs.COMMON_SPEC);
// 客户端侧延迟注册 FMLClientSetupEvent 阶段执行包含 MenuScreens 绑定等
modEventBus.addListener((FMLClientSetupEvent e) -> ClientProxy.onClientSetup(e));
// 由下面的 ClientModEvents 负责在客户端总线上接收事件并委派
}
/**
* 通用初始化设置
*/
@ -78,4 +77,31 @@ public class ExtendedAEPlus {
public static ResourceLocation id(String path) {
return new ResourceLocation(MODID, path);
}
/**
* 客户端专用事件订阅类
* 完成客户端相关的延迟注册操作如菜单界面绑定渲染器注册模型加载等确保这些操作只在客户端执行避免服务端崩溃
*/
@Mod.EventBusSubscriber(
modid = ExtendedAEPlus.MODID,
bus = Mod.EventBusSubscriber.Bus.MOD,
value = Dist.CLIENT
)
public static class ClientModEvents {
@SubscribeEvent
public static void onClientSetup(final FMLClientSetupEvent event) {
// 直接在此处执行客户端一次性注册UI/屏幕/渲染器绑定
// 注册客户端配置界面
ClientProxy.registerConfigScreen();
// 菜单 -> 屏幕 绑定
ClientProxy.registerMenuScreens();
}
@SubscribeEvent
public static void onRegisterGeometryLoaders(final ModelEvent.RegisterGeometryLoaders evt) {
try {
ClientProxy.initBuiltInModels();
} catch (Exception ignored) {}
}
}
}

View File

@ -22,6 +22,6 @@ public final class ClientModelEvents {
event.register(ExtendedAEPlus.id("block/crafting/64x_accelerator_formed_v2"));
event.register(ExtendedAEPlus.id("block/crafting/256x_accelerator_formed_v2"));
event.register(ExtendedAEPlus.id("block/crafting/1024x_accelerator_formed_v2"));
ClientProxy.init();
ClientProxy.initBuiltInModels();
}
}

View File

@ -4,13 +4,12 @@ import appeng.client.render.crafting.CraftingCubeModel;
import com.extendedae_plus.ExtendedAEPlus;
import com.extendedae_plus.client.render.crafting.EPlusCraftingCubeModelProvider;
import com.extendedae_plus.client.screen.GlobalProviderModesScreen;
import com.extendedae_plus.init.ModMenuTypes;
import com.extendedae_plus.content.crafting.EPlusCraftingUnitType;
import com.extendedae_plus.hooks.BuiltInModelHooks;
import com.extendedae_plus.init.ModMenuTypes;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraft.client.gui.screens.MenuScreens;
/**
* 客户端模型注册 formed 模型注册为内置模型
@ -20,7 +19,10 @@ public final class ClientProxy {
private static boolean REGISTERED = false;
public static void init() {
/**
* 注册内置模型formed 模型等可被 ModelEvent 或启动阶段直接调用
*/
public static void initBuiltInModels() {
if (REGISTERED) return;
REGISTERED = true;
// 注册四种形成态模型为内置模型
@ -46,17 +48,10 @@ public final class ClientProxy {
}
/**
* 客户端设置阶段延迟执行需要访问注册对象的客户端注册
* 将菜单类型与对应的屏幕绑定
*/
public static void onClientSetup(final FMLClientSetupEvent event) {
event.enqueueWork(() -> {
// 确保在首次资源加载前完成内置模型注册REGISTERED 保护避免重复
init();
// 仅在客户端设置阶段执行与 UI 相关的一次性绑定
registerConfigScreen();
// 菜单 -> 屏幕 绑定
MenuScreens.register(ModMenuTypes.NETWORK_PATTERN_CONTROLLER.get(), GlobalProviderModesScreen::new);
});
public static void registerMenuScreens() {
MenuScreens.register(ModMenuTypes.NETWORK_PATTERN_CONTROLLER.get(), GlobalProviderModesScreen::new);
}
/**
@ -68,7 +63,7 @@ public final class ClientProxy {
ModLoadingContext.get().registerExtensionPoint(
ConfigScreenHandler.ConfigScreenFactory.class,
() -> new ConfigScreenHandler.ConfigScreenFactory(
(mc, parent) -> new com.extendedae_plus.client.ModConfigScreen(parent))
(mc, parent) -> new ModConfigScreen(parent))
);
}
}