[Changes] Allow ItemStacks with NBT-Data as recipe results in Builders

This commit is contained in:
Frank 2023-04-29 14:21:58 +02:00
parent 093f3465fb
commit fe404d1aa4
4 changed files with 58 additions and 20 deletions

View file

@ -33,12 +33,15 @@ public abstract class AbstractBaseRecipeBuilder<T extends AbstractBaseRecipeBuil
protected boolean alright; protected boolean alright;
protected AbstractBaseRecipeBuilder(ResourceLocation id, ItemStack output) {
this.id = id;
this.output = output;
this.category = RecipeCategory.MISC;
this.alright = RecipeHelper.exists(output.getItem());
}
protected AbstractBaseRecipeBuilder(ResourceLocation id, ItemLike output) { protected AbstractBaseRecipeBuilder(ResourceLocation id, ItemLike output) {
this.id = id; this(id, new ItemStack(output, 1));
this.output = new ItemStack(output, 1);
this.category = RecipeCategory.MISC;
this.alright = RecipeHelper.exists(output);
} }
public T setCategory(RecipeCategory category) { public T setCategory(RecipeCategory category) {

View file

@ -6,6 +6,7 @@ import org.betterx.bclib.util.RecipeHelper;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.ItemLike;
@ -13,9 +14,12 @@ public abstract class AbstractSimpleRecipeBuilder<T extends AbstractSimpleRecipe
protected Ingredient primaryInput; protected Ingredient primaryInput;
protected AbstractSimpleRecipeBuilder(ResourceLocation id, ItemLike output) { protected AbstractSimpleRecipeBuilder(ResourceLocation id, ItemLike output) {
super(id, output); this(id, new ItemStack(output, 1));
} }
protected AbstractSimpleRecipeBuilder(ResourceLocation id, ItemStack stack) {
super(id, stack);
}
public T setPrimaryInput(ItemLike... inputs) { public T setPrimaryInput(ItemLike... inputs) {
for (ItemLike item : inputs) { for (ItemLike item : inputs) {

View file

@ -12,6 +12,7 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
import net.minecraft.world.Container; import net.minecraft.world.Container;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.ItemLike;
@ -26,6 +27,10 @@ public abstract class AbstractSingleInputRecipeBuilder<T extends AbstractSingleI
protected AbstractSingleInputRecipeBuilder(ResourceLocation id, ItemLike output) { protected AbstractSingleInputRecipeBuilder(ResourceLocation id, ItemLike output) {
this(id, new ItemStack(output, 1));
}
protected AbstractSingleInputRecipeBuilder(ResourceLocation id, ItemStack output) {
super(id, output); super(id, output);
this.advancement = Advancement.Builder.advancement(); this.advancement = Advancement.Builder.advancement();
} }
@ -56,7 +61,7 @@ public abstract class AbstractSingleInputRecipeBuilder<T extends AbstractSingleI
protected abstract RecipeSerializer<R> getSerializer(); protected abstract RecipeSerializer<R> getSerializer();
protected void serializeRecipeData(JsonObject root) { protected void serializeRecipeData(JsonObject root) {
root.add("input", primaryInput.toJson()); root.add("input", ItemUtil.toJsonIngredientWithNBT(primaryInput));
if (group != null && !group.isEmpty()) { if (group != null && !group.isEmpty()) {
root.addProperty("group", group); root.addProperty("group", group);

View file

@ -11,8 +11,10 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper; import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.ItemLike;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -59,17 +61,41 @@ public class ItemUtil {
return null; return null;
} }
public static ItemStack fromJsonRecipeWithNBT(JsonObject recipe) { public static CompoundTag readNBT(JsonObject recipe) {
ItemStack output = ItemUtil.fromJsonRecipe(recipe); if (recipe.has("nbt")) {
if (output != null && recipe.has("nbt")) {
try { try {
String nbtData = GsonHelper.getAsString(recipe, "nbt"); String nbtData = GsonHelper.getAsString(recipe, "nbt");
CompoundTag nbt = TagParser.parseTag(nbtData); CompoundTag nbt = TagParser.parseTag(nbtData);
output.setTag(nbt); return nbt;
} catch (CommandSyntaxException ex) { } catch (CommandSyntaxException ex) {
BCLib.LOGGER.warning("Error parse nbt data for output.", ex); BCLib.LOGGER.warning("Error parse nbt data for output.", ex);
} }
} }
return null;
}
public static void writeNBT(JsonObject root, CompoundTag nbt) {
if (nbt != null) {
final String nbtData = NbtUtils.prettyPrint(nbt);
root.addProperty("nbt", nbtData);
}
}
public static Ingredient fromJsonIngredientWithNBT(JsonObject ingredient) {
Ingredient ing = Ingredient.fromJson(ingredient);
CompoundTag nbt = readNBT(ingredient);
if (nbt != null && !ing.isEmpty()) {
ing.getItems()[0].setTag(nbt);
}
return ing;
}
public static ItemStack fromJsonRecipeWithNBT(JsonObject recipe) {
ItemStack output = ItemUtil.fromJsonRecipe(recipe);
CompoundTag nbt = ItemUtil.readNBT(recipe);
if (output != null && nbt != null) {
output.setTag(nbt);
}
return output; return output;
} }
@ -91,6 +117,15 @@ public class ItemUtil {
return null; return null;
} }
public static JsonElement toJsonIngredientWithNBT(Ingredient ing) {
JsonElement el = ing.toJson();
if (el.isJsonObject() && !ing.isEmpty() && ing.getItems()[0].hasTag()) {
JsonObject obj = el.getAsJsonObject();
writeNBT(obj, ing.getItems()[0].getTag());
}
return el;
}
public static JsonObject toJsonRecipeWithNBT(ItemStack stack) { public static JsonObject toJsonRecipeWithNBT(ItemStack stack) {
return toJsonRecipeWithNBT(stack.getItem(), stack.getCount(), stack.getTag()); return toJsonRecipeWithNBT(stack.getItem(), stack.getCount(), stack.getTag());
@ -98,16 +133,7 @@ public class ItemUtil {
public static JsonObject toJsonRecipeWithNBT(ItemLike item, int count, CompoundTag nbt) { public static JsonObject toJsonRecipeWithNBT(ItemLike item, int count, CompoundTag nbt) {
JsonObject root = toJsonRecipe(item, count); JsonObject root = toJsonRecipe(item, count);
if (nbt != null) { writeNBT(root, nbt);
final String nbtData = NbtUtils.prettyPrint(nbt);
root.addProperty("nbt", nbtData);
//TODO: just for testing
try {
TagParser.parseTag(nbtData);
} catch (CommandSyntaxException e) {
throw new RuntimeException(e);
}
}
return root; return root;
} }