Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const MESSAGES: &[&str] = &["This is a chat message!", "Wow", "Server = on?"];

#[cfg(unix)]
const UDS_PREFIX: &str = "unix://";
const PROTOCOL_VERSION: u32 = 767;
const PROTOCOL_VERSION: u32 = 770;

type Error = Box<dyn std::error::Error + Send + Sync>;

Expand Down
8 changes: 4 additions & 4 deletions src/packet_processors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ pub fn lookup_packet(state: ProtocolState, packet: u8) -> Option<PacketHandler>

ProtocolState::Play => {
match packet {
0x16 => return Some(play::process_cookie_request_packet), // KEEP_ALIVE
0x15 => return Some(config::process_cookie_request_packet), // KEEP_ALIVE
0x26 => return Some(play::process_keep_alive_packet), // KEEP_ALIVE
0x2B => return Some(play::process_join_game), // JOIN_GAME
0x1D => return Some(play::process_kick), // DISCONNECT
0x40 => return Some(play::process_teleport), // PLAYER_POSITION_AND_LOOK
0x73 => return Some(config::process_transfer),
0x1C => return Some(play::process_kick), // DISCONNECT
0x41 => return Some(play::process_teleport), // PLAYER_POSITION_AND_LOOK
0x7A => return Some(config::process_transfer),
_ => {}
}
}
Expand Down
29 changes: 27 additions & 2 deletions src/states/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::usize;

use crate::{packet_utils::Buf, Bot, Compression, ProtocolState};

/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Cookie_Request_(configuration)
pub fn process_cookie_request_packet(buf: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
let identifier = buf.read_sized_string();
bot.send_packet(write_cookie_response(identifier), compression);
}

/// Finish Configuration
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Finish_Configuration
pub fn process_finish_configuration(
_buffer: &mut Buf,
bot: &mut Bot,
Expand All @@ -19,23 +19,28 @@ pub fn process_finish_configuration(
}

/// Clientbound Keep Alive (configuration)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Clientbound_Keep_Alive_(configuration)
pub fn process_keep_alive_packet(buffer: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
bot.send_packet(write_keep_alive_packet(buffer.read_u64()), compression);
}

/// Ping (configuration)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Ping_(configuration)
pub fn process_ping(buffer: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
bot.send_packet(write_pong(buffer.read_u32()), compression);
}

/// Add Resource Pack (configuration)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Add_Resource_Pack_(configuration)
pub fn process_resource_pack(buffer: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
bot.send_packet(
write_acknowledge_resource_pack(buffer.read_u128()),
compression,
);
}

/// Transfer (configuration)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Transfer_(configuration)
pub fn process_transfer(buffer: &mut Buf, bot: &mut Bot, _compression: &mut Compression) {
// Goofy lifetimes
let address = buffer.read_sized_string().to_owned();
Expand All @@ -45,6 +50,8 @@ pub fn process_transfer(buffer: &mut Buf, bot: &mut Bot, _compression: &mut Comp
bot.kicked = true;
}

/// Known Packs (configuration)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Clientbound_Known_Packs
pub fn process_known_packs(_buffer: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
bot.send_packet(write_known_packets(), compression);
}
Expand Down Expand Up @@ -118,14 +125,32 @@ pub fn write_client_settings() -> Buf {
let mut buf = Buf::new();
buf.write_packet_id(0x00);

// locale
buf.write_sized_str("en_US");

// view distance
buf.write_u8(VIEW_DISTANCE);

// chat mode
buf.write_var_u32(0);

// chat colors
buf.write_bool(true);

// skin flags
buf.write_u8(0xFF);

// main hand
buf.write_var_u32(1);

// enable text filtering
buf.write_bool(false);

// allow server listing
buf.write_bool(true);

// particle status
buf.write_var_u32(0);

buf
}
7 changes: 5 additions & 2 deletions src/states/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn write_cookie_response(identifier: &str) -> Buf {
}

//s2c

//https://minecraft.wiki/w/Java_Edition_protocol/Packets#Encryption_Request
pub fn process_encryption_request_packet(
_buffer: &mut Buf,
bot: &mut Bot,
Expand All @@ -72,6 +72,7 @@ pub fn process_encryption_request_packet(
}

/// Login Success
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Login_Success
pub fn process_login_success_packet(
buffer: &mut Buf,
bot: &mut Bot,
Expand All @@ -80,7 +81,6 @@ pub fn process_login_success_packet(
let _uuid = buffer.read_u128();
let _name = buffer.read_sized_string();
let _properties = buffer.read_var_u32();
let _strict_error_handeling = buffer.read_bool();

bot.state = ProtocolState::Config;

Expand All @@ -89,6 +89,7 @@ pub fn process_login_success_packet(
}

/// Set Compression
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Set_Compression
pub fn process_set_compression_packet(
buf: &mut Buf,
bot: &mut Bot,
Expand All @@ -97,11 +98,13 @@ pub fn process_set_compression_packet(
bot.compression_threshold = buf.read_var_u32().0 as i32;
}

/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Login_Plugin_Request
pub fn process_plugin_message_request(buf: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
let identifier = buf.read_var_u32().0;
bot.send_packet(write_plugin_message_response(identifier), compression);
}

/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Cookie_Request_(login)
pub fn process_cookie_request_packet(buf: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
let identifier = buf.read_sized_string();
bot.send_packet(write_cookie_response(identifier), compression);
Expand Down
48 changes: 19 additions & 29 deletions src/states/play.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
use crate::packet_utils::Buf;
use crate::{Bot, Compression};

/// Cookie Request (play)
pub fn process_cookie_request_packet(
buffer: &mut Buf,
bot: &mut Bot,
compression: &mut Compression,
) {
let identifier = buffer.read_sized_string();
bot.send_packet(write_cookie_response(identifier), compression);
}

/// Clientbound Keep Alive (play)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Clientbound_Keep_Alive_(play)
pub fn process_keep_alive_packet(buffer: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
bot.send_packet(write_keep_alive_packet(buffer.read_u64()), compression);
}

/// Disconnect (login/config/play)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Disconnect_(login)
pub fn process_kick(buffer: &mut Buf, bot: &mut Bot, _compression: &mut Compression) {
println!("bot was kicked for \"{}\"", buffer.read_sized_string());
bot.kicked = true;
}

/// Login (play)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Login_(play)
pub fn process_join_game(buffer: &mut Buf, bot: &mut Bot, _compression: &mut Compression) {
bot.entity_id = buffer.read_u32();
}

/// Synchronize Player Position
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Synchronize_Player_Position
pub fn process_teleport(buffer: &mut Buf, bot: &mut Bot, compression: &mut Compression) {
let x = buffer.read_f64();
let y = buffer.read_f64();
Expand All @@ -54,22 +48,12 @@ pub fn process_teleport(buffer: &mut Buf, bot: &mut Bot, compression: &mut Compr
bot.teleported = true;
}

/// Cookie Response (play)
pub fn write_cookie_response(identifier: &str) -> Buf {
let mut buf = Buf::new();
buf.write_packet_id(0x11);

buf.write_sized_str(identifier);
buf.write_bool(false);

buf
}

/// Chat Message
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Chat_Message
pub fn write_chat_message(message: &str) -> Buf {
// ClientChatMessagePacket
let mut buf = Buf::new();
buf.write_packet_id(0x06);
buf.write_packet_id(0x07);

buf.write_sized_str(message);

Expand All @@ -79,26 +63,28 @@ pub fn write_chat_message(message: &str) -> Buf {
buf.write_bool(false); // has signature
buf.write_var_u32(0); // count
buf.write_bytes(&[0; 3]); // bitset
buf.write_var_u32(0); // signature count

buf
}

/// Swing Arm
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Swing_Arm
pub fn write_animation(off_hand: bool) -> Buf {
// ClientAnimationPacket
let mut buf = Buf::new();
buf.write_packet_id(0x36);

buf.write_packet_id(0x3B);
buf.write_var_u32(if off_hand { 1 } else { 0 });

buf
}

/// Player Command
/// Player Action (serverbound)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Player_Command
pub fn write_entity_action(entity_id: u32, action_id: u32, jump_boost: u32) -> Buf {
// ClientEntityActionPacket
let mut buf = Buf::new();
buf.write_packet_id(0x25);
buf.write_packet_id(0x28);

buf.write_var_u32(entity_id);
buf.write_var_u32(action_id);
Expand All @@ -108,17 +94,19 @@ pub fn write_entity_action(entity_id: u32, action_id: u32, jump_boost: u32) -> B
}

/// Set Held Item (serverbound)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Set_Held_Item_(serverbound)
pub fn write_held_slot(slot: u16) -> Buf {
// ClientHeldItemChangePacket
let mut buf = Buf::new();
buf.write_packet_id(0x2F);
buf.write_packet_id(0x33);

buf.write_u16(slot);

buf
}

/// Confirm Teleportation
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Confirm_Teleportation
pub fn write_tele_confirm(id: u32) -> Buf {
// ClientTeleportConfirmPacket
let mut buf = Buf::new();
Expand All @@ -130,10 +118,11 @@ pub fn write_tele_confirm(id: u32) -> Buf {
}

/// Serverbound Keep Alive (play)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Serverbound_Keep_Alive_(play)
pub fn write_keep_alive_packet(id: u64) -> Buf {
// ClientKeepAlivePacket
let mut buf = Buf::new();
buf.write_packet_id(0x18);
buf.write_packet_id(0x1a);

buf.write_u64(id);

Expand All @@ -145,10 +134,11 @@ pub fn write_current_pos(bot: &Bot) -> Buf {
}

/// Set Player Position and Rotation
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Set_Player_Position_and_Rotation
pub fn write_pos(x: f64, y: f64, z: f64, yaw: f32, pitch: f32) -> Buf {
// ClientPlayerPositionAndRotationPacket
let mut buf = Buf::new();
buf.write_packet_id(0x1B);
buf.write_packet_id(0x1D);

buf.write_f64(x);
buf.write_f64(y);
Expand Down
2 changes: 2 additions & 0 deletions src/states/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use crate::packet_utils::Buf;
use crate::{Bot, Compression};

/// Status Response
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Status_Response
pub fn process_status_response(buffer: &mut Buf, _bot: &mut Bot, _compression: &mut Compression) {
let server_response = buffer.read_sized_string();
println!("got response {}", server_response)
}

/// Ping Response (status)
/// https://minecraft.wiki/w/Java_Edition_protocol/Packets#Pong_Response_(status)
pub fn process_pong(buffer: &mut Buf, _bot: &mut Bot, _compression: &mut Compression) {
let payload = buffer.read_sized_string();
println!("got pong {}", payload)
Expand Down