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