Skip to content

Cardinal Components Item

Pyrofab edited this page Sep 8, 2020 · 14 revisions

This module allows mods to attach components to ItemStack instances. Item components are automatically saved and synchronized with the stack they are attached to, and are copied whenever the stack's NBT is copied (notably in some recipes). They are also factored in the results of all ItemStack equality methods that consider NBT - for this reason, component implementations that get attached to item stacks must define a meaningful equals method!

Features

Registration

Item components are registered by an ItemComponentInitializer, exposed as cardinal-components-item in the mod json (more information on the component registration page).

Component factories can be registered either to all stacks, or to all stacks of a given Item. Registering a factory to both all stacks and to specific items will cause the latter factory to override the former, letting you eg. use a different implementation for tools than for other items.

Instead of a specific item identifier, you can also register a component factory with a Predicate<Item>. This lets you use your own criteria like "implements a specific interface".

Synchronization

Item components are automatically and systematically synchronized whenever their stack is. The AutoSyncedComponent interface has no effect when the component is attached to a stack.

Ticking

Item components do not support ticking. If you want the stack to change over time, you should store the time it was created/last updated and compute the age when needed.

Equality

Stack equality methods areTagsEqual and isEqualIgnoreDamage are modified to check component equality. If you have issues when attaching components to item stacks, it usually means you forgot to implement a meaningful equals check on your component.

Emptiness guarantee

Empty item stacks never expose any components, no matter what was originally attached to them. The components will become available again with no loss if the stack stops being empty at any point.

Vanilla Alternative: Stack NBT

+ No dependency
+ No setup required, readily available on every stack
= Automatically synchronized
- Requires a mixin to add data to every created stack
- Must be (de)serialized for every use (can be slow)
- Can only carry raw public data, no private fields, no methods, no interface implementing