Home Commands Examples Getting Started With Scripts Global Keywords 1.21 Migration Guide
Equipable IItemStack ItemCooldowns ItemDefinition ItemStack UseOnContext
BracketDumpers BracketHandlers BracketValidators ResourceLocationBracketHandler

ItemStack

Importing the class

If you need to reference this type directly, like when casting an Array, or as a parameter, you will need to import it. Simply add the import at the top of the file.

script.zs
import crafttweaker.api.item.ItemStack;

Description

This is the vanilla ItemStack. It is recommended that you use IItemStack whenever possible


They can be cast from each other, though.

Implements

ItemStack implements the following interfaces:

DataComponentHolder,MutableDataComponentHolder

Undocumented Interfaces

IItemStackExtension,IDataComponentHolderExtension

Operators

in(type as DataComponentType<?>) as bool
Checks whether the holder contains the given DataComponent, which is indentified by the type.
script.zs
// ((type as DataComponentType<?>) in ItemStack) as bool
myDataComponentType<?> in myItemStack

Parameters:

type Type: DataComponentType<?> - The componenttype to check for.

Return Type: bool

Members

applyComponents(components as DataComponentMap)
script.zs
// ItemStack.applyComponents(components as DataComponentMap);
myItemStack.applyComponents(myDataComponentMap);

Parameters:

applyComponents(patch as DataComponentPatch)
script.zs
// ItemStack.applyComponents(patch as DataComponentPatch);
myItemStack.applyComponents(myDataComponentPatch);

Parameters:

asIIngredient() as IIngredient
script.zs
// ItemStack.asIIngredient() as IIngredient;
myItemStack.asIIngredient();

Return Type: IIngredient

implicit as IIngredient
script.zs
// ItemStack as IIngredient
myItemStack as IIngredient

Return Type: IIngredient

asIItemStack() as IItemStack
script.zs
// ItemStack.asIItemStack() as IItemStack;
myItemStack.asIItemStack();

Return Type: IItemStack

implicit as IItemStack
script.zs
// ItemStack as IItemStack
myItemStack as IItemStack

Return Type: IItemStack

Getter
Gets the internal map of ComponentType -> Data
script.zs
// ItemStack.components as DataComponentMap
myItemStack.components

Return Type: DataComponentMap

copyFrom(src as DataComponentHolder, componentTypes as DataComponentType<?>[])
script.zs
// ItemStack.copyFrom(src as DataComponentHolder, componentTypes as DataComponentType<?>[]);
myItemStack.copyFrom(myDataComponentHolder, myDataComponentType<?>[]);

Parameters:

componentTypes Type: DataComponentType<?>[]
getComponent(type as DataComponentType<T>) as T
Gets the data identified by the given DataComponentType. If the ComponentHolder does not have the Component, then null is returned.

Returns: The value if it exists or null.

script.zs
// ItemStack.getComponent<T>(type as DataComponentType<T>) as T;
myItemStack.getComponent<T>(<componenttype:minecraft:stack_size>);

Parameters:

type Type: DataComponentType<T> - The componenttype to get

Return Type: T

getJsonComponent(type as DataComponentType<T>) as IData
Get an arbitrary component by type. This is useful to interact with components added by mods that do not have a CraftTweaker method.
If the ComponentAccess does not have the type, an exception is thrown. If the ComponentAccess is not serializable, an exception is thrown.

Returns: A IData representation of the Serialized DataComponent

script.zs
// ItemStack.getJsonComponent(type as DataComponentType<T>) as IData;
myItemStack.getJsonComponent(<componenttype:minecraft:stack_size>);

Parameters:

type Type: DataComponentType<T> - The componenttype to target.

Return Type: IData

getOrDefault(type as DataComponentType<T>, defaultValue as T) as T
Gets the data identified by the given DataComponentType. If the ComponentHolder does not have the Component, then the default value is returned.

Returns: The value if it exists or the default value.

script.zs
// ItemStack.getOrDefault<T>(type as DataComponentType<T>, defaultValue as T) as T;
myItemStack.getOrDefault<T>(<componenttype:minecraft:stack_size>, 64);

Parameters:

type Type: DataComponentType<T> - The componenttype to get
defaultValue Type: T - The default value to return in the event that the holder does not have the component.

Return Type: T

has(type as DataComponentType<?>) as bool
Checks whether the holder contains the given DataComponent, which is indentified by the type.

Returns: Whether the holder contains the DataComponent.

script.zs
// ItemStack.has(type as DataComponentType<?>) as bool;
myItemStack.has(<componenttype:minecraft:stack_size>);

Parameters:

type Type: DataComponentType<?> - The componenttype to check for.

Return Type: bool

remove(componentType as DataComponentType<T>) as T?
script.zs
// ItemStack.remove<T>(componentType as DataComponentType<T>) as T?;
myItemStack.remove<T>(myDataComponentType);

Parameters:

componentType Type: DataComponentType<T>

Return Type: T?

setComponent(componentType as DataComponentType<T>, value as T?) as T?
script.zs
// ItemStack.setComponent<T>(componentType as DataComponentType<T>, value as T?) as T?;
myItemStack.setComponent<T>(myDataComponentType, myT);

Parameters:

componentType Type: DataComponentType<T>
value Type: T?

Return Type: T?

update(componentType as DataComponentType<T>, value as T, updater as UnaryOperator<T>) as T?
script.zs
// ItemStack.update<T>(componentType as DataComponentType<T>, value as T, updater as UnaryOperator<T>) as T?;
myItemStack.update<T>(myDataComponentType, myT, myUnaryOperator);

Parameters:

componentType Type: DataComponentType<T>
value Type: T
updater Type: UnaryOperator<T>

Return Type: T?

update(componentType as DataComponentType<T>, value as T, updateContext as U, updater as BiFunction<T, T, U>) as T?
script.zs
// ItemStack.update<T, U>(componentType as DataComponentType<T>, value as T, updateContext as U, updater as BiFunction<T, T, U>) as T?;
myItemStack.update<T, U>(myDataComponentType, myT, myU, myBiFunction);

Parameters:

componentType Type: DataComponentType<T>
value Type: T
updateContext Type: U
updater Type: BiFunction<T, T, U>

Return Type: T?