Skip to content

server.inventory

fn new #

fn new() Inventory

new returns an empty Inventory.

struct Inventory #

@[heap]
struct Inventory {
mut:
	mutex   &sync.Mutex = sync.new_mutex()
	stacks  map[int]types.ItemStack
	slots   map[int]int
	next_id int = 1
}

Inventory holds the item stacks a player carries. A stack is tracked by the network id the client knows it as, and a slot points at the network id it currently holds, so a stack keeps its identity while it moves between slots.

Every method is safe to call from several threads: the inventory carries its own lock rather than borrowing the owner's.

fn (Inventory) stack #

fn (inv &Inventory) stack(net_id int) ?types.ItemStack

stack returns the stack tracked under net_id, or none if that id is unknown.

fn (Inventory) put_stack #

fn (mut inv Inventory) put_stack(net_id int, stack types.ItemStack)

put_stack stores stack under net_id, replacing whatever that id tracked.

fn (Inventory) delete_stack #

fn (mut inv Inventory) delete_stack(net_id int)

delete_stack stops tracking the stack under net_id.

fn (Inventory) track_stack #

fn (mut inv Inventory) track_stack(stack types.ItemStack) int

track_stack stores stack under a freshly allocated network id and returns that id. It is the only place the id counter advances.

fn (Inventory) slot #

fn (inv &Inventory) slot(slot int) ?int

slot returns the network id held by slot, or none if the slot is empty.

fn (Inventory) has_slot #

fn (inv &Inventory) has_slot(slot int) bool

has_slot reports whether slot currently holds a stack.

fn (Inventory) set_slot #

fn (mut inv Inventory) set_slot(slot int, net_id int)

set_slot points slot at the stack tracked under net_id.

fn (Inventory) delete_slot #

fn (mut inv Inventory) delete_slot(slot int)

delete_slot empties slot, leaving the stack itself tracked.

fn (Inventory) snapshot_slot_stacks #

fn (inv &Inventory) snapshot_slot_stacks() map[int]types.ItemStack

snapshot_slot_stacks returns the slot layout with the network id indirection already resolved, so callers see one consistent view.

fn (Inventory) clear #

fn (mut inv Inventory) clear()

clear drops every stack and every slot, leaving the id counter alone so stacks handed out earlier can never be confused with new ones.