diff --git a/src/main/java/vazkii/quark/base/client/handler/RequiredModTooltipHandler.java b/src/main/java/vazkii/quark/base/client/handler/RequiredModTooltipHandler.java index c81ad9953f..53174e69bd 100644 --- a/src/main/java/vazkii/quark/base/client/handler/RequiredModTooltipHandler.java +++ b/src/main/java/vazkii/quark/base/client/handler/RequiredModTooltipHandler.java @@ -44,7 +44,6 @@ public static class Client { public Client(Zeta z) { this.z = z; - z.playBus.subscribe(this); } @PlayEvent diff --git a/src/main/java/vazkii/quark/base/proxy/ClientProxy.java b/src/main/java/vazkii/quark/base/proxy/ClientProxy.java index 463b5b0c50..7e268f11a7 100644 --- a/src/main/java/vazkii/quark/base/proxy/ClientProxy.java +++ b/src/main/java/vazkii/quark/base/proxy/ClientProxy.java @@ -55,7 +55,6 @@ public void start() { Quark.ZETA.loadBus .subscribe(ModelHandler.class) //TODO: Make this especially not a singleton, move it into respective modules .subscribe(ContributorRewardHandler.Client.class) - .subscribe(new RenderLayerRegistry.Client(Quark.ZETA.renderLayerRegistry)) .subscribe(WoodSetHandler.Client.class); Quark.ZETA.playBus diff --git a/src/main/java/vazkii/zeta/Zeta.java b/src/main/java/vazkii/zeta/Zeta.java index 89325848ba..e52d53d17a 100644 --- a/src/main/java/vazkii/zeta/Zeta.java +++ b/src/main/java/vazkii/zeta/Zeta.java @@ -58,6 +58,9 @@ public Zeta(String modid, Logger log, ZetaSide side) { this.dyeables = createDyeablesRegistry(); this.craftingExtensions = createCraftingExtensionsRegistry(); this.brewingRegistry = createBrewingRegistry(); + + loadBus.subscribe(craftingExtensions) + .subscribe(brewingRegistry); } public final Logger log; diff --git a/src/main/java/vazkii/zeta/client/ClientRegistryExtension.java b/src/main/java/vazkii/zeta/client/ClientRegistryExtension.java index 63f8b675be..1193780711 100644 --- a/src/main/java/vazkii/zeta/client/ClientRegistryExtension.java +++ b/src/main/java/vazkii/zeta/client/ClientRegistryExtension.java @@ -1,31 +1,64 @@ package vazkii.zeta.client; +import java.util.EnumMap; +import java.util.Map; import java.util.function.Function; import net.minecraft.client.color.block.BlockColor; import net.minecraft.client.color.item.ItemColor; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.item.ClampedItemPropertyFunction; +import net.minecraft.client.renderer.item.ItemProperties; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import vazkii.zeta.Zeta; import vazkii.zeta.client.event.ZAddBlockColorHandlers; import vazkii.zeta.client.event.ZAddItemColorHandlers; +import vazkii.zeta.client.event.ZClientSetup; import vazkii.zeta.event.bus.LoadEvent; +import vazkii.zeta.registry.DyeablesRegistry; +import vazkii.zeta.registry.RenderLayerRegistry; import vazkii.zeta.registry.ZetaRegistry; -public class ClientRegistryExtension { +/** + * Just a grab-bag of stuff that needs to be done on the physical client + */ +public abstract class ClientRegistryExtension { protected final Zeta z; protected final ZetaRegistry registry; + protected final Map resolvedTypes = new EnumMap<>(RenderLayerRegistry.Layer.class); + public ClientRegistryExtension(Zeta z) { this.z = z; this.registry = z.registry; - z.loadBus.subscribe(this); + resolvedTypes.put(RenderLayerRegistry.Layer.SOLID, RenderType.solid()); + resolvedTypes.put(RenderLayerRegistry.Layer.CUTOUT, RenderType.cutout()); + resolvedTypes.put(RenderLayerRegistry.Layer.CUTOUT_MIPPED, RenderType.cutoutMipped()); + resolvedTypes.put(RenderLayerRegistry.Layer.TRANSLUCENT, RenderType.translucent()); + } + + @LoadEvent + public void registerItemColorHandlers(ZAddItemColorHandlers event) { + DyeablesRegistry dyeables = z.dyeables; + + ClampedItemPropertyFunction isDyed = (stack, level, entity, i) -> dyeables.isDyed(stack) ? 1 : 0; + ItemColor color = (stack, layer) -> layer == 0 ? dyeables.getColor(stack) : 0xFF_FF_FF; + //apparently ItemPropertyFunctions are weird and can only be assigned to the minecraft: namespace + ResourceLocation isDyedId = new ResourceLocation("minecraft", z.modid + "_dyed"); + + for(Item item : dyeables.dyeableConditions.keySet()) { + ItemProperties.register(item, isDyedId, isDyed); + event.register(color, item); + } } + //these are in Post events to give other listeners a chance to populate .registerNamed @LoadEvent - public void registerBlockColors(ZAddBlockColorHandlers.Post event) { - registry.submitBlockColors((block, name) -> { + public void registerBlockColorsPost(ZAddBlockColorHandlers.Post event) { + registry.finalizeBlockColors((block, name) -> { Function blockColorCreator = event.getNamedBlockColors().get(name); if(blockColorCreator == null) z.log.error("Unknown block color creator {} used on block {}", name, block); @@ -35,8 +68,8 @@ public void registerBlockColors(ZAddBlockColorHandlers.Post event) { } @LoadEvent - public void registerItemColors(ZAddItemColorHandlers.Post event) { - registry.submitItemColors((item, name) -> { + public void registerItemColorsPost(ZAddItemColorHandlers.Post event) { + registry.finalizeItemColors((item, name) -> { Function itemColorCreator = event.getNamedItemColors().get(name); if(itemColorCreator == null) z.log.error("Unknown item color creator {} used on item {}", name, item); @@ -44,4 +77,11 @@ public void registerItemColors(ZAddItemColorHandlers.Post event) { event.register(itemColorCreator.apply(item), item); }); } + + @LoadEvent + public void registerRenderLayers(ZClientSetup event) { + z.renderLayerRegistry.finalize(this::doSetRenderLayer); + } + + protected abstract void doSetRenderLayer(Block block, RenderLayerRegistry.Layer layer); } diff --git a/src/main/java/vazkii/zeta/client/ClientTicker.java b/src/main/java/vazkii/zeta/client/ClientTicker.java index 5c5716e110..5891698644 100644 --- a/src/main/java/vazkii/zeta/client/ClientTicker.java +++ b/src/main/java/vazkii/zeta/client/ClientTicker.java @@ -12,10 +12,6 @@ public final class ClientTicker { public float delta = 0; public float total = 0; - public ClientTicker(ZetaClient zc) { - zc.playBus.subscribe(this); - } - @PlayEvent public void onRenderTick(ZRenderTick event) { if(event.isStartPhase()) diff --git a/src/main/java/vazkii/zeta/client/ZetaClient.java b/src/main/java/vazkii/zeta/client/ZetaClient.java index a94de884d6..e8166d9b55 100644 --- a/src/main/java/vazkii/zeta/client/ZetaClient.java +++ b/src/main/java/vazkii/zeta/client/ZetaClient.java @@ -13,7 +13,6 @@ import vazkii.zeta.event.bus.IZetaLoadEvent; import vazkii.zeta.event.bus.IZetaPlayEvent; import vazkii.zeta.event.bus.ZetaEventBus; -import vazkii.zeta.registry.DyeablesRegistry; public abstract class ZetaClient { public ZetaClient(Zeta zeta) { @@ -24,9 +23,11 @@ public ZetaClient(Zeta zeta) { this.ticker = createClientTicker(); this.clientConfigManager = createClientConfigManager(); this.topLayerTooltipHandler = createTopLayerTooltipHandler(); - this.clientDyeablesRegistry = createClientDyeablesRegistry(); this.clientRegistryExtension = createClientRegistryExtension(); + loadBus.subscribe(clientRegistryExtension); + + playBus.subscribe(ticker); playBus.subscribe(topLayerTooltipHandler); } @@ -39,11 +40,10 @@ public ZetaClient(Zeta zeta) { public final ClientTicker ticker; public final ClientConfigManager clientConfigManager; public final TopLayerTooltipHandler topLayerTooltipHandler; - public final DyeablesRegistry.Client clientDyeablesRegistry; public final ClientRegistryExtension clientRegistryExtension; public ClientTicker createClientTicker() { - return new ClientTicker(this); + return new ClientTicker(); } public ClientConfigManager createClientConfigManager() { @@ -54,13 +54,8 @@ public TopLayerTooltipHandler createTopLayerTooltipHandler() { return new TopLayerTooltipHandler(); } - public DyeablesRegistry.Client createClientDyeablesRegistry() { - return zeta.dyeables.new Client(this); - } - - public ClientRegistryExtension createClientRegistryExtension() { - return new ClientRegistryExtension(this.zeta); - } + //kinda a grab bag of stuff that needs to happen client-only; hmm, not the best design + public abstract ClientRegistryExtension createClientRegistryExtension(); //forge makes these weird public abstract @Nullable BlockColor getBlockColor(BlockColors bcs, Block block); diff --git a/src/main/java/vazkii/zeta/registry/DyeablesRegistry.java b/src/main/java/vazkii/zeta/registry/DyeablesRegistry.java index 3c41379f86..116b6bb0ca 100644 --- a/src/main/java/vazkii/zeta/registry/DyeablesRegistry.java +++ b/src/main/java/vazkii/zeta/registry/DyeablesRegistry.java @@ -5,9 +5,6 @@ import java.util.Map; import java.util.function.Supplier; -import net.minecraft.client.color.item.ItemColor; -import net.minecraft.client.renderer.item.ClampedItemPropertyFunction; -import net.minecraft.client.renderer.item.ItemProperties; import net.minecraft.core.BlockPos; import net.minecraft.core.Registry; import net.minecraft.core.cauldron.CauldronInteraction; @@ -23,13 +20,14 @@ import net.minecraft.world.level.block.LayeredCauldronBlock; import net.minecraft.world.level.block.state.BlockState; import vazkii.zeta.Zeta; -import vazkii.zeta.client.ZetaClient; -import vazkii.zeta.client.event.ZAddItemColorHandlers; import vazkii.zeta.event.ZRegister; import vazkii.zeta.event.bus.LoadEvent; import vazkii.zeta.module.ZetaModule; import vazkii.zeta.recipe.ZetaDyeRecipe; +/** + * @see vazkii.zeta.client.ClientRegistryExtension + */ public class DyeablesRegistry { protected final Zeta z; @@ -158,26 +156,4 @@ public ItemStack dyeItem(ItemStack stack, List dyes) { return ItemStack.EMPTY; } - //TODO: uhhh i think this is good enough for classloading ... - public class Client { - protected final ZetaClient zc; - - public Client(ZetaClient zc) { - this.zc = zc; - } - - @LoadEvent - public void colorHandlers(ZAddItemColorHandlers event) { - ClampedItemPropertyFunction isDyed = (stack, level, entity, i) -> isDyed(stack) ? 1 : 0; - ItemColor color = (stack, layer) -> layer == 0 ? getColor(stack) : 0xFF_FF_FF; - - //apparently ItemPropertyFunctions are weird and can only be assigned to the minecraft: namespace - ResourceLocation isDyedId = new ResourceLocation("minecraft", zc.zeta.modid + "_dyed"); - - for(Item item : dyeableConditions.keySet()) { - ItemProperties.register(item, isDyedId, isDyed); - event.register(color, item); - } - } - } } diff --git a/src/main/java/vazkii/zeta/registry/RenderLayerRegistry.java b/src/main/java/vazkii/zeta/registry/RenderLayerRegistry.java index 10f0539081..a52d178a13 100644 --- a/src/main/java/vazkii/zeta/registry/RenderLayerRegistry.java +++ b/src/main/java/vazkii/zeta/registry/RenderLayerRegistry.java @@ -1,22 +1,20 @@ package vazkii.zeta.registry; -import java.util.EnumMap; import java.util.HashMap; import java.util.Map; +import java.util.function.BiConsumer; -import net.minecraft.client.renderer.ItemBlockRenderTypes; -import net.minecraft.client.renderer.RenderType; import net.minecraft.world.level.block.Block; -import vazkii.zeta.client.event.ZClientSetup; -import vazkii.zeta.event.bus.LoadEvent; /** * note this is NOT client-side code, for ease of registering stuff in constructors + * + * @see vazkii.zeta.client.ClientRegistryExtension */ public class RenderLayerRegistry { - protected final Map mapping = new HashMap<>(); - protected final Map inheritances = new HashMap<>(); + protected Map mapping = new HashMap<>(); + protected Map inheritances = new HashMap<>(); public enum Layer { SOLID, @@ -26,53 +24,34 @@ public enum Layer { } public void put(Block block, Layer layer) { + if(mapping == null) + throw new IllegalStateException("Already finalized RenderLayerRegistry"); + mapping.put(block, layer); } public void mock(Block block, Block inheritFrom) { + if(inheritances == null) + throw new IllegalStateException("Already finalized RenderLayerRegistry"); + inheritances.put(block, inheritFrom); } - /** - * but this *is* client-side code :blush: - */ - public static class Client { - - protected final RenderLayerRegistry reg; - protected final Map resolvedTypes = new EnumMap<>(Layer.class); + public void finalize(BiConsumer action) { + //Note: only handles one layer of inheritances + for(Block b : inheritances.keySet()) { + Block inheritFrom = inheritances.get(b); + Layer layer = mapping.get(inheritFrom); - public Client(RenderLayerRegistry reg) { - this.reg = reg; - - resolvedTypes.put(Layer.SOLID, RenderType.solid()); - resolvedTypes.put(Layer.CUTOUT, RenderType.cutout()); - resolvedTypes.put(Layer.CUTOUT_MIPPED, RenderType.cutoutMipped()); - resolvedTypes.put(Layer.TRANSLUCENT, RenderType.translucent()); + if(layer != null) + mapping.put(b, layer); } - @LoadEvent - public void register(ZClientSetup event) { - //Note: only handles one layer of inheritances - for(Block b : reg.inheritances.keySet()) { - Block inheritFrom = reg.inheritances.get(b); - Layer layer = reg.mapping.get(inheritFrom); - - if(layer != null) - reg.mapping.put(b, layer); - } - - reg.mapping.forEach(this::doSetRenderLayer); - - reg.inheritances.clear(); - reg.mapping.clear(); - } - - //Forge has some weirdo extension, they want you to use json or something. - //Doing it from java is easier and more akin to how it happens on Fabric. - @SuppressWarnings("removal") - protected void doSetRenderLayer(Block block, Layer layer) { - ItemBlockRenderTypes.setRenderLayer(block, resolvedTypes.get(layer)); - } + mapping.forEach(action); + //and we're done + mapping = null; + inheritances = null; } + } diff --git a/src/main/java/vazkii/zeta/registry/ZetaRegistry.java b/src/main/java/vazkii/zeta/registry/ZetaRegistry.java index 37edc62a26..0dabd2ff89 100644 --- a/src/main/java/vazkii/zeta/registry/ZetaRegistry.java +++ b/src/main/java/vazkii/zeta/registry/ZetaRegistry.java @@ -34,6 +34,7 @@ public abstract class ZetaRegistry { protected final Map internalNames = new IdentityHashMap<>(); // "named color provider" system allows blocks and items to choose their own color providers in a side-safe way + // TODO: should this go somewhere else and not be so tightly-integrated? private final Map blocksToColorProviderName = new HashMap<>(); private final Map itemsToColorProviderName = new HashMap<>(); @@ -147,12 +148,12 @@ public void clearDeferCache(ResourceLocation resourceLocation) { defers.removeAll(resourceLocation); } - public void submitBlockColors(BiConsumer consumer) { + public void finalizeBlockColors(BiConsumer consumer) { blocksToColorProviderName.forEach(consumer); blocksToColorProviderName.clear(); } - public void submitItemColors(BiConsumer consumer) { + public void finalizeItemColors(BiConsumer consumer) { itemsToColorProviderName.forEach(consumer); itemsToColorProviderName.clear(); } diff --git a/src/main/java/vazkii/zetaimplforge/ForgeZeta.java b/src/main/java/vazkii/zetaimplforge/ForgeZeta.java index 17fe935fe3..3d573fab9d 100644 --- a/src/main/java/vazkii/zetaimplforge/ForgeZeta.java +++ b/src/main/java/vazkii/zetaimplforge/ForgeZeta.java @@ -98,7 +98,7 @@ public ZetaRegistry createRegistry() { @Override public CraftingExtensionsRegistry createCraftingExtensionsRegistry() { - return new ForgeCraftingExtensionsRegistry(this); + return new ForgeCraftingExtensionsRegistry(); } @Override diff --git a/src/main/java/vazkii/zetaimplforge/client/ForgeClientRegistryExtension.java b/src/main/java/vazkii/zetaimplforge/client/ForgeClientRegistryExtension.java new file mode 100644 index 0000000000..14823c2d65 --- /dev/null +++ b/src/main/java/vazkii/zetaimplforge/client/ForgeClientRegistryExtension.java @@ -0,0 +1,21 @@ +package vazkii.zetaimplforge.client; + +import net.minecraft.client.renderer.ItemBlockRenderTypes; +import net.minecraft.world.level.block.Block; +import vazkii.zeta.Zeta; +import vazkii.zeta.client.ClientRegistryExtension; +import vazkii.zeta.registry.RenderLayerRegistry; + +public class ForgeClientRegistryExtension extends ClientRegistryExtension { + public ForgeClientRegistryExtension(Zeta z) { + super(z); + } + + //Forge has some weirdo extension, they want you to use json or something. + //Doing it from java is easier and more akin to how it happens on Fabric. + @Override + @SuppressWarnings("removal") + protected void doSetRenderLayer(Block block, RenderLayerRegistry.Layer layer) { + ItemBlockRenderTypes.setRenderLayer(block, resolvedTypes.get(layer)); + } +} diff --git a/src/main/java/vazkii/zetaimplforge/client/ForgeZetaClient.java b/src/main/java/vazkii/zetaimplforge/client/ForgeZetaClient.java index 1d34a9b4e5..fbf370ac10 100644 --- a/src/main/java/vazkii/zetaimplforge/client/ForgeZetaClient.java +++ b/src/main/java/vazkii/zetaimplforge/client/ForgeZetaClient.java @@ -18,6 +18,7 @@ import vazkii.quark.mixin.zeta_forge.client.AccessorBlockColors; import vazkii.quark.mixin.zeta_forge.client.AccessorItemColors; import vazkii.zeta.Zeta; +import vazkii.zeta.client.ClientRegistryExtension; import vazkii.zeta.client.ZetaClient; import net.minecraftforge.client.event.*; @@ -45,6 +46,11 @@ public ForgeZetaClient(Zeta z) { .orElse(null); } + @Override + public ClientRegistryExtension createClientRegistryExtension() { + return new ForgeClientRegistryExtension(zeta); + } + @Override public void start() { IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); diff --git a/src/main/java/vazkii/zetaimplforge/registry/ForgeBrewingRegistry.java b/src/main/java/vazkii/zetaimplforge/registry/ForgeBrewingRegistry.java index bcb269a721..d30195a60d 100644 --- a/src/main/java/vazkii/zetaimplforge/registry/ForgeBrewingRegistry.java +++ b/src/main/java/vazkii/zetaimplforge/registry/ForgeBrewingRegistry.java @@ -17,8 +17,6 @@ public class ForgeBrewingRegistry extends BrewingRegistry { public ForgeBrewingRegistry(ForgeZeta zeta) { super(zeta); - - zeta.loadBus.subscribe(this); } private record DelayedPotion(Potion input, Supplier reagentSupplier, Potion output) { diff --git a/src/main/java/vazkii/zetaimplforge/registry/ForgeCraftingExtensionsRegistry.java b/src/main/java/vazkii/zetaimplforge/registry/ForgeCraftingExtensionsRegistry.java index 8199091827..43e43b36af 100644 --- a/src/main/java/vazkii/zetaimplforge/registry/ForgeCraftingExtensionsRegistry.java +++ b/src/main/java/vazkii/zetaimplforge/registry/ForgeCraftingExtensionsRegistry.java @@ -15,15 +15,10 @@ import vazkii.zeta.recipe.IZetaConditionSerializer; import vazkii.zeta.recipe.IZetaIngredientSerializer; import vazkii.zeta.registry.CraftingExtensionsRegistry; -import vazkii.zetaimplforge.ForgeZeta; public class ForgeCraftingExtensionsRegistry implements CraftingExtensionsRegistry { public final Map, IIngredientSerializer> toForgeIngredientSerializers = new HashMap<>(); - public ForgeCraftingExtensionsRegistry(ForgeZeta z) { - z.loadBus.subscribe(this); - } - @Override public IZetaConditionSerializer registerConditionSerializer(IZetaConditionSerializer serializer) { CraftingHelper.register(toForgeConditionSerializer(serializer));