Skip to content

Commit

Permalink
Add NBT parsing to itemstack parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tterrag1098 authored and HenryLoenwind committed Mar 30, 2016
1 parent 565dcb1 commit 91a4936
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/main/java/com/enderio/core/common/util/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import net.minecraft.inventory.InventoryLargeChest;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.BlockPos;
Expand Down Expand Up @@ -132,22 +134,38 @@ public static Object parseStringIntoRecipeItem(String string, boolean forceItemS
*/
public static ItemStack parseStringIntoItemStack(String string) {
int size = 1;
int idx = string.indexOf('#');
int numIdx = string.indexOf('#');
int nbtIdx = string.indexOf('$');

if (idx != -1) {
String num = string.substring(idx + 1);
NBTTagCompound tag = null;

if (numIdx != -1) {
String num = string.substring(numIdx + 1, nbtIdx == -1 ? string.length() : nbtIdx);

try {
size = Integer.parseInt(num);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(num + " is not a valid stack size");
throw new IllegalArgumentException(num + " is not a valid stack size", e);
}

string = string.substring(0, idx);
string = string.replace('#' + num, "");
nbtIdx -= num.length() + 1;
}

if (nbtIdx != -1) {
String nbt = string.substring(nbtIdx + 1);
try {
tag = (NBTTagCompound) JsonToNBT.getTagFromJson(nbt);
} catch (NBTException e) {
throw new IllegalArgumentException(nbt + " is not valid NBT json.", e);
}

string = string.replace('$' + nbt, "");
}

ItemStack stack = (ItemStack) parseStringIntoRecipeItem(string, true);
stack.stackSize = MathHelper.clamp_int(size, 1, stack.getMaxStackSize());
stack.setTagCompound(tag);
return stack;
}

Expand Down

0 comments on commit 91a4936

Please sign in to comment.