mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
add documentation
Former-commit-id: 5abbbaf9b24702dffc345466b1af60ee594bf01a
This commit is contained in:
parent
cf3235d351
commit
447bf18a63
@ -1,3 +1,7 @@
|
|||||||
|
//! #Implementing new commands
|
||||||
|
//! To implement a new command, add an instance of `ChatCommand` to `CHAT_COMMANDS`
|
||||||
|
//! and provide a handler function
|
||||||
|
|
||||||
use crate::Server;
|
use crate::Server;
|
||||||
use common::{comp, msg::ServerMsg};
|
use common::{comp, msg::ServerMsg};
|
||||||
use specs::{Entity as EcsEntity, join::Join};
|
use specs::{Entity as EcsEntity, join::Join};
|
||||||
@ -6,14 +10,20 @@ use vek::*;
|
|||||||
use scan_fmt::scan_fmt;
|
use scan_fmt::scan_fmt;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
/// Struct representing a command that a user can run from server chat
|
||||||
pub struct ChatCommand {
|
pub struct ChatCommand {
|
||||||
|
/// The keyword used to invoke the function, omitting the leading '/'
|
||||||
pub keyword: &'static str,
|
pub keyword: &'static str,
|
||||||
|
/// the format string used by `scan_fmt` to parse arguments
|
||||||
arg_fmt: &'static str,
|
arg_fmt: &'static str,
|
||||||
|
/// message to explain how the command is used
|
||||||
help_string: &'static str,
|
help_string: &'static str,
|
||||||
|
/// handler function called when the command is executed
|
||||||
handler: fn(&mut Server, EcsEntity, String, &ChatCommand),
|
handler: fn(&mut Server, EcsEntity, String, &ChatCommand),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChatCommand {
|
impl ChatCommand {
|
||||||
|
/// Creates a new chat command
|
||||||
pub fn new(
|
pub fn new(
|
||||||
keyword: &'static str,
|
keyword: &'static str,
|
||||||
arg_fmt: &'static str,
|
arg_fmt: &'static str,
|
||||||
@ -27,12 +37,14 @@ impl ChatCommand {
|
|||||||
handler,
|
handler,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Calls the contained handler function, passing `&self` as the last argument
|
||||||
pub fn execute(&self, server: &mut Server, entity: EcsEntity, args: String) {
|
pub fn execute(&self, server: &mut Server, entity: EcsEntity, args: String) {
|
||||||
(self.handler)(server, entity, args, self);
|
(self.handler)(server, entity, args, self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
/// Static list of chat commands available to the server
|
||||||
pub static ref CHAT_COMMANDS: Vec<ChatCommand> = vec![
|
pub static ref CHAT_COMMANDS: Vec<ChatCommand> = vec![
|
||||||
ChatCommand::new(
|
ChatCommand::new(
|
||||||
"jump",
|
"jump",
|
||||||
|
Loading…
Reference in New Issue
Block a user