From 2dad32ba4a5f487cb825adca60b35cec985ff488 Mon Sep 17 00:00:00 2001 From: blackfur <64478051+theblackfurguy@users.noreply.github.com> Date: Sat, 20 Mar 2021 19:05:45 +0100 Subject: [PATCH] Changed Vec3 to TileVec3 and added a Vec3 using f32 type, added ::from() and ::from_vector() to both of them, added get_player_pos() and set_player_pos() --- src/lib.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 00c8c88..ae80d36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,13 +5,16 @@ use std::cmp::{min, max}; #[cfg(test)] mod tests { - use crate::{create}; + use crate::{create, Vec3}; #[test] - fn it_works() { + 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) } } @@ -23,13 +26,56 @@ struct Connection { stream:TcpStream } -#[derive(Clone, Copy)] -pub struct Vec3 { +#[derive(Clone, Copy, Debug)] +pub struct TileVec3 { pub x:i32, pub y:i32, pub z:i32 } +#[derive(Clone, Copy, Debug)] +pub struct Vec3 { + pub x:f32, + pub y:f32, + pub z:f32 +} + +impl TileVec3 { + pub fn from(x:i32, y:i32, z:i32) -> TileVec3 { + TileVec3 { + x, + y, + z + } + } + + pub fn from_vector(vec:&Vec) -> TileVec3 { + TileVec3 { + x: vec[0], + y: vec[1], + z: vec[2] + } + } +} + +impl Vec3 { + pub fn from(x:f32, y:f32, z:f32) -> Vec3 { + Vec3 { + x, + y, + z + } + } + + pub fn from_vector(vec:&Vec) -> Vec3 { + Vec3{ + x: vec[0], + y: vec[1], + z: vec[2] + } + } +} + impl Connection { pub fn send(&mut self, msg:&str) { self.stream.write(&format!("{}\n", msg).as_bytes()).expect("Failed to send! Is MCPI still running?"); @@ -53,15 +99,15 @@ impl Minecraft { self.conn.send(&format!("chat.post({})", msg)); } - pub fn get_block(&mut self, pos:Vec3) -> 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:Vec3) -> 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:Vec3, pos2:Vec3) -> 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 { @@ -73,7 +119,7 @@ impl Minecraft { results } - pub fn get_blocks_with_data(&mut self, pos1:Vec3, pos2:Vec3) -> 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 { @@ -85,15 +131,15 @@ impl Minecraft { results } - pub fn set_block(&mut self, pos:Vec3, 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:Vec3, pos2:Vec3, 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:Vec3) -> i8 { + pub fn get_height(&mut self, pos:TileVec3) -> i8 { self.conn.send_receive(&format!("world.getHeight({},{})", pos.x,pos.z)).parse::().unwrap() } @@ -112,6 +158,15 @@ impl Minecraft { pub fn get_player_entity_ids(&mut self) -> Vec { self.conn.send_receive(&format!("world.getPlayerIds()")).split("|").map(|s| s.parse()).collect::, _>>().unwrap() } + + 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) { + self.conn.send(&format!("player.setPos({},{},{})", pos.x, pos.y, pos.z)); + } } pub fn create(adress:&str) -> Minecraft {