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

     1  package enchantment
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/item"
     5  	"github.com/df-mc/dragonfly/server/world"
     6  	"math/rand"
     7  )
     8  
     9  // Unbreaking is an enchantment that gives a chance for an item to avoid durability reduction when it
    10  // is used, effectively increasing the item's durability.
    11  type Unbreaking struct{}
    12  
    13  // Name ...
    14  func (Unbreaking) Name() string {
    15  	return "Unbreaking"
    16  }
    17  
    18  // MaxLevel ...
    19  func (Unbreaking) MaxLevel() int {
    20  	return 3
    21  }
    22  
    23  // Cost ...
    24  func (Unbreaking) Cost(level int) (int, int) {
    25  	min := 5 + 8*(level-1)
    26  	return min, min + 50
    27  }
    28  
    29  // Rarity ...
    30  func (Unbreaking) Rarity() item.EnchantmentRarity {
    31  	return item.EnchantmentRarityUncommon
    32  }
    33  
    34  // CompatibleWithEnchantment ...
    35  func (Unbreaking) CompatibleWithEnchantment(item.EnchantmentType) bool {
    36  	return true
    37  }
    38  
    39  // CompatibleWithItem ...
    40  func (Unbreaking) CompatibleWithItem(i world.Item) bool {
    41  	_, ok := i.(item.Durable)
    42  	return ok
    43  }
    44  
    45  // Reduce returns the amount of damage that should be reduced with unbreaking.
    46  func (Unbreaking) Reduce(it world.Item, level, amount int) int {
    47  	after := amount
    48  	_, ok := it.(item.Armour)
    49  	for i := 0; i < amount; i++ {
    50  		if (!ok || rand.Float64() >= 0.6) && rand.Intn(level+1) > 0 {
    51  			after--
    52  		}
    53  	}
    54  	return after
    55  }