uuid read&write + ver 1.0.6
This commit is contained in:
parent
a74aa5856c
commit
75ad081cfe
@ -7,9 +7,10 @@ license-file = "LICENSE"
|
||||
readme = "README.md"
|
||||
keywords = ["minecraft", "protocol", "packets", "lightweight"]
|
||||
|
||||
version = "0.1.5"
|
||||
version = "0.1.6"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
flate2 = "1.0.30"
|
||||
bytebuffer = "2.2.0"
|
||||
uuid = "1.8.0"
|
@ -38,12 +38,12 @@ fn read_status_response(conn: &mut MCConnTcp) -> Result<String, ProtocolError> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut conn = MCConnTcp::connect("localhost:25565").unwrap();
|
||||
let mut conn = MCConnTcp::connect("sloganmc.ru:25565").unwrap();
|
||||
|
||||
send_handshake(&mut conn, 765, "localhost", 25565, 1).unwrap();
|
||||
send_handshake(&mut conn, 765, "sloganmc.ru", 25565, 1).unwrap();
|
||||
send_status_request(&mut conn).unwrap();
|
||||
|
||||
let motd = read_status_response(&mut conn).unwrap();
|
||||
|
||||
println!("Motd: {}", motd);
|
||||
dbg!(motd);
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
10
src/lib.rs
10
src/lib.rs
@ -3,6 +3,7 @@ use std::net::{TcpStream, ToSocketAddrs};
|
||||
|
||||
use flate2::{Compress, Compression, Decompress, FlushCompress, Status, FlushDecompress};
|
||||
use bytebuffer::ByteBuffer;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub trait Zigzag<T> { fn zigzag(&self) -> T; }
|
||||
impl Zigzag<u8> for i8 { fn zigzag(&self) -> u8 { ((self << 1) ^ (self >> 7)) as u8 } }
|
||||
@ -139,6 +140,12 @@ pub trait DataBufferReader {
|
||||
Err(_) => Err(ProtocolError::ReadError),
|
||||
}
|
||||
}
|
||||
fn read_uuid(&mut self) -> Result<Uuid, ProtocolError> {
|
||||
match self.read_bytes(16)?.try_into() {
|
||||
Ok(i) => Ok(Uuid::from_u128(u128::from_be_bytes(i))),
|
||||
Err(_) => Err(ProtocolError::ReadError),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_usize_varint(&mut self) -> Result<usize, ProtocolError> { read_varint!(usize, self) }
|
||||
fn read_u8_varint(&mut self) -> Result<u8, ProtocolError> { read_varint!(u8, self) }
|
||||
@ -166,6 +173,9 @@ pub trait DataBufferWriter {
|
||||
self.write_usize_varint(bytes.len())?;
|
||||
self.write_bytes(bytes)
|
||||
}
|
||||
fn write_uuid(&mut self, val: &Uuid) -> Result<(), ProtocolError> {
|
||||
self.write_bytes(&val.as_u128().to_be_bytes())
|
||||
}
|
||||
fn write_unsigned_short(&mut self, val: u16) -> Result<(), ProtocolError> {
|
||||
match self.write_bytes(&val.to_be_bytes()) {
|
||||
Ok(_) => Ok(()),
|
||||
|
34
src/main.rs
34
src/main.rs
@ -1,34 +0,0 @@
|
||||
use std::{net::TcpListener, sync::Arc, thread};
|
||||
|
||||
use rust_mc_proto::{DataBufferReader, DataBufferWriter, MCConn, MCConnTcp, MinecraftConnection, Packet, ProtocolError};
|
||||
|
||||
/*
|
||||
|
||||
Example of simple server that sends motd
|
||||
to client like an vanilla minecraft server
|
||||
|
||||
*/
|
||||
|
||||
fn accept_client(mut conn: MCConnTcp) {
|
||||
loop {
|
||||
let packet = match conn.read_packet() {
|
||||
Ok(p) => p,
|
||||
Err(_) => { break },
|
||||
};
|
||||
|
||||
dbg!(packet);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let listener = TcpListener::bind("localhost:25565").unwrap();
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let stream = stream.unwrap();
|
||||
|
||||
|
||||
thread::spawn(move || {
|
||||
accept_client(MinecraftConnection::new(stream));
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user