Search in sources :

Example 1 with NBTException

use of net.minecraft.nbt.NBTException in project MinecraftForge by MinecraftForge.

the class GameRegistry method makeItemStack.

/**
     * Makes an {@link ItemStack} based on the itemName reference, with supplied meta, stackSize and nbt, if possible
     * <p/>
     * Will return null if the item doesn't exist (because it's not from a loaded mod for example)
     * Will throw a {@link RuntimeException} if the nbtString is invalid for use in an {@link ItemStack}
     *
     * @param itemName  a registry name reference
     * @param meta      the meta
     * @param stackSize the stack size
     * @param nbtString an nbt stack as a string, will be processed by {@link JsonToNBT}
     * @return a new itemstack
     */
@Nonnull
public static ItemStack makeItemStack(String itemName, int meta, int stackSize, String nbtString) {
    if (itemName == null) {
        throw new IllegalArgumentException("The itemName cannot be null");
    }
    Item item = GameData.getItemRegistry().getObject(new ResourceLocation(itemName));
    if (item == null) {
        FMLLog.getLogger().log(Level.TRACE, "Unable to find item with name {}", itemName);
        return ItemStack.EMPTY;
    }
    ItemStack is = new ItemStack(item, stackSize, meta);
    if (!Strings.isNullOrEmpty(nbtString)) {
        NBTBase nbttag = null;
        try {
            nbttag = JsonToNBT.getTagFromJson(nbtString);
        } catch (NBTException e) {
            FMLLog.getLogger().log(Level.WARN, "Encountered an exception parsing ItemStack NBT string {}", nbtString, e);
            throw Throwables.propagate(e);
        }
        if (!(nbttag instanceof NBTTagCompound)) {
            FMLLog.getLogger().log(Level.WARN, "Unexpected NBT string - multiple values {}", nbtString);
            throw new RuntimeException("Invalid NBT JSON");
        } else {
            is.setTagCompound((NBTTagCompound) nbttag);
        }
    }
    return is;
}
Also used : Item(net.minecraft.item.Item) NBTBase(net.minecraft.nbt.NBTBase) ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ItemStack(net.minecraft.item.ItemStack) NBTException(net.minecraft.nbt.NBTException) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 NBTBase (net.minecraft.nbt.NBTBase)1 NBTException (net.minecraft.nbt.NBTException)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ResourceLocation (net.minecraft.util.ResourceLocation)1