Skip to content

Commit

Permalink
Merge pull request #331 from Dragon-Seeker/1.21.2-NbtOptionalDeserial…
Browse files Browse the repository at this point in the history
…izationFix

[1.21.2] NBT Optional deserialization fix
  • Loading branch information
gliscowo authored Dec 11, 2024
2 parents c41c5a1 + 28d3f2d commit 26f816b
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 70 deletions.
18 changes: 9 additions & 9 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
org.gradle.jvmargs=-Xmx2G
# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_base_version=1.21.2
minecraft_version=1.21.2
yarn_mappings=1.21.2+build.1
loader_version=0.16.7
minecraft_base_version=1.21.4
minecraft_version=1.21.4
yarn_mappings=1.21.4+build.1
loader_version=0.16.9
# Mod Properties
mod_version=0.12.18
maven_group=io.wispforest
archives_base_name=owo-lib
# Dependencies
fabric_version=0.106.1+1.21.2
fabric_version=0.111.0+1.21.4

# https://maven.shedaniel.me/me/shedaniel/RoughlyEnoughItems-fabric/
rei_version=15.0.728
rei_version=17.0.792

# https://maven.terraformersmc.com/releases/dev/emi/emi-fabric/
emi_version=1.1.6+1.20.6
emi_version=1.1.18+1.21.1

# https://search.maven.org/artifact/blue.endless/jankson
jankson_version=1.2.2

# https://maven.terraformersmc.com/releases/com/terraformersmc/modmenu
modmenu_version=12.0.0-beta.1
modmenu_version=13.0.0-beta.1

# https://maven.nucleoid.xyz/xyz/nucleoid/server-translations-api/
stapi_version=2.3.1+1.21-pre2
stapi_version=2.4.0+1.21.2-rc1
5 changes: 4 additions & 1 deletion owo-ui.xsd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">

<xs:element name="owo-ui">
<xs:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,24 @@
import io.wispforest.owo.util.RecipeRemainderStorage;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeEntry;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.input.RecipeInput;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.resource.JsonDataLoader;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceFinder;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Mixin(JsonDataLoader.class)
public abstract class JsonDataLoaderMixin {

@WrapOperation(method = "load", at = @At(value = "INVOKE", target = "Lcom/google/gson/JsonParser;parseReader(Ljava/io/Reader;)Lcom/google/gson/JsonElement;"))
private static JsonElement deserializeRecipeSpecificRemainders(Reader jsonReader, Operation<JsonElement> original, @Local(argsOnly = true) String dataType, @Local(ordinal = 1) Identifier recipeId) {
@WrapOperation(method = "load(Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/resource/ResourceFinder;Lcom/mojang/serialization/DynamicOps;Lcom/mojang/serialization/Codec;Ljava/util/Map;)V", at = @At(value = "INVOKE", target = "Lcom/google/gson/JsonParser;parseReader(Ljava/io/Reader;)Lcom/google/gson/JsonElement;"))
private static JsonElement deserializeRecipeSpecificRemainders(Reader jsonReader, Operation<JsonElement> original, @Local(argsOnly = true) ResourceFinder finder, @Local(ordinal = 1) Identifier recipeId) {
var element = original.call(jsonReader);

if (dataType.equals(RegistryKeys.getPath(RegistryKeys.RECIPE)) && element instanceof JsonObject json) {
if (ServerRecipeManagerAccessor.owo$getFinder() == finder && element instanceof JsonObject json) {
if (json.has("owo:remainders")) {
var remainders = new HashMap<Item, ItemStack>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.wispforest.owo.mixin.recipe_remainders;

import net.minecraft.recipe.ServerRecipeManager;
import net.minecraft.resource.ResourceFinder;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ServerRecipeManager.class)
public interface ServerRecipeManagerAccessor {

@Accessor("FINDER")
static ResourceFinder owo$getFinder() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.wispforest.owo.serialization.format.nbt;

record IdentityHolder<T>(T t) {
@Override
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != this.getClass()) return false;
return this.t == ((IdentityHolder<?>) obj).t;
}

@Override
public int hashCode() {
return System.identityHashCode(this.t);
}

@Override
public String toString() {
return "IdentityHolder[t=" + t + ']';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ public byte[] readBytes(SerializationContext ctx) {
return this.getAs(this.getValue(), NbtByteArray.class).getByteArray();
}

private final Set<IdentityHolder<NbtElement>> encodedOptionals = Collections.newSetFromMap(new WeakHashMap<>());

@Override
public <V> Optional<V> readOptional(SerializationContext ctx, Endec<V> endec) {
var value = this.getValue();
if (this.encodedOptionals.contains(new IdentityHolder<>(value))) {
return Optional.of(endec.decode(ctx, this));
}

var struct = this.struct();
return struct.field("present", ctx, Endec.BOOLEAN)
? Optional.of(struct.field("value", ctx, endec))
Expand Down Expand Up @@ -239,8 +246,10 @@ public Struct(NbtCompound compound) {

return defaultValueFactory.get();
}
var element = this.compound.get(name);
if (defaultValueFactory != null) NbtDeserializer.this.encodedOptionals.add(new IdentityHolder<>(element));
return NbtDeserializer.this.frame(
() -> this.compound.get(name),
() -> element,
() -> endec.decode(ctx, NbtDeserializer.this)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void writeBytes(SerializationContext ctx, byte[] bytes) {
this.consume(new NbtByteArray(bytes));
}

private final Set<NbtElement> encodedOptionals = Collections.newSetFromMap(new WeakHashMap<>());
private final Set<IdentityHolder<NbtElement>> encodedOptionals = Collections.newSetFromMap(new WeakHashMap<>());

@Override
public <V> void writeOptional(SerializationContext ctx, Endec<V> endec, Optional<V> optional) {
Expand All @@ -115,7 +115,7 @@ public <V> void writeOptional(SerializationContext ctx, Endec<V> endec, Optional

var compound = encoded.require("optional representation");

encodedOptionals.add(compound);
encodedOptionals.add(new IdentityHolder<>(compound));
frameData.setValue(compound);
});

Expand Down Expand Up @@ -177,7 +177,7 @@ public <F> Struct field(String name, SerializationContext ctx, Endec<F> endec, F

var element = encoded.require("struct field");

if (mayOmit && NbtSerializer.this.encodedOptionals.contains(element)) {
if (mayOmit && NbtSerializer.this.encodedOptionals.contains(new IdentityHolder<>(element))) {
var nbtCompound = (NbtCompound) element;

if(!nbtCompound.getBoolean("present")) return;
Expand Down
33 changes: 15 additions & 18 deletions src/main/java/io/wispforest/owo/ui/component/BlockComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,24 @@ public void draw(OwoUIDrawContext context, int mouseX, int mouseY, float partial

context.getMatrices().translate(-.5, -.5, -.5);

// TODO: [1.21.2-Porting] Figure out if this is still needed or not
//RenderSystem.runAsFancy(() -> {
final var vertexConsumers = client.getBufferBuilders().getEntityVertexConsumers();
if (this.state.getRenderType() != BlockRenderType.ENTITYBLOCK_ANIMATED) {
this.client.getBlockRenderManager().renderBlockAsEntity(
this.state, context.getMatrices(), vertexConsumers,
LightmapTextureManager.MAX_LIGHT_COORDINATE, OverlayTexture.DEFAULT_UV
);
}
final var vertexConsumers = client.getBufferBuilders().getEntityVertexConsumers();
if (this.state.getRenderType() != BlockRenderType.INVISIBLE) {
this.client.getBlockRenderManager().renderBlockAsEntity(
this.state, context.getMatrices(), vertexConsumers,
LightmapTextureManager.MAX_LIGHT_COORDINATE, OverlayTexture.DEFAULT_UV
);
}

if (this.entity != null) {
var медведь = this.client.getBlockEntityRenderDispatcher().get(this.entity);
if (медведь != null) {
медведь.render(entity, partialTicks, context.getMatrices(), vertexConsumers, LightmapTextureManager.MAX_LIGHT_COORDINATE, OverlayTexture.DEFAULT_UV);
}
if (this.entity != null) {
var медведь = this.client.getBlockEntityRenderDispatcher().get(this.entity);
if (медведь != null) {
медведь.render(entity, partialTicks, context.getMatrices(), vertexConsumers, LightmapTextureManager.MAX_LIGHT_COORDINATE, OverlayTexture.DEFAULT_UV);
}
}

RenderSystem.setShaderLights(new Vector3f(-1.5f, -.5f, 0), new Vector3f(0, -1, 0));
vertexConsumers.draw();
DiffuseLighting.enableGuiDepthLighting();
//});
RenderSystem.setShaderLights(new Vector3f(-1.5f, -.5f, 0), new Vector3f(0, -1, 0));
vertexConsumers.draw();
DiffuseLighting.enableGuiDepthLighting();

context.getMatrices().pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ profile, new WorldSession(TelemetrySender.NOOP, false, Duration.ZERO, ""),

this.skinTextures = DefaultSkinHelper.getSkinTextures(completeProfile);
this.client.getSkinProvider().fetchSkinTextures(completeProfile).thenAccept(textures -> {
this.skinTextures = textures;
textures.ifPresent($ -> this.skinTextures = $);
});
});
}
Expand Down
30 changes: 13 additions & 17 deletions src/main/java/io/wispforest/owo/ui/component/ItemComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@
import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.item.ItemModelManager;
import net.minecraft.client.render.DiffuseLighting;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.item.ModelTransformationMode;
import net.minecraft.client.render.item.ItemRenderState;
import net.minecraft.command.argument.ItemStringReader;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ModelTransformationMode;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.w3c.dom.Element;

import java.util.ArrayList;
Expand All @@ -38,17 +37,15 @@

public class ItemComponent extends BaseComponent {

protected static final Matrix4f ITEM_SCALING = new Matrix4f().scaling(16, -16, 16);
protected static final ItemRenderState ITEM_RENDER_STATE = new ItemRenderState();

protected final VertexConsumerProvider.Immediate entityBuffers;
protected final ItemRenderer itemRenderer;
protected final ItemModelManager itemModelManager;
protected ItemStack stack;
protected boolean showOverlay = false;
protected boolean setTooltipFromStack = false;

protected ItemComponent(ItemStack stack) {
this.entityBuffers = MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers();
this.itemRenderer = MinecraftClient.getInstance().getItemRenderer();
this.itemModelManager = MinecraftClient.getInstance().getItemModelManager();
this.stack = stack;
}

Expand All @@ -64,8 +61,11 @@ protected int determineVerticalContentSize(Sizing sizing) {

@Override
public void draw(OwoUIDrawContext context, int mouseX, int mouseY, float partialTicks, float delta) {
final boolean notSideLit = !this.itemRenderer.getModel(this.stack, null, null, 0).isSideLit();
this.itemModelManager.update(ITEM_RENDER_STATE, this.stack, ModelTransformationMode.GUI, false, null, null, 0);

final boolean notSideLit = !ITEM_RENDER_STATE.isSideLit();
if (notSideLit) {
context.draw();
DiffuseLighting.disableGuiDepthLighting();
}

Expand All @@ -80,16 +80,12 @@ public void draw(OwoUIDrawContext context, int mouseX, int mouseY, float partial
matrices.translate(8.0, 8.0, 0.0);

// Vanilla scaling and y inversion
if (notSideLit) {
matrices.scale(16, -16, 16);
} else {
matrices.multiplyPositionMatrix(ITEM_SCALING);
}
matrices.scale(16, -16, 16);

var client = MinecraftClient.getInstance();

this.itemRenderer.renderItem(this.stack, ModelTransformationMode.GUI, LightmapTextureManager.MAX_LIGHT_COORDINATE, OverlayTexture.DEFAULT_UV, matrices, entityBuffers, client.world, 0);
this.entityBuffers.draw();
ITEM_RENDER_STATE.render(matrices, context.vertexConsumers(), LightmapTextureManager.MAX_LIGHT_COORDINATE, OverlayTexture.DEFAULT_UV);
context.draw();

// Clean up
matrices.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void inflate(Size space) {
int cursor = this.editBox.getCursor();
int selection = ((EditBoxAccessor) this.editBox).owo$getSelectionEnd();

((EditBoxAccessor) this.editBox).owo$setWidth(this.width() - this.getPaddingDoubled() - 9);
((EditBoxAccessor) this.editBox).owo$setWidth(this.width() - this.getPadding() - 9);
this.editBox.setText(this.getText());

super.inflate(space);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.resource.JsonDataLoader;
import net.minecraft.resource.ResourceFinder;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;
Expand Down Expand Up @@ -258,7 +259,7 @@ public static class MetadataLoader extends JsonDataLoader<NinePatchTexture> impl
private static final Map<Identifier, NinePatchTexture> LOADED_TEXTURES = new HashMap<>();

public MetadataLoader() {
super(CodecUtils.toCodec(NinePatchTexture.ENDEC), "nine_patch_textures");
super(CodecUtils.toCodec(NinePatchTexture.ENDEC), ResourceFinder.json("nine_patch_textures"));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/owo.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"offline.WorldSaveHandlerMixin",
"recipe_remainders.CraftingResultSlotMixin",
"recipe_remainders.JsonDataLoaderMixin",
"recipe_remainders.ServerRecipeManagerAccessor",
"registry.ReferenceAccessor",
"registry.SimpleRegistryMixin",
"text.LanguageMixin",
Expand Down
13 changes: 13 additions & 0 deletions src/testmod/java/io/wispforest/uwu/rei/UiCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
import me.shedaniel.rei.api.client.registry.display.DisplayCategory;
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.Display;
import me.shedaniel.rei.api.common.display.DisplaySerializer;
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.util.EntryIngredients;
import me.shedaniel.rei.api.common.util.EntryStacks;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class UiCategory implements DisplayCategory<Display> {

Expand Down Expand Up @@ -92,5 +95,15 @@ public List<EntryIngredient> getOutputEntries() {
public CategoryIdentifier<?> getCategoryIdentifier() {
return ID;
}

@Override
public Optional<Identifier> getDisplayLocation() {
return Optional.empty();
}

@Override
public @Nullable DisplaySerializer<? extends Display> getSerializer() {
return null;
}
}
}

0 comments on commit 26f816b

Please sign in to comment.