IItemComponentRegistrar
public interface IItemComponentRegistrar : IPluginService
Provides methods for registering custom implementations of ItemComponent that can be added to items.
Remarks
The methods are instance methods. The examples assume an instance has been retrieved and stored, e.g.:
public static readonly IItemComponentRegistrar ItemComponentRegistrar = PluginServiceProvider.GetService<IItemComponentRegistrar>();
Members
| Member | Description |
|---|---|
RegisterItemComponent<T>() |
Registers a custom item component to be recognized by the game. |
RegisterItemComponents() |
Registers multiple custom item components to be recognized by the game. |
RegisterAllItemComponents() |
Registers all types in the calling plugin that inherit ItemComponent. |
AddItemComponentToItem() |
Adds a custom item component to an already existing item. |
Methods
RegisterItemComponent
void RegisterItemComponent<T>() where T : ItemComponent
Registers a custom item component to be recognized by the game.
| Type parameter | Description |
|---|---|
T |
Implementation of ItemComponent to register. |
Example
MyItemComponent.cs:
public class MyItemComponent(Item item, ContentXElement element) : ItemComponent(item, element)
{
[Serialize(0, IsPropertySaveable.Yes, description: "My custom property")]
public int MyProperty { get; set; }
}
Plugin init:
ItemComponentRegistrar.RegisterItemComponent<MyItemComponent>();
RegisterItemComponents
void RegisterItemComponents(params Type[] types)
Registers multiple custom item components to be recognized by the game.
| Parameter | Type | Description |
|---|---|---|
types |
Type[] |
list of types inheriting ItemComponent to register. |
| Exception | Condition |
|---|---|
ArgumentException |
if a type does not inherit ItemComponent. |
Example
MyItemComponent.cs:
public class MyItemComponent(Item item, ContentXElement element) : ItemComponent(item, element)
{
[Serialize("", IsPropertySaveable.Yes, description: "My custom property")]
public int MyProperty { get; set; }
}
public class AnotherItemComponent(Item item, ContentXElement element) : ItemComponent(item, element)
{
[Serialize("", IsPropertySaveable.Yes, description: "Another custom property")]
public string AnotherProperty { get; set; }
}
Plugin init:
ItemComponentRegistrar.RegisterItemComponents(typeof(MyItemComponent), typeof(AnotherItemComponent));
RegisterAllItemComponents
void RegisterAllItemComponents()
Registers all types in the calling plugin that inherit ItemComponent.
Remarks
The method will find all types in the calling assembly that inherit ItemComponent via reflection and register them using RegisterItemComponents.
Example
Plugin init:
ItemComponentRegistrar.RegisterAllItemComponents();
AddItemComponentToItem
void AddItemComponentToItem(Identifier identifierOrTag, ContentXElement element)
Adds a custom item component to an already existing item.
| Parameter | Type | Description |
|---|---|---|
identifierOrTag |
Identifier |
Identifier of the item to add the component to. |
element |
ContentXElement |
The initial prefab element of the item component. |
Example
Plugin init:
ItemComponentRegistrar.AddItemComponentToItem("planter".ToIdentifier(), prefabElement);