[Changes] Allow ItemStacks with NBT-Data as recipe results in Builders
This commit is contained in:
parent
093f3465fb
commit
fe404d1aa4
4 changed files with 58 additions and 20 deletions
|
@ -33,12 +33,15 @@ public abstract class AbstractBaseRecipeBuilder<T extends AbstractBaseRecipeBuil
|
|||
|
||||
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) {
|
||||
this.id = id;
|
||||
this.output = new ItemStack(output, 1);
|
||||
this.category = RecipeCategory.MISC;
|
||||
this.alright = RecipeHelper.exists(output);
|
||||
this(id, new ItemStack(output, 1));
|
||||
}
|
||||
|
||||
public T setCategory(RecipeCategory category) {
|
||||
|
|
|
@ -6,6 +6,7 @@ import org.betterx.bclib.util.RecipeHelper;
|
|||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
|
||||
|
@ -13,9 +14,12 @@ public abstract class AbstractSimpleRecipeBuilder<T extends AbstractSimpleRecipe
|
|||
protected Ingredient primaryInput;
|
||||
|
||||
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) {
|
||||
for (ItemLike item : inputs) {
|
||||
|
|
|
@ -12,6 +12,7 @@ import net.minecraft.resources.ResourceLocation;
|
|||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.world.Container;
|
||||
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.RecipeSerializer;
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
|
@ -26,6 +27,10 @@ public abstract class AbstractSingleInputRecipeBuilder<T extends AbstractSingleI
|
|||
|
||||
|
||||
protected AbstractSingleInputRecipeBuilder(ResourceLocation id, ItemLike output) {
|
||||
this(id, new ItemStack(output, 1));
|
||||
}
|
||||
|
||||
protected AbstractSingleInputRecipeBuilder(ResourceLocation id, ItemStack output) {
|
||||
super(id, output);
|
||||
this.advancement = Advancement.Builder.advancement();
|
||||
}
|
||||
|
@ -56,7 +61,7 @@ public abstract class AbstractSingleInputRecipeBuilder<T extends AbstractSingleI
|
|||
protected abstract RecipeSerializer<R> getSerializer();
|
||||
|
||||
protected void serializeRecipeData(JsonObject root) {
|
||||
root.add("input", primaryInput.toJson());
|
||||
root.add("input", ItemUtil.toJsonIngredientWithNBT(primaryInput));
|
||||
|
||||
if (group != null && !group.isEmpty()) {
|
||||
root.addProperty("group", group);
|
||||
|
|
|
@ -11,8 +11,10 @@ import net.minecraft.resources.ResourceLocation;
|
|||
import net.minecraft.util.GsonHelper;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -59,17 +61,41 @@ public class ItemUtil {
|
|||
return null;
|
||||
}
|
||||
|
||||
public static ItemStack fromJsonRecipeWithNBT(JsonObject recipe) {
|
||||
ItemStack output = ItemUtil.fromJsonRecipe(recipe);
|
||||
if (output != null && recipe.has("nbt")) {
|
||||
public static CompoundTag readNBT(JsonObject recipe) {
|
||||
if (recipe.has("nbt")) {
|
||||
try {
|
||||
String nbtData = GsonHelper.getAsString(recipe, "nbt");
|
||||
CompoundTag nbt = TagParser.parseTag(nbtData);
|
||||
output.setTag(nbt);
|
||||
return nbt;
|
||||
} catch (CommandSyntaxException 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;
|
||||
}
|
||||
|
||||
|
@ -91,6 +117,15 @@ public class ItemUtil {
|
|||
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) {
|
||||
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) {
|
||||
JsonObject root = toJsonRecipe(item, count);
|
||||
if (nbt != null) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
writeNBT(root, nbt);
|
||||
return root;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue