add documentation

Former-commit-id: 5abbbaf9b24702dffc345466b1af60ee594bf01a
This commit is contained in:
sxv20_ 2019-04-16 17:08:36 +01:00
parent cf3235d351
commit 447bf18a63

View File

@ -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",