From 49166f8dab5ce053921d01777c756d54cedd71cd Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Sat, 13 May 2023 15:16:51 +0200 Subject: [PATCH] Fixed Bogey registry loading flywheel classes --- .../com/simibubi/create/AllBogeyStyles.java | 36 +++++++------ .../logistics/trains/entity/BogeyStyle.java | 50 ++++++++++++------- 2 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/simibubi/create/AllBogeyStyles.java b/src/main/java/com/simibubi/create/AllBogeyStyles.java index dde09ec26..d9487f089 100644 --- a/src/main/java/com/simibubi/create/AllBogeyStyles.java +++ b/src/main/java/com/simibubi/create/AllBogeyStyles.java @@ -23,6 +23,8 @@ import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.fml.DistExecutor; public class AllBogeyStyles { public static final Map BOGEY_STYLES = new HashMap<>(); @@ -36,10 +38,10 @@ public class AllBogeyStyles { public static final String STANDARD_CYCLE_GROUP = "standard"; public static final BogeyStyle STANDARD = - create("standard", STANDARD_CYCLE_GROUP).commonRenderer(CommonStandardBogeyRenderer::new) + create("standard", STANDARD_CYCLE_GROUP).commonRenderer(() -> CommonStandardBogeyRenderer::new) .displayName(Components.translatable("create.bogey.style.standard")) - .size(BogeySizes.SMALL, SmallStandardBogeyRenderer::new, AllBlocks.SMALL_BOGEY) - .size(BogeySizes.LARGE, LargeStandardBogeyRenderer::new, AllBlocks.LARGE_BOGEY) + .size(BogeySizes.SMALL, () -> SmallStandardBogeyRenderer::new, AllBlocks.SMALL_BOGEY) + .size(BogeySizes.LARGE, () -> LargeStandardBogeyRenderer::new, AllBlocks.LARGE_BOGEY) .build(); private static BogeyStyleBuilder create(String name, String cycleGroup) { @@ -53,7 +55,7 @@ public class AllBogeyStyles { public static void register() {} public static class BogeyStyleBuilder { - protected final Map sizes = new HashMap<>(); + protected final Map> sizes = new HashMap<>(); protected final ResourceLocation name; protected final ResourceLocation cycleGroup; @@ -84,15 +86,18 @@ public class AllBogeyStyles { return this; } - public BogeyStyleBuilder size(BogeySizes.BogeySize size, Supplier renderer, - BlockEntry> blockEntry) { + public BogeyStyleBuilder size(BogeySizes.BogeySize size, Supplier> renderer, + BlockEntry> blockEntry) { this.size(size, renderer, blockEntry.getId()); return this; } - public BogeyStyleBuilder size(BogeySizes.BogeySize size, Supplier renderer, - ResourceLocation location) { - this.sizes.put(size, new BogeyStyle.SizeData(location, renderer, renderer.get())); + public BogeyStyleBuilder size(BogeySizes.BogeySize size, Supplier> renderer, + ResourceLocation location) { + DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> { + this.sizes.put(size, () -> new BogeyStyle.SizeData(location, renderer.get(), renderer.get() + .get())); + }); return this; } @@ -106,16 +111,19 @@ public class AllBogeyStyles { return this; } - public BogeyStyleBuilder commonRenderer(Supplier commonRenderer) { - this.commonRenderer = Optional.of(commonRenderer); + public BogeyStyleBuilder commonRenderer(Supplier> commonRenderer) { + DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> { + this.commonRenderer = Optional.of(commonRenderer.get()); + }); return this; } public BogeyStyle build() { - BogeyStyle entry = - new BogeyStyle(name, cycleGroup, displayName, soundType, contactParticle, smokeParticle, defaultData, sizes, commonRenderer); + BogeyStyle entry = new BogeyStyle(name, cycleGroup, displayName, soundType, contactParticle, smokeParticle, + defaultData, sizes, commonRenderer); BOGEY_STYLES.put(name, entry); - CYCLE_GROUPS.computeIfAbsent(cycleGroup, l -> new HashMap<>()).put(name, entry); + CYCLE_GROUPS.computeIfAbsent(cycleGroup, l -> new HashMap<>()) + .put(name, entry); return entry; } } diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyStyle.java b/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyStyle.java index 2b3efc8d7..1c4adf8b4 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyStyle.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/entity/BogeyStyle.java @@ -1,10 +1,18 @@ package com.simibubi.create.content.logistics.trains.entity; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.Supplier; +import java.util.stream.Stream; + +import org.jetbrains.annotations.NotNull; + import com.jozufozu.flywheel.api.MaterialManager; import com.simibubi.create.AllBogeyStyles; import com.simibubi.create.AllSoundEvents; import com.simibubi.create.content.logistics.trains.BogeyRenderer; - import com.simibubi.create.content.logistics.trains.BogeyRenderer.CommonRenderer; import com.simibubi.create.content.logistics.trains.BogeySizes; @@ -14,32 +22,32 @@ import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; import net.minecraft.world.level.block.Block; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.registries.ForgeRegistries; -import org.jetbrains.annotations.NotNull; - -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.function.Supplier; -import java.util.stream.Stream; - public class BogeyStyle { - private final Optional> commonRendererFactory; - + public final ResourceLocation name; public final ResourceLocation cycleGroup; - private final Optional commonRenderer; - private final Map sizes; public final Component displayName; public final ResourceLocation soundType; public final ParticleOptions contactParticle; public final ParticleOptions smokeParticle; public final CompoundTag defaultData; + + private Optional> commonRendererFactory; + + @OnlyIn(Dist.CLIENT) + private Map sizes; + + @OnlyIn(Dist.CLIENT) + private Optional commonRenderer; public BogeyStyle(ResourceLocation name, ResourceLocation cycleGroup, Component displayName, ResourceLocation soundType, ParticleOptions contactParticle, ParticleOptions smokeParticle, - CompoundTag defaultData, Map sizes, Optional> commonRenderer) { + CompoundTag defaultData, Map> sizes, Optional> commonRenderer) { this.name = name; this.cycleGroup = cycleGroup; this.displayName = displayName; @@ -48,10 +56,13 @@ public class BogeyStyle { this.smokeParticle = smokeParticle; this.defaultData = defaultData; - this.sizes = sizes; - - this.commonRendererFactory = commonRenderer; - this.commonRenderer = commonRenderer.map(Supplier::get); + DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> { + this.sizes = new HashMap<>(); + sizes.forEach((k, v) -> this.sizes.put(k, v.get())); + + this.commonRendererFactory = commonRenderer; + this.commonRenderer = commonRenderer.map(Supplier::get); + }); } public Map getCycleGroup() { @@ -81,10 +92,12 @@ public class BogeyStyle { return entry.getMainEvent(); } + @OnlyIn(Dist.CLIENT) public BogeyRenderer createRendererInstance(BogeySizes.BogeySize size) { return this.sizes.get(size).createRenderInstance(); } + @OnlyIn(Dist.CLIENT) public BogeyRenderer getInWorldRenderInstance(BogeySizes.BogeySize size) { SizeData sizeData = this.sizes.get(size); return sizeData != null ? sizeData.getInWorldInstance() : BackupBogeyRenderer.INSTANCE; @@ -102,6 +115,7 @@ public class BogeyStyle { return new BogeyInstance(bogey, this, size, materialManager); } + @OnlyIn(Dist.CLIENT) public record SizeData(ResourceLocation block, Supplier rendererFactory, BogeyRenderer instance) { public BogeyRenderer createRenderInstance() { return rendererFactory.get();