Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not load client-only classes in server environments #20

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 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 @@ -17,8 +23,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 @@ -34,13 +38,12 @@ 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 @@ -60,13 +63,6 @@ public Copperative() {
modEventBus.addListener(CTabs::addCreativeTabs);
}

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