Documentation ¶
Index ¶
- Variables
- type Armour
- func (a *Armour) Boots() item.Stack
- func (a *Armour) Chestplate() item.Stack
- func (a *Armour) Clear()
- func (a *Armour) Close() error
- func (a *Armour) Helmet() item.Stack
- func (a *Armour) Inv() *Inventory
- func (a *Armour) Items() []item.Stack
- func (a *Armour) Leggings() item.Stack
- func (a *Armour) SetBoots(boots item.Stack)
- func (a *Armour) SetChestplate(chestplate item.Stack)
- func (a *Armour) SetHelmet(helmet item.Stack)
- func (a *Armour) SetLeggings(leggings item.Stack)
- func (a *Armour) String() string
- type Handler
- type Inventory
- func (inv *Inventory) AddItem(it item.Stack) (n int, err error)
- func (inv *Inventory) Clear()
- func (inv *Inventory) Close() error
- func (inv *Inventory) Contents() []item.Stack
- func (inv *Inventory) Empty() bool
- func (inv *Inventory) First(item item.Stack) (int, bool)
- func (inv *Inventory) FirstEmpty() (int, bool)
- func (inv *Inventory) Handle(h Handler)
- func (inv *Inventory) Handler() Handler
- func (inv *Inventory) Item(slot int) (item.Stack, error)
- func (inv *Inventory) Items() []item.Stack
- func (inv *Inventory) LockSlot(slot int)
- func (inv *Inventory) RemoveItem(it item.Stack) error
- func (inv *Inventory) SetItem(slot int, item item.Stack) error
- func (inv *Inventory) Size() int
- func (inv *Inventory) SlotLocked(slot int) bool
- func (inv *Inventory) String() string
- func (inv *Inventory) Swap(slotA, slotB int) error
- func (inv *Inventory) UnlockSlot(slot int)
- type NopHandler
Constants ¶
This section is empty.
Variables ¶
var ErrSlotLocked = errors.New("slot is locked and cannot be edited")
ErrSlotLocked is returned by a call to SetItem if the slot passed is locked and cannot be edited.
var ErrSlotOutOfRange = errors.New("slot is out of range: must be in range 0 <= slot < inventory.Size()")
ErrSlotOutOfRange is returned by any methods on inventory when a slot is passed which is not within the range of valid values for the inventory.
Functions ¶
This section is empty.
Types ¶
type Armour ¶
type Armour struct {
// contains filtered or unexported fields
}
Armour represents an inventory for armour. It has 4 slots, one for a helmet, chestplate, leggings and boots respectively. NewArmour() must be used to create a valid armour inventory. Armour inventories, like normal Inventories, are safe for concurrent usage.
func NewArmour ¶
NewArmour returns an armour inventory that is ready to be used. The zero value of an inventory.Armour is not valid for usage. The function passed is called when a slot is changed. It may be nil to not call anything.
func (*Armour) Chestplate ¶
Chestplate returns the item stack set as chestplate in the inventory.
func (*Armour) Clear ¶
func (a *Armour) Clear()
Clear clears the armour inventory, removing all items currently present.
func (*Armour) Items ¶ added in v0.1.0
Items returns all items (including) air of the armour inventory in the order of helmet, chestplate, leggings, boots.
func (*Armour) SetChestplate ¶
SetChestplate sets the item stack passed as the chestplate in the inventory.
func (*Armour) SetLeggings ¶
SetLeggings sets the item stack passed as the leggings in the inventory.
type Handler ¶ added in v0.3.0
type Handler interface { // HandleTake handles an item.Stack being taken from a slot in the inventory. This item might be the whole stack or // part of the stack currently present in that slot. HandleTake(ctx *event.Context, slot int, it item.Stack) // HandlePlace handles an item.Stack being placed in a slot of the inventory. It might either be added to an empty // slot or a slot that contains an item of the same type. HandlePlace(ctx *event.Context, slot int, it item.Stack) // HandleDrop handles the dropping of an item.Stack in a slot out of the inventory. HandleDrop(ctx *event.Context, slot int, it item.Stack) }
Handler is a type that may be used to handle actions performed on an inventory by a player.
type Inventory ¶
type Inventory struct {
// contains filtered or unexported fields
}
Inventory represents an inventory containing items. These inventories may be carried by entities or may be held by blocks such as chests. The size of an inventory may be specified upon construction, but cannot be changed after. The zero value of an inventory is invalid. Use New() to obtain a new inventory. Inventory is safe for concurrent usage: Its values are protected by a mutex.
func New ¶
New creates a new inventory with the size passed. The inventory size cannot be changed after it has been constructed. A function may be passed which is called every time a slot is changed. The function may also be nil, if nothing needs to be done.
func (*Inventory) AddItem ¶
AddItem attempts to add an item to the inventory. It does so in a couple of steps: It first iterates over the inventory to make sure no existing stacks of the same type exist. If these stacks do exist, the item added is first added on top of those stacks to make sure they are fully filled. If no existing stacks with leftover space are left, empty slots will be filled up with the remainder of the item added. If the item could not be fully added to the inventory, an error is returned along with the count that was added to the inventory.
func (*Inventory) Clear ¶
func (inv *Inventory) Clear()
Clear clears the entire inventory. Items items are removed, except for items in locked slots.
func (*Inventory) Close ¶
Close closes the inventory, freeing the function called for every slot change. It also clears any items that may currently be in the inventory. The returned error is always nil.
func (*Inventory) Contents ¶
Contents returns a list of all contents of the inventory. This method excludes air items, so the method only ever returns item stacks which actually represent an item.
func (*Inventory) Empty ¶
Empty checks if the inventory is fully empty: It iterates over the inventory and makes sure every stack in it is empty.
func (*Inventory) First ¶
First returns the first slot with an item if found. Second return value describes whether the item was found.
func (*Inventory) FirstEmpty ¶
FirstEmpty returns the first empty slot if found. Second return value describes whether an empty slot was found.
func (*Inventory) Handle ¶ added in v0.3.0
Handle assigns a Handler to an Inventory so that its methods are called for the respective events. Nil may be passed to set the default NopHandler.
func (*Inventory) Handler ¶ added in v0.3.0
Handler returns the Handler currently assigned to the Inventory. This is the NopHandler by default.
func (*Inventory) Item ¶
Item attempts to obtain an item from a specific slot in the inventory. If an item was present in that slot, the item is returned and the error is nil. If no item was present in the slot, a Stack with air as its item and a count of 0 is returned. Stack.Empty() may be called to check if this is the case. Item only returns an error if the slot passed is out of range. (0 <= slot < inventory.Size())
func (*Inventory) Items ¶ added in v0.1.0
Items returns the full content of the inventory, copying all items into a new slice.
func (*Inventory) LockSlot ¶
LockSlot locks a slot in the inventory at the offset passed, so that setting items to it will return an error.
func (*Inventory) RemoveItem ¶
RemoveItem attempts to remove an item from the inventory. It will visit all slots in the inventory and empties them until it.Count() items have been removed from the inventory. If less than it.Count() items could be found in the inventory, an error is returned.
func (*Inventory) SetItem ¶
SetItem sets a stack of items to a specific slot in the inventory. If an item is already present in the slot, that item will be overwritten. SetItem will return an error if the slot passed is out of range. (0 <= slot < inventory.Size())
func (*Inventory) Size ¶
Size returns the size of the inventory. It is always the same value as that passed in the call to New() and is always at least 1.
func (*Inventory) SlotLocked ¶
SlotLocked checks if the slot passed is currently locked.
func (*Inventory) Swap ¶
Swap swaps the items between two slots. Returns an error if either slot A or B are invalid.
func (*Inventory) UnlockSlot ¶
UnlockSlot unlocks a slot after having called LockSlot, so that calling SetItem on the slot will work again.
type NopHandler ¶ added in v0.3.0
type NopHandler struct{}
NopHandler is an implementation of Handler that does not run any code in any of its methods. It is the default Handler of an Inventory.