Skip to content

server.plugin

fn new_manager #

fn new_manager(commands &cmd.Registry, events &event.Bus, sched &scheduler.Scheduler, server ServerView, log &logger.Logger) &Manager

new_manager builds a Manager sharing the server's command registry, event bus and a ServerView. Plugins registered later enable against this same Api.

interface Plugin #

interface Plugin {
	meta() Meta
mut:
	set_log(l &logger.Logger)
	on_enable(mut api Api)
	on_disable()
}

Plugin is implemented by every plugin. on_enable runs once at startup with an Api handle for registering commands and listeners; on_disable runs at shutdown so a plugin can flush state.

interface ServerView #

interface ServerView {
mut:
	broadcast_message(text string)
	online_count() int
	player_names() []string
	// spawn_entity spawns a registered entity type by name at a position,
	// returning false if the type is unknown.
	spawn_entity(name string, x f32, y f32, z f32) bool
	entity_type_names() []string
	// set_block sets a block by namespaced id in the default world and broadcasts
	// the change, returning false if the block name is unknown.
	set_block(name string, x int, y int, z int) bool
	// get_block returns the block network id at a position in the default world.
	get_block(x int, y int, z int) int
	// place_water sets a water source at the position and starts it spreading.
	place_water(x int, y int, z int)
	// capture_area snapshots the block ids over the box between the two corners,
	// or none if the region is too large. Restore it with restore_area.
	capture_area(x1 int, y1 int, z1 int, x2 int, y2 int, z2 int) ?&arena.Snapshot
	// restore_area writes a snapshot back so viewers see the arena reset.
	restore_area(snapshot &arena.Snapshot)
	// register_generator adds or overrides a named world generator: a plugin
	// can add a brand new one or replace a builtin by registering the same name.
	register_generator(name string, factory fn (dim world.Dimension) world.Generator)
	// generator_type_names lists every registered generator name.
	generator_type_names() []string
	// register_custom_item registers a data-driven item definition and returns
	// its allocated runtime id.
	register_custom_item(def item.CustomItemDefinition) int
	// register_custom_block registers a data-driven block definition and
	// returns its allocated runtime id.
	register_custom_block(def block.CustomBlockDefinition) int
	// register_custom_entity registers a custom entity type together with the
	// Behaviour factory used to spawn it, returning false if the id is taken.
	register_custom_entity(def entity.CustomEntityDefinition, factory entity.BehaviourFactory) bool
	// register_enchantment adds an enchantment, returning false if its id or
	// name is already taken.
	register_enchantment(e enchant.Enchantment) bool
	// next_enchantment_id returns the next free custom enchantment id.
	next_enchantment_id() int
	custom_item_names() []string
	custom_block_names() []string
	custom_entity_names() []string
}

ServerView is the narrow slice of the running server a plugin is allowed to poke. Hub satisfies it structurally, so the plugin package never imports the session package and no import cycle forms.

struct Api #

@[heap]
struct Api {
mut:
	commands  &cmd.Registry        = unsafe { nil }
	events    &event.Bus           = unsafe { nil }
	scheduler &scheduler.Scheduler = unsafe { nil }
pub mut:
	server ServerView
	log    &logger.Logger = unsafe { nil }
}

Api is the single handle handed to a plugin on enable. Everything a plugin can do to the server goes through here: register commands, register event listeners, reach the ServerView, or log.

fn (Api) register_command #

fn (mut a Api) register_command(c cmd.Command)

register_command adds a command to the shared registry, exactly like a built-in command.

fn (Api) register_listener #

fn (mut a Api) register_listener(h event.Handler, priority event.Priority)

register_listener subscribes a Handler to the event Bus at the given priority.

fn (Api) run_delayed #

fn (mut a Api) run_delayed(task scheduler.Task, delay i64) &scheduler.TaskHandler

run_delayed schedules task to run once after delay server ticks (20 ticks = 1s).

fn (Api) run_repeating #

fn (mut a Api) run_repeating(task scheduler.Task, period i64) &scheduler.TaskHandler

run_repeating schedules task to run every period ticks.

fn (Api) register_generator #

fn (mut a Api) register_generator(name string, factory fn (dim world.Dimension) world.Generator)

fn (Api) register_custom_item #

fn (mut a Api) register_custom_item(def item.CustomItemDefinition) int

register_custom_item registers a data-driven item and returns its runtime id.

fn (Api) register_custom_block #

fn (mut a Api) register_custom_block(def block.CustomBlockDefinition) int

register_custom_block registers a data-driven block and returns its runtime id.

fn (Api) register_custom_entity #

fn (mut a Api) register_custom_entity(def entity.CustomEntityDefinition, factory entity.BehaviourFactory) bool

register_custom_entity registers a custom entity type with its Behaviour factory, making it spawnable via /summon and spawn_entity.

fn (Api) register_enchantment #

fn (mut a Api) register_enchantment(e enchant.Enchantment) bool

register_enchantment adds an enchantment to the shared registry.

fn (Api) next_enchantment_id #

fn (mut a Api) next_enchantment_id() int

next_enchantment_id returns the next free custom enchantment id.

struct Base #

struct Base {
pub mut:
	log &logger.Logger = unsafe { nil }
}

Base is an embeddable helper carrying a scoped logger. A plugin embeds it, keeps its own Meta, and gets log for free. Manager calls set_log before on_enable, so log is never the zero value nil by the time a plugin can observe it. It does not implement the rest of the Plugin interface itself - the concrete plugin still defines meta/on_enable/on_disable.

fn (Base) set_log #

fn (mut b Base) set_log(l &logger.Logger)

struct Manager #

@[heap]
struct Manager {
mut:
	plugins []Plugin
	api     &Api           = unsafe { nil }
	log     &logger.Logger = unsafe { nil }
}

Manager owns the plugin lifecycle: register plugins at boot, enable them all once the server is wired, disable them all on shutdown. Loosely mirrors PocketMine's PluginManager, minus the disk loading a compiled server can't do.

fn (Manager) register #

fn (mut m Manager) register(p Plugin)

register queues a plugin. It is not enabled until enable_all runs.

fn (Manager) count #

fn (m &Manager) count() int

count reports how many plugins are registered.

fn (Manager) enable_all #

fn (mut m Manager) enable_all()

enable_all enables every registered plugin in registration order, giving each a logger scoped to its name.

fn (Manager) disable_all #

fn (mut m Manager) disable_all()

disable_all disables every plugin in reverse order so later plugins tear down before the ones they may depend on.

struct Meta #

struct Meta {
pub:
	name    string
	version string
	authors []string
}

Meta is the static identity of a plugin. Inspired by PocketMine's PluginDescription but kept to what a compiled, in-tree plugin needs.