Added entity methods

Added entity struct + related methods, also added a test file for all entity methods
This commit is contained in:
blackfur 2021-04-16 11:16:32 +02:00
parent f51468d70b
commit cb6c0e8f0e
2 changed files with 45 additions and 3 deletions

View file

@ -9,7 +9,7 @@ use std::cmp::{min, max};
mod tests { mod tests {
#[test] #[test]
fn development_tests() { fn development_test() {
} }
} }
@ -27,6 +27,10 @@ pub struct Player<'a> {
conn:&'a mut Connection conn:&'a mut Connection
} }
pub struct Entity<'a> {
conn:&'a mut Connection
}
///Struct used to specify tile positions. ///Struct used to specify tile positions.
#[derive(Debug)] #[derive(Debug)]
pub struct TileVec3 { pub struct TileVec3 {
@ -177,6 +181,12 @@ impl Minecraft {
conn: &mut self.conn conn: &mut self.conn
} }
} }
pub fn entity(&mut self) -> Entity {
Entity {
conn: &mut self.conn
}
}
} }
///# Panics ///# Panics
/// All functions implemented on the Player struct might panic if the API is not running anymore or packages fail to send. /// 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<'_> { impl Player<'_> {
///Get the position of the main player ///Get the position of the main player
pub fn get_pos(&mut self) -> Vec3 { pub fn get_pos(&mut self) -> Vec3 {
let vec:Vec<f32> = self.conn.send_receive(&format!("player.getPos()")).split(',').map(|s| s.parse()).collect::<Result<Vec<f32>, _>>().unwrap(); Vec3::from_vector(&self.conn.send_receive(&format!("player.getPos()")).split(',').map(|s| s.parse()).collect::<Result<Vec<f32>, _>>().unwrap())
Vec3::from_vector(&vec)
} }
///Set the position of the main player ///Set the position of the main player
pub fn set_pos(&mut self, pos:&Vec3) { 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::<Result<Vec<f32>, _>>().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<i32> = self.conn.send_receive(&format!("entity.getTile({})", id)).split(',').map(|s| s.parse()).collect::<Result<Vec<i32>, _>>().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. ///Function to create a Minecraft struct.
/// Takes a IP adress and a port as arguments. /// Takes a IP adress and a port as arguments.
/// # Examples /// # Examples

13
tests/entity.rs Normal file
View file

@ -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);
}