github.com/df-mc/dragonfly@v0.9.13/server/item/enchantment.go (about) 1 package item 2 3 import ( 4 "github.com/df-mc/dragonfly/server/world" 5 "golang.org/x/exp/maps" 6 ) 7 8 // Enchantment is an enchantment that can be applied to a Stack. It holds an EnchantmentType and level that influences 9 // the power of the enchantment. 10 type Enchantment struct { 11 t EnchantmentType 12 lvl int 13 } 14 15 // NewEnchantment creates and returns an Enchantment with a specific EnchantmentType and level. If the level passed 16 // exceeds EnchantmentType.MaxLevel, NewEnchantment panics. 17 func NewEnchantment(t EnchantmentType, lvl int) Enchantment { 18 if lvl < 1 { 19 panic("enchantment level must never be below 1") 20 } 21 return Enchantment{t: t, lvl: lvl} 22 } 23 24 // Level returns the current level of the Enchantment as passed to NewEnchantment upon construction. 25 func (e Enchantment) Level() int { 26 return e.lvl 27 } 28 29 // Type returns the EnchantmentType of the Enchantment as passed to NewEnchantment upon construction. 30 func (e Enchantment) Type() EnchantmentType { 31 return e.t 32 } 33 34 // EnchantmentType represents an enchantment type that can be applied to a Stack, with specific behaviour that modifies 35 // the Stack's behaviour. 36 // An instance of an EnchantmentType may be created using NewEnchantment. 37 type EnchantmentType interface { 38 // Name returns the name of the enchantment. 39 Name() string 40 // MaxLevel returns the maximum level the enchantment should be able to have. 41 MaxLevel() int 42 // Cost returns the minimum and maximum cost the enchantment may inhibit. The higher this range is, the more likely 43 // better enchantments are to be selected. 44 Cost(level int) (int, int) 45 // Rarity returns the enchantment's rarity. 46 Rarity() EnchantmentRarity 47 // CompatibleWithEnchantment is called when an enchantment is added to an item. It can be used to check if 48 // the enchantment is compatible with other enchantments. 49 CompatibleWithEnchantment(t EnchantmentType) bool 50 // CompatibleWithItem is also called when an enchantment is added to an item. It can be used to check if 51 // the enchantment is compatible with the item type. 52 CompatibleWithItem(i world.Item) bool 53 } 54 55 // Enchantable is an interface that can be implemented by items that can be enchanted through an enchanting table. 56 type Enchantable interface { 57 // EnchantmentValue returns the value the item may inhibit on possible enchantments. 58 EnchantmentValue() int 59 } 60 61 // RegisterEnchantment registers an enchantment with the ID passed. Once registered, enchantments may be received 62 // by instantiating an EnchantmentType struct (e.g. enchantment.Protection{}) 63 func RegisterEnchantment(id int, enchantment EnchantmentType) { 64 enchantmentsMap[id] = enchantment 65 enchantmentIDs[enchantment] = id 66 } 67 68 var ( 69 enchantmentsMap = map[int]EnchantmentType{} 70 enchantmentIDs = map[EnchantmentType]int{} 71 ) 72 73 // EnchantmentByID attempts to return an enchantment by the ID it was registered with. If found, the enchantment found 74 // is returned and the bool true. 75 func EnchantmentByID(id int) (EnchantmentType, bool) { 76 e, ok := enchantmentsMap[id] 77 return e, ok 78 } 79 80 // EnchantmentID attempts to return the ID the enchantment was registered with. If found, the id is returned and 81 // the bool true. 82 func EnchantmentID(e EnchantmentType) (int, bool) { 83 id, ok := enchantmentIDs[e] 84 return id, ok 85 } 86 87 // Enchantments returns a slice of all registered enchantments. 88 func Enchantments() []EnchantmentType { 89 return maps.Values(enchantmentsMap) 90 }