1
1
# Items API
2
2
3
- The Items API provides functionality for creating, managing, and interacting with in-game items.
3
+ The Items API provides functionality for working with in-game items, item definitions, and item instances .
4
4
5
5
## Namespace
6
6
@@ -10,183 +10,159 @@ using S1API.Items;
10
10
11
11
## Key Classes
12
12
13
- ### Item
13
+ ### ItemDefinition
14
14
15
- Represents a single item in the game .
15
+ Represents an item definition in-game. A definition describes "what" the item is, such as "This is a Soda" .
16
16
17
17
``` csharp
18
- public class Item : SaveableBase
18
+ public class ItemDefinition : IGUIDReference
19
19
{
20
- public string ItemID { get ; }
21
- public string Name { get ; set ; }
22
- public string Description { get ; set ; }
23
- public float Weight { get ; set ; }
24
- public float Value { get ; set ; }
25
- public Sprite Icon { get ; set ; }
26
- public GameObject Model { get ; set ; }
27
- public ItemType Type { get ; set ; }
28
- public Dictionary <string , object > Properties { get ; }
20
+ public string GUID { get ; }
21
+ public string ID { get ; }
22
+ public string Name { get ; }
23
+ public string Description { get ; }
24
+ public ItemCategory Category { get ; }
25
+ public int StackLimit { get ; }
29
26
30
- public Item (string itemID , string name , string description );
31
-
32
- public bool CanUse ();
33
- public void Use ();
34
- public Item Clone ();
35
- public void SetProperty <T >(string key , T value );
36
- public T GetProperty <T >(string key , T defaultValue = default );
27
+ public virtual ItemInstance CreateInstance (int quantity = 1 );
37
28
}
38
29
```
39
30
40
- ### ItemStack
31
+ ### ItemInstance
41
32
42
- Represents a stack of items with a quantity.
33
+ Represents an item instance in the game. An instance is the item existing in the game world, such as "I have five sodas in my hand."
43
34
44
35
``` csharp
45
- public class ItemStack : SaveableBase
36
+ public class ItemInstance
46
37
{
47
- public Item Item { get ; }
48
- public int Quantity { get ; set ; }
49
- public float TotalWeight { get ; }
50
- public float TotalValue { get ; }
51
-
52
- public ItemStack (Item item , int quantity = 1 );
53
-
54
- public ItemStack Split (int quantity );
55
- public bool CanMerge (ItemStack other );
56
- public void Merge (ItemStack other );
57
- public bool CanUse ();
58
- public void Use ();
38
+ public ItemDefinition Definition { get ; }
59
39
}
60
40
```
61
41
62
- ### ItemType
42
+ ### ItemCategory
63
43
64
- Enum representing different types of items.
44
+ Enum representing different categories of items.
65
45
66
46
``` csharp
67
- public enum ItemType
47
+ public enum ItemCategory
68
48
{
69
- General ,
70
- Weapon ,
71
- Armor ,
49
+ Product ,
50
+ Packaging ,
51
+ Growing ,
52
+ Tools ,
53
+ Furniture ,
54
+ Lighting ,
55
+ Cash ,
72
56
Consumable ,
73
- Quest ,
74
- Key ,
75
- Material ,
76
- Tool ,
77
- Valuable ,
78
- Special
57
+ Equipment ,
58
+ Ingredient ,
59
+ Decoration ,
60
+ Clothing
79
61
}
80
62
```
81
63
82
64
### ItemManager
83
65
84
- Static class for managing all items in the game.
66
+ Static class for managing items across the game.
85
67
86
68
``` csharp
87
69
public static class ItemManager
88
70
{
89
- public static void RegisterItem (Item itemTemplate );
90
- public static void UnregisterItem (string itemID );
91
- public static Item GetItemTemplate (string itemID );
92
- public static bool TryGetItemTemplate (string itemID , out Item itemTemplate );
93
- public static Item CreateItem (string itemID );
94
- public static ItemStack CreateItemStack (string itemID , int quantity = 1 );
95
- public static List <Item > GetAllItems ();
96
- public static List <Item > GetItemsByType (ItemType type );
71
+ public static ItemDefinition GetItemDefinition (string itemID );
72
+ }
73
+ ```
74
+
75
+ ### ItemSlotInstance
76
+
77
+ Represents an item slot within the game. These are present within storage, the hot bar, etc.
78
+
79
+ ``` csharp
80
+ public class ItemSlotInstance
81
+ {
82
+ public int Quantity { get ; }
83
+ public ItemInstance ? ItemInstance { get ; }
84
+
85
+ public void AddQuantity (int amount );
97
86
}
98
87
```
99
88
100
89
## Usage Examples
101
90
102
- ### Creating a Custom Item
91
+ ### Getting an Item Definition
103
92
104
93
``` csharp
105
- // Create a new item template
106
- var medkit = new Item (" mymod.medkit" , " Advanced Medkit" , " Fully restores health and cures ailments" );
107
- medkit .Weight = 0 . 5 f ;
108
- medkit .Value = 150 . 0 f ;
109
- medkit .Type = ItemType .Consumable ;
110
-
111
- // Set custom properties
112
- medkit .SetProperty (" healAmount" , 100 );
113
- medkit .SetProperty (" curesPoison" , true );
114
- medkit .SetProperty (" cooldown" , 30 . 0 f );
115
-
116
- // Load and set the icon
117
- medkit .Icon = Resources .Load <Sprite >(" MyMod/Icons/medkit_icon" );
118
-
119
- // Register the item with the item manager
120
- ItemManager .RegisterItem (medkit );
94
+ // Get an item definition by its ID
95
+ var sodaDefinition = ItemManager .GetItemDefinition (" cuke" );
96
+
97
+ // Access properties of the definition
98
+ Console .WriteLine ($" Name: {sodaDefinition .Name }" );
99
+ Console .WriteLine ($" Description: {sodaDefinition .Description }" );
100
+ Console .WriteLine ($" Category: {sodaDefinition .Category }" );
101
+ Console .WriteLine ($" Stack Limit: {sodaDefinition .StackLimit }" );
121
102
```
122
103
123
- ### Working with ItemStacks
104
+ ### Creating an Item Instance
124
105
125
106
``` csharp
126
- // Create an item stack from a registered item
127
- var medkitStack = ItemManager .CreateItemStack ( " mymod.medkit " , 3 );
107
+ // Get an item definition
108
+ var cukeDefinition = ItemManager .GetItemDefinition ( " cuke " );
128
109
129
- // Split the stack
130
- var splitStack = medkitStack .Split (1 );
131
- Console .WriteLine ($" Original stack: {medkitStack .Quantity }" ); // 2
132
- Console .WriteLine ($" Split stack: {splitStack .Quantity }" ); // 1
110
+ // Create an instance of the item with a quantity of 5
111
+ var cukeInstance = cukeDefinition .CreateInstance (5 );
112
+ ```
133
113
134
- // Merge compatible stacks
135
- if (medkitStack .CanMerge (splitStack ))
136
- {
137
- medkitStack .Merge (splitStack );
138
- Console .WriteLine ($" Merged stack: {medkitStack .Quantity }" ); // 3
139
- }
114
+ ### Working with Item Slots
140
115
141
- // Use an item from the stack
142
- if (medkitStack .CanUse ())
116
+ ``` csharp
117
+ // Example: Working with an item slot
118
+ public void AddItemToSlot (ItemSlotInstance slot , ItemDefinition itemDef , int quantity )
143
119
{
144
- medkitStack .Use (); // Automatically decrements quantity
145
- Console .WriteLine ($" After use: {medkitStack .Quantity }" ); // 2
120
+ // Check if slot is empty or contains the same item type
121
+ if (slot .ItemInstance == null || slot .ItemInstance .Definition .ID == itemDef .ID )
122
+ {
123
+ // Create a new instance if needed
124
+ if (slot .ItemInstance == null )
125
+ {
126
+ var instance = itemDef .CreateInstance (quantity );
127
+ // Code to add instance to slot would go here
128
+ }
129
+ else
130
+ {
131
+ // Add to existing quantity
132
+ slot .AddQuantity (quantity );
133
+ }
134
+ }
146
135
}
147
136
```
148
137
149
- ### Querying Items
138
+ ### Checking Item Categories
150
139
151
140
``` csharp
152
- // Get all consumable items
153
- var consumables = ItemManager .GetItemsByType (ItemType .Consumable );
154
- foreach (var item in consumables )
141
+ // Get an item definition
142
+ var itemDef = ItemManager .GetItemDefinition (" cuke" );
143
+
144
+ // Check item category
145
+ if (itemDef .Category == ItemCategory .Consumable )
155
146
{
156
- Console .WriteLine ($" {item .Name }: {item .Description }" );
147
+ // Handle consumable item
148
+ Console .WriteLine (" This is a consumable item" );
157
149
}
158
-
159
- // Check if an item exists and get it
160
- if (ItemManager .TryGetItemTemplate (" mymod.medkit" , out var medkit ))
150
+ else if (itemDef .Category == ItemCategory .Tools )
161
151
{
162
- float healAmount = medkit .GetProperty <float >(" healAmount" , 0 );
163
- bool curesPoison = medkit .GetProperty <bool >(" curesPoison" , false );
164
-
165
- Console .WriteLine ($" Medkit heals {healAmount } and cures poison: {curesPoison }" );
152
+ // Handle tool item
153
+ Console .WriteLine (" This is a tool item" );
166
154
}
167
155
```
168
156
169
- ## Events
170
-
171
- The Items API provides events to hook into item-related actions:
172
-
173
- ``` csharp
174
- // Item creation
175
- ItemManager .OnItemCreated += (item ) => { /* ... */ };
157
+ ## Best Practices
176
158
177
- // Item use
178
- ItemManager .OnItemUsed += (item , user ) => { /* ... */ };
159
+ 1 . Use the appropriate abstraction level when working with items:
160
+ - ` ItemDefinition ` for referencing item types/definitions
161
+ - ` ItemInstance ` for working with specific instances of items
162
+ - ` ItemSlotInstance ` for working with inventory slots
179
163
180
- // Item registration
181
- ItemManager .OnItemRegistered += (itemTemplate ) => { /* ... */ };
182
- ItemManager .OnItemUnregistered += (itemID ) => { /* ... */ };
183
- ```
164
+ 2 . When changing quantities in item slots, use the ` AddQuantity ` method
184
165
185
- ## Best Practices
166
+ 3 . Always check if an item instance exists before accessing its properties
186
167
187
- 1 . Always register item templates at mod startup
188
- 2 . Use unique, namespaced IDs for all items
189
- 3 . Consider performance when designing items with complex behaviors
190
- 4 . Keep item property keys consistent across related items
191
- 5 . Set reasonable weight and value properties based on game balance
192
- 6 . Implement proper usage logic in the CanUse and Use methods
168
+ 4 . Use the item GUID or ID consistently when referencing items across your code
0 commit comments