From ea821156560781bcb6868792ccbb26185a1618ca Mon Sep 17 00:00:00 2001 From: KyoZM Date: Mon, 7 Oct 2019 00:19:46 -0300 Subject: [PATCH] create /removelights command --- server/src/cmd.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index ff2ddbac34..4a83b18c52 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -220,6 +220,13 @@ lazy_static! { true, handle_exp, ), + ChatCommand::new( + "removelights", + "{}", + "/removelights [radius] : Removes all lights spawned by players", + true, + handle_remove_lights, + ), ]; } @@ -953,3 +960,50 @@ fn handle_exp(server: &mut Server, entity: EcsEntity, args: String, action: &Cha } } } + +fn handle_remove_lights(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { + let opt_radius = scan_fmt_some!(&args, action.arg_fmt, f32); + let opt_player_pos = server.state.read_component_cloned::(entity); + let mut to_delete = vec![]; + + match opt_player_pos { + Some(player_pos) => { + let ecs = server.state.ecs(); + let entities = &ecs.entities(); + let light_emitters = &ecs.read_storage::(); + let lights = (entities, light_emitters).join(); + for (entity, _) in lights { + let pos = ecs + .read_storage::() + .get(entity) + .copied(); + if let Some(pos) = pos { + if opt_radius.is_some() { + if pos.0.distance(player_pos.0) < opt_radius.unwrap() { + to_delete.push(entity); + } + } else { + to_delete.push(entity); + } + } + } + }, + None => server.clients.notify( + entity, + ServerMsg::private(String::from("You have no position.")), + ), + } + + let size = to_delete.len(); + + for entity in to_delete.iter() { + let _ = server.state.ecs_mut().delete_entity_synced(*entity); + } + + server.clients.notify( + entity, + ServerMsg::private(String::from( + format!("Removed {} lights!", size) + )), + ); +}