Skip to content

Commit 971414b

Browse files
committed
Added tag_stack ingredient type
1 parent ba782ed commit 971414b

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
modid = jumbofurnace
3-
mod_version = 2.0.0.1
3+
mod_version = 2.0.1.0
44
mc_version = 1.16.3
55
forge_version = 34.1.0
66
mappings_version = 20200916-1.16.2

src/main/java/commoble/jumbofurnace/JumboFurnace.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import commoble.jumbofurnace.recipes.JumboFurnaceRecipe;
2121
import commoble.jumbofurnace.recipes.JumboFurnaceRecipeSerializer;
2222
import commoble.jumbofurnace.recipes.RecipeSorter;
23+
import commoble.jumbofurnace.recipes.TagStackIngredient;
2324
import net.minecraft.block.Block;
2425
import net.minecraft.block.BlockState;
2526
import net.minecraft.block.Blocks;
@@ -40,6 +41,7 @@
4041
import net.minecraft.util.ActionResultType;
4142
import net.minecraft.util.Direction;
4243
import net.minecraft.util.Hand;
44+
import net.minecraft.util.ResourceLocation;
4345
import net.minecraft.util.SoundCategory;
4446
import net.minecraft.util.SoundEvents;
4547
import net.minecraft.util.math.BlockPos;
@@ -51,7 +53,9 @@
5153
import net.minecraftforge.api.distmarker.OnlyIn;
5254
import net.minecraftforge.common.MinecraftForge;
5355
import net.minecraftforge.common.Tags;
56+
import net.minecraftforge.common.crafting.CraftingHelper;
5457
import net.minecraftforge.event.AddReloadListenerEvent;
58+
import net.minecraftforge.event.RegistryEvent;
5559
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock;
5660
import net.minecraftforge.event.world.BlockEvent.EntityMultiPlaceEvent;
5761
import net.minecraftforge.event.world.BlockEvent.EntityPlaceEvent;
@@ -130,6 +134,8 @@ public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextC
130134
containers.register(Names.JUMBO_FURNACE, () -> new ContainerType<>(JumboFurnaceContainer::getClientContainer));
131135

132136
recipeSerializers.register(Names.JUMBO_SMELTING, () -> new JumboFurnaceRecipeSerializer(JUMBO_SMELTING_RECIPE_TYPE));
137+
138+
modBus.addGenericListener(IRecipeSerializer.class, this::onRegisterRecipeStuff);
133139
}
134140

135141
private <T extends IForgeRegistryEntry<T>> DeferredRegister<T> makeDeferredRegister(IEventBus modBus, IForgeRegistry<T> registry)
@@ -248,6 +254,12 @@ private void onRightClickBlockLow(RightClickBlock event)
248254
}
249255
}
250256

257+
private void onRegisterRecipeStuff(RegistryEvent.Register<IRecipeSerializer<?>> event)
258+
{
259+
// forge registers ingredient serializers here for some reason, might as well do it here too
260+
CraftingHelper.register(new ResourceLocation("jumbofurnace:tag_stack"), TagStackIngredient.SERIALIZER);
261+
}
262+
251263
private static void addItemsToList(List<ItemStack> stacks, IItemHandler handler)
252264
{
253265
int slots = handler.getSlots();
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)