Dragonfly Rust Plugin API
Welcome to the Rust Plugin API for Dragonfly server software. This library provides the tools to build high-performance, safe, and asynchronous plugins using native Rust.
The API is designed to be simple and explicit. You define your plugin's logic by implementing the PluginEventHandler trait and register your event subscriptions using the #[derive(Handler)] macro.
Features
- Asynchronous by Default: Built on
tokioand native Rustasync/await. - Type-Safe: All events and actions are strongly typed, catching bugs at compile time.
- Simple Subscriptions: A clean
#[derive(Handler)]macro handles all event subscription logic.
Quick Start Guide
Here is the complete process for creating a "Hello, World!" plugin.
1. Create a New Plugin
First, create a new binary crate for your plugin:
2. Update Cargo.toml
Next, add dragonfly-plugin and tokio to your dependencies.
[]
= "my_plugin"
= "0.1.0"
= "2021"
[]
# This is the main API library
= "0.1" # Or use a version number
# Tokio is required for the async runtime
= { = "1", = ["full"] }
3. Write Your Plugin (src/main.rs)
This is the complete code for a simple plugin that greets players on join and adds a prefix to their chat messages.
// --- Import all the necessary items ---
use dragonfly-;
// --- 1. Define Your Plugin Struct ---
//
// `#[derive(Handler)]` is the "trigger" that runs the macro.
// `#[derive(Default)]` is common for simple, stateless plugins.
//
// `#[subscriptions(...)]` is the "helper attribute" that lists
// all the events this plugin needs to listen for.
;
// --- 2. Implement the Event Handlers ---
//
// This is where all your plugin's logic lives.
// You only need to implement the `async fn` handlers
// for the events you subscribed to.
// --- 3. Start the Plugin ---
//
// This is the entry point that connects to the server.
async
Writing Event Handlers
All plugin logic is built by implementing functions from the PluginEventHandler trait.
async fn and Lifetimes
Because this API uses native async fn in traits, you must include the anonymous lifetime ('_) annotation in the EventContext type:
- Correct:
event: &mut EventContext<'_, types::ChatEvent> - Incorrect:
event: &mut EventContext<types::ChatEvent>
Reading Event Data
You can read immutable data directly from the event:
let player_name = &event.data.name;
println!;
Mutating Events
Some events are mutable. The EventContext provides helper methods (like set_message) to modify the event before the server processes it:
event.set_message;
Cancelling Events
You can also cancel compatible events to stop them from happening:
event.cancel;
Available Events
The #[subscriptions(...)] macro accepts any variant from the types::EventType enum.
You can find a complete list of all available event names (e.g., PlayerJoin, Chat, BlockBreak) and their corresponding data structs (e.g., types::PlayerJoinEvent) by looking in the ./src/types.rs file or by consulting the API documentation.