Skip to content

Commit

Permalink
Unify a few of the client registration classes (hmm). Generally try t…
Browse files Browse the repository at this point in the history
…o avoid subscribing in constructors too
  • Loading branch information
quat1024 committed Nov 5, 2023
1 parent 8c9cdd4 commit 772a06a
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public static class Client {

public Client(Zeta z) {
this.z = z;
z.playBus.subscribe(this);
}

@PlayEvent
Expand Down
1 change: 0 additions & 1 deletion src/main/java/vazkii/quark/base/proxy/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/vazkii/zeta/Zeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 46 additions & 6 deletions src/main/java/vazkii/zeta/client/ClientRegistryExtension.java
Original file line number Diff line number Diff line change
@@ -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<RenderLayerRegistry.Layer, RenderType> 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<Block, BlockColor> blockColorCreator = event.getNamedBlockColors().get(name);
if(blockColorCreator == null)
z.log.error("Unknown block color creator {} used on block {}", name, block);
Expand All @@ -35,13 +68,20 @@ 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<Item, ItemColor> itemColorCreator = event.getNamedItemColors().get(name);
if(itemColorCreator == null)
z.log.error("Unknown item color creator {} used on item {}", name, item);
else
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);
}
4 changes: 0 additions & 4 deletions src/main/java/vazkii/zeta/client/ClientTicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/vazkii/zeta/client/ZetaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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() {
Expand All @@ -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);
Expand Down
30 changes: 3 additions & 27 deletions src/main/java/vazkii/zeta/registry/DyeablesRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -158,26 +156,4 @@ public ItemStack dyeItem(ItemStack stack, List<DyeItem> 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);
}
}
}
}
67 changes: 23 additions & 44 deletions src/main/java/vazkii/zeta/registry/RenderLayerRegistry.java
Original file line number Diff line number Diff line change
@@ -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<Block, Layer> mapping = new HashMap<>();
protected final Map<Block, Block> inheritances = new HashMap<>();
protected Map<Block, Layer> mapping = new HashMap<>();
protected Map<Block, Block> inheritances = new HashMap<>();

public enum Layer {
SOLID,
Expand All @@ -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<Layer, RenderType> resolvedTypes = new EnumMap<>(Layer.class);
public void finalize(BiConsumer<Block, Layer> 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;
}

}
5 changes: 3 additions & 2 deletions src/main/java/vazkii/zeta/registry/ZetaRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class ZetaRegistry {
protected final Map<Object, ResourceLocation> 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<Block, String> blocksToColorProviderName = new HashMap<>();
private final Map<Item, String> itemsToColorProviderName = new HashMap<>();

Expand Down Expand Up @@ -147,12 +148,12 @@ public void clearDeferCache(ResourceLocation resourceLocation) {
defers.removeAll(resourceLocation);
}

public void submitBlockColors(BiConsumer<Block, String> consumer) {
public void finalizeBlockColors(BiConsumer<Block, String> consumer) {
blocksToColorProviderName.forEach(consumer);
blocksToColorProviderName.clear();
}

public void submitItemColors(BiConsumer<Item, String> consumer) {
public void finalizeItemColors(BiConsumer<Item, String> consumer) {
itemsToColorProviderName.forEach(consumer);
itemsToColorProviderName.clear();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vazkii/zetaimplforge/ForgeZeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public ZetaRegistry createRegistry() {

@Override
public CraftingExtensionsRegistry createCraftingExtensionsRegistry() {
return new ForgeCraftingExtensionsRegistry(this);
return new ForgeCraftingExtensionsRegistry();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading

0 comments on commit 772a06a

Please sign in to comment.