From d1e768434e6d814ec61b27d4de72b074a0cc5cf4 Mon Sep 17 00:00:00 2001 From: Niklas Widmann Date: Fri, 28 Jul 2023 23:32:02 +0200 Subject: [PATCH] do not load client-only classes in server environments (#22) --- .../java/galena/copperative/Copperative.java | 23 ++++++++----------- .../copperative/client/CopperativeClient.java | 17 +++++++++++--- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main/java/galena/copperative/Copperative.java b/src/main/java/galena/copperative/Copperative.java index 9963dac..e3b8a42 100644 --- a/src/main/java/galena/copperative/Copperative.java +++ b/src/main/java/galena/copperative/Copperative.java @@ -4,7 +4,13 @@ import galena.copperative.client.DynamicCopperativeDataPack; import galena.copperative.config.CommonConfig; import galena.copperative.config.OverwriteEnabledCondition; -import galena.copperative.data.*; +import galena.copperative.data.CBlockStateOverwrites; +import galena.copperative.data.CBlockStates; +import galena.copperative.data.CItemModels; +import galena.copperative.data.CLang; +import galena.copperative.data.CLoot; +import galena.copperative.data.CRecipes; +import galena.copperative.data.CTags; import galena.copperative.index.CBlocks; import galena.copperative.index.CItems; import galena.copperative.index.CLootInjects; @@ -16,8 +22,6 @@ import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; -import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.DeferredRegister; import org.apache.logging.log4j.LogManager; @@ -33,13 +37,11 @@ public Copperative() { CommonConfig.register(); DynamicCopperativeDataPack.INSTANCE.register(); - DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> CopperativeClient::registerDynamicResources); + //noinspection Convert2MethodRef - passing a lambda reference crashes server due to loading of client-only classes + DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> CopperativeClient.register()); IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); - modEventBus.addListener(this::setup); - modEventBus.addListener(this::clientSetup); modEventBus.addListener(this::gatherData); - modEventBus.addListener(CopperativeClient::registerBlockColors); CraftingHelper.register(new OverwriteEnabledCondition.Serializer()); @@ -57,13 +59,6 @@ public Copperative() { } } - private void setup(FMLCommonSetupEvent event) { - } - - private void clientSetup(FMLClientSetupEvent event) { - CopperativeClient.registerBlockRenderers(); - } - public void gatherData(GatherDataEvent event) { DataGenerator generator = event.getGenerator(); ExistingFileHelper helper = event.getExistingFileHelper(); diff --git a/src/main/java/galena/copperative/client/CopperativeClient.java b/src/main/java/galena/copperative/client/CopperativeClient.java index c9a1ffb..b8ce1d0 100644 --- a/src/main/java/galena/copperative/client/CopperativeClient.java +++ b/src/main/java/galena/copperative/client/CopperativeClient.java @@ -5,7 +5,10 @@ import net.minecraft.client.renderer.RenderType; import net.minecraft.world.level.block.Block; import net.minecraftforge.client.event.RegisterColorHandlersEvent; +import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.RegistryObject; import java.util.List; @@ -40,7 +43,7 @@ private static void render(List> blocks, RenderType render } } - public static void registerBlockRenderers() { + private static void registerBlockRenderers() { RenderType cutout = RenderType.cutout(); render(EXPOSED_REPEATER, cutout); @@ -66,7 +69,7 @@ public static void registerBlockRenderers() { COG_BLOCKS.weathered().forEach(it -> render(it, cutout)); } - public static void registerBlockColors(RegisterColorHandlersEvent.Block event) { + private static void registerBlockColors(RegisterColorHandlersEvent.Block event) { if (ModList.get().isLoaded("supplementaries")) { COG_BLOCKS.weathered().forEach(it -> event.register(new CogBlockColor(), it.get()) @@ -74,7 +77,15 @@ public static void registerBlockColors(RegisterColorHandlersEvent.Block event) { } } - public static void registerDynamicResources() { + public static void register() { + IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); + DynamicCopperativeResourcePack.INSTANCE.register(); + modEventBus.addListener(CopperativeClient::registerBlockColors); + modEventBus.addListener(CopperativeClient::setup); + } + + private static void setup(FMLClientSetupEvent event) { + CopperativeClient.registerBlockRenderers(); } }