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()

master
blackfur 2021-03-20 19:05:45 +01:00
parent 8fa51daae4
commit f724116215
1 changed files with 66 additions and 11 deletions

View File

@ -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<i32>) -> 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<f32>) -> 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::<u8>().unwrap()
}
pub fn get_block_with_data(&mut self, pos:Vec3) -> Vec<u8> {
pub fn get_block_with_data(&mut self, pos:TileVec3) -> Vec<u8> {
self.conn.send_receive(&format!("world.getBlockWithData({},{},{})", pos.x, pos.y, pos.z)).split(',').map(|s| s.parse()).collect::<Result<Vec<u8>, _>>().unwrap()
}
pub fn get_blocks(&mut self, pos1:Vec3, pos2:Vec3) -> Vec<u8> {
pub fn get_blocks(&mut self, pos1:TileVec3, pos2:TileVec3) -> Vec<u8> {
let mut results:Vec<u8> = 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<Vec<u8>> {
pub fn get_blocks_with_data(&mut self, pos1:TileVec3, pos2:TileVec3) -> Vec<Vec<u8>> {
let mut results:Vec<Vec<u8>> = 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::<i8>().unwrap()
}
@ -112,6 +158,15 @@ impl Minecraft {
pub fn get_player_entity_ids(&mut self) -> Vec<u16> {
self.conn.send_receive(&format!("world.getPlayerIds()")).split("|").map(|s| s.parse()).collect::<Result<Vec<u16>, _>>().unwrap()
}
pub fn get_player_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(&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 {