Skip to content

Commit

Permalink
mcpi-rs update : format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Caviar-X committed Nov 6, 2021
1 parent 19b7e86 commit b6b3aa7
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 117 deletions.
82 changes: 54 additions & 28 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
//!
//! conn.send_s("Hello World");
//! ```
use std::net::{TcpStream, ToSocketAddrs, Shutdown};
use std::io::{Read, Write, BufReader, BufRead};
use std::fmt::Display;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{Shutdown, TcpStream, ToSocketAddrs};
/// The connection struct
pub struct Connection {
socket : TcpStream,
auto_flush: bool
socket: TcpStream,
auto_flush: bool,
}
impl Clone for Connection {
fn clone(&self) -> Self {
Connection {
socket : self.socket.try_clone().expect("Failed to clone"),
auto_flush: self.auto_flush
socket: self.socket.try_clone().expect("Failed to clone"),
auto_flush: self.auto_flush,
}
}
fn clone_from(&mut self, source: &Self) {
Expand All @@ -41,59 +41,85 @@ impl Connection {
///
/// let mut conn = Connection::new(SocketAddr::new(IpAddr::V4(Ipv4Addr(127,0,0,1)),1000));
/// ```
pub fn new<A : ToSocketAddrs>(address : A) -> Connection {
pub fn new<A: ToSocketAddrs>(address: A) -> Connection {
Connection {
socket : TcpStream::connect(address).expect("Couldn't connect to Minecraft, is it running?"),
auto_flush: true
socket: TcpStream::connect(address)
.expect("Couldn't connect to Minecraft, is it running?"),
auto_flush: true,
}
}
pub fn send<T : Display>(self,parts : Vec<T>) {
for (cnt,i) in parts.iter().enumerate() {
self.clone().socket.write_all(i.to_string().as_bytes()).expect("Failed to write from socket");
pub fn send<T: Display>(self, parts: Vec<T>) {
for (cnt, i) in parts.iter().enumerate() {
self.clone()
.socket
.write_all(i.to_string().as_bytes())
.expect("Failed to write from socket");
if cnt == 0 {
self.clone().socket.write_all("(".as_bytes()).expect("Failed to write from socket");
}
else if cnt < parts.len() - 1 {
self.clone().socket.write_all(",".as_bytes()).expect("Failed to write from socket");
self.clone()
.socket
.write_all("(".as_bytes())
.expect("Failed to write from socket");
} else if cnt < parts.len() - 1 {
self.clone()
.socket
.write_all(",".as_bytes())
.expect("Failed to write from socket");
}
}
self.clone().socket.write_all(")\n".as_bytes()).expect("Failed to write from socket");
self.clone()
.socket
.write_all(")\n".as_bytes())
.expect("Failed to write from socket");
if self.auto_flush {
self.flush();
}
}
pub fn send_s<T : Display>(self,str : T) {
pub fn send_s<T: Display>(self, str: T) {
self.clone().drain();
self.clone().socket.write_all(str.to_string().as_bytes()).expect("Failed to write from socket");
self.clone().socket.write_all("\n".as_bytes()).expect("Failed to write from socket");
self.clone()
.socket
.write_all(str.to_string().as_bytes())
.expect("Failed to write from socket");
self.clone()
.socket
.write_all("\n".as_bytes())
.expect("Failed to write from socket");
if self.auto_flush {
self.flush();
}
}
pub fn drain(self) {
self.socket.set_nonblocking(true).expect("Failed to set non-blocking mode");
let mut c : [u8;1] = [0];
self.socket
.set_nonblocking(true)
.expect("Failed to set non-blocking mode");
let mut c: [u8; 1] = [0];
while self.clone().socket.read(&mut c).is_ok() {
eprint!("{}",c[0]);
eprint!("{}", c[0]);
}
}
pub fn flush(mut self) {
self.socket.flush().expect("Failed to flush");
}
pub fn receive(self) -> String {
self.socket.try_clone().unwrap().set_nonblocking(false).unwrap();
let mut b : BufReader<TcpStream> = BufReader::new(self.socket.try_clone().unwrap());
self.socket
.try_clone()
.unwrap()
.set_nonblocking(false)
.unwrap();
let mut b: BufReader<TcpStream> = BufReader::new(self.socket.try_clone().unwrap());
let mut s = String::new();
b.read_line(&mut s).expect("Failed to read line");
s
}
pub fn close(self) {
self.socket.shutdown(Shutdown::Both).expect("Failed to close");
self.socket
.shutdown(Shutdown::Both)
.expect("Failed to close");
}
pub fn auto_flush(mut self,flush : bool) {
pub fn auto_flush(mut self, flush: bool) {
self.auto_flush = flush;
if flush {
self.flush();
}
}
}
}
43 changes: 28 additions & 15 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
use std::any::Any;

pub struct BlockEvent {
pos : (i32,i32,i32),
face : i32,
entity_id : i32
pos: (i32, i32, i32),
face: i32,
entity_id: i32,
}
impl BlockEvent {
pub fn hit(pos : (i32,i32,i32),face : i32,entity_id : i32) -> BlockEvent {
pub fn hit(pos: (i32, i32, i32), face: i32, entity_id: i32) -> BlockEvent {
BlockEvent {
pos,
face,
entity_id
entity_id,
}
}
}
impl ToString for BlockEvent {
fn to_string(&self) -> String {
format!("BlockEvent({:?},{},{},{},{},{}",self.type_id(),self.pos.0,self.pos.1,self.pos.2,self.face,self.entity_id)
format!(
"BlockEvent({:?},{},{},{},{},{}",
self.type_id(),
self.pos.0,
self.pos.1,
self.pos.2,
self.face,
self.entity_id
)
}
}
pub fn decode_xyz(encoded : String) {
let mut ret : (i32,i32,i32) = (0,0,0);
pub fn decode_xyz(encoded: String) {
let mut ret: (i32, i32, i32) = (0, 0, 0);
let mut cnt = 0;
for i in encoded.split("\\,") {
if i.is_empty() || i == " " || i == "\n" {
if i.is_empty() || i == " " || i == "\n" {
continue;
}
match cnt {
0 => {ret.0 = i.parse().expect("Failed to parse");}
1 => {ret.1 = i.parse().expect("Failed to parse");}
2 => {ret.2 = i.parse().expect("Failed to parse");}
_ => break
0 => {
ret.0 = i.parse().expect("Failed to parse");
}
1 => {
ret.1 = i.parse().expect("Failed to parse");
}
2 => {
ret.2 = i.parse().expect("Failed to parse");
}
_ => break,
};
cnt+=1;
cnt += 1;
}
}

39 changes: 16 additions & 23 deletions src/items.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use self::Item::Id;
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq,Hash)]
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum Item {
Id(u32),
}
Expand Down Expand Up @@ -82,33 +82,26 @@ pub const MELON: Item = Id(103);
pub const FENCE_GATE: Item = Id(107);
pub const GLOWING_OBSIDIAN: Item = Id(246);
pub const NETHER_REACTOR_CORE: Item = Id(247);
#[derive(Clone,Ord, PartialOrd, Eq, PartialEq)]
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Block {
id : Item,
data : i32
id: Item,
data: i32,
}
impl Block {
pub fn new(data : i32,id : Item) -> Block {
Block {
id,
data
}
pub fn new(data: i32, id: Item) -> Block {
Block { id, data }
}
pub fn new_without_data(id : Item) -> Block{
Block {
id,
data: 0
}
pub fn new_without_data(id: Item) -> Block {
Block { id, data: 0 }
}
pub fn from_item(id : Item) -> Block {
Block {
id,
data : 0
}
pub fn from_item(id: Item) -> Block {
Block { id, data: 0 }
}
pub fn decode(s : String) -> Block{
pub fn decode(s: String) -> Block {
return if !s.contains(',') {
Block::new_without_data(Id(s.split_whitespace().collect::<Vec<&str>>()[0].parse::<u32>().expect("Failed to parse")))
Block::new_without_data(Id(s.split_whitespace().collect::<Vec<&str>>()[0]
.parse::<u32>()
.expect("Failed to parse")))
} else {
let vec = s.split(',').collect::<Vec<&str>>();
let id = Id(vec[0].parse::<u32>().expect("Failed to parse"));
Expand All @@ -119,6 +112,6 @@ impl Block {
}
impl ToString for Block {
fn to_string(&self) -> String {
format!("{}",self.clone().id.unwrap())
format!("{}", self.clone().id.unwrap())
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod connection;
pub mod items;
pub mod events;
pub mod items;
pub mod minecraft;
pub mod prelude;
Loading

0 comments on commit b6b3aa7

Please sign in to comment.