Class ItemSet<P extends PieceType,T extends net.minecraft.world.item.Item>
- Type Parameters:
P- the piece type used to identify individual items in the setT- the common item type represented by this set
Item instances,
where each item is identified by a piece type.
An ItemSet associates each PieceType with a lazily initialized
item reference and an item factory used during registration. Items are not
created until their corresponding supplier is resolved, allowing item sets
to be declared before the actual registry instances are available.
Each piece is assigned a registry name derived from the set name and the piece's registry suffix. These names can be used to generate registration entries and to bind the resulting registered item instances back to the set.
This class is intended to serve as a common abstraction for specialized
item-set implementations such as ToolSet
and ArmorSet.
- Since:
- 2.0.0
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedItemSet(String setName, Collection<P> pieceTypes, BiFunction<P, net.minecraft.world.item.Item.Properties, T> factory) Constructs an item set with the specified name, piece types, and item factory. -
Method Summary
Modifier and TypeMethodDescriptionvoidBinds a supplier to the lazy reference associated with a registry name.Returns the lazy reference associated with the specified piece.getAll()Resolves and returns all items belonging to this set.Returns the piece types belonging to this set.getPiecesForRegistration(String modId) Generates the registry entries for all pieces in this set.Returns the base name used to generate registry names for this set.
-
Field Details
-
setName
The base name of this item set, used for generating registry IDs.
-
-
Constructor Details
-
ItemSet
protected ItemSet(String setName, Collection<P> pieceTypes, BiFunction<P, net.minecraft.world.item.Item.Properties, T> factory) Constructs an item set with the specified name, piece types, and item factory.Each supplied piece is registered internally with a lazy reference and a factory capable of creating the corresponding item from
Item.Properties. The supplied collection is copied into the internal data structures, so subsequent modifications to the collection do not affect this set.- Parameters:
setName- the base name used to generate registry namespieceTypes- the piece types belonging to this set; must not be emptyfactory- the factory used to create an item for each piece- Throws:
NullPointerException- ifsetName,pieceTypes,factory, or a piece isnullIllegalArgumentException- ifpieceTypesis empty
-
-
Method Details
-
getPiecesForRegistration
public Map<net.minecraft.resources.ResourceLocation,Function<net.minecraft.world.item.Item.Properties, getPiecesForRegistrationT>> (String modId) Generates the registry entries for all pieces in this set.Each entry maps a
ResourceLocationconstructed from the supplied namespace and the piece's generated registry name to a factory acceptingItem.Properties. The returned factories create the actual item instances when invoked by the registry system.The registry names are generated when the set is constructed and therefore remain consistent across registration and later item access.
- Parameters:
modId- the namespace used for the generated resource locations- Returns:
- an insertion-ordered map containing one registration entry for each piece in this set
- Throws:
NullPointerException- ifmodIdisnullIllegalArgumentException- ifmodIdis not a valid resource location namespace- See Also:
-
bind
Binds a supplier to the lazy reference associated with a registry name.This method is intended for use by the registration layer after an item instance has been registered. The supplied value is subsequently resolved when the corresponding piece is accessed through
get(P)orgetAll().- Parameters:
registryName- the registry name of the piece to bindsupplier- the supplier used to resolve the registered item- Throws:
NullPointerException- ifsupplierisnullIllegalArgumentException- ifregistryNamedoes not identify a piece in this set- See Also:
-
get
Returns the lazy reference associated with the specified piece.The returned reference implements
Supplier, allowing the item to be resolved when required rather than requiring the item instance to be created at the time the set itself is constructed.- Parameters:
piece- the piece whose item reference should be returned- Returns:
- the lazy reference for the specified piece
- Throws:
NullPointerException- ifpieceisnullIllegalArgumentException- ifpieceis not part of this set
-
getAll
Resolves and returns all items belonging to this set.Calling this method causes every item's lazy reference to be resolved. Consequently, this method should generally be used when the complete set of item instances is required rather than when only an individual piece is needed.
The returned collection follows the insertion order of the pieces supplied when this set was constructed.
- Returns:
- an unmodifiable list containing every item in this set
-
getPieces
Returns the piece types belonging to this set.The returned set is backed by the set's internal piece map and cannot be modified through the returned view.
- Returns:
- an unmodifiable set of the pieces in this set
-
getSetName
Returns the base name used to generate registry names for this set.- Returns:
- the set's base registry name
-