From 3dec711b78bf6345476552fce97501d525ecb4d6 Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 4 Aug 2023 18:32:41 +0100 Subject: [PATCH] Fix #127 relative to CaffeineMC/sodium-fabric#1602 --- .../client/gui/spellbook/IngredientTree.java | 7 +- .../render/PassThroughVertexConsumer.java | 111 +++++++++--------- .../client/render/WorldRenderDelegate.java | 4 +- src/main/resources/unicopia.aw | 1 + 4 files changed, 59 insertions(+), 64 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java index d54a80e1..09e26699 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java @@ -261,9 +261,8 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder { } static class HiddenStacks extends Stacks { - private static final PassThroughVertexConsumer.Parameters FIXTURE = new PassThroughVertexConsumer.Parameters().color((parent, r, g, b, a) -> { - parent.color(0, 0, 0, 0.6F); - }); + private static final PassThroughVertexConsumer.Parameters FIXTURE = new PassThroughVertexConsumer.Parameters() + .color((parent, r, g, b, a) -> parent.color(0, 0, 0, 0.6F)); HiddenStacks(ItemStack stack) { super(stack); @@ -288,7 +287,7 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder { } RenderSystem.disableDepthTest(); try { - itemRenderer.renderItem(stack, ModelTransformationMode.GUI, false, matrices, layer -> PassThroughVertexConsumer.of(immediate.getBuffer(layer), FIXTURE), 0, OverlayTexture.DEFAULT_UV, model); + itemRenderer.renderItem(stack, ModelTransformationMode.GUI, false, matrices, layer -> FIXTURE.build(immediate.getBuffer(layer)), 0, OverlayTexture.DEFAULT_UV, model); immediate.draw(); } catch (Exception e) { // Sodium diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/PassThroughVertexConsumer.java b/src/main/java/com/minelittlepony/unicopia/client/render/PassThroughVertexConsumer.java index 7f1ebbbd..40df7b5c 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/render/PassThroughVertexConsumer.java +++ b/src/main/java/com/minelittlepony/unicopia/client/render/PassThroughVertexConsumer.java @@ -1,88 +1,57 @@ package com.minelittlepony.unicopia.client.render; +import org.jetbrains.annotations.Nullable; + import net.minecraft.client.render.VertexConsumer; +import net.minecraft.client.render.VertexConsumers; -public class PassThroughVertexConsumer implements VertexConsumer { - private static final ColorFix COLOR = VertexConsumer::color; - private static final FUvFix TEXTURE = VertexConsumer::texture; - private static final IUvFix OVERLAY = VertexConsumer::overlay; - private static final IUvFix LIGHT = VertexConsumer::light; +public class PassThroughVertexConsumer extends VertexConsumers.Union implements VertexConsumer { + private final Applicate colorFix; + private final Applicate textureFix; + private final Applicate overlayFix; + private final Applicate lightFix; - private final VertexConsumer parent; - - private final ColorFix colorFix; - private final FUvFix textureFix; - private final IUvFix overlayFix; - private final IUvFix lightFix; - - public static VertexConsumer of(VertexConsumer parent, Parameters parameters) { - return new PassThroughVertexConsumer(parent, parameters); - } - - PassThroughVertexConsumer(VertexConsumer parent, Parameters parameters) { - this.parent = parent; - colorFix = parameters.colorFix; - textureFix = parameters.textureFix; - overlayFix = parameters.overlayFix; - lightFix = parameters.lightFix; - } - - @Override - public VertexConsumer vertex(double x, double y, double z) { - parent.vertex(x, y, z); - return this; + private PassThroughVertexConsumer(VertexConsumer parent, Parameters parameters) { + super(new VertexConsumer[] {parent}); + colorFix = Applicate.of(parameters.colorFix, (self, r, g, b, a) -> super.color(r, g, b, a)); + textureFix = Applicate.of(parameters.textureFix, (self, u, v) -> super.texture(u, v)); + overlayFix = Applicate.of(parameters.overlayFix, (self, u, v) -> super.overlay(u, v)); + lightFix = Applicate.of(parameters.lightFix, (self, u, v) -> super.light(u, v)); } @Override public VertexConsumer color(int r, int g, int b, int a) { - colorFix.apply(parent, r, g, b, a); + colorFix.getFix().apply(this, r, g, b, a); + colorFix.nested = false; return this; } @Override public VertexConsumer texture(float u, float v) { - textureFix.apply(parent, u, v); + textureFix.getFix().apply(this, u, v); + textureFix.nested = false; return this; } @Override public VertexConsumer overlay(int u, int v) { - overlayFix.apply(parent, u, v); + overlayFix.getFix().apply(this, u, v); + overlayFix.nested = false; return this; } @Override public VertexConsumer light(int u, int v) { - lightFix.apply(parent, u, v); + lightFix.getFix().apply(this, u, v); + lightFix.nested = false; return this; } - @Override - public VertexConsumer normal(float x, float y, float z) { - parent.normal(x, y, z); - return this; - } - - @Override - public void next() { - parent.next(); - } - - @Override - public void fixedColor(int r, int g, int b, int a) { - parent.fixedColor(r, g, b, a); - } - - @Override - public void unfixColor() { - parent.unfixColor(); - } - public static class Parameters { - private ColorFix colorFix = COLOR; - private FUvFix textureFix = TEXTURE; - private IUvFix overlayFix = OVERLAY; - private IUvFix lightFix = LIGHT; + private @Nullable ColorFix colorFix; + private @Nullable FUvFix textureFix; + private @Nullable IUvFix overlayFix; + private @Nullable IUvFix lightFix; public Parameters color(ColorFix fix) { colorFix = fix; @@ -103,6 +72,34 @@ public class PassThroughVertexConsumer implements VertexConsumer { lightFix = fix; return this; } + + public VertexConsumer build(VertexConsumer parent) { + return new PassThroughVertexConsumer(parent, this); + } + } + + private static class Applicate { + public final T fix; + public final T fallback; + + public boolean nested; + + public Applicate(T fix, T fallback) { + this.fix = fix; + this.fallback = fallback; + } + + public T getFix() { + try { + return nested ? fallback : fix; + } finally { + nested = true; + } + } + + static Applicate of(@Nullable T fix, T fallback) { + return new Applicate<>(fix == null ? fallback : fix, fallback); + } } public interface PosFix { diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/WorldRenderDelegate.java b/src/main/java/com/minelittlepony/unicopia/client/render/WorldRenderDelegate.java index e4adfdf2..152dab5b 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/render/WorldRenderDelegate.java +++ b/src/main/java/com/minelittlepony/unicopia/client/render/WorldRenderDelegate.java @@ -75,9 +75,7 @@ public class WorldRenderDelegate { if (!recurseMinion && pony instanceof Creature creature && creature.isMinion()) { recurseMinion = true; - dispatcher.render(creature.asEntity(), x, y, z, yaw, tickDelta, matrices, layer -> { - return PassThroughVertexConsumer.of(vertices.getBuffer(layer), MINION_OVERLAY); - }, light); + dispatcher.render(creature.asEntity(), x, y, z, yaw, tickDelta, matrices, layer -> MINION_OVERLAY.build(vertices.getBuffer(layer)), light); recurseMinion = false; return true; diff --git a/src/main/resources/unicopia.aw b/src/main/resources/unicopia.aw index 18495bde..dc52051b 100644 --- a/src/main/resources/unicopia.aw +++ b/src/main/resources/unicopia.aw @@ -2,6 +2,7 @@ accessWidener v1 named accessible class net/minecraft/client/render/RenderLayer$MultiPhaseParameters accessible method net/minecraft/client/render/RenderLayer of (Ljava/lang/String;Lnet/minecraft/client/render/VertexFormat;Lnet/minecraft/client/render/VertexFormat$DrawMode;IZZLnet/minecraft/client/render/RenderLayer$MultiPhaseParameters;)Lnet/minecraft/client/render/RenderLayer$MultiPhase; accessible class net/minecraft/client/render/item/HeldItemRenderer$HandRenderType +accessible class net/minecraft/client/render/VertexConsumers$Union accessible method net/minecraft/world/GameRules register (Ljava/lang/String;Lnet/minecraft/world/GameRules$Category;Lnet/minecraft/world/GameRules$Type;)Lnet/minecraft/world/GameRules$Key; accessible method net/minecraft/world/GameRules$BooleanRule create (Z)Lnet/minecraft/world/GameRules$Type;