diff --git a/common/src/comp/admin.rs b/common/src/comp/admin.rs index fd4c30c5fb..025fcc9bf2 100644 --- a/common/src/comp/admin.rs +++ b/common/src/comp/admin.rs @@ -1,7 +1,8 @@ -use specs::{Component, VecStorage}; +use specs::{Component, NullStorage}; -pub struct AdminPerms; +#[derive(Default)] +pub struct Admin; -impl Component for AdminPerms { - type Storage = VecStorage; +impl Component for Admin { + type Storage = NullStorage; } diff --git a/common/src/comp/mod.rs b/common/src/comp/mod.rs index 3e2874862a..da82ee09af 100644 --- a/common/src/comp/mod.rs +++ b/common/src/comp/mod.rs @@ -14,7 +14,7 @@ mod visual; // Reexports pub use action_state::ActionState; -pub use admin::AdminPerms; +pub use admin::Admin; pub use agent::Agent; pub use animation::{Animation, AnimationInfo}; pub use body::{humanoid, object, quadruped, quadruped_medium, Body}; diff --git a/common/src/state.rs b/common/src/state.rs index d6b4dbdace..c73a3602b6 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -145,7 +145,7 @@ impl State { ecs.register::(); ecs.register::(); ecs.register::(); - ecs.register::(); + ecs.register::(); // Controller effects ecs.register::(); ecs.register::(); diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 5b26f76fda..2b75512f6c 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -161,25 +161,14 @@ lazy_static! { ]; } -fn is_admin(server: &mut Server, entity: EcsEntity) -> bool { - if server - .state - .read_storage::() - .get(entity) - .is_some() - { - true - } else { +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 permissions to do that")), + ServerMsg::private(String::from("You have no permission to do that")), ); - false - } -} - -fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { - if is_admin(server, entity) { + return; + } else { if let Ok((x, y, z)) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32) { match server.state.read_component_cloned::(entity) { Some(current_pos) => { @@ -198,7 +187,13 @@ fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &Ch } fn handle_goto(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { - if is_admin(server, entity) { + 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 server .state @@ -233,7 +228,13 @@ fn handle_kill(server: &mut Server, entity: EcsEntity, _args: String, _action: & } fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { - if is_admin(server, entity) { + 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 new_time = match time.as_ref().map(|s| s.as_str()) { Some("night") => NaiveTime::from_hms(0, 0, 0), @@ -284,7 +285,13 @@ fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &Ch } fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { - if is_admin(server, entity) { + 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 Some(stats) = server .state @@ -324,7 +331,13 @@ fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &C } fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { - if is_admin(server, entity) { + 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) { let ecs = server.state.ecs(); let opt_player = (&ecs.entities(), &ecs.read_storage::()) @@ -371,7 +384,13 @@ fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &Chat } fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { - if is_admin(server, entity) { + 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) { (Some(opt_align), Some(id), opt_amount) => { if let Some(agent) = alignment_to_agent(&opt_align, entity) { @@ -445,7 +464,13 @@ fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action } fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { - if is_admin(server, entity) { + 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 .state .read_storage::() @@ -475,6 +500,7 @@ fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: } } +// TODO: Don't display commands that the player cannot use. fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { for cmd in CHAT_COMMANDS.iter() { server @@ -504,7 +530,13 @@ fn kind_to_body(kind: NpcKind) -> comp::Body { } fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { - if is_admin(server, entity) { + 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 mut stats = ecs.write_storage::(); let players = ecs.read_storage::(); @@ -523,7 +555,13 @@ fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _actio } fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action: &ChatCommand) { - if is_admin(server, entity) { + 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 pos = server @@ -623,7 +661,13 @@ fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action: } fn handle_light(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { - if is_admin(server, entity) { + 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) = scan_fmt_some!(&args, action.arg_fmt, f32, f32, f32, f32, f32, f32, f32); diff --git a/server/src/lib.rs b/server/src/lib.rs index ea2f090418..e7a44c5930 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -204,7 +204,7 @@ impl Server { // Make sure physics are accepted. state.write_component(entity, comp::ForceUpdate); - // Give the AdminPerms component to the player if their name exists in admin list + // Give the Admin component to the player if their name exists in admin list if server_settings.admins.contains( &state .ecs() @@ -213,7 +213,7 @@ impl Server { .unwrap() .alias, ) { - state.write_component(entity, comp::AdminPerms); + state.write_component(entity, comp::Admin); } // Tell the client its request was successful. client.allow_state(ClientState::Character); @@ -822,17 +822,13 @@ impl Server { } else { let message = match self.state.ecs().read_storage::().get(entity) { - Some(player) => match self - .state - .ecs() - .read_storage::() - .get(entity) - { - Some(_perms) => { + Some(player) => { + if self.entity_is_admin(entity) { format!("[ADMIN][{}] {}", &player.alias, message) + } else { + format!("[{}] {}", &player.alias, message) } - None => format!("[{}] {}", &player.alias, message), - }, + } None => format!("[] {}", message), }; self.clients @@ -1182,6 +1178,13 @@ impl Server { } } } + + fn entity_is_admin(&self, entity: EcsEntity) -> bool { + self.state + .read_storage::() + .get(entity) + .is_some() + } } impl Drop for Server { diff --git a/server/src/settings.rs b/server/src/settings.rs index 5b2997e27a..4a83b6569e 100644 --- a/server/src/settings.rs +++ b/server/src/settings.rs @@ -61,6 +61,18 @@ impl ServerSettings { Ok(()) } + pub fn singleplayer() -> Self { + Self { + address: SocketAddr::from(([0; 4], 14004)), + world_seed: 1337, + server_name: "Singleplayer".to_owned(), + server_description: "This is the best Veloren singleplayer server.".to_owned(), + max_players: 100, + 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 + } + } + fn get_settings_path() -> PathBuf { PathBuf::from(r"settings.ron") } diff --git a/voxygen/src/singleplayer.rs b/voxygen/src/singleplayer.rs index 0b294b02f6..66b6b5aaf1 100644 --- a/voxygen/src/singleplayer.rs +++ b/voxygen/src/singleplayer.rs @@ -36,7 +36,7 @@ impl Singleplayer { )); // Create server - let server = Server::bind(sock.clone(), ServerSettings::default()) + let server = Server::bind(sock.clone(), ServerSettings::singleplayer()) .expect("Failed to create server instance!"); let server = match client {