Skip to main content

Item and Ingredient

When making recipes you can specify items in many ways, the most common is just to use 'namspace:id', like 'minecraft:diamond', however you can also use Item#of and Ingredient#of for advanced additions, such as NBT or count.

Note that Item and Ingredient are are not the same! They may work similarly but there are differences. Item can only ever represent a single item type whereas Ingredient can represent multiple item types (and multiple instances of the same item type with different properties such as NBT data). For most cases Ingredient should be preferred over Item.

Item/ItemWrapper

Its Java class name is ItemWrapper but it is bound to Item in JS.

Name

Return Type Info
of(ItemStackJS in) ItemStackJS

Returns an ItemStackJS based on what was input.  

Note that this relies mostly on Rhinos type wrapping to function, see paragraph below about ItemStackJS#of for more info

of(ItemStackJS in, int count) ItemStackJS

See above. count will override any other count set from the first parameter.

of(ItemStackJS in, CompoundTag tag) ItemStackJS

See above. NBT is merged, with the input NBT taking priority over existing NBT.

of(ItemStackJS in, int count, CompoundTag nbt) ItemStackJS

Combines the functionality of the above two.

withNBT(ItemStackJS in, CompoundTag nbt) ItemStackJS

Same as the corresponding #of.

withChance(ItemStackJS in, double chance) ItemStackJS

Same as #of, chance will override currently set chance.

getList() ListJS

Returns a list of ItemStackJS, one per registered item.

getTypeList() ListJS

Returns a list of String, one per registered item.

getEmpty() ItemStackJS

Returns ItemSTackJS.EMPTY

clearListCache() void

Clears the caches used for #getList and #getTypeList #getTypeList 

fireworks(Map<String, Object> properties) FireworkJS

Returns a FireworkJS based on the input map of propeties. See FireworkJS#of on the FireworkJS page for more information <TODO: Make and link FireworkJS page>

getItem(ResourceLocation id) Item

Returns the instance of the Item class associated with the item id passed in.

@Nullable findGroup(String id) CreativeModTab

Returns the Creative tab associated with the id passed in, returns null if none found.

exists(ResourceLocation id) boolean

Returns if the item id passed in exists or not.

isItem(@Nullable Object o) boolean

Just does an instanceof ItemStackJS check on the object passed in.

Item#of relies on Rhinos type wrapping to function, which calls ItemStackJS#of. This tries its best to turn the input into an ItemStackJS. If no match is found ItemStackJS.EMPTY is returned. Valid inputs:

  • null/ItemStack.EMPTY/Items.EMPTY/ItemStackJS.EMPTY - will return ItemStackJS.EMPTY
  • ItemStackJS ItemStackJS  - will return the same object passed in.
  • FluidStackJS - will return a new DummyFluidItemStackJS DummyFluidItemStackJS 
  • IngredientJS - will return the first item in the Ingredient
  • ItemStack - will return a new ItemStackJS wrapping the ItemStack passed in
  • ResourceLocation ResourceLocation  - will lookup this ResourceLocation in the item registry and return it if found. If not found will return ItemStackJS.EMPTY, and throw an error if RecipeJS.itemErrors is true
  • ItemLike - will return a new ItemStackJS of the input
  • JsonObject - will return an item based on properties in the Json. item will be used as the item id, or tag if item does not exist. count, chance and nbt all set their respective properties
  • RegEx - will return a new ItemStackJS of the first item id that matches this regex.
  • String (CharSequence) - will parse it and return a new ItemStackJS based on the input item id. Prefix with nx nx  to change the count (where n is any number between 1 and 64). Put # before the item id to parse it as a tag instead. Put @ before the item id to parse it as a modid instead. Prefix with % to parse it as a creative menu tab group. Surround in / to parse as a RegEx. NOTE: will only be the first item in any of the groups mentioned above!
  • Map/JS Object - uses the same rules as a JsonObject.

Ingredient/IngredientWrapper

Its Java class name is IngredientWrapper but it is bound to Ingredient in JS. All static methods.

Name

Return Type Info
getNone() IngredientJS

Returns ItemStack.EMPTY

getAll() IngredientJS

Returns an IngredientJS of every single item in game. All of them.

of(Object object) IngredientJS

Works exactly the same as Item#of except it recognises Ingredient and forge json ingredient syntax.

of(Object object, int count) IngredientJS

Same as above. The count passed in will override any from the first parameter.

custom(Predicate<ItemStackJS> predicate) IngredientJS

Takes the arrow function or anonymous function passed in and makes an IngredientJS with that as IngredientJS#test.

Return true from the function if the ItemStackJS passed should match as an ingredient.  

custom(IngredientJS in, Predicate<ItemStackJS> predicate) IngredientJS

Same as above except it must match the IngredientJS passed in as the first parameter before the custom function is called.

customNBT(IngredientJS in, Predicate<CompoundTag> predicate) IngredientJS

Same as above except the Predicate is passed the items NBT instead of the full ItemStackJS. Useful for advanced NBT matching.  

matchAny(Object objects) IngredientJS

Adds the passed in object to an ingredient. If it is a list then it adds all items in the list. All objects are passed through #of before adding.

registerCustomIngredientAction(String id, CustomIngredientActionCallback callback) void

Registers a custom ingredient action. See the recipe page for more information.

isIngredient(@Nullable Object o) boolean

Just does an instanceof IngredientJS check on the object passed in.

Remember that Item and Ingredient are not equivalent!

Examples

<TODO: examples>

ItemStackJS

A wrapper class for vanilla's ItemStack. All methods listed here are instance methods, all useful static methods are wrapped in ItemWrapper. Implements IngredientJS and overrides most of its default methods.

Name

Return Type Info
getItem() Item Returns the instance of the Item class associated with this ItemStackJS.
getItemStack() ItemStack

Returns the vanilla ItemStack that this wraps.

getId() String

Returns the item id associated with this ItemStackJS in the form mod_name:item_name

getTags() Colletion<ResourceLocation>

Returns all item tags the item has. (NOT NBT tags).

hasTag(ResourceLocation tag) boolean

Returns if the item has the input tag or not.

copy() ItemStackJS

Returns a copy of this ItemStackJS.

setCount(int count) void

Sets the count on this ItemStackJS.

getCount() int

Gets the count.

withCount() ItemStackJS

Returns a copy of this ItemStackJS with a different count.

isEmpty() boolean

Returns if this is an empty item or not.

isInvalidRecipeIngredient() boolean

Returns if this is a valid recipe ingredient.

isBlock() boolean

Returns if this item is a BlockItem, that is it can be placed and form a block.

@Nullable getNbt() CompoundTag

Gets this items NBT data.

setNbt(@Nullable CompoundTag tag) void

Sets this items NBT data

hasNBT() boolean

Returns if this item has NBT data.

getNbtString() String

Returns this items NBT data as a string. If you want to display it to the player see Text#prettyPrintNbt.

removeNBT() ItemStackJS

Returns a copy with no NBT data.

withNBT(CompoundTag nbt) ItemStackJS

Returns a copy with the specified NBT data. Any tags from the original NBT are kept if not overwritten by the NBT passed in.

hasChance() boolean

Returns if the ItemStackJS has a chance.

removeChance() void

Removes the chance from this ItemStackJS.

setChance(double c) void

Sets the chance for this ItemStackJS.

getChance() double

Returns the chance.

withChance(double c) ItemStackJS

Returns a copy with the chance passed in, unless the chance passed in is the same as the current chance, in which case it returns this.

getName() Components

Returns this items name. Probably a Translateable Component unless its been overridden by something else (ie method below).

withName(@Nullable Component displayName) ItemStackJS

Returns a copy with a different display name set.  

toString() String

Returns a string representing this ItemStackJS. The same method used for the /kubejs hand command.

test(ItemStackJS other) boolean

Returns if this ItemStackJS equals another one. Tests for item type and NBT data.

testVanilla(ItemStack other) boolean

Returns if this ItemStackJS equals the passed in ItemStack. Tests for item type and NBT data.

testVanillaItem(Item item)

 

boolean

Returns if the Item passed in is the same as this ItemStackJS's Item. Basically checks they are the same item type.

getStacks() Set<ItemStackJS>

Returns this ItemStackJS as the only entry in a Set.

getVanillaItems() Set<Item>

Returns this ItemStackJS associated Item as the only entry in a Set.

getFirst() ItemStackJS

Retuns a copy of this ItemStackJS

hashCode() int

Returns a hash code of the Item and NBT data.

equals(Object o) boolean

Returns if this is equal to the input object.  

strongEquals(Object o) boolean

Returns if this is equal to the input object. Checks count as well.

getEnchantments() MapJS

Returns a MapJS of this itemStackJS enchament id's to their level.

hasEnchantment(Enchantment enchantment, int level) boolean

Returns if this ItemStackJS is enchanted with a minimum of the passed in enchantment level.

enchant(MapJS enchantments) ItemStackJS

Enchants a copy of this ItemStackJS with the MapJS passed in (it should be a map of enchantment ids to levels), then returns the copy.

enchant(Enchantment enchantment, int level) ItemStackJS

Enchants a copy of this item with the passed in Enchantment at the specified level, then returns the copy.

getMod() String

Returns the mod id of the mod this item is from.

ignoreNBT() IngredientJS

Returns a new IgnoreNBTIngredientJS of this item.

weakNBT() IngredientJS

Returns a new WeakNBTIngredientJS of this item.

areItemsEqual(ItemStackJS other) boolean

Returns if this item type is equal to the item type of the passed in ItemStackJS

areItemsEqual(ItemStack other) boolean

Returns if this item type is equal to the item type of the passed in ItemStack

isNBTEqual(ItemStackJS other) boolean

Returns if the NBT of this ItemStackJS is equal to the NBT of the ItemStackJS passed in.

isNBTEqual(ItemStack other) boolean

Returns if the NBT of this ItemStackJS is equal to the NBT of the ItemStack passed in.

getHarvestSpeed(@Nullable BlockContainerJS block) float

Returns the mining speed of this ItemStackJS if used to mine the passed in BlockContainerJS

getHarvestSpeed() float

Returns this items default mining speed

toJson()

toResultJson()

toRawResultJson()

JsonElement

Returns a Json representation of this ItemStackJS. They all appear to work almost identically.

toNBT() CompoundTag

Returns an NBT representation of this ItemStackJS, the same sort that vanilla uses to store items in blocks.

onChanged(@Nullable Tag o) void

Sets the items NBT data to the tag passed in, only if it is a CompoundTag or null.

getItemGroup() String

Returns the name of the creative tab this item belongs in. An empty string if it does not exist in the creative tabs (like a jigsaw block).

getItemIds() Set<String> Returns a set with this items id as the only entry.
getFluidStack() FluidStackJS Returns null, by default. Overriden by some superclasses to return the FluidStackJS that this item represents.
getTypeData() CompoundTag Unknown purpose.
<TODO: Examples>