Class InventoryBase
- Namespace
- Vintagestory.API.Common
- Assembly
- VintagestoryAPI.dll
Basic class representing an item inventory
public abstract class InventoryBase : IInventory, IReadOnlyCollection<ItemSlot>, IEnumerable<ItemSlot>, IEnumerable
- Inheritance
-
InventoryBase
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
InventoryBase(string, string, ICoreAPI)
Create a new instance of an inventory. You may choose any value for className and instanceID, but if more than one of these inventories can be opened at the same time, make sure for both of them to have a different id
public InventoryBase(string className, string instanceID, ICoreAPI api)
Parameters
InventoryBase(string, ICoreAPI)
Create a new instance of an inventory. InvetoryID must have the format [className]-[instanceId]. You may choose any value for className and instanceID, but if more than one of these inventories can be opened at the same time, make sure for both of them to have a different id
public InventoryBase(string inventoryID, ICoreAPI api)
Parameters
Fields
Api
The world in which the inventory is operating in. Gives inventories access to block types, item types and the ability to drop items on the ground.
public ICoreAPI Api
Field Value
InvNetworkUtil
The network utility for the inventory
public IInventoryNetworkUtil InvNetworkUtil
Field Value
Pos
Optional field that can be used to define in-world position of the inventory. Is set by most container block entities. Might be null!
public BlockPos Pos
Field Value
baseWeight
Is this inventory generally better suited to hold items? (e.g. set to 3 for armor in armor inventory, 2 for any item in hotbar inventory, 1 for any item in normal inventory)
protected float baseWeight
Field Value
className
The name of the class for the invnentory.
protected string className
Field Value
dirtySlots
Slots that have been recently modified. This list is used on the server to update the clients (then cleared) and on the client to redraw itemstacks in guis (then cleared)
public HashSet<int> dirtySlots
Field Value
instanceID
the ID of the instance for the inventory.
protected string instanceID
Field Value
lastChangedSinceServerStart
(Not implemented!) The time it was last changed since the server was started.
public long lastChangedSinceServerStart
Field Value
openedByPlayerGUIds
The players that had opened the inventory.
public HashSet<string> openedByPlayerGUIds
Field Value
Properties
ClassName
The class name of the inventory.
public string ClassName { get; }
Property Value
Count
Returns the number of slots in this inventory.
public abstract int Count { get; }
Property Value
CountForNetworkPacket
public virtual int CountForNetworkPacket { get; }
Property Value
DirtySlots
The slots that have been modified server side and need to be resent to the client or need to be redrawn on the client
public HashSet<int> DirtySlots { get; }
Property Value
Empty
Convenience method to check if this inventory contains anything
public virtual bool Empty { get; }
Property Value
FirstNonEmptySlot
Returns the first slot that is not empty or null
public ItemSlot FirstNonEmptySlot { get; }
Property Value
InventoryID
The internal name of the inventory instance.
public string InventoryID { get; }
Property Value
IsDirty
True if this inventory has to be resent to the client or when the client has to redraw them
public virtual bool IsDirty { get; }
Property Value
this[int]
Gets or sets the slot at the given slot number. Returns null for invalid slot number (below 0 or above Count-1). The setter allows for replacing slots with custom ones, though caution is advised.
public abstract ItemSlot this[int slotId] { get; set; }
Parameters
slotId
int
Property Value
LastChanged
Milliseconds since server startup when the inventory was last changed
public long LastChanged { get; }
Property Value
MaxContentDimensions
Optional field, if set, will check against the collectible dimensions and deny placecment if too large
public virtual Size3f MaxContentDimensions { get; set; }
Property Value
PutLocked
Called by item slot, if true, player cannot put items into this inventory
public virtual bool PutLocked { get; set; }
Property Value
RemoveOnClose
If true, the inventory will be removed from the list of available inventories once closed (i.e. is not a personal inventory that the player carries with him)
public virtual bool RemoveOnClose { get; }
Property Value
TakeLocked
Called by item slot, if true, player cannot take items from this inventory
public virtual bool TakeLocked { get; set; }
Property Value
Methods
ActivateSlot(int, ItemSlot, ref ItemStackMoveOperation)
Call when a player has clicked on this slot. The source slot is the mouse cursor slot. This handles the logic of either taking, putting or exchanging items.
public virtual object ActivateSlot(int slotId, ItemSlot sourceSlot, ref ItemStackMoveOperation op)
Parameters
slotId
intsourceSlot
ItemSlotop
ItemStackMoveOperation
Returns
- object
The appropriate packet needed to reflect the changes on the opposing side
AfterBlocksLoaded(IWorldAccessor)
The event fired after all the blocks have loaded.
public virtual void AfterBlocksLoaded(IWorldAccessor world)
Parameters
world
IWorldAccessor
CanContain(ItemSlot, ItemSlot)
public virtual bool CanContain(ItemSlot sinkSlot, ItemSlot sourceSlot)
Parameters
Returns
CanPlayerAccess(IPlayer, EntityPos)
Determines whether or not the player can access the invnetory.
public virtual bool CanPlayerAccess(IPlayer player, EntityPos position)
Parameters
Returns
CanPlayerModify(IPlayer, EntityPos)
Determines whether or not the player can modify the invnetory.
public virtual bool CanPlayerModify(IPlayer player, EntityPos position)
Parameters
Returns
Clear()
Deletes the contents of all the slots
public void Clear()
Close(IPlayer)
Removes ability to interact with this inventory for this player. Returns a close inventory packet that can be sent to the server for synchronization.
public virtual object Close(IPlayer player)
Parameters
player
IPlayer
Returns
DidModifyItemSlot(ItemSlot, ItemStack)
Called when one of the containing slots has been modified
public virtual void DidModifyItemSlot(ItemSlot slot, ItemStack extractedStack = null)
Parameters
DiscardAll()
Discards everything in the item slots.
public virtual void DiscardAll()
DropAll(Vec3d, int)
Drops the contents of all the slots into the world.
public virtual void DropAll(Vec3d pos, int maxStackSize = 0)
Parameters
pos
Vec3dWhere to drop all this stuff.
maxStackSize
intIf non-zero, will split up the stacks into stacks of give max stack size
DropSlotIfHot(ItemSlot, IPlayer)
public virtual void DropSlotIfHot(ItemSlot slot, IPlayer player = null)
Parameters
DropSlots(Vec3d, params int[])
Drops the contents of the specified slots in the world.
public virtual void DropSlots(Vec3d pos, params int[] slotsIds)
Parameters
pos
Vec3dThe position of the inventory attached to the slots.
slotsIds
int[]The slots to have their inventory drop.
FromTreeAttributes(ITreeAttribute)
Called when the game is loaded or loaded from server
public abstract void FromTreeAttributes(ITreeAttribute tree)
Parameters
tree
ITreeAttribute
GenEmptySlots(int)
Gets a specified number of empty slots.
public ItemSlot[] GenEmptySlots(int quantity)
Parameters
quantity
intthe number of empty slots to get.
Returns
- ItemSlot[]
The pre-specified slots.
GetAutoPullFromSlot(BlockFacing)
Return the slot where a chute may pull items from. Return null if it is now allowed to pull any items from this inventory
public virtual ItemSlot GetAutoPullFromSlot(BlockFacing atBlockFace)
Parameters
atBlockFace
BlockFacing
Returns
GetAutoPushIntoSlot(BlockFacing, ItemSlot)
Return the slot where a chute may push items into. Return null if it shouldn't move items into this inventory.
public virtual ItemSlot GetAutoPushIntoSlot(BlockFacing atBlockFace, ItemSlot fromSlot)
Parameters
atBlockFace
BlockFacingfromSlot
ItemSlot
Returns
GetBestSuitedSlot(ItemSlot, List<ItemSlot>)
[Obsolete("Use GetBestSuitedSlot(ItemSlot sourceSlot, ItemStackMoveOperation op, List<ItemSlot> skipSlots = null) instead")]
public WeightedSlot GetBestSuitedSlot(ItemSlot sourceSlot, List<ItemSlot> skipSlots)
Parameters
Returns
GetBestSuitedSlot(ItemSlot, ItemStackMoveOperation, List<ItemSlot>)
Gets the best sorted slot for the given item.
public virtual WeightedSlot GetBestSuitedSlot(ItemSlot sourceSlot, ItemStackMoveOperation op = null, List<ItemSlot> skipSlots = null)
Parameters
sourceSlot
ItemSlotThe source item slot.
op
ItemStackMoveOperationCan be null. If provided allows the inventory to make a better guess at suitability
skipSlots
List<ItemSlot>The slots to skip.
Returns
- WeightedSlot
A weighted slot set.
GetDefaultTransitionSpeedMul(EnumTransitionType)
protected virtual float GetDefaultTransitionSpeedMul(EnumTransitionType transType)
Parameters
transType
EnumTransitionType
Returns
GetEnumerator()
Gets the enumerator for the inventory.
public IEnumerator<ItemSlot> GetEnumerator()
Returns
GetSlotId(ItemSlot)
Will return -1 if the slot is not found in this inventory
public virtual int GetSlotId(ItemSlot slot)
Parameters
slot
ItemSlot
Returns
GetSlotsIfExists(IPlayer, string[], int[])
Attempts to get specified slots if the slots exists.
public virtual ItemSlot[] GetSlotsIfExists(IPlayer player, string[] invIds, int[] slotIds)
Parameters
player
IPlayerThe player owning the slots
invIds
string[]The inventory IDs
slotIds
int[]The slot ids
Returns
- ItemSlot[]
The slots obtained.
GetSuitability(ItemSlot, ItemSlot, bool)
How well a stack fits into this inventory. By default 1 for new itemstacks and 3 for an itemstack merge. Chests and other stationary container also add a +1 to the suitability if the source slot is from the players inventory.
public virtual float GetSuitability(ItemSlot sourceSlot, ItemSlot targetSlot, bool isMerge)
Parameters
Returns
GetTransitionSpeedMul(EnumTransitionType, ItemStack)
Does this inventory speed up or slow down a transition for given itemstack? (Default: 1 for perish and 0 otherwise)
public virtual float GetTransitionSpeedMul(EnumTransitionType transType, ItemStack stack)
Parameters
transType
EnumTransitionTypestack
ItemStack
Returns
HasOpened(IPlayer)
Checks if given player has this inventory currently opened
public virtual bool HasOpened(IPlayer player)
Parameters
player
IPlayer
Returns
InvokeTransitionSpeedDelegates(EnumTransitionType, ItemStack, float)
public float InvokeTransitionSpeedDelegates(EnumTransitionType transType, ItemStack stack, float mul)
Parameters
transType
EnumTransitionTypestack
ItemStackmul
float
Returns
LateInitialize(string, ICoreAPI)
You can initialize an InventoryBase with null as parameters and use LateInitialize to set these values later. This is sometimes required during chunk loading.
public virtual void LateInitialize(string inventoryID, ICoreAPI api)
Parameters
MarkSlotDirty(int)
Server Side: Will resent the slot contents to the client and mark them dirty there as well Client Side: Will refresh stack size, model and stuff if this stack is currently being rendered
public virtual void MarkSlotDirty(int slotId)
Parameters
slotId
int
NewSlot(int)
A command to build a new empty slot.
protected virtual ItemSlot NewSlot(int i)
Parameters
i
intthe index of the slot.
Returns
- ItemSlot
An empty slot bound to this inventory.
OnItemSlotModified(ItemSlot)
Called when one of the containing slots has been modified
public virtual void OnItemSlotModified(ItemSlot slot)
Parameters
slot
ItemSlot
OnOwningEntityDeath(Vec3d)
public virtual void OnOwningEntityDeath(Vec3d pos)
Parameters
pos
Vec3d
OnSearchTerm(string)
The event fired when the search is applied to the item.
public virtual void OnSearchTerm(string text)
Parameters
text
string
Open(IPlayer)
Marks the inventory available for interaction for this player. Returns a open inventory packet that can be sent to the server for synchronization.
public virtual object Open(IPlayer player)
Parameters
player
IPlayer
Returns
PerformNotifySlot(int)
Called when one of the containing slot was notified via NotifySlot
public virtual void PerformNotifySlot(int slotId)
Parameters
slotId
int
ResolveBlocksOrItems()
Tells the invnetory to update blocks and items within the invnetory.
public virtual void ResolveBlocksOrItems()
SlotsFromTreeAttributes(ITreeAttribute, ItemSlot[], List<ItemSlot>)
Creates a collection of slots from a tree.
public virtual ItemSlot[] SlotsFromTreeAttributes(ITreeAttribute tree, ItemSlot[] slots = null, List<ItemSlot> modifiedSlots = null)
Parameters
tree
ITreeAttributeThe tree to build slots from
slots
ItemSlot[]pre-existing slots. (default: null)
modifiedSlots
List<ItemSlot>Pre-modified slots. (default: null)
Returns
- ItemSlot[]
SlotsToTreeAttributes(ItemSlot[], ITreeAttribute)
Sets the tree attribute using the slots.
public void SlotsToTreeAttributes(ItemSlot[] slots, ITreeAttribute tree)
Parameters
slots
ItemSlot[]tree
ITreeAttribute
ToTreeAttributes(ITreeAttribute)
Called when the game is saved or sent to client
public abstract void ToTreeAttributes(ITreeAttribute tree)
Parameters
tree
ITreeAttribute
TryFlipItemStack(IPlayer, string[], int[], long[])
Attempts to flip the inventory slots.
public virtual bool TryFlipItemStack(IPlayer owningPlayer, string[] invIds, int[] slotIds, long[] lastChanged)
Parameters
owningPlayer
IPlayerThe player owner of the invnetory slots.
invIds
string[]The IDs of the player inventory.
slotIds
int[]The IDs of the target inventory.
lastChanged
long[]The times these ids were last changed.
Returns
TryFlipItems(int, ItemSlot)
Attempts to flip the contents of both slots
public object TryFlipItems(int targetSlotId, ItemSlot itemSlot)
Parameters
Returns
TryMoveItemStack(IPlayer, string[], int[], ref ItemStackMoveOperation)
Attempts to move the item stack from the inventory to another slot.
public virtual bool TryMoveItemStack(IPlayer player, string[] invIds, int[] slotIds, ref ItemStackMoveOperation op)
Parameters
player
IPlayerThe player moving the items
invIds
string[]The player inventory IDs
slotIds
int[]The target Ids
op
ItemStackMoveOperationThe operation type.
Returns
Events
OnAcquireTransitionSpeed
If set, the value is returned when GetTransitionSpeedMul() is called instead of the default value.
public event CustomGetTransitionSpeedMulDelegate OnAcquireTransitionSpeed
Event Type
OnInventoryClosed
Called whenever this inventory was closed
public event OnInventoryClosedDelegate OnInventoryClosed
Event Type
OnInventoryOpened
Called whenever this inventory was opened
public event OnInventoryOpenedDelegate OnInventoryOpened
Event Type
SlotModified
Called whenever a slot has been modified
public event Action<int> SlotModified
Event Type
SlotNotified
Called whenever a slot notification event has been fired. Is used by the slot grid gui element to visually wiggle the slot contents
public event Action<int> SlotNotified