github.com/df-mc/dragonfly@v0.9.13/server/item/use.go (about)

     1  package item
     2  
     3  // UseContext is passed to every item Use methods. It may be used to subtract items or to deal damage to them
     4  // after the action is complete.
     5  type UseContext struct {
     6  	// Damage is the amount of damage that should be dealt to the item as a result of using it.
     7  	Damage int
     8  	// CountSub is how much of the count should be subtracted after using the item.
     9  	CountSub int
    10  	// IgnoreBBox specifies if placing the item should ignore the BBox of the player placing this. This is the case for
    11  	// items such as cocoa beans.
    12  	IgnoreBBox bool
    13  	// NewItem is the item that is added after the item is used. If the player no longer has an item in the
    14  	// hand, it'll be added there.
    15  	NewItem Stack
    16  	// ConsumedItems contains a list of items that were consumed in the process of using the item.
    17  	ConsumedItems []Stack
    18  	// NewItemSurvivalOnly will add any new items only in survival mode.
    19  	NewItemSurvivalOnly bool
    20  
    21  	// FirstFunc returns the first item in the context holder's inventory if found. The second return value describes
    22  	// whether the item was found. The comparable function is used to compare the item to the given item.
    23  	FirstFunc func(comparable func(Stack) bool) (Stack, bool)
    24  
    25  	// SwapHeldWithArmour holds a function that swaps the item currently held by a User with armour slot i.
    26  	SwapHeldWithArmour func(i int)
    27  }
    28  
    29  // Consume consumes the provided item when the context is handled.
    30  func (ctx *UseContext) Consume(s Stack) {
    31  	ctx.ConsumedItems = append(ctx.ConsumedItems, s)
    32  }
    33  
    34  // DamageItem damages the item used by d points.
    35  func (ctx *UseContext) DamageItem(d int) { ctx.Damage += d }
    36  
    37  // SubtractFromCount subtracts d from the count of the item stack used.
    38  func (ctx *UseContext) SubtractFromCount(d int) { ctx.CountSub += d }