From 9cdf2f6bf6eefc289d9168df591455b85f625068 Mon Sep 17 00:00:00 2001 From: blackfur <64478051+theblackfurguy@users.noreply.github.com> Date: Sun, 21 Mar 2021 16:56:19 +0100 Subject: [PATCH] Added Player struct with implementation for get_player_pos() and set_player_pos(), added player() method on the Minecraft struct, added two tests base.rs for functions in the Minecraft struct and player.rs for functions in the Player struct --- src/lib.rs | 41 +++++++++++++++++++++++------------------ tests/base.rs | 19 +++++++++++++++++++ tests/player.rs | 9 +++++++++ 3 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 tests/base.rs create mode 100644 tests/player.rs diff --git a/src/lib.rs b/src/lib.rs index ae80d36..5412134 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,16 +5,9 @@ use std::cmp::{min, max}; #[cfg(test)] mod tests { - use crate::{create, Vec3}; #[test] fn development_tests() { - assert_eq!(2 + 2, 4); - let pos1 = Vec3::from(-31.0, 7.0, -11.0); - let mut mc = create("localhost:4711"); - mc.post_to_chat("Hello World!"); - println!("{:?}",mc.get_player_pos()); - mc.set_player_pos(pos1) } } @@ -26,14 +19,18 @@ struct Connection { stream:TcpStream } -#[derive(Clone, Copy, Debug)] +pub struct Player<'a> { + conn:&'a mut Connection +} + +#[derive(Debug)] pub struct TileVec3 { pub x:i32, pub y:i32, pub z:i32 } -#[derive(Clone, Copy, Debug)] +#[derive(Debug)] pub struct Vec3 { pub x:f32, pub y:f32, @@ -99,15 +96,15 @@ impl Minecraft { self.conn.send(&format!("chat.post({})", msg)); } - pub fn get_block(&mut self, pos:TileVec3) -> u8 { + pub fn get_block(&mut self, pos:&TileVec3) -> u8 { self.conn.send_receive(&format!("world.getBlock({},{},{})", pos.x, pos.y, pos.z)).parse::().unwrap() } - pub fn get_block_with_data(&mut self, pos:TileVec3) -> Vec { + pub fn get_block_with_data(&mut self, pos:&TileVec3) -> Vec { self.conn.send_receive(&format!("world.getBlockWithData({},{},{})", pos.x, pos.y, pos.z)).split(',').map(|s| s.parse()).collect::, _>>().unwrap() } - pub fn get_blocks(&mut self, pos1:TileVec3, pos2:TileVec3) -> Vec { + pub fn get_blocks(&mut self, pos1:&TileVec3, pos2:&TileVec3) -> Vec { let mut results:Vec = vec![]; for y in min(pos1.y, pos2.y)..max(pos1.y, pos2.y)+1 { for x in min(pos1.x, pos2.x)..max(pos1.x, pos2.x)+1 { @@ -119,7 +116,7 @@ impl Minecraft { results } - pub fn get_blocks_with_data(&mut self, pos1:TileVec3, pos2:TileVec3) -> Vec> { + pub fn get_blocks_with_data(&mut self, pos1:&TileVec3, pos2:&TileVec3) -> Vec> { let mut results:Vec> = vec![]; for y in min(pos1.y, pos2.y)..max(pos1.y, pos2.y)+1 { for x in min(pos1.x, pos2.x)..max(pos1.x, pos2.x)+1 { @@ -131,15 +128,15 @@ impl Minecraft { results } - pub fn set_block(&mut self, pos:TileVec3, blocktype:u8, blockdata:u8) { + pub fn set_block(&mut self, pos:&TileVec3, blocktype:u8, blockdata:u8) { self.conn.send(&format!("world.setBlock({},{},{},{},{})", pos.x, pos.y, pos.z, blocktype, blockdata)); } - pub fn set_blocks(&mut self, pos1:TileVec3, pos2:TileVec3, blocktype:u8, blockdata:u8) { + pub fn set_blocks(&mut self, pos1:&TileVec3, pos2:&TileVec3, blocktype:u8, blockdata:u8) { self.conn.send(&format!("world.setBlocks({},{},{},{},{},{},{},{})", pos1.x,pos1.y,pos1.z,pos2.x,pos2.y,pos2.z,blocktype,blockdata)); } - pub fn get_height(&mut self, pos:TileVec3) -> i8 { + pub fn get_height(&mut self, pos:&TileVec3) -> i8 { self.conn.send_receive(&format!("world.getHeight({},{})", pos.x,pos.z)).parse::().unwrap() } @@ -159,12 +156,20 @@ impl Minecraft { self.conn.send_receive(&format!("world.getPlayerIds()")).split("|").map(|s| s.parse()).collect::, _>>().unwrap() } - pub fn get_player_pos(&mut self) -> Vec3 { + pub fn player(&mut self) -> Player { + Player { + conn: &mut self.conn + } + } +} + +impl Player<'_> { + pub fn get_player_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) } - pub fn set_player_pos(&mut self, pos:Vec3) { + pub fn set_player_pos(&mut self, pos:&Vec3) { self.conn.send(&format!("player.setPos({},{},{})", pos.x, pos.y, pos.z)); } } diff --git a/tests/base.rs b/tests/base.rs new file mode 100644 index 0000000..e6d34e8 --- /dev/null +++ b/tests/base.rs @@ -0,0 +1,19 @@ +use mcpi::{create, TileVec3, Vec3}; + +#[test] +fn main() { + let mut mc = create("localhost:4711"); + mc.post_to_chat("Hello world!"); + let pos1 = TileVec3::from(-31,6,-11); + let pos2 = TileVec3::from(-32,6,-12); + let pos3 = TileVec3::from(-31,7,-11); + let pos4 = TileVec3::from(-30,8,-10); + println!("{:?}",mc.get_player_entity_ids()); + println!("{:?}", mc.get_block(&pos1)); + println!("{:?}", mc.get_block_with_data(&pos1)); + println!("{:?}", mc.get_blocks(&pos1, &pos2)); + println!("{:?}", mc.get_blocks_with_data(&pos1, &pos2)); + mc.set_blocks(&pos3, &pos4, 2,0); + mc.set_block(&pos3, 1,0); + println!("{:?}", mc.get_height(&pos4)); +} \ No newline at end of file diff --git a/tests/player.rs b/tests/player.rs new file mode 100644 index 0000000..f39bab5 --- /dev/null +++ b/tests/player.rs @@ -0,0 +1,9 @@ +use mcpi::{Vec3, create}; + +#[test] +fn main() { + let mut mc = create("localhost:4711"); + let posp = mc.player().get_player_pos(); + let posp2 = Vec3::from(posp.x, posp.y + 10.0, posp.z); + mc.player().set_player_pos(&posp2); +} \ No newline at end of file