Add needs_admin bool to check if the commands needs admin perms

This commit is contained in:
Piotr Korgól 2019-08-15 16:33:00 +02:00
parent 28c47663d0
commit 892855a348
2 changed files with 346 additions and 399 deletions

View File

@ -26,6 +26,8 @@ pub struct ChatCommand {
arg_fmt: &'static str, arg_fmt: &'static str,
/// A message that explains how the command is used. /// A message that explains how the command is used.
help_string: &'static str, help_string: &'static str,
/// A boolean that is used to check whether the command requires administrator permissions or not.
needs_admin: bool,
/// Handler function called when the command is executed. /// Handler function called when the command is executed.
/// # Arguments /// # Arguments
/// * `&mut Server` - the `Server` instance executing the command. /// * `&mut Server` - the `Server` instance executing the command.
@ -42,19 +44,33 @@ impl ChatCommand {
keyword: &'static str, keyword: &'static str,
arg_fmt: &'static str, arg_fmt: &'static str,
help_string: &'static str, help_string: &'static str,
needs_admin: bool,
handler: fn(&mut Server, EcsEntity, String, &ChatCommand), handler: fn(&mut Server, EcsEntity, String, &ChatCommand),
) -> Self { ) -> Self {
Self { Self {
keyword, keyword,
arg_fmt, arg_fmt,
help_string, help_string,
needs_admin,
handler, handler,
} }
} }
/// Calls the contained handler function, passing `&self` as the last argument. /// 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) {
if self.needs_admin {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that.")),
);
return;
} else {
(self.handler)(server, entity, args, self); (self.handler)(server, entity, args, self);
} }
} else {
(self.handler)(server, entity, args, self);
}
}
} }
lazy_static! { lazy_static! {
@ -64,111 +80,120 @@ lazy_static! {
"jump", "jump",
"{d} {d} {d}", "{d} {d} {d}",
"/jump <dx> <dy> <dz> : Offset your current position", "/jump <dx> <dy> <dz> : Offset your current position",
true,
handle_jump, handle_jump,
), ),
ChatCommand::new( ChatCommand::new(
"goto", "goto",
"{d} {d} {d}", "{d} {d} {d}",
"/goto <x> <y> <z> : Teleport to a position", "/goto <x> <y> <z> : Teleport to a position",
true,
handle_goto, handle_goto,
), ),
ChatCommand::new( ChatCommand::new(
"alias", "alias",
"{}", "{}",
"/alias <name> : Change your alias", "/alias <name> : Change your alias",
false,
handle_alias, handle_alias,
), ),
ChatCommand::new( ChatCommand::new(
"tp", "tp",
"{}", "{}",
"/tp <alias> : Teleport to another player", "/tp <alias> : Teleport to another player",
true,
handle_tp, handle_tp,
), ),
ChatCommand::new( ChatCommand::new(
"kill", "kill",
"{}", "{}",
"/kill : Kill yourself", "/kill : Kill yourself",
false,
handle_kill, handle_kill,
), ),
ChatCommand::new( ChatCommand::new(
"time", "time",
"{} {s}", "{} {s}",
"/time <XY:XY> or [Time of day] : Set the time of day", "/time <XY:XY> or [Time of day] : Set the time of day",
true,
handle_time, handle_time,
), ),
ChatCommand::new( ChatCommand::new(
"spawn", "spawn",
"{} {} {d}", "{} {} {d}",
"/spawn <alignment> <entity> [amount] : Spawn a test entity", "/spawn <alignment> <entity> [amount] : Spawn a test entity",
true,
handle_spawn, handle_spawn,
), ),
ChatCommand::new( ChatCommand::new(
"players", "players",
"{}", "{}",
"/players : Lists players currently online", "/players : Lists players currently online",
false,
handle_players, handle_players,
), ),
ChatCommand::new( ChatCommand::new(
"help", "", "/help: Display this message", handle_help), "help", "", "/help: Display this message", false, handle_help),
ChatCommand::new( ChatCommand::new(
"health", "health",
"{}", "{}",
"/health : Set your current health", "/health : Set your current health",
true,
handle_health, handle_health,
), ),
ChatCommand::new( ChatCommand::new(
"build", "build",
"", "",
"/build : Toggles build mode on and off", "/build : Toggles build mode on and off",
true,
handle_build, handle_build,
), ),
ChatCommand::new( ChatCommand::new(
"tell", "tell",
"{}", "{}",
"/tell <alias> <message>: Send a message to another player", "/tell <alias> <message>: Send a message to another player",
false,
handle_tell, handle_tell,
), ),
ChatCommand::new( ChatCommand::new(
"killnpcs", "killnpcs",
"{}", "{}",
"/killnpcs : Kill the NPCs", "/killnpcs : Kill the NPCs",
true,
handle_killnpcs, handle_killnpcs,
), ),
ChatCommand::new( ChatCommand::new(
"object", "object",
"{}", "{}",
"/object [Name]: Spawn an object", "/object [Name]: Spawn an object",
true,
handle_object, handle_object,
), ),
ChatCommand::new( ChatCommand::new(
"light", "light",
"{} {} {} {} {} {} {}", "{} {} {} {} {} {} {}",
"/light <opt: <<cr> <cg> <cb>> <<ox> <oy> <oz>> <<strength>>>: Spawn entity with light", "/light <opt: <<cr> <cg> <cb>> <<ox> <oy> <oz>> <<strength>>>: Spawn entity with light",
true,
handle_light, handle_light,
), ),
ChatCommand::new( ChatCommand::new(
"lantern", "lantern",
"{}", "{}",
"/lantern : adds/remove light near player", "/lantern : adds/remove light near player",
false,
handle_lantern, handle_lantern,
), ),
ChatCommand::new( ChatCommand::new(
"explosion", "explosion",
"{}", "{}",
"/explosion <radius> : Explodes the ground around you", "/explosion <radius> : Explodes the ground around you",
false,
handle_explosion, handle_explosion,
), ),
]; ];
} }
fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
if let Ok((x, y, z)) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32) { if let Ok((x, y, z)) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32) {
match server.state.read_component_cloned::<comp::Pos>(entity) { match server.state.read_component_cloned::<comp::Pos>(entity) {
Some(current_pos) => { Some(current_pos) => {
@ -184,16 +209,8 @@ fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &Ch
} }
} }
} }
}
fn handle_goto(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_goto(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
if let Ok((x, y, z)) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32) { if let Ok((x, y, z)) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32) {
if server if server
.state .state
@ -216,7 +233,6 @@ fn handle_goto(server: &mut Server, entity: EcsEntity, args: String, action: &Ch
.notify(entity, ServerMsg::private(String::from(action.help_string))); .notify(entity, ServerMsg::private(String::from(action.help_string)));
} }
} }
}
fn handle_kill(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { fn handle_kill(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
server server
@ -228,13 +244,6 @@ fn handle_kill(server: &mut Server, entity: EcsEntity, _args: String, _action: &
} }
fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
let time = scan_fmt_some!(&args, action.arg_fmt, String); let time = scan_fmt_some!(&args, action.arg_fmt, String);
let new_time = match time.as_ref().map(|s| s.as_str()) { let new_time = match time.as_ref().map(|s| s.as_str()) {
Some("night") => NaiveTime::from_hms(0, 0, 0), Some("night") => NaiveTime::from_hms(0, 0, 0),
@ -282,16 +291,8 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch
)), )),
); );
} }
}
fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
if let Ok(hp) = scan_fmt!(&args, action.arg_fmt, u32) { if let Ok(hp) = scan_fmt!(&args, action.arg_fmt, u32) {
if let Some(stats) = server if let Some(stats) = server
.state .state
@ -313,7 +314,6 @@ fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: &
); );
} }
} }
}
fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) { if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) {
@ -331,13 +331,6 @@ fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &C
} }
fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) { if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) {
let ecs = server.state.ecs(); let ecs = server.state.ecs();
let opt_player = (&ecs.entities(), &ecs.read_storage::<comp::Player>()) let opt_player = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
@ -353,10 +346,7 @@ fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &Chat
} }
None => server.clients.notify( None => server.clients.notify(
entity, entity,
ServerMsg::private(format!( ServerMsg::private(format!("Unable to teleport to player '{}'!", alias)),
"Unable to teleport to player '{}'!",
alias
)),
), ),
}, },
None => { None => {
@ -381,16 +371,8 @@ fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &Chat
.notify(entity, ServerMsg::private(String::from(action.help_string))); .notify(entity, ServerMsg::private(String::from(action.help_string)));
} }
} }
}
fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
match scan_fmt_some!(&args, action.arg_fmt, String, NpcKind, String) { match scan_fmt_some!(&args, action.arg_fmt, String, NpcKind, String) {
(Some(opt_align), Some(id), opt_amount) => { (Some(opt_align), Some(id), opt_amount) => {
if let Some(agent) = alignment_to_agent(&opt_align, entity) { if let Some(agent) = alignment_to_agent(&opt_align, entity) {
@ -418,9 +400,7 @@ fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &C
} }
server.clients.notify( server.clients.notify(
entity, entity,
ServerMsg::private( ServerMsg::private(format!("Spawned {} entities", amount).to_owned()),
format!("Spawned {} entities", amount).to_owned(),
),
); );
} }
None => server.clients.notify( None => server.clients.notify(
@ -437,7 +417,6 @@ fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &C
} }
} }
} }
}
fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
let ecs = server.state.ecs(); let ecs = server.state.ecs();
@ -464,13 +443,6 @@ fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action
} }
fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
if server if server
.state .state
.read_storage::<comp::CanBuild>() .read_storage::<comp::CanBuild>()
@ -498,7 +470,6 @@ fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action:
); );
} }
} }
}
// TODO: Don't display commands that the player cannot use. // TODO: Don't display commands that the player cannot use.
fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
@ -530,13 +501,6 @@ fn kind_to_body(kind: NpcKind) -> comp::Body {
} }
fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
let ecs = server.state.ecs(); let ecs = server.state.ecs();
let mut stats = ecs.write_storage::<comp::Stats>(); let mut stats = ecs.write_storage::<comp::Stats>();
let players = ecs.read_storage::<comp::Player>(); let players = ecs.read_storage::<comp::Player>();
@ -552,16 +516,8 @@ fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _actio
}; };
server.clients.notify(entity, ServerMsg::private(text)); server.clients.notify(entity, ServerMsg::private(text));
} }
}
fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action: &ChatCommand) { fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
let obj_type = scan_fmt!(&args, _action.arg_fmt, String); let obj_type = scan_fmt!(&args, _action.arg_fmt, String);
let pos = server let pos = server
@ -658,16 +614,8 @@ fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action:
.notify(entity, ServerMsg::private(format!("You have no position!"))); .notify(entity, ServerMsg::private(format!("You have no position!")));
} }
} }
}
fn handle_light(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_light(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if !server.entity_is_admin(entity) {
server.clients.notify(
entity,
ServerMsg::private(String::from("You have no permission to do that")),
);
return;
} else {
let (opt_r, opt_g, opt_b, opt_x, opt_y, opt_z, opt_s) = let (opt_r, opt_g, opt_b, opt_x, opt_y, opt_z, opt_s) =
scan_fmt_some!(&args, action.arg_fmt, f32, f32, f32, f32, f32, f32, f32); scan_fmt_some!(&args, action.arg_fmt, f32, f32, f32, f32, f32, f32, f32);
@ -709,7 +657,6 @@ fn handle_light(server: &mut Server, entity: EcsEntity, args: String, action: &C
.notify(entity, ServerMsg::private(format!("You have no position!"))); .notify(entity, ServerMsg::private(format!("You have no position!")));
} }
} }
}
fn handle_lantern(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { fn handle_lantern(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
let opt_s = scan_fmt_some!(&args, action.arg_fmt, f32); let opt_s = scan_fmt_some!(&args, action.arg_fmt, f32);

View File

@ -66,7 +66,7 @@ impl ServerSettings {
address: SocketAddr::from(([0; 4], 14004)), address: SocketAddr::from(([0; 4], 14004)),
world_seed: 1337, world_seed: 1337,
server_name: "Singleplayer".to_owned(), server_name: "Singleplayer".to_owned(),
server_description: "This is the best Veloren singleplayer server.".to_owned(), server_description: "The main feature is loneliness!".to_owned(),
max_players: 100, max_players: 100,
start_time: 9.0 * 3600.0, start_time: 9.0 * 3600.0,
admins: vec!["singleplayer".to_string()], // TODO: Let the player choose if they want to use admin commands or not admins: vec!["singleplayer".to_string()], // TODO: Let the player choose if they want to use admin commands or not