diff --git a/src/lib.rs b/src/lib.rs index d8e487b..b494777 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ use std::cmp::{min, max}; mod tests { #[test] - fn development_tests() { + fn development_test() { } } @@ -27,6 +27,10 @@ pub struct Player<'a> { conn:&'a mut Connection } +pub struct Entity<'a> { + conn:&'a mut Connection +} + ///Struct used to specify tile positions. #[derive(Debug)] pub struct TileVec3 { @@ -177,6 +181,12 @@ impl Minecraft { conn: &mut self.conn } } + + pub fn entity(&mut self) -> Entity { + Entity { + conn: &mut self.conn + } + } } ///# Panics /// All functions implemented on the Player struct might panic if the API is not running anymore or packages fail to send. @@ -184,8 +194,7 @@ impl Minecraft { impl Player<'_> { ///Get the position of the main player pub fn get_pos(&mut self) -> Vec3 { - let vec:Vec = self.conn.send_receive(&format!("player.getPos()")).split(',').map(|s| s.parse()).collect::, _>>().unwrap(); - Vec3::from_vector(&vec) + Vec3::from_vector(&self.conn.send_receive(&format!("player.getPos()")).split(',').map(|s| s.parse()).collect::, _>>().unwrap()) } ///Set the position of the main player pub fn set_pos(&mut self, pos:&Vec3) { @@ -207,6 +216,26 @@ impl Player<'_> { } } +impl Entity<'_> { + ///Get the position of a player entity + pub fn get_pos(&mut self, id:u16) -> Vec3 { + Vec3::from_vector(&self.conn.send_receive(&format!("entity.getPos({})", id)).split(',').map(|s| s.parse()).collect::, _>>().unwrap()) + } + ///Set the position of a player entity + pub fn set_pos(&mut self, id:u16, pos:&Vec3) { + self.conn.send(&format!("entity.setPos({},{},{},{})", id, pos.x, pos.y, pos.z)); + } + ///Get the tile position of a player entity + pub fn get_tile_pos(&mut self, id:u16) -> TileVec3 { + let vec:Vec = self.conn.send_receive(&format!("entity.getTile({})", id)).split(',').map(|s| s.parse()).collect::, _>>().unwrap(); + TileVec3::from_vector(&vec) + } + ///Set the tile position of a player entity + pub fn set_tile_pos(&mut self, id:u16, pos:&TileVec3) { + self.conn.send(&format!("entity.setTile({},{},{},{})",id, pos.x, pos.y, pos.z)) + } +} + ///Function to create a Minecraft struct. /// Takes a IP adress and a port as arguments. /// # Examples diff --git a/tests/entity.rs b/tests/entity.rs new file mode 100644 index 0000000..fe2cd28 --- /dev/null +++ b/tests/entity.rs @@ -0,0 +1,13 @@ +use mcpi_api::{Vec3, create, TileVec3}; + +#[test] +fn main() { + let mut mc = create("localhost:4711"); + let id = mc.get_player_entity_ids()[0]; + let posp = mc.entity().get_pos(id); + let posp2 = Vec3::from(posp.x, posp.y + 10.0, posp.z); + mc.entity().set_pos(id, &posp2); + let post = mc.entity().get_tile_pos(id); + let post2 = TileVec3::from(post.x, post.y + 10, post.z); + mc.entity().set_tile_pos(id, &post2); +} \ No newline at end of file