|
| 1 | +package commoble.jumbofurnace.recipes; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.stream.Collectors; |
| 5 | +import java.util.stream.Stream; |
| 6 | + |
| 7 | +import javax.annotation.Nonnull; |
| 8 | + |
| 9 | +import com.google.gson.JsonObject; |
| 10 | + |
| 11 | +import net.minecraft.item.Item; |
| 12 | +import net.minecraft.item.ItemStack; |
| 13 | +import net.minecraft.item.crafting.Ingredient; |
| 14 | +import net.minecraft.network.PacketBuffer; |
| 15 | +import net.minecraft.tags.ITag; |
| 16 | +import net.minecraft.tags.TagCollectionManager; |
| 17 | +import net.minecraft.util.JSONUtils; |
| 18 | +import net.minecraft.util.ResourceLocation; |
| 19 | +import net.minecraftforge.common.crafting.IIngredientSerializer; |
| 20 | + |
| 21 | +public class TagStackIngredient extends Ingredient |
| 22 | +{ |
| 23 | + private final ITag<Item> tag; public ITag<Item> getTag() { return this.tag;} |
| 24 | + private final int count; public final int getCount() { return this.count; } |
| 25 | + |
| 26 | + public TagStackIngredient(ITag<Item> tag, int count) |
| 27 | + { |
| 28 | + super(makeItemStacks(tag, count)); |
| 29 | + this.tag = tag; |
| 30 | + this.count = count; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public boolean isSimple() |
| 35 | + { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public IIngredientSerializer<? extends Ingredient> getSerializer() |
| 41 | + { |
| 42 | + return SERIALIZER; |
| 43 | + } |
| 44 | + |
| 45 | + public static Stream<TagStackList> makeItemStacks(ITag<Item> tag, int count) |
| 46 | + { |
| 47 | + return tag == null |
| 48 | + ? Stream.of() // invalid tag, no stacks to match to |
| 49 | + : Stream.of(new TagStackList(tag, count)); |
| 50 | + } |
| 51 | + |
| 52 | + public static final IIngredientSerializer<TagStackIngredient> SERIALIZER = new IIngredientSerializer<TagStackIngredient>() |
| 53 | + { |
| 54 | + |
| 55 | + @Override |
| 56 | + public void write(PacketBuffer buffer, TagStackIngredient ingredient) |
| 57 | + { |
| 58 | + buffer.writeString(TagCollectionManager.getManager().getItemTags().getValidatedIdFromTag(ingredient.tag).toString()); |
| 59 | + buffer.writeInt(ingredient.count); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public TagStackIngredient parse(PacketBuffer buffer) |
| 64 | + { |
| 65 | + String tagName = buffer.readString(); |
| 66 | + int count = buffer.readInt(); |
| 67 | + ITag<Item> tag = TagCollectionManager.getManager().getItemTags().get(new ResourceLocation(tagName)); // can return null if tag is invalid |
| 68 | + return new TagStackIngredient(tag, count); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public TagStackIngredient parse(JsonObject json) |
| 73 | + { |
| 74 | + ResourceLocation tagID = new ResourceLocation(JSONUtils.getString(json, "tag")); // throws JsonSyntaxException if no tag field |
| 75 | + int count = JSONUtils.getInt(json, "count", 1); |
| 76 | + ITag<Item> tag = TagCollectionManager.getManager().getItemTags().get(tagID); // can return null if tag is invalid |
| 77 | + return new TagStackIngredient(tag, count); |
| 78 | + } |
| 79 | + |
| 80 | + }; |
| 81 | + |
| 82 | + public static class TagStackList implements Ingredient.IItemList |
| 83 | + { |
| 84 | + private final @Nonnull ITag<Item> tag; |
| 85 | + private final int count; |
| 86 | + private final Collection<ItemStack> stacks; |
| 87 | + |
| 88 | + public TagStackList(@Nonnull ITag<Item> tag, int count) |
| 89 | + { |
| 90 | + this.tag = tag; |
| 91 | + this.count = count; |
| 92 | + |
| 93 | + this.stacks = tag.getAllElements() |
| 94 | + .stream() |
| 95 | + .map(item -> new ItemStack(item, count)) |
| 96 | + .collect(Collectors.toList()); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Collection<ItemStack> getStacks() |
| 101 | + { |
| 102 | + return this.stacks; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public JsonObject serialize() |
| 107 | + { |
| 108 | + JsonObject json = new JsonObject(); |
| 109 | + |
| 110 | + json.addProperty("tag", TagCollectionManager.getManager().getItemTags().getValidatedIdFromTag(this.tag).toString()); |
| 111 | + |
| 112 | + if (this.count > 1) |
| 113 | + { |
| 114 | + json.addProperty("count", this.count); |
| 115 | + } |
| 116 | + |
| 117 | + return json; |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments