diff --git a/azalea-world/src/entity_storage.rs b/azalea-world/src/entity_storage.rs index 71c5413c..2a4c83ff 100755 --- a/azalea-world/src/entity_storage.rs +++ b/azalea-world/src/entity_storage.rs @@ -240,18 +240,18 @@ impl PartialEntityStorage { .update_entity_chunk(entity_id, old_chunk, new_chunk); } - pub fn find_entity(&self, mut f: F) -> Option> + pub fn entity_by(&self, mut f: F) -> Option> where F: FnMut(&Arc) -> bool, { - self.shared.read().find_entity(|e| f(e)) + self.shared.read().entity_by(|e| f(e)) } - pub fn find_entity_in_chunk(&self, chunk: &ChunkPos, mut f: F) -> Option> + pub fn entity_by_in_chunk(&self, chunk: &ChunkPos, mut f: F) -> Option> where F: FnMut(&EntityData) -> bool, { - self.shared.read().find_entity_in_chunk(chunk, |e| f(e)) + self.shared.read().entity_by_in_chunk(chunk, |e| f(e)) } } @@ -352,7 +352,7 @@ impl WeakEntityStorage { .and_then(|id| self.data_by_id.get(id).and_then(|e| e.upgrade())) } - pub fn find_entity(&self, mut f: F) -> Option> + pub fn entity_by(&self, mut f: F) -> Option> where F: FnMut(&Arc) -> bool, { @@ -366,7 +366,22 @@ impl WeakEntityStorage { None } - pub fn find_entity_in_chunk(&self, chunk: &ChunkPos, mut f: F) -> Option> + pub fn entities_by(&self, mut f: F) -> Vec> + where + F: FnMut(&Arc) -> bool, + { + let mut entities = Vec::new(); + for entity in self.entities() { + if let Some(entity) = entity.upgrade() { + if f(&entity) { + entities.push(entity); + } + } + } + entities + } + + pub fn entity_by_in_chunk(&self, chunk: &ChunkPos, mut f: F) -> Option> where F: FnMut(&EntityData) -> bool, { diff --git a/azalea-world/src/world.rs b/azalea-world/src/world.rs index 648613b9..ad6037a5 100644 --- a/azalea-world/src/world.rs +++ b/azalea-world/src/world.rs @@ -164,11 +164,18 @@ impl WeakWorld { self.entity_storage.read().get_by_uuid(uuid) } - pub fn find_entity(&self, mut f: F) -> Option> + pub fn entity_by(&self, mut f: F) -> Option> where F: FnMut(&EntityData) -> bool, { - self.entity_storage.read().find_entity(|entity| f(entity)) + self.entity_storage.read().entity_by(|e| f(e)) + } + + pub fn entities_by(&self, mut f: F) -> Vec> + where + F: FnMut(&EntityData) -> bool, + { + self.entity_storage.read().entities_by(|e| f(e)) } pub fn set_entity_pos(&self, entity_id: u32, new_pos: Vec3) -> Result<(), MoveEntityError> { diff --git a/azalea/examples/pvp.rs b/azalea/examples/pvp.rs index 76507e64..9d2fdc35 100755 --- a/azalea/examples/pvp.rs +++ b/azalea/examples/pvp.rs @@ -50,7 +50,7 @@ async fn swarm_handle( if let Some(target) = swarm .worlds .read() - .find_entity(|e| e.id == "minecraft:player") + .entity_by(|e| e.id == "minecraft:player") { for (bot, bot_state) in swarm { bot.tick_goto_goal(pathfinder::Goals::Reach(target.bounding_box));