server.enchant
Constants #
const custom_enchantment_id_start = 256
Vanilla enchantment ids end below this; plugin enchantments allocate from here up.
const id_protection = 0
const id_fire_protection = 1
const id_feather_falling = 2
const id_blast_protection = 3
const id_projectile_protection = 4
const id_thorns = 5
const id_respiration = 6
const id_depth_strider = 7
const id_aqua_affinity = 8
const id_sharpness = 9
const id_smite = 10
const id_bane_of_arthropods = 11
const id_knockback = 12
const id_fire_aspect = 13
const id_looting = 14
const id_efficiency = 15
const id_silk_touch = 16
const id_unbreaking = 17
const id_fortune = 18
const id_power = 19
const id_punch = 20
const id_flame = 21
const id_infinity = 22
const id_luck_of_the_sea = 23
const id_lure = 24
const id_frost_walker = 25
const id_mending = 26
const id_curse_of_binding = 27
const id_curse_of_vanishing = 28
const id_impaling = 29
const id_riptide = 30
const id_loyalty = 31
const id_channeling = 32
const id_multishot = 33
const id_piercing = 34
const id_quick_charge = 35
const id_soul_speed = 36
const id_swift_sneak = 37
fn attack_bonus #
fn attack_bonus(applied []Applied) f32
fn ench_nbt #
fn ench_nbt(entries []Applied) nbt.Tag
ench_nbt builds the Bedrock 'ench' list tag for an item stack: one compound per enchantment with short id and lvl fields.
fn get #
fn get(id int) ?Enchantment
fn get_by_name #
fn get_by_name(name string) ?Enchantment
fn new_registry #
fn new_registry() Registry
new_registry builds a Registry pre-populated with the vanilla enchantments.
fn protection_factor #
fn protection_factor(applied []Applied) f32
fn register_custom #
fn register_custom(e Enchantment) bool
register_custom adds "e" to the enchantment registry (registry.v). Call it from your own setup, before server.new(). Returns false if the id or name is already taken, vanilla or custom.
fn register_defaults #
fn register_defaults(mut r Registry)
register_defaults registers the vanilla Bedrock enchantment set with their numeric ids and level caps.
interface Enchantment #
interface Enchantment {
// id returns the numeric enchantment id used in item NBT.
id() int
// name returns the identifier without namespace, e.g. 'sharpness'.
name() string
// min_level is the lowest valid level.
min_level() int
// max_level is the highest valid level.
max_level() int
// can_enchant reports whether the enchantment applies to the item id.
can_enchant(item_id string) bool
// attack_bonus is extra melee damage granted at level, 0 for non-offense.
attack_bonus(level int) f32
// protection_factor is the damage reduction weight at level, 0 for
// non-armor enchantments.
protection_factor(level int) f32
}
Enchantment is the behaviour contract every enchantment implements. Vanilla enchantments and plugin ones share the same interface, so combat and armor code can query them uniformly.
struct Applied #
struct Applied {
pub:
eid int
level int
}
Applied is one enchantment instance on an item stack.
struct Registry #
struct Registry {
mut:
by_id map[int]Enchantment
by_name map[string]Enchantment
next_custom int = custom_enchantment_id_start
}
Registry maps enchantment ids and names to their classes. It boots with the vanilla set registered; plugins add theirs with ids from custom_enchantment_id_start up.
fn (Registry) register #
fn (mut r Registry) register(e Enchantment) bool
register adds an enchantment, returning false when its id or name is taken.
fn (Registry) next_custom_id #
fn (r &Registry) next_custom_id() int
next_custom_id returns the next free id in the custom range without claiming it.
fn (Registry) get #
fn (r &Registry) get(id int) ?Enchantment
fn (Registry) get_by_name #
fn (r &Registry) get_by_name(name string) ?Enchantment
fn (Registry) len #
fn (r &Registry) len() int
fn (Registry) names #
fn (r &Registry) names() []string
fn (Registry) attack_bonus #
fn (r &Registry) attack_bonus(applied []Applied) f32
attack_bonus sums the melee damage bonus of every applied enchantment.
fn (Registry) protection_factor #
fn (r &Registry) protection_factor(applied []Applied) f32
protection_factor sums the damage reduction weight of every applied enchantment.
struct SimpleEnchantment #
struct SimpleEnchantment {
pub:
eid int
ident string
min_lvl int = 1
max_lvl int = 1
// item_match holds substrings an item id must contain, e.g. ['sword',
// 'axe']. Empty matches every item.
item_match []string
attack_per_level f32
protection_per_level f32
}
SimpleEnchantment is the base class for enchantments that carry flat per-level bonuses and match items by id substring.
fn (SimpleEnchantment) id #
fn (e SimpleEnchantment) id() int
fn (SimpleEnchantment) name #
fn (e SimpleEnchantment) name() string
fn (SimpleEnchantment) min_level #
fn (e SimpleEnchantment) min_level() int
fn (SimpleEnchantment) max_level #
fn (e SimpleEnchantment) max_level() int
fn (SimpleEnchantment) can_enchant #
fn (e SimpleEnchantment) can_enchant(item_id string) bool
fn (SimpleEnchantment) attack_bonus #
fn (e SimpleEnchantment) attack_bonus(level int) f32
fn (SimpleEnchantment) protection_factor #
fn (e SimpleEnchantment) protection_factor(level int) f32