Added documentation and bumped the version to 0.1.1
This commit is contained in:
parent
4f7d46f070
commit
baa9fff16c
|
@ -1,11 +1,11 @@
|
||||||
[package]
|
[package]
|
||||||
name = "mcpi_api"
|
name = "mcpi_api"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["blackfur <64478051+theblackfurguy@users.noreply.github.com>"]
|
authors = ["blackfur <64478051+theblackfurguy@users.noreply.github.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Wrapper for the Minecraft Pi Edition API handling parsing and other aspects for you"
|
description = "Wrapper for the Minecraft Pi Edition API handling parsing and other aspects for you"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
repository = "https://github.com/TheBlackfurGuy/mcpi-api-rust"
|
repository = "https://github.com/MCPI-Revival/mcpi-api-rust"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
54
src/lib.rs
54
src/lib.rs
|
@ -44,6 +44,7 @@ pub struct Vec3 {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TileVec3 {
|
impl TileVec3 {
|
||||||
|
/// Function to generate a TileVec3 from 3 i32's
|
||||||
pub fn from(x:i32, y:i32, z:i32) -> TileVec3 {
|
pub fn from(x:i32, y:i32, z:i32) -> TileVec3 {
|
||||||
TileVec3 {
|
TileVec3 {
|
||||||
x,
|
x,
|
||||||
|
@ -51,7 +52,9 @@ impl TileVec3 {
|
||||||
z
|
z
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
///Function to generate a TileVec3 from a Vec<i32>
|
||||||
|
/// # Panics
|
||||||
|
/// This function panics if the vector contains less then 3 elements
|
||||||
pub fn from_vector(vec:&Vec<i32>) -> TileVec3 {
|
pub fn from_vector(vec:&Vec<i32>) -> TileVec3 {
|
||||||
TileVec3 {
|
TileVec3 {
|
||||||
x: vec[0],
|
x: vec[0],
|
||||||
|
@ -62,6 +65,7 @@ impl TileVec3 {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vec3 {
|
impl Vec3 {
|
||||||
|
///Function to generate a Vec3 from 3 f32's
|
||||||
pub fn from(x:f32, y:f32, z:f32) -> Vec3 {
|
pub fn from(x:f32, y:f32, z:f32) -> Vec3 {
|
||||||
Vec3 {
|
Vec3 {
|
||||||
x,
|
x,
|
||||||
|
@ -69,7 +73,9 @@ impl Vec3 {
|
||||||
z
|
z
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
///Function to generate a Vec3 from a Vec<f32>
|
||||||
|
/// # Panics
|
||||||
|
/// This function panics if the vector contains less then 3 elements
|
||||||
pub fn from_vector(vec:&Vec<f32>) -> Vec3 {
|
pub fn from_vector(vec:&Vec<f32>) -> Vec3 {
|
||||||
Vec3{
|
Vec3{
|
||||||
x: vec[0],
|
x: vec[0],
|
||||||
|
@ -96,20 +102,23 @@ impl Connection {
|
||||||
self.receive()
|
self.receive()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
///# Panics
|
||||||
|
/// All functions implemented on the Minecraft struct might panic if the API is not running anymore or packages fail to send.
|
||||||
|
/// This might change in a 0.2.0 version of this crate
|
||||||
impl Minecraft {
|
impl Minecraft {
|
||||||
|
///Post a message to the chat
|
||||||
pub fn post_to_chat(&mut self, msg:&str) {
|
pub fn post_to_chat(&mut self, msg:&str) {
|
||||||
self.conn.send(&format!("chat.post({})", msg));
|
self.conn.send(&format!("chat.post({})", msg));
|
||||||
}
|
}
|
||||||
|
///Get the block at a specific position
|
||||||
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::<u8>().unwrap()
|
self.conn.send_receive(&format!("world.getBlock({},{},{})", pos.x, pos.y, pos.z)).parse::<u8>().unwrap()
|
||||||
}
|
}
|
||||||
|
///Get the block with data at a specific position
|
||||||
pub fn get_block_with_data(&mut self, pos:&TileVec3) -> 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()
|
self.conn.send_receive(&format!("world.getBlockWithData({},{},{})", pos.x, pos.y, pos.z)).split(',').map(|s| s.parse()).collect::<Result<Vec<u8>, _>>().unwrap()
|
||||||
}
|
}
|
||||||
|
///Get a array of blocks contained in the specified area
|
||||||
pub fn get_blocks(&mut self, pos1:&TileVec3, pos2:&TileVec3) -> Vec<u8> {
|
pub fn get_blocks(&mut self, pos1:&TileVec3, pos2:&TileVec3) -> Vec<u8> {
|
||||||
let mut results:Vec<u8> = vec![];
|
let mut results:Vec<u8> = vec![];
|
||||||
for y in min(pos1.y, pos2.y)..max(pos1.y, pos2.y)+1 {
|
for y in min(pos1.y, pos2.y)..max(pos1.y, pos2.y)+1 {
|
||||||
|
@ -121,7 +130,7 @@ impl Minecraft {
|
||||||
}
|
}
|
||||||
results
|
results
|
||||||
}
|
}
|
||||||
|
///Get a array of blocks with their data contained in the specified area
|
||||||
pub fn get_blocks_with_data(&mut self, pos1:&TileVec3, pos2:&TileVec3) -> 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![];
|
let mut results:Vec<Vec<u8>> = vec![];
|
||||||
for y in min(pos1.y, pos2.y)..max(pos1.y, pos2.y)+1 {
|
for y in min(pos1.y, pos2.y)..max(pos1.y, pos2.y)+1 {
|
||||||
|
@ -133,61 +142,66 @@ impl Minecraft {
|
||||||
}
|
}
|
||||||
results
|
results
|
||||||
}
|
}
|
||||||
|
///Set a block at a specific position
|
||||||
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));
|
self.conn.send(&format!("world.setBlock({},{},{},{},{})", pos.x, pos.y, pos.z, blocktype, blockdata));
|
||||||
}
|
}
|
||||||
|
///Fill the specified area with the a block
|
||||||
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));
|
self.conn.send(&format!("world.setBlocks({},{},{},{},{},{},{},{})", pos1.x,pos1.y,pos1.z,pos2.x,pos2.y,pos2.z,blocktype,blockdata));
|
||||||
}
|
}
|
||||||
|
///Get the highest point at the specified position
|
||||||
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::<i8>().unwrap()
|
self.conn.send_receive(&format!("world.getHeight({},{})", pos.x,pos.z)).parse::<i8>().unwrap()
|
||||||
}
|
}
|
||||||
|
///Save the current world state as a checkpoint
|
||||||
pub fn save_checkpoint(&mut self) {
|
pub fn save_checkpoint(&mut self) {
|
||||||
self.conn.send("world.checkpoint.save()");
|
self.conn.send("world.checkpoint.save()");
|
||||||
}
|
}
|
||||||
|
///Restore a previously saved world state
|
||||||
pub fn restore_checkpoint(&mut self) {
|
pub fn restore_checkpoint(&mut self) {
|
||||||
self.conn.send("world.checkpoint.restore()");
|
self.conn.send("world.checkpoint.restore()");
|
||||||
}
|
}
|
||||||
|
///Set a world setting to true or false.
|
||||||
|
/// Available settings: "world_immutable", "nametags_visible"
|
||||||
pub fn setting(&mut self, setting:&str, status:bool) {
|
pub fn setting(&mut self, setting:&str, status:bool) {
|
||||||
self.conn.send(&format!("world.setting({},{})",setting,if status == true {1} else {0}));
|
self.conn.send(&format!("world.setting({},{})",setting,if status == true {1} else {0}));
|
||||||
}
|
}
|
||||||
|
///Get a list of entity ids for all online players
|
||||||
pub fn get_player_entity_ids(&mut self) -> Vec<u16> {
|
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()
|
self.conn.send_receive(&format!("world.getPlayerIds()")).split("|").map(|s| s.parse()).collect::<Result<Vec<u16>, _>>().unwrap()
|
||||||
}
|
}
|
||||||
|
///Get a instance of the Player struct containing player related functions
|
||||||
pub fn player(&mut self) -> Player {
|
pub fn player(&mut self) -> Player {
|
||||||
Player {
|
Player {
|
||||||
conn: &mut self.conn
|
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.
|
||||||
|
/// This might change in a 0.2.0 version of this crate
|
||||||
impl Player<'_> {
|
impl 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();
|
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)
|
Vec3::from_vector(&vec)
|
||||||
}
|
}
|
||||||
|
///Set the position of the main player
|
||||||
pub fn set_pos(&mut self, pos:&Vec3) {
|
pub fn set_pos(&mut self, pos:&Vec3) {
|
||||||
self.conn.send(&format!("player.setPos({},{},{})", pos.x, pos.y, pos.z));
|
self.conn.send(&format!("player.setPos({},{},{})", pos.x, pos.y, pos.z));
|
||||||
}
|
}
|
||||||
|
///Get the tile position of the main player
|
||||||
pub fn get_tile_pos(&mut self) -> TileVec3 {
|
pub fn get_tile_pos(&mut self) -> TileVec3 {
|
||||||
let vec:Vec<i32> = self.conn.send_receive(&format!("player.getTile()")).split(',').map(|s| s.parse()).collect::<Result<Vec<i32>, _>>().unwrap();
|
let vec:Vec<i32> = self.conn.send_receive(&format!("player.getTile()")).split(',').map(|s| s.parse()).collect::<Result<Vec<i32>, _>>().unwrap();
|
||||||
TileVec3::from_vector(&vec)
|
TileVec3::from_vector(&vec)
|
||||||
}
|
}
|
||||||
|
///Set the tile position of the main player
|
||||||
pub fn set_tile_pos(&mut self, pos:&TileVec3) {
|
pub fn set_tile_pos(&mut self, pos:&TileVec3) {
|
||||||
self.conn.send(&format!("player.setTile({},{},{})", pos.x, pos.y, pos.z))
|
self.conn.send(&format!("player.setTile({},{},{})", pos.x, pos.y, pos.z))
|
||||||
}
|
}
|
||||||
|
///Set a setting for the main player
|
||||||
|
/// Available settings: "autojump"
|
||||||
pub fn setting(&mut self, setting:&str, status:bool) {
|
pub fn setting(&mut self, setting:&str, status:bool) {
|
||||||
self.conn.send(&format!("player.setting({},{})",setting,if status {1} else {0}));
|
self.conn.send(&format!("player.setting({},{})",setting,if status {1} else {0}));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue