inventory

package
v0.0.0-...-183dbea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func NewArmour(f func(slot int, before, after item.Stack)) *Armour

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) Boots

func (a *Armour) Boots() item.Stack

Boots returns the item stack set as boots in the inventory.

func (*Armour) Chestplate

func (a *Armour) Chestplate() item.Stack

Chestplate returns the item stack set as chestplate in the inventory.

func (*Armour) Clear

func (a *Armour) Clear() []item.Stack

Clear clears the armour inventory, removing all items currently present.

func (*Armour) Close

func (a *Armour) Close() error

Close closes the armour inventory, removing the slot change function.

func (*Armour) Damage

func (a *Armour) Damage(dmg float64, f DamageFunc)

Damage deals damage (hearts) to Armour. The resulting item damage depends on the dmg passed and the DamageFunc used.

func (*Armour) DamageReduction

func (a *Armour) DamageReduction(dmg float64, src world.DamageSource) float64

DamageReduction returns the amount of damage that is reduced by the Armour for an amount of damage and damage source. The value returned takes into account the armour itself and its enchantments.

func (*Armour) Handle

func (a *Armour) Handle(h Handler)

Handle assigns a Handler to an Armour inventory so that its methods are called for the respective events. Nil may be passed to set the default NopHandler. Handle is the equivalent of calling (*Armour).Inventory().Handle.

func (*Armour) Helmet

func (a *Armour) Helmet() item.Stack

Helmet returns the item stack set as helmet in the inventory.

func (*Armour) HighestEnchantmentLevel

func (a *Armour) HighestEnchantmentLevel(t item.EnchantmentType) int

HighestEnchantmentLevel looks up the highest level of an item.EnchantmentType that any of the Armour items have and returns it, or 0 if none of the items have the enchantment.

func (*Armour) Inventory

func (a *Armour) Inventory() *Inventory

Inventory returns the underlying Inventory instance.

func (*Armour) Items

func (a *Armour) Items() []item.Stack

Items returns a slice of all non-empty armour items equipped.

func (*Armour) KnockBackResistance

func (a *Armour) KnockBackResistance() float64

KnockBackResistance returns the combined knock back resistance of all Armour items. A value of 0 means normal knock back force, while a value of 1 means all knock back is ignored.

func (*Armour) Leggings

func (a *Armour) Leggings() item.Stack

Leggings returns the item stack set as leggings in the inventory.

func (*Armour) Set

func (a *Armour) Set(helmet, chestplate, leggings, boots item.Stack)

Set sets all individual pieces of armour in one go. It is equivalent to calling SetHelmet, SetChestplate, SetLeggings and SetBoots sequentially.

func (*Armour) SetBoots

func (a *Armour) SetBoots(boots item.Stack)

SetBoots sets the item stack passed as the boots in the inventory.

func (*Armour) SetChestplate

func (a *Armour) SetChestplate(chestplate item.Stack)

SetChestplate sets the item stack passed as the chestplate in the inventory.

func (*Armour) SetHelmet

func (a *Armour) SetHelmet(helmet item.Stack)

SetHelmet sets the item stack passed as the helmet in the inventory.

func (*Armour) SetLeggings

func (a *Armour) SetLeggings(leggings item.Stack)

SetLeggings sets the item stack passed as the leggings in the inventory.

func (*Armour) Slots

func (a *Armour) Slots() []item.Stack

Slots returns all items (including) air of the armour inventory in the order of helmet, chestplate, leggings, boots.

func (*Armour) String

func (a *Armour) String() string

String converts the armour to a readable string representation.

func (*Armour) ThornsDamage

func (a *Armour) ThornsDamage(f DamageFunc) float64

ThornsDamage checks if any of the Armour items are enchanted with Thorns. If this is the case and the Thorns enchantment activates (15% chance per level), a random Armour piece is damaged. The damage to be dealt to the attacker is returned.

type DamageFunc

type DamageFunc func(s item.Stack, d int) item.Stack

DamageFunc is a function that deals d damage points to an item stack s. The resulting item.Stack is returned. Depending on the game mode of a player, damage may not be dealt at all.

type Handler

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

func New(size int, f func(slot int, before, after item.Stack)) *Inventory

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

func (inv *Inventory) AddItem(it item.Stack) (n int, err error)

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() []item.Stack

Clear clears the entire inventory. All non-zero items are returned.

func (*Inventory) Close

func (inv *Inventory) Close() error

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) ContainsItem

func (inv *Inventory) ContainsItem(it item.Stack) bool

ContainsItem checks if the Inventory contains an item.Stack. It will visit all slots in the Inventory until it finds at enough items. If enough were found, true is returned.

func (*Inventory) ContainsItemFunc

func (inv *Inventory) ContainsItemFunc(n int, comparable func(stack item.Stack) bool) bool

ContainsItemFunc checks if the Inventory contains at least n items. It will visit all slots in the Inventory until it finds n items on which the comparable function returns true. ContainsItemFunc returns true if this is the case.

func (*Inventory) Empty

func (inv *Inventory) Empty() bool

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

func (inv *Inventory) First(item item.Stack) (int, bool)

First returns the first slot with an item if found. Second return value describes whether the item was found.

func (*Inventory) FirstEmpty

func (inv *Inventory) FirstEmpty() (int, bool)

FirstEmpty returns the first empty slot if found. Second return value describes whether an empty slot was found.

func (*Inventory) FirstFunc

func (inv *Inventory) FirstFunc(comparable func(stack item.Stack) bool) (int, bool)

FirstFunc finds the first slot with an item.Stack that results in the comparable function passed returning true. The function returns false if no such item was found.

func (*Inventory) Handle

func (inv *Inventory) Handle(h Handler)

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

func (inv *Inventory) Handler() Handler

Handler returns the Handler currently assigned to the Inventory. This is the NopHandler by default.

func (*Inventory) Item

func (inv *Inventory) Item(slot int) (item.Stack, error)

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

func (inv *Inventory) Items() []item.Stack

Items 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) RemoveItem

func (inv *Inventory) RemoveItem(it item.Stack) error

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 were removed from the inventory, an error is returned.

func (*Inventory) RemoveItemFunc

func (inv *Inventory) RemoveItemFunc(n int, comparable func(stack item.Stack) bool) error

RemoveItemFunc removes up to n items from the Inventory. It will visit all slots in the inventory and empties them until n items have been removed from the inventory, assuming the comparable function returns true for the slots visited. No items will be deducted from slots if the comparable function returns false. If less than n items were removed, an error is returned.

func (*Inventory) SetItem

func (inv *Inventory) SetItem(slot int, item item.Stack) error

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

func (inv *Inventory) Size() int

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) Slots

func (inv *Inventory) Slots() []item.Stack

Slots returns the all slots in the inventory as a slice. The index in the slice is the slot of the inventory that a specific item.Stack is in. Note that this item.Stack might be empty.

func (*Inventory) String

func (inv *Inventory) String() string

String implements the fmt.Stringer interface.

func (*Inventory) Swap

func (inv *Inventory) Swap(slotA, slotB int) error

Swap swaps the items between two slots. Returns an error if either slot A or B are invalid.

type NopHandler

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.

func (NopHandler) HandleDrop

func (NopHandler) HandleDrop(*event.Context, int, item.Stack)

func (NopHandler) HandlePlace

func (NopHandler) HandlePlace(*event.Context, int, item.Stack)

func (NopHandler) HandleTake

func (NopHandler) HandleTake(*event.Context, int, item.Stack)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL