Skip to content

Commit e61f1bc

Browse files
committed
add recipe parser support for default items
1 parent 645ed2b commit e61f1bc

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/main/java/io/github/cottonmc/libcd/api/tweaker/recipe/RecipeParser.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.JsonObject;
66
import com.mojang.datafixers.Dynamic;
77
import io.github.cottonmc.libcd.api.CDSyntaxError;
8+
import io.github.cottonmc.libcd.api.tag.TagHelper;
89
import io.github.cottonmc.libcd.api.tweaker.util.TweakerUtils;
910
import io.github.cottonmc.libcd.api.util.GsonOps;
1011
import io.github.cottonmc.libcd.api.util.NbtMatchType;
@@ -110,31 +111,45 @@ public static ItemStack processItemStack(Object input) throws CDSyntaxError {
110111
else if (input instanceof String) {
111112
String in = (String)input;
112113
int atIndex = in.lastIndexOf('@');
114+
int nbtIndex = in.indexOf('{');
113115
int count = 1;
114116
if (atIndex != -1 && atIndex > in.lastIndexOf('}')) {
115117
count = Integer.parseInt(in.substring(atIndex + 1));
116118
in = in.substring(0, atIndex);
117119
}
118-
if (in.contains("->") && in.indexOf("->") < in.indexOf('{')) {
120+
Item item;
121+
String nbt = "";
122+
if (in.indexOf('#') == 0) {
123+
if (nbtIndex != -1) {
124+
nbt = in.substring(nbtIndex);
125+
in = in.substring(0, nbtIndex);
126+
}
127+
String tag = in.substring(1);
128+
Tag<Item> itemTag = ItemTags.getContainer().get(new Identifier(tag));
129+
if (itemTag == null) throw new CDSyntaxError("Failed to get item tag for output: " + in);
130+
item = TagHelper.ITEM.getDefaultEntry(itemTag);
131+
} else if (in.contains("->") && in.indexOf("->") < in.indexOf('{')) {
119132
ItemStack stack = TweakerUtils.INSTANCE.getSpecialStack(in);
120133
if (stack.isEmpty())
121-
throw new CDSyntaxError("Failed to get special stack for input: " + in);
134+
throw new CDSyntaxError("Failed to get special stack for output: " + in);
122135
if (stack.isStackable()) {
123136
stack.setCount(count);
124137
}
125138
return stack;
126139
} else {
127-
int nbtIndex = in.indexOf('{');
128140
if (nbtIndex != -1) {
129-
String nbt = in.substring(nbtIndex);
141+
nbt = in.substring(nbtIndex);
130142
in = in.substring(0, nbtIndex);
131-
ItemStack stack = new ItemStack(TweakerUtils.INSTANCE.getItem(in), count);
132-
TweakerUtils.INSTANCE.addNbtToStack(stack, nbt);
133-
return stack;
134-
} else {
135-
return new ItemStack(TweakerUtils.INSTANCE.getItem(in), count);
136143
}
144+
item = TweakerUtils.INSTANCE.getItem(in);
145+
}
146+
147+
ItemStack stack = new ItemStack(item, count);
148+
if (!nbt.equals("")) {
149+
TweakerUtils.INSTANCE.addNbtToStack(stack, nbt);
137150
}
151+
152+
return stack;
138153
}
139154
else throw new CDSyntaxError("Illegal object passed to recipe parser of type " + input.getClass().getName());
140155
}

src/main/java/io/github/cottonmc/libcd/api/tweaker/util/TweakerUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mojang.brigadier.StringReader;
44
import com.mojang.brigadier.exceptions.CommandSyntaxException;
55
import io.github.cottonmc.libcd.api.CDCommons;
6+
import io.github.cottonmc.libcd.api.tag.TagHelper;
67
import io.github.cottonmc.libcd.api.tweaker.TweakerManager;
78
import io.github.cottonmc.libcd.api.tweaker.TweakerStackFactory;
89
import io.github.cottonmc.libcd.api.tweaker.recipe.RecipeParser;
@@ -14,6 +15,7 @@
1415
import net.minecraft.fluid.Fluid;
1516
import net.minecraft.item.Item;
1617
import net.minecraft.item.ItemStack;
18+
import net.minecraft.item.Items;
1719
import net.minecraft.nbt.CompoundTag;
1820
import net.minecraft.nbt.ListTag;
1921
import net.minecraft.nbt.StringNbtReader;
@@ -43,6 +45,18 @@ public Item getItem(String id) {
4345
return Registry.ITEM.get(new Identifier(id));
4446
}
4547

48+
/**
49+
* Get the default item from an item tag.
50+
* @param id The ID of the tag to get from.
51+
* @return The default item for that tag.
52+
*/
53+
public Item getDefaultItem(String id) {
54+
Identifier tagId = new Identifier(id);
55+
Tag<Item> tag = ItemTags.getContainer().get(tagId);
56+
if (tag == null) return Items.AIR;
57+
return TagHelper.ITEM.getDefaultEntry(tag);
58+
}
59+
4660
/**
4761
* Get the item from an item stack.
4862
* @param stack The stack to check.

0 commit comments

Comments
 (0)