Skip to content

Commit

Permalink
do not load client-only classes in server environments (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle authored Jul 28, 2023
1 parent 2382f35 commit d1e7684
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
23 changes: 9 additions & 14 deletions src/main/java/galena/copperative/Copperative.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());

Expand All @@ -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();
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/galena/copperative/client/CopperativeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,7 +43,7 @@ private static void render(List<RegistryObject<Block>> blocks, RenderType render
}
}

public static void registerBlockRenderers() {
private static void registerBlockRenderers() {
RenderType cutout = RenderType.cutout();

render(EXPOSED_REPEATER, cutout);
Expand All @@ -66,15 +69,23 @@ 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())
);
}
}

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();
}
}

0 comments on commit d1e7684

Please sign in to comment.