Server mods (Umbra)
MOD 制作ドキュメントは、いまのところ韓国語だけです。
Code that runs on Umbra, the 月蝕 server (called isrv inside the repository): new commands, intercepting events such as chat, block breaking and damage, reading and changing player and world state, explosions, summoning and messages. You use the server fields of veil_mod! (server_init, event, server_tick). The same .wasm runs on the client too, where server functions trap, so only call them from server callbacks.
- Server mods only run on Umbra. On Paper or vanilla servers they are off.
- In singleplayer the embedded server loads the client's mods folder. A dedicated server takes a folder with
serve <world dir> … --mods <mods dir>(server/isrv/examples/serve.rs). - Running a server mod on a public server needs 月蝕's human review (../../rules.md). On your own machine and on servers that allow it, it runs straight away.
The shape of a server mod
This is what mc189 mod new my_srv --template server generates: one command that reads its arguments, plus a counter shown on join.
//! my srv — server mod template: one command and one event.
#![no_std]
use core::sync::atomic::{AtomicU32, Ordering};
use mc189_mod_sdk::prelude::*;
use mc189_mod_sdk::srvevent::{self as se, kind, Event as ServerEvent};
static HELLO: AtomicU32 = AtomicU32::new(u32::MAX);
static JOINS: AtomicU32 = AtomicU32::new(0);
/// Runs once, on the server only. Register commands and subscribe to events here.
fn server_init() {
if let Some(n) = srvdata::register_command("hello", 0) {
HELLO.store(n as u32, Ordering::Relaxed);
}
se::subscribe(kind::JOIN, se::POST, 0);
}
fn event(e: &ServerEvent) -> bool {
match e.kind {
// Commands arrive without subscribing: int(0) = sender eid, int(1) = command number
kind::CMD_RUN if e.int(1) as u32 == HELLO.load(Ordering::Relaxed) => {
let args = se::args(e);
let who = args.first().map(|s| s.as_str()).unwrap_or("world");
let _ = srvdata::tell(e.int(0), &format!("Hello, {who}!"));
}
kind::JOIN => {
let n = JOINS.fetch_add(1, Ordering::Relaxed) + 1;
let _ = srvdata::tell(e.int(0), &format!("Welcome, you are visitor number {n}. Try /hello"));
}
_ => {}
}
false // returning true cancels a PRE event
}
// Any server field makes this version 5. Nothing to register, so no `common`.
veil_mod! {
id: "my_srv",
name: "my srv",
version: "0.1.0",
description: "my srv",
authors: ["me"],
groups: ["base", "server"],
server_init: server_init,
event: event,
}
Fill in only the fields you need, in any order. common is only needed if you register blocks or items by hand. Mod state lives in statics (atomics or a static with interior mutability), since there is no Mod value on the server side.
The easy_server example is a slightly larger version: a /dice command, a join greeting, and a server_tick counter.
The event bus: srvevent
se::subscribe(kind, phase, priority). The phase is se::PRE (before it happens, can be cancelled) or se::POST (after). Kinds without a PRE phase only deliver POST.
- Reading. Numeric fields are
e.int(k)ande.num(k), text ise.text(). The field layout is fixed per kind and documented in square brackets on eachsrvevent::kind::*constant in ../../reference.md. For exampleCHATis[eid]plus the message, andENTITY_DAMAGEis[eid, 1 player / 2 mob, cause, attacker eid, damage, …]. - Cancelling. Return
truefromevent, or callse::cancel(true). A cancelled event ends up looking exactly as if vanilla had refused it: a block break leaves the block, a move is rolled back, and so on. POST events cannot be cancelled. - Changing. Only fields marked ✎ in the reference can be changed, with
set_num,set_textorset_list(the chat message, the damage amount, the list of blocks an explosion destroys). - Order. Lower priority runs first; ties go by mod id. Events raised inside a hook or event handler are queued and delivered after the outer call returns, never re-entrantly.
- Budget. About 2 ms per mod per tick.
MOVEhas its own 500 µs cap; chunk loads, window clicks, pickups and projectiles have 1 ms each. A move event is only raised after moving more than 1/256 of a block squared or turning more than 10°.
Commands
srvdata::register_command("name", permission) returns the command's number, and CMD_RUN events for it arrive without subscribing. se::args(e) splits the arguments on spaces. For tab completion, handle CMD_COMPLETE and call se::suggest("candidate"). Permission is the op level, 0 to 4. Names match [a-z0-9_]{1,32} and cannot shadow vanilla commands.
kind::CMD_COMPLETE => {
// `/home s` → set
let t = e.text();
if (t.starts_with("home ") || t == "home") && "home set".starts_with(t) {
se::suggest("set");
}
}
Reading and changing state: srvdata
These go through the same code paths as vanilla, so the effect matches /tp, /effect, /summon and friends.
| To… | Use |
|---|---|
| Blocks | block_at, set_block_at, tile, tile_set |
| Find and read entities | near_entities, entity, entity_type, entity_nbt, player_list, player_name, player_by_name |
| Change entities | summon, kill_silent, teleport_to, set_motion, hurt, heal_entity, potion, custom_name, equipment, set_equipment |
| Players | player_info, slot, set_slot, add_xp, set_food, set_gamemode |
| Talk to players | tell, action_bar, show_title, set_title_times (text_json, tell_json for JSON chat) |
| Scores and teams | score_set, score_add, score_get, team_join, team_of, same_team |
| Per-player storage | player_data, set_player_data (16 KiB per mod, saved with the player) |
| Enchantments | register_enchant, enchant, enchant_level |
| The world | world_time, set_world_time, set_weather, game_rule, set_game_rule, world_spawn, set_world_spawn, explosion, play_sound, particles |
| Run a vanilla command | run_command("tp @a 0 70 0") → Result<String, String> |
Item, entity and tile data are binary NBT bytes. From srv_showcase, saving a home position in per-player storage:
fn home(eid: i32, args: &str) {
if args.trim() == "set" {
let Some(me) = sd::entity(eid) else { return };
let mut b = [0u8; HOME_LEN];
b[0..8].copy_from_slice(&me.x.to_le_bytes());
b[8..16].copy_from_slice(&me.y.to_le_bytes());
b[16..24].copy_from_slice(&me.z.to_le_bytes());
if sd::set_player_data(eid, &b) {
let _ = sd::tell(eid, &format!("집 저장: {:.1} {:.1} {:.1}", me.x, me.y, me.z));
}
return;
}
Server ticks and long jobs
server_tick: f is called every tick (same 2 ms budget). Split work that cannot finish in one tick. Version 7 (abi: 7) adds body::Jobs, which hands out work while watching the remaining budget, and the body module for placing, mounting and overlapping entities on the server (set_pos, mount, overlap, …; see the rtm example). That part is unverified in these docs.
Common pitfalls
- Server functions only return -1 or
None. They were called outside a hook,server_tickorevent. Only registration calls (register_command,register_enchant) work inserver_init. - The mod gets switched off on the client. A server function was called from a client path (
client,main's tick). The same wasm runs on both sides, so keep server calls in server-only callbacks. - Subscribed, but no events arrive. Did you subscribe in
server_init? Isevent:inveil_mod!? Did you subscribe to PRE for a kind that only has POST? - The server unloaded the mod. It went over budget or trapped. The server log says which.
- Time of day.
srvdata::world_time()is vanilla DayTime in ticks. It keeps growing past 24000, so takerem_euclid(24000). 20 ticks = 1 second; 0 sunrise, 6000 noon, 13000 night (/time set night), 18000 midnight, 23000 dawn. For a clock that always advances usesrvblock::time()(world age) instead. - What goes in
groups/mod checksays "human review needed — groupserver".srvdata,srveventandnetare in theservergroup (abi.md, "Permission groups"). Put["base", "server"]ingroupslike the template (leavingserverout makes the game refuse to link). Mods that use server functions are reviewed by a person before they can be used on public servers, so not being auto-approvable is expected.
Examples: easy_server, srv_showcase (commands, area TNT, enchantments, mod packets), blocks_logic, mob_basic.