DataComponentHolder

Link to datacomponentholder

An interface that stores DataComponents

Importing the class

Link to importing-the-class

It might be required for you to import the package if you encounter any issues (like casting an Array), so better be safe than sorry and add the import at the very top of the file.

ZenScript
Copy
import crafttweaker.api.component.DataComponentHolder;

Name: getComponent

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.
Return Type: T

ZenScript
Copy
// DataComponentHolder.getComponent<T : Object>(type as DataComponentType<T>) as T

myDataComponentHolder.getComponent<int?>(<componenttype:minecraft:stack_size>);
ParameterTypeDescription
Parameter
type
Type
DataComponentType<T>
Description
The componenttype to get
Parameter
T
Type
Object
Description
The type of the class stored within the component.

Link to getJsonComponent

Name: getJsonComponent

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
Return Type: IData?

ZenScript
Copy
// DataComponentHolder.getJsonComponent(type as DataComponentType) as IData?

myDataComponentHolder.getJsonComponent(<componenttype:minecraft:stack_size>);
ParameterTypeDescription
Parameter
type
Type
DataComponentType
Description
The componenttype to target.

Name: getOrDefault

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.
Return Type: T

ZenScript
Copy
// DataComponentHolder.getOrDefault<T : Object>(type as DataComponentType<T>, defaultValue as T) as T

myDataComponentHolder.getOrDefault<int?>(<componenttype:minecraft:stack_size>, 64);
ParameterTypeDescription
Parameter
type
Type
DataComponentType<T>
Description
The componenttype to get
Parameter
defaultValue
Type
T
Description
The default value to return in the event that the holder does not have the component.
Parameter
T
Type
Object
Description
The type of the ComponentType

Name: has

Checks whether the holder contains the given DataComponent, which is indentified by the type.

Returns: Whether the holder contains the DataComponent.
Return Type: boolean

ZenScript
Copy
// DataComponentHolder.has(type as DataComponentType) as boolean

myDataComponentHolder.has(<componenttype:minecraft:stack_size>);
ParameterTypeDescription
Parameter
type
Type
DataComponentType
Description
The componenttype to check for.
NameTypeHas GetterHas SetterDescription
Name
components
Type
DataComponentMap
Has Getter
true
Has Setter
false
Description
Gets the internal map of ComponentType -> Data