Skip to content

Commit b30a4c7

Browse files
committed
updating docs
1 parent bb06ca9 commit b30a4c7

File tree

4 files changed

+195
-501
lines changed

4 files changed

+195
-501
lines changed

.vitepress/docs/api/items/index.md

Lines changed: 98 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Items API
22

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.
44

55
## Namespace
66

@@ -10,183 +10,159 @@ using S1API.Items;
1010

1111
## Key Classes
1212

13-
### Item
13+
### ItemDefinition
1414

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".
1616

1717
```csharp
18-
public class Item : SaveableBase
18+
public class ItemDefinition : IGUIDReference
1919
{
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; }
2926

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);
3728
}
3829
```
3930

40-
### ItemStack
31+
### ItemInstance
4132

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."
4334

4435
```csharp
45-
public class ItemStack : SaveableBase
36+
public class ItemInstance
4637
{
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; }
5939
}
6040
```
6141

62-
### ItemType
42+
### ItemCategory
6343

64-
Enum representing different types of items.
44+
Enum representing different categories of items.
6545

6646
```csharp
67-
public enum ItemType
47+
public enum ItemCategory
6848
{
69-
General,
70-
Weapon,
71-
Armor,
49+
Product,
50+
Packaging,
51+
Growing,
52+
Tools,
53+
Furniture,
54+
Lighting,
55+
Cash,
7256
Consumable,
73-
Quest,
74-
Key,
75-
Material,
76-
Tool,
77-
Valuable,
78-
Special
57+
Equipment,
58+
Ingredient,
59+
Decoration,
60+
Clothing
7961
}
8062
```
8163

8264
### ItemManager
8365

84-
Static class for managing all items in the game.
66+
Static class for managing items across the game.
8567

8668
```csharp
8769
public static class ItemManager
8870
{
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);
9786
}
9887
```
9988

10089
## Usage Examples
10190

102-
### Creating a Custom Item
91+
### Getting an Item Definition
10392

10493
```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.5f;
108-
medkit.Value = 150.0f;
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.0f);
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}");
121102
```
122103

123-
### Working with ItemStacks
104+
### Creating an Item Instance
124105

125106
```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");
128109

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+
```
133113

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
140115

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)
143119
{
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+
}
146135
}
147136
```
148137

149-
### Querying Items
138+
### Checking Item Categories
150139

151140
```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)
155146
{
156-
Console.WriteLine($"{item.Name}: {item.Description}");
147+
// Handle consumable item
148+
Console.WriteLine("This is a consumable item");
157149
}
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)
161151
{
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");
166154
}
167155
```
168156

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
176158

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
179163

180-
// Item registration
181-
ItemManager.OnItemRegistered += (itemTemplate) => { /* ... */ };
182-
ItemManager.OnItemUnregistered += (itemID) => { /* ... */ };
183-
```
164+
2. When changing quantities in item slots, use the `AddQuantity` method
184165

185-
## Best Practices
166+
3. Always check if an item instance exists before accessing its properties
186167

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

Comments
 (0)