[Fix] NBT-Data is not serialized correct to recipes

This commit is contained in:
Frank 2023-04-29 15:15:20 +02:00
parent 61f5f41941
commit 8613014c11

View file

@ -5,7 +5,6 @@ import org.betterx.bclib.BCLib;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.TagParser; import net.minecraft.nbt.TagParser;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper; import net.minecraft.util.GsonHelper;
@ -17,24 +16,9 @@ import net.minecraft.world.level.ItemLike;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public class ItemUtil { public class ItemUtil {
public static String toStackString(@NotNull ItemStack stack) {
try {
if (stack == null) {
throw new IllegalStateException("Stack can't be null!");
}
Item item = stack.getItem();
return BuiltInRegistries.ITEM.getKey(item) + ":" + stack.getCount();
} catch (Exception ex) {
BCLib.LOGGER.error("ItemStack serialization error!", ex);
}
return "";
}
@Nullable @Nullable
public static ItemStack fromStackString(String stackString) { public static ItemStack fromStackString(String stackString) {
if (stackString == null || stackString.equals("")) { if (stackString == null || stackString.equals("")) {
@ -51,9 +35,8 @@ public class ItemUtil {
return new ItemStack(item); return new ItemStack(item);
} }
ResourceLocation itemId = new ResourceLocation(parts[0], parts[1]); ResourceLocation itemId = new ResourceLocation(parts[0], parts[1]);
Item item = BuiltInRegistries.ITEM.getOptional(itemId).orElseThrow(() -> { Item item = BuiltInRegistries.ITEM.getOptional(itemId)
return new IllegalStateException("Output item " + itemId + " does not exists!"); .orElseThrow(() -> new IllegalStateException("Output item " + itemId + " does not exists!"));
});
return new ItemStack(item, Integer.valueOf(parts[2])); return new ItemStack(item, Integer.valueOf(parts[2]));
} catch (Exception ex) { } catch (Exception ex) {
BCLib.LOGGER.error("ItemStack deserialization error!", ex); BCLib.LOGGER.error("ItemStack deserialization error!", ex);
@ -68,7 +51,7 @@ public class ItemUtil {
CompoundTag nbt = TagParser.parseTag(nbtData); CompoundTag nbt = TagParser.parseTag(nbtData);
return nbt; return nbt;
} catch (CommandSyntaxException ex) { } catch (CommandSyntaxException ex) {
BCLib.LOGGER.warning("Error parse nbt data for output.", ex); BCLib.LOGGER.warning("Error parsing nbt data for output.", ex);
} }
} }
return null; return null;
@ -76,7 +59,7 @@ public class ItemUtil {
public static void writeNBT(JsonObject root, CompoundTag nbt) { public static void writeNBT(JsonObject root, CompoundTag nbt) {
if (nbt != null) { if (nbt != null) {
final String nbtData = NbtUtils.prettyPrint(nbt); final String nbtData = nbt.toString();
root.addProperty("nbt", nbtData); root.addProperty("nbt", nbtData);
} }
} }