主题
GameCore.Container
ComponentExtension 双端
命名空间 GameCore.Container
csharp
public static class ComponentExtension方法
GetComponent 双端
csharp
public static InventoryManager? GetComponent<TComponent>(this Unit entity) where TComponent : InventoryManagerGetOrCreateComponent 双端
csharp
public static InventoryManager GetOrCreateComponent<TComponent>(this Unit entity) where TComponent : InventoryManagerInventory 双端
命名空间 GameCore.Container
csharp
public class Inventory : DisposableObject, IAttachedObject, ISyncIdOwner, IDisposable, IGameObject<GameDataInventory>, IGameObject, IPropertyOwner<PropertyAttachedObject>, IPropertyOwnerRepresents an inventory container that manages a collection of item slots for storing and organizing items.
备注
The Inventory class provides a structured way to store, organize, and manipulate items within a game system. It consists of a collection of slots, each capable of holding specific types of items based on configuration. The inventory supports item absorption (stacking), assignment, and various operations for item management. Each inventory is associated with a carrier unit and managed by an InventoryManager for lifecycle control. The system handles item state management, ownership changes, and synchronization for multiplayer scenarios.
构造函数
Inventory 双端
csharp
public Inventory(Unit host, IGameLink<GameDataInventory> link)属性
Slots 双端
csharp
public List<InventorySlot> Slots { get; }Gets the collection of inventory slots that make up this inventory.
备注
Each slot can hold one or more items (if stacking is supported) and has its own configuration for item type restrictions and requirements.
Manager 双端
csharp
public InventoryManager Manager { get; }Gets the inventory manager that owns this inventory.
Carrier 双端
csharp
public Unit Carrier { get; }Gets the unit that carries this inventory.
备注
The carrier unit determines the context for item requirements, ownership changes, and spatial operations like dropping items to the ground.
Items 双端
csharp
public IEnumerable<ItemPickable> Items { get; }Gets all items currently stored in this inventory.
备注
This property provides a convenient way to iterate through all items without having to check individual slots. Empty slots are automatically excluded.
DebuggerDisplay 双端
csharp
protected virtual string? DebuggerDisplay { get; }Gets the display string used by the debugger.
Host 双端
csharp
public ICooldownHost Host { get; }Link 双端
csharp
public IGameLink<GameDataInventory> Link { get; }Cache 双端
csharp
public GameDataInventory Cache { get; }AllowDrop 双端
csharp
public bool AllowDrop { get; set; }AllowUse 双端
csharp
public bool AllowUse { get; set; }HandlePickUpRequest 双端
csharp
public bool HandlePickUpRequest { get; set; }If true, the inventory will handle the pick up request. Otherwise, only none-request pick up orders will be handled.
方法
Absorb 双端
csharp
public uint Absorb(ItemPickable inItem)Attempts to absorb (stack) the specified item into existing items in this inventory.
inItem— The item to absorb into existing stacks.
返回: The total amount of the item that was absorbed.
备注
This method tries to merge the incoming item with existing items of the same type in the inventory. It's particularly useful for stackable items like resources or consumables. The method will distribute the item across multiple stacks if necessary and possible.
CanAssign 双端
csharp
public InventorySlot? CanAssign(ItemPickable inItem, ReasonItemAssign reason = ReasonItemAssign.Take, InventorySlot? swapSource = null)Checks if the specified item can be assigned to any slot in this inventory.
inItem— The item to test for assignment compatibility.reason— The reason for the assignment attempt.swapSource— The source slot for swap operations, if applicable.
返回: The first slot that can accept this item, or null if no suitable slot is found.
备注
This method checks all slots in order to find the first one that can accept the item based on the slot's restrictions, item requirements, and the assignment reason. It's useful for previewing assignment operations before actually performing them.
Assign 双端
csharp
public InventorySlot? Assign(ItemPickable inItem, ReasonItemAssign reason = ReasonItemAssign.Take, InventorySlot? swapSource = null)Attempts to assign the specified item to the first available suitable slot in this inventory.
inItem— The item to assign to this inventory.reason— The reason for the assignment.swapSource— The source slot for swap operations, if applicable.
返回: The slot where the item was assigned, or null if the assignment failed.
备注
This method first checks if the item can be assigned using CanAssign, then performs the actual assignment if a suitable slot is found. It provides a convenient way to add items to the inventory without manually managing slot selection.
Take 双端
csharp
public bool Take(ItemPickable inItem)Attempts to take the specified item into this inventory, using absorption and assignment as needed.
inItem— The item to take into this inventory.
返回: True if the item was successfully taken; otherwise, false.
备注
This method first tries to absorb the item into existing stacks, then attempts to assign it to an empty slot if absorption doesn't fully consume the item. It provides the most convenient way to add items to an inventory with automatic handling of stacking behavior.
ToString 双端
csharp
public override string ToString()Returns a string representation of this inventory including its type, name, ID, and slot/item counts.
返回: A formatted string describing this inventory.
ReplicateFromKeyValue 仅客户端
csharp
public virtual bool ReplicateFromKeyValue(Enum propertyEnum, ReadOnlySpan<byte> value, bool isHostReplicating = false)DisposeManaged 双端
csharp
protected override void DisposeManaged()GetProperty 双端
csharp
public TValue? GetProperty<TValue>(PropertyAttachedObject property)SetProperty 仅服务端
csharp
public void SetProperty<TValue>(PropertyAttachedObject property, TValue value)AddProperty 仅服务端
csharp
public void AddProperty(PropertyAttachedObject property, double value)InventoryContentsDisposition 双端
命名空间 GameCore.Container
csharp
public enum InventoryContentsDisposition物品栏销毁时,其中物品的处理方式。
枚举值
Remove— 将物品从游戏中移除。DropToGround— 将物品放置到物品栏携带者销毁前所在的位置。
InventoryManager 双端
命名空间 GameCore.Container
csharp
public class InventoryManager : TagComponent, IObjectManager<Inventory>, IContextTypeService<Entity, InventoryManager>, ICommandHandler构造函数
InventoryManager 双端
csharp
public InventoryManager(Entity host)属性
Items 双端
csharp
public IEnumerable<ItemPickable> Items { get; }Inventories 双端
csharp
public OrderedSet<Inventory> Inventories { get; }PickUpRange 双端
csharp
public float PickUpRange { get; }方法
AddInventory 双端
csharp
public void AddInventory(Inventory inventory)Absorb 双端
csharp
public uint Absorb(ItemPickable inItem)CanAssign 双端
csharp
public InventorySlot? CanAssign(ItemPickable inItem, ReasonItemAssign reason = ReasonItemAssign.Take, InventorySlot? swapSource = null, bool isRequest = false)Assign 双端
csharp
public InventorySlot? Assign(ItemPickable inItem, ReasonItemAssign reason = ReasonItemAssign.Take, InventorySlot? swapSource = null, bool isRequest = false)Take 双端
csharp
public bool Take(ItemPickable inItem, bool isRequest = false)Dispose 双端
csharp
protected override void Dispose(bool disposing)CreateInstance 双端
csharp
public static InventoryManager CreateInstance(Entity content)CreateOrder 仅服务端
csharp
public virtual CmdResult<Order> CreateOrder(Command command)事件
InventoryAttached 双端
csharp
public event Action<Inventory, InventoryManager>? InventoryAttached当Inventory被添加到InventoryManager时触发
InventoryDetached 双端
csharp
public event Action<Inventory, InventoryManager>? InventoryDetached当单个Inventory从InventoryManager中移除时触发,但整个InventoryManager被销毁时不会触发
InventorySlot 双端
命名空间 GameCore.Container
csharp
public class InventorySlot : ICommandTarget, IDisposableRepresents a single slot within an inventory that can hold an item with specific restrictions and requirements.
备注
InventorySlot is the fundamental unit of item storage within an inventory system. Each slot can hold one item (or a stack of identical items) and enforces specific rules about what types of items can be assigned to it. Slots support item category filtering, requirement validation, and various assignment/drop operations. The slot handles item state management, ownership changes, and synchronization when items are assigned or removed. It integrates with the broader inventory system to provide flexible and controlled item management capabilities.
构造函数
InventorySlot 双端
csharp
public InventorySlot(Inventory inventory, IInventorySlotData data)Represents a single slot within an inventory that can hold an item with specific restrictions and requirements.
备注
InventorySlot is the fundamental unit of item storage within an inventory system. Each slot can hold one item (or a stack of identical items) and enforces specific rules about what types of items can be assigned to it. Slots support item category filtering, requirement validation, and various assignment/drop operations. The slot handles item state management, ownership changes, and synchronization when items are assigned or removed. It integrates with the broader inventory system to provide flexible and controlled item management capabilities.
属性
Inventory 双端
csharp
public Inventory Inventory { get; }Gets the inventory that contains this slot.
Item 双端
csharp
public ItemPickable? Item { get; set; }Gets or sets the item currently stored in this slot.
备注
This property is managed internally by the slot assignment system and should not be modified directly. Use the Assign and Drop methods to properly manage slot contents.
Cache 双端
csharp
public IInventorySlotData Cache { get; }Gets the configuration data that defines the restrictions and behavior of this slot.
Carrier 双端
csharp
public Unit Carrier { get; }Gets the unit that carries the inventory containing this slot.
MeetRequirement 双端
csharp
public bool MeetRequirement { get; }Gets a value indicating whether the item in this slot meets its requirements.
备注
This property reflects whether the currently stored item satisfies all requirements for proper functionality. Items that don't meet requirements may be disabled or restricted.
DebuggerDisplay 双端
csharp
protected virtual string? DebuggerDisplay { get; }Gets the display string used by the debugger.
SlotIndex 双端
csharp
public int SlotIndex { get; }Gets the index of this slot within its parent inventory.
备注
This property is calculated on first access and cached for efficiency. It's used for slot identification and ordering within the inventory system.
IsValid 双端
csharp
public bool IsValid { get; }方法
CanDrop 双端
csharp
public bool CanDrop(ReasonItemDrop reason = ReasonItemDrop.DropToGround, InventorySlot? swapSource = null)Determines whether an item can be dropped from this slot for the specified reason.
reason— The reason for dropping the item.swapSource— The source slot for swap operations, if applicable.
返回: True if the item can be dropped; otherwise, false.
备注
This method checks inventory drop permissions and swap compatibility to determine if the drop operation is allowed. Different drop reasons have different requirements.
Drop 双端
csharp
public bool Drop(ReasonItemDrop reason = ReasonItemDrop.DropToGround, InventorySlot? swapSource = null)Drops the item from this slot according to the specified reason and parameters.
reason— The reason for dropping the item.swapSource— The source slot for swap operations, if applicable.
返回: True if the drop was successful; otherwise, false.
备注
This method removes the item from the slot and handles the appropriate consequences based on the drop reason, such as placing the item on the ground or preparing it for swapping.
CanAssign 双端
csharp
public bool CanAssign(ItemPickable inItem, ReasonItemAssign reason = ReasonItemAssign.Take, InventorySlot? swapSource = null)Determines whether the specified item can be assigned to this slot.
inItem— The item to test for assignment compatibility.reason— The reason for the assignment attempt.swapSource— The source slot for swap operations, if applicable.
返回: True if the item can be assigned to this slot; otherwise, false.
备注
This method performs comprehensive validation including item validity, slot availability, category restrictions, requirements checking, and swap compatibility. It's used to preview assignment operations before actually performing them.
Assign 双端
csharp
public bool Assign(ItemPickable inItem, ReasonItemAssign reason = ReasonItemAssign.Take, InventorySlot? swapSource = null)Assigns the specified item to this slot, handling swapping and state management as needed.
inItem— The item to assign to this slot.reason— The reason for the assignment.swapSource— The source slot for swap operations, if applicable.
返回: True if the assignment was successful; otherwise, false.
备注
This method performs the actual assignment operation after validation, handling item swapping, inventory state application, and proper cleanup of source slots. It coordinates with the broader inventory system to ensure consistent item management.
Put 双端
csharp
public bool Put(ItemPickable inItem)Attempts to put an item in this slot, using absorption or assignment as appropriate.
inItem— The item to put in this slot.
返回: True if the item was successfully placed; otherwise, false.
备注
This method first attempts to absorb the item into an existing stack if possible, then falls back to assignment with swapping if absorption fails or is not applicable. It provides a convenient high-level interface for item placement.
ToString 双端
csharp
public override string ToString()Returns a string representation of this inventory slot including its configuration, index, and contained item.
返回: A formatted string describing this inventory slot.
Dispose 双端
csharp
protected virtual void Dispose(bool disposing)ItemSlotType 双端
命名空间 GameCore.Container
csharp
public enum ItemSlotType枚举值
CarryEquip
ReasonItemAssign 双端
命名空间 GameCore.Container
csharp
public enum ReasonItemAssign枚举值
TakeSwapForcedReplication
ReasonItemDrop 双端
命名空间 GameCore.Container
csharp
public enum ReasonItemDrop枚举值
DropToGroundSwapGiveDestroyReplication
GameDataInventory 双端
命名空间 GameCore.Container.Data
csharp
public class GameDataInventory : GameDataCategory<GameDataInventory>, IAttachedData, IGameDataCategory<GameDataInventory>, IGameData<GameDataInventory>, IGameData物品栏数据:定义单位的背包/装备栏槽位配置及拾取、丢弃、使用权限。
构造函数
GameDataInventory 双端
csharp
public GameDataInventory(GameLink<GameDataInventory, GameDataInventory> link)GameDataInventory 双端
csharp
public GameDataInventory(IGameLink<GameDataInventory> link)属性
Slots 双端
csharp
public List<InventorySlotData> Slots { get; set; }槽位列表,每个槽位可配置类型、允许/禁止的物品类别。
InventoryFlags 双端
csharp
public InventoryFlags InventoryFlags { get; set; }物品栏行为标志:是否处理拾取、允许丢弃、允许使用。
ContentsDispositionOnDestroy 双端
csharp
public InventoryContentsDisposition ContentsDispositionOnDestroy { get; set; }物品栏销毁时,其中物品的处理方式。默认将物品从游戏中移除。
SyncType 双端
csharp
public SyncType SyncType { get; set; }同步范围:仅自己、队伍或全服可见。
方法
CreateInventory 双端
csharp
public virtual Inventory CreateInventory(Unit unit)GameDataInventorySlot 双端
命名空间 GameCore.Container.Data
csharp
public class GameDataInventorySlot : GameDataCategory<GameDataInventorySlot>, IInventorySlotData, IGameDataCategory<GameDataInventorySlot>, IGameData<GameDataInventorySlot>, IGameData物品栏槽位:定义单个槽位的类型及可放入的物品类别限制。
构造函数
GameDataInventorySlot 双端
csharp
public GameDataInventorySlot(GameLink<GameDataInventorySlot, GameDataInventorySlot> link)GameDataInventorySlot 双端
csharp
public GameDataInventorySlot(IGameLink<GameDataInventorySlot> link)属性
Type 双端
csharp
public ItemSlotType Type { get; set; }槽位类型(如武器、防具、背包格等)。
Required 双端
csharp
public List<ItemCategory> Required { get; set; }必须满足的物品类别,物品需至少属于其中一类才能放入。
Excluded 双端
csharp
public List<ItemCategory> Excluded { get; set; }禁止的物品类别,属于其中任一类别的物品不能放入。
DisallowItemWithFailedRequirement 双端
csharp
public bool DisallowItemWithFailedRequirement { get; set; }当 Required 不满足时,是否禁止放入(否则可能允许但显示警告)。
IInventorySlotData 双端
命名空间 GameCore.Container.Data.Struct
csharp
public interface IInventorySlotData属性
Type 双端
csharp
public ItemSlotType Type { get; }Required 双端
csharp
public List<ItemCategory> Required { get; }Excluded 双端
csharp
public List<ItemCategory> Excluded { get; }DisallowItemWithFailedRequirement 双端
csharp
public bool DisallowItemWithFailedRequirement { get; }InventorySlotData 双端
命名空间 GameCore.Container.Data.Struct
csharp
public class InventorySlotData : IInventorySlotData构造函数
InventorySlotData 双端
csharp
public InventorySlotData()属性
Type 双端
csharp
public ItemSlotType Type { get; set; }Required 双端
csharp
public List<ItemCategory> Required { get; set; }Excluded 双端
csharp
public List<ItemCategory> Excluded { get; set; }DisallowItemWithFailedRequirement 双端
csharp
public bool DisallowItemWithFailedRequirement { get; set; }