Skip to content

Commit

Permalink
Add Serverbound Player Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 6, 2024
1 parent 81f82a4 commit d1c3b2f
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 88 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pumpkin-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ serde_json = "1.0"

thiserror = "1.0.63"
log = "0.4"
num-traits = "0.2"
num-derive = "0.4"

# for text component
fastnbt = { git = "https://github.com/owengage/fastnbt.git" }
Expand Down
32 changes: 32 additions & 0 deletions pumpkin-protocol/src/server/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

use crate::{ServerPacket, VarInt};

pub struct SConfirmTeleport {
Expand Down Expand Up @@ -48,6 +51,35 @@ impl ServerPacket for SPlayerPosition {
}
}

pub struct SPlayerCommand {
pub entitiy_id: VarInt,
pub action: Action,
pub jump_boost: VarInt,
}
#[derive(FromPrimitive)]
pub enum Action {
StartSneaking = 0,
StopSneaking,
LeaveBed,
StartSprinting,
StopSprinting,
StartHourseJump,
OpenVehicleInventory,
StartFlyingElytra,
}

impl ServerPacket for SPlayerCommand {
const PACKET_ID: VarInt = 0x25;

fn read(bytebuf: &mut crate::bytebuf::ByteBuffer) -> Self {
Self {
entitiy_id: bytebuf.get_var_int(),
action: Action::from_i32(bytebuf.get_var_int()).unwrap(),
jump_boost: bytebuf.get_var_int(),
}
}
}

pub struct SPlayerPositionRotation {
pub x: f64,
pub feet_y: f64,
Expand Down
2 changes: 2 additions & 0 deletions pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ toml = "0.8.19"

rand = "0.8.5"

num-traits = "0.2"
num-derive = "0.4"
num-bigint = "0.4.6"

# encryption
Expand Down
5 changes: 3 additions & 2 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use num_traits::FromPrimitive;
use pumpkin_protocol::{
client::{
config::{CFinishConfig, CKnownPacks, CRegistryData},
Expand Down Expand Up @@ -140,10 +141,10 @@ impl Client {
self.config = Some(PlayerConfig {
locale: client_information.locale,
view_distance: client_information.view_distance,
chat_mode: ChatMode::from(client_information.chat_mode),
chat_mode: ChatMode::from_i32(client_information.chat_mode).unwrap(),
chat_colors: client_information.chat_colors,
skin_parts: client_information.skin_parts,
main_hand: Hand::from(client_information.main_hand),
main_hand: Hand::from_i32(client_information.main_hand).unwrap(),
text_filtering: client_information.text_filtering,
server_listing: client_information.server_listing,
});
Expand Down
10 changes: 7 additions & 3 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{

use authentication::GameProfile;
use mio::{event::Event, net::TcpStream, Token};
use num_traits::ToPrimitive;
use pumpkin_protocol::{
client::{
config::CConfigDisconnect,
Expand All @@ -25,8 +26,8 @@ use pumpkin_protocol::{
handshake::SHandShake,
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
play::{
SChatCommand, SConfirmTeleport, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation,
SChatCommand, SConfirmTeleport, SPlayerCommand, SPlayerPosition,
SPlayerPositionRotation, SPlayerRotation,
},
status::{SPingRequest, SStatusRequest},
},
Expand Down Expand Up @@ -139,7 +140,7 @@ impl Client {
}

pub fn set_gamemode(&mut self, gamemode: GameMode) {
self.send_packet(CGameEvent::new(3, gamemode.to_byte() as f32))
self.send_packet(CGameEvent::new(3, gamemode.to_f32().unwrap()))
.unwrap_or_else(|e| self.kick(&e.to_string()));
}

Expand Down Expand Up @@ -241,6 +242,9 @@ impl Client {
SPlayerRotation::PACKET_ID => {
self.handle_rotation(server, SPlayerRotation::read(bytebuf))
}
SPlayerCommand::PACKET_ID => {
self.handle_player_command(server, SPlayerCommand::read(bytebuf))
}
_ => log::error!("Failed to handle player packet id {}", packet.id),
}
}
Expand Down
22 changes: 21 additions & 1 deletion pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use pumpkin_protocol::server::play::{
SChatCommand, SConfirmTeleport, SPlayerPosition, SPlayerPositionRotation, SPlayerRotation,
SChatCommand, SConfirmTeleport, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation,
};

use crate::{
Expand Down Expand Up @@ -72,4 +73,23 @@ impl Client {
pub fn handle_chat_command(&mut self, server: &mut Server, command: SChatCommand) {
handle_command(&mut CommandSender::Player(self), command.command, server);
}

pub fn handle_player_command(&mut self, _server: &mut Server, command: SPlayerCommand) {
let player = self.player.as_mut().unwrap();

if command.entitiy_id != player.entity.entity_id {
return;
}

match command.action {
pumpkin_protocol::server::play::Action::StartSneaking => player.sneaking = true,
pumpkin_protocol::server::play::Action::StopSneaking => player.sneaking = false,
pumpkin_protocol::server::play::Action::LeaveBed => todo!(),
pumpkin_protocol::server::play::Action::StartSprinting => player.sprinting = true,
pumpkin_protocol::server::play::Action::StopSprinting => player.sprinting = false,
pumpkin_protocol::server::play::Action::StartHourseJump => todo!(),
pumpkin_protocol::server::play::Action::OpenVehicleInventory => todo!(),
pumpkin_protocol::server::play::Action::StartFlyingElytra => todo!(),
}
}
}
22 changes: 18 additions & 4 deletions pumpkin/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@ const CURRENT_BASE_VERSION: &str = "1.0.0";
pub struct AdvancedConfiguration {
/// Requires Online mode
/// Should player have skins
use_skins: bool,
pub use_skins: bool,
/// Should chat be enabled
enable_chat: bool,
pub enable_chat: bool,

pub commands: Commands,
}

#[derive(Deserialize, Serialize)]
pub struct Commands {
// Are commands from the Console accepted ?
pub use_console: bool,
}

impl Default for Commands {
fn default() -> Self {
Self { use_console: true }
}
}

/// Important: The Configuration should match Vanilla by default
Expand All @@ -25,6 +39,7 @@ impl Default for AdvancedConfiguration {
Self {
use_skins: true,
enable_chat: true,
commands: Commands::default(),
}
}
}
Expand Down Expand Up @@ -67,7 +82,6 @@ pub struct BasicConfiguration {
pub default_gamemode: GameMode,
}


impl Default for BasicConfiguration {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -96,7 +110,7 @@ impl AdvancedConfiguration {
pub fn load<P: AsRef<Path>>(path: P) -> AdvancedConfiguration {
if path.as_ref().exists() {
let toml = std::fs::read_to_string(path).expect("Couldn't read configuration");
toml::from_str(toml.as_str()).expect("Couldn't parse")
toml::from_str(toml.as_str()).expect("Couldn't parse, Proberbly old config")
} else {
let config = AdvancedConfiguration::default();
let toml = toml::to_string(&config).expect("Couldn't create toml!");
Expand Down
67 changes: 10 additions & 57 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::str::FromStr;

use num_derive::{FromPrimitive, ToPrimitive};
use pumpkin_protocol::VarInt;
use serde::{Deserialize, Serialize};

Expand All @@ -16,6 +17,9 @@ pub struct Player {
// Client side value, Should be not trusted
pub on_ground: bool,

pub sneaking: bool,
pub sprinting: bool,

// Current awaiting teleport id, None if did not teleport
pub awaiting_teleport: Option<VarInt>,
}
Expand All @@ -31,6 +35,8 @@ impl Player {
pitch: 0.0,
on_ground: false,
awaiting_teleport: None,
sneaking: false,
sprinting: false,
}
}

Expand All @@ -39,47 +45,22 @@ impl Player {
}
}

#[derive(FromPrimitive)]
pub enum Hand {
Main,
Off,
}

impl From<VarInt> for Hand {
fn from(value: VarInt) -> Self {
match value {
0 => Self::Off,
1 => Self::Main,
_ => {
log::info!("Unexpected Hand {}", value);
Self::Main
}
}
}
}

#[derive(FromPrimitive)]
pub enum ChatMode {
Enabled,
CommandsOnly,
Hidden,
}

impl From<VarInt> for ChatMode {
fn from(value: VarInt) -> Self {
match value {
0 => Self::Enabled,
1 => Self::CommandsOnly,
2 => Self::Hidden,
_ => {
log::info!("Unexpected ChatMode {}", value);
Self::Enabled
}
}
}
}

#[derive(Clone, Copy, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, PartialEq, Serialize, Deserialize, FromPrimitive, ToPrimitive)]
pub enum GameMode {
Undefined,
Undefined = -1,
Survival,
Creative,
Adventure,
Expand All @@ -102,31 +83,3 @@ impl FromStr for GameMode {
}
}
}

impl From<i8> for GameMode {
fn from(value: i8) -> Self {
match value {
-1 => GameMode::Undefined,
0 => GameMode::Survival,
1 => GameMode::Creative,
2 => GameMode::Adventure,
3 => GameMode::Spectator,
_ => {
log::info!("Unexpected GameMode {}", value);
Self::Survival
}
}
}
}

impl GameMode {
pub fn to_byte(self) -> i8 {
match self {
Self::Undefined => -1,
Self::Survival => 0,
Self::Creative => 1,
Self::Adventure => 2,
Self::Spectator => 3,
}
}
}
Loading

0 comments on commit d1c3b2f

Please sign in to comment.