Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Recipe NBT Serialization #2105

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions src/main/java/com/gregtechceu/gtceu/syncdata/GTRecipePayload.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.gregtechceu.gtceu.syncdata;

import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.GTRecipeSerializer;
import com.gregtechceu.gtceu.common.data.GTRecipeTypes;

import com.lowdragmc.lowdraglib.Platform;
import com.lowdragmc.lowdraglib.syncdata.payload.ObjectTypedPayload;

import net.minecraft.client.Minecraft;
import net.minecraft.nbt.ByteArrayTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.*;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.item.crafting.SmeltingRecipe;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
Expand All @@ -27,15 +28,32 @@ public class GTRecipePayload extends ObjectTypedPayload<GTRecipe> {
@Nullable
@Override
public Tag serializeNBT() {
return StringTag.valueOf(payload.id.toString());
CompoundTag tag = new CompoundTag();
tag.putString("id", payload.id.toString());
tag.put("recipe",
GTRecipeSerializer.CODEC.encodeStart(NbtOps.INSTANCE, payload).result().orElse(new CompoundTag()));
return tag;
}

@Override
public void deserializeNBT(Tag tag) {
RecipeManager recipeManager = Platform.getMinecraftServer().getRecipeManager();
if (tag instanceof StringTag stringTag) {
payload = (GTRecipe) recipeManager.byKey(new ResourceLocation(stringTag.getAsString())).orElse(null);
} else if (tag instanceof ByteArrayTag byteArray) {
if (tag instanceof CompoundTag compoundTag) {
payload = GTRecipeSerializer.CODEC.parse(NbtOps.INSTANCE, compoundTag.get("recipe")).result().orElse(null);
if (payload != null) {
payload.id = new ResourceLocation(compoundTag.getString("id"));
}
} else if (tag instanceof StringTag stringTag) { // Backwards Compatibility
var recipe = recipeManager.byKey(new ResourceLocation(stringTag.getAsString())).orElse(null);
if (recipe instanceof GTRecipe gtRecipe) {
payload = gtRecipe;
} else if (recipe instanceof SmeltingRecipe smeltingRecipe) {
payload = GTRecipeTypes.FURNACE_RECIPES.toGTrecipe(new ResourceLocation(stringTag.getAsString()),
smeltingRecipe);
} else {
payload = null;
}
} else if (tag instanceof ByteArrayTag byteArray) { // Backwards Compatibility
ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(byteArray.getAsByteArray());
FriendlyByteBuf buf = new FriendlyByteBuf(copiedDataBuffer);
payload = (GTRecipe) recipeManager.byKey(buf.readResourceLocation()).orElse(null);
Expand All @@ -46,16 +64,22 @@ public void deserializeNBT(Tag tag) {
@Override
public void writePayload(FriendlyByteBuf buf) {
buf.writeResourceLocation(this.payload.id);
GTRecipeSerializer.SERIALIZER.toNetwork(buf, this.payload);
}

@Override
public void readPayload(FriendlyByteBuf buf) {
RecipeManager recipeManager;
if (!Platform.isClient()) {
recipeManager = Platform.getMinecraftServer().getRecipeManager();
} else {
recipeManager = Minecraft.getInstance().getConnection().getRecipeManager();
var id = buf.readResourceLocation();
if (buf.isReadable()) {
this.payload = GTRecipeSerializer.SERIALIZER.fromNetwork(id, buf);
} else { // Backwards Compatibility
RecipeManager recipeManager;
if (!Platform.isClient()) {
recipeManager = Platform.getMinecraftServer().getRecipeManager();
} else {
recipeManager = Minecraft.getInstance().getConnection().getRecipeManager();
}
this.payload = (GTRecipe) recipeManager.byKey(id).orElse(null);
}
this.payload = (GTRecipe) recipeManager.byKey(buf.readResourceLocation()).orElse(null);
}
}