Table of Contents

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

className string
instanceID string
api ICoreAPI

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

inventoryID string
api ICoreAPI

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

ICoreAPI

InvNetworkUtil

The network utility for the inventory

public IInventoryNetworkUtil InvNetworkUtil

Field Value

IInventoryNetworkUtil

OnAcquireTransitionSpeed

If set, the value is returned when GetTransitionSpeedMul() is called instead of the default value.

public CustomGetTransitionSpeedMulDelegate OnAcquireTransitionSpeed

Field Value

CustomGetTransitionSpeedMulDelegate

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

BlockPos

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

float

className

The name of the class for the invnentory.

protected string className

Field Value

string

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

HashSet<int>

instanceID

the ID of the instance for the inventory.

protected string instanceID

Field Value

string

lastChangedSinceServerStart

(Not implemented!) The time it was last changed since the server was started.

public long lastChangedSinceServerStart

Field Value

long

openedByPlayerGUIds

The players that had opened the inventory.

public HashSet<string> openedByPlayerGUIds

Field Value

HashSet<string>

Properties

ClassName

The class name of the inventory.

public string ClassName { get; }

Property Value

string

Count

Returns the number of slots in this inventory.

public abstract int Count { get; }

Property Value

int

CountForNetworkPacket

public virtual int CountForNetworkPacket { get; }

Property Value

int

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

HashSet<int>

Empty

Convenience method to check if this inventory contains anything

public virtual bool Empty { get; }

Property Value

bool

FirstNonEmptySlot

Returns the first slot that is not empty or null

public ItemSlot FirstNonEmptySlot { get; }

Property Value

ItemSlot

InventoryID

The internal name of the inventory instance.

public string InventoryID { get; }

Property Value

string

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

bool

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

ItemSlot

LastChanged

Milliseconds since server startup when the inventory was last changed

public long LastChanged { get; }

Property Value

long

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

Size3f

PutLocked

Called by item slot, if true, player cannot put items into this inventory

public virtual bool PutLocked { get; set; }

Property Value

bool

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

bool

TakeLocked

Called by item slot, if true, player cannot take items from this inventory

public virtual bool TakeLocked { get; set; }

Property Value

bool

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 int
sourceSlot ItemSlot
op 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

sinkSlot ItemSlot
sourceSlot ItemSlot

Returns

bool

CanPlayerAccess(IPlayer, EntityPos)

Determines whether or not the player can access the invnetory.

public virtual bool CanPlayerAccess(IPlayer player, EntityPos position)

Parameters

player IPlayer

The player attempting access.

position EntityPos

The postion of the entity.

Returns

bool

CanPlayerModify(IPlayer, EntityPos)

Determines whether or not the player can modify the invnetory.

public virtual bool CanPlayerModify(IPlayer player, EntityPos position)

Parameters

player IPlayer

The player attempting access.

position EntityPos

The postion of the entity.

Returns

bool

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

object

DidModifyItemSlot(ItemSlot, ItemStack)

Called when one of the containing slots has been modified

public virtual void DidModifyItemSlot(ItemSlot slot, ItemStack extractedStack = null)

Parameters

slot ItemSlot
extractedStack ItemStack

If non null the itemstack that was taken out

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 Vec3d

Where to drop all this stuff.

maxStackSize int

If 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

slot ItemSlot
player IPlayer

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 Vec3d

The 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 int

the 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

ItemSlot

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 BlockFacing
fromSlot ItemSlot

Returns

ItemSlot

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

sourceSlot ItemSlot
skipSlots List<ItemSlot>

Returns

WeightedSlot

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 ItemSlot

The source item slot.

op ItemStackMoveOperation

Can 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

float

GetEnumerator()

Gets the enumerator for the inventory.

public IEnumerator<ItemSlot> GetEnumerator()

Returns

IEnumerator<ItemSlot>

GetSlotId(ItemSlot)

Will return -1 if the slot is not found in this inventory

public virtual int GetSlotId(ItemSlot slot)

Parameters

slot ItemSlot

Returns

int

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 IPlayer

The 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

sourceSlot ItemSlot
targetSlot ItemSlot
isMerge bool

Returns

float

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 EnumTransitionType
stack ItemStack

Returns

float

HasOpened(IPlayer)

Checks if given player has this inventory currently opened

public virtual bool HasOpened(IPlayer player)

Parameters

player IPlayer

Returns

bool

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

inventoryID string
api ICoreAPI

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 int

the 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

object

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 ITreeAttribute

The 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 IPlayer

The 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

bool

TryFlipItems(int, ItemSlot)

Attempts to flip the contents of both slots

public object TryFlipItems(int targetSlotId, ItemSlot itemSlot)

Parameters

targetSlotId int
itemSlot ItemSlot

Returns

object

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 IPlayer

The player moving the items

invIds string[]

The player inventory IDs

slotIds int[]

The target Ids

op ItemStackMoveOperation

The operation type.

Returns

bool

Events

OnInventoryClosed

Called whenever this inventory was closed

public event OnInventoryClosedDelegate OnInventoryClosed

Event Type

OnInventoryClosedDelegate

OnInventoryOpened

Called whenever this inventory was opened

public event OnInventoryOpenedDelegate OnInventoryOpened

Event Type

OnInventoryOpenedDelegate

SlotModified

Called whenever a slot has been modified

public event Action<int> SlotModified

Event Type

Action<int>

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

Event Type

Action<int>