Skip to content

Commit

Permalink
Propogate player properties
Browse files Browse the repository at this point in the history
  • Loading branch information
StackDoubleFlow committed Aug 19, 2024
1 parent 9d12cef commit 83e3803
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 57 deletions.
24 changes: 19 additions & 5 deletions crates/core/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use byteorder::{BigEndian, ReadBytesExt};
use mchprs_blocks::block_entities::{ContainerType, InventoryEntry};
use mchprs_blocks::items::{Item, ItemStack};
use mchprs_blocks::{BlockDirection, BlockFacing, BlockPos};
use mchprs_network::packets::clientbound::*;
use mchprs_network::packets::{clientbound::*, PlayerProperty};
use mchprs_network::packets::{PacketEncoder, SlotData};
use mchprs_network::{PlayerConn, PlayerPacketSender};
use mchprs_text::{ColorCode, TextComponent, TextComponentBuilder};
Expand Down Expand Up @@ -122,6 +122,7 @@ impl std::fmt::Display for PlayerPos {
pub struct Player {
pub uuid: u128,
pub username: String,
pub properties: Vec<PlayerProperty>,
pub skin_parts: SkinParts,
pub inventory: Vec<Option<ItemStack>>,
/// The selected slot of the player's hotbar (1-9)
Expand Down Expand Up @@ -186,6 +187,7 @@ impl Player {
player_data: PlayerData,
uuid: u128,
username: String,
properties: Vec<PlayerProperty>,
client: PlayerConn,
) -> Player {
// Load inventory
Expand All @@ -207,6 +209,7 @@ impl Player {
Player {
uuid,
username,
properties,
skin_parts: Default::default(),
inventory,
selected_slot: player_data.selected_item_slot as u32,
Expand Down Expand Up @@ -242,7 +245,12 @@ impl Player {

/// This will load the player from the file. If the file does not exist,
/// It will be created.
pub fn load_player(uuid: u128, username: String, client: PlayerConn) -> Player {
pub fn load_player(
uuid: u128,
username: String,
properties: Vec<PlayerProperty>,
client: PlayerConn,
) -> Player {
let filename = format!("./world/players/{:032x}", uuid);
if let Ok(data) = fs::read(&filename) {
let player_data: PlayerData = match bincode::deserialize(&data) {
Expand All @@ -252,13 +260,19 @@ impl Player {
if let Err(err) = fs::rename(&filename, filename.clone() + ".bak") {
error!("Failed to back up player data: {}", err);
}
return Player::from_data(Default::default(), uuid, username, client);
return Player::from_data(
Default::default(),
uuid,
username,
properties,
client,
);
}
};

Player::from_data(player_data, uuid, username, client)
Player::from_data(player_data, uuid, username, properties, client)
} else {
Player::from_data(Default::default(), uuid, username, client)
Player::from_data(Default::default(), uuid, username, properties, client)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ impl Plot {
let mut actions: CPlayerInfoActions = Default::default();
actions.add_player = Some(CPlayerInfoAddPlayer {
name: player_join_info.username,
properties: Vec::new(),
properties: player_join_info.properties,
});
actions.update_gamemode = Some(player_join_info.gamemode.get_id());
actions
Expand Down
17 changes: 11 additions & 6 deletions crates/core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use mchprs_network::packets::serverbound::{
SAcknowledgeFinishConfiguration, SHandshake, SLoginAcknowledged, SLoginPluginResponse,
SLoginStart, SPing, SRequest, ServerBoundPacketHandler, VelocityResponseData,
};
use mchprs_network::packets::{PacketEncoderExt, SlotData, COMPRESSION_THRESHOLD};
use mchprs_network::packets::{PacketEncoderExt, PlayerProperty, SlotData, COMPRESSION_THRESHOLD};
use mchprs_network::{NetworkServer, NetworkState, PlayerPacketSender};
use mchprs_text::TextComponent;
use mchprs_utils::map;
Expand Down Expand Up @@ -100,13 +100,15 @@ pub struct PlayerJoinInfo {
pub username: String,
pub uuid: u128,
pub gamemode: Gamemode,
pub properties: Vec<PlayerProperty>,
}

#[derive(Debug, Clone)]
struct PlayerListEntry {
plot_x: i32,
plot_z: i32,
username: String,
properties: Vec<PlayerProperty>,
gamemode: Gamemode,
}

Expand Down Expand Up @@ -262,6 +264,7 @@ impl MinecraftServer {
plot_x,
plot_z,
username: player.username.clone(),
properties: player.properties.clone(),
gamemode: player.gamemode,
};
self.online_players.insert(player.uuid, player_list_entry);
Expand Down Expand Up @@ -306,7 +309,8 @@ impl MinecraftServer {

let uuid = client.uuid.clone().unwrap();
let username = client.username.clone().unwrap();
let player = Player::load_player(uuid, username, client.into());
let properties = client.properties.clone();
let player = Player::load_player(uuid, username, properties, client.into());

let join_game = CLogin {
entity_id: player.entity_id as i32,
Expand Down Expand Up @@ -351,7 +355,7 @@ impl MinecraftServer {
let mut actions: CPlayerInfoActions = Default::default();
actions.add_player = Some(CPlayerInfoAddPlayer {
name: player.username.clone(),
properties: Vec::new(),
properties: player.properties.clone(),
});
actions.update_gamemode = Some(player.gamemode.get_id());
actions.update_listed = Some(true);
Expand All @@ -361,7 +365,7 @@ impl MinecraftServer {
let mut actions: CPlayerInfoActions = Default::default();
actions.add_player = Some(CPlayerInfoAddPlayer {
name: player.username.clone(),
properties: Vec::new(),
properties: player.properties.clone(),
});
actions.update_gamemode = Some(player.gamemode.get_id());
actions.update_listed = Some(true);
Expand Down Expand Up @@ -489,8 +493,7 @@ impl MinecraftServer {
let login_success = CLoginSuccess {
uuid,
username,
// TODO: send player properties
properties: Vec::new(),
properties: clients[client_idx].properties.clone(),
}
.encode();
clients[client_idx].send_packet(&login_success);
Expand All @@ -505,6 +508,7 @@ impl MinecraftServer {
username: player.username.clone(),
uuid: player.uuid,
gamemode: player.gamemode,
properties: player.properties.clone(),
};
database::ensure_user(&format!("{:032x}", player.uuid), &player.username);
self.broadcaster
Expand Down Expand Up @@ -831,6 +835,7 @@ impl ServerBoundPacketHandler for MinecraftServer {
};

clients[client_idx].uuid = Some(velocity_response.uuid);
clients[client_idx].properties = velocity_response.properties;
self.complete_player_login(client_idx);
}
}
4 changes: 3 additions & 1 deletion crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod nbt_util;
pub mod packets;

use packets::serverbound::ServerBoundPacket;
use packets::{read_packet, PacketEncoder};
use packets::{read_packet, PacketEncoder, PlayerProperty};
use std::net::{Shutdown, TcpListener, TcpStream};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
Expand Down Expand Up @@ -48,6 +48,7 @@ pub struct HandshakingConn {
pub username: Option<String>,
pub uuid: Option<u128>,
pub forwarding_message_id: Option<i32>,
pub properties: Vec<PlayerProperty>,
}

impl HandshakingConn {
Expand Down Expand Up @@ -211,6 +212,7 @@ impl NetworkServer {
username: None,
uuid: None,
forwarding_message_id: None,
properties: vec![],
}),
Err(mpsc::TryRecvError::Empty) => break,
Err(mpsc::TryRecvError::Disconnected) => {
Expand Down
27 changes: 5 additions & 22 deletions crates/network/src/packets/clientbound.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PacketEncoder, PacketEncoderExt, PalettedContainer, SlotData};
use super::{PacketEncoder, PacketEncoderExt, PalettedContainer, PlayerProperty, SlotData};
use crate::nbt_util::{NBTCompound, NBTMap};
use bitvec::bits;
use bitvec::prelude::Lsb0;
Expand Down Expand Up @@ -60,7 +60,7 @@ impl ClientBoundPacket for CPong {
pub struct CLoginSuccess {
pub uuid: u128,
pub username: String,
pub properties: Vec<CPlayerInfoAddPlayerProperty>,
pub properties: Vec<PlayerProperty>,
}

impl ClientBoundPacket for CLoginSuccess {
Expand All @@ -70,7 +70,7 @@ impl ClientBoundPacket for CLoginSuccess {
buf.write_string(16, &self.username);
buf.write_varint(self.properties.len() as i32);
for prop in &self.properties {
prop.encode(&mut buf);
buf.write_player_property(prop);
}
PacketEncoder::new(buf, 0x02)
}
Expand Down Expand Up @@ -890,26 +890,9 @@ impl ClientBoundPacket for CPlayerInfoRemove {
}
}

pub struct CPlayerInfoAddPlayerProperty {
name: String,
value: String,
signature: Option<String>,
}

impl CPlayerInfoAddPlayerProperty {
fn encode(&self, buf: &mut Vec<u8>) {
buf.write_string(32767, &self.name);
buf.write_string(32767, &self.value);
buf.write_bool(self.signature.is_some());
if let Some(signature) = &self.signature {
buf.write_string(32767, signature);
}
}
}

pub struct CPlayerInfoAddPlayer {
pub name: String,
pub properties: Vec<CPlayerInfoAddPlayerProperty>,
pub properties: Vec<PlayerProperty>,
}

#[derive(Default)]
Expand Down Expand Up @@ -949,7 +932,7 @@ impl CPlayerInfoActions {
buf.write_string(16, &add_player.name);
buf.write_varint(add_player.properties.len() as i32);
for prop in &add_player.properties {
prop.encode(buf);
buf.write_player_property(prop);
}
}
if let Some(gamemode) = self.update_gamemode {
Expand Down
34 changes: 34 additions & 0 deletions crates/network/src/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub struct SlotData {
pub nbt: Option<NBTCompound>,
}

#[derive(Debug, Clone)]
pub struct PlayerProperty {
pub name: String,
pub value: String,
pub signature: Option<String>,
}

#[derive(Debug)]
pub struct PalettedContainer {
pub bits_per_entry: u8,
Expand Down Expand Up @@ -277,6 +284,21 @@ pub trait PacketDecoderExt: Read + Sized {

Ok(compound)
}

fn read_player_property(&mut self) -> DecodeResult<PlayerProperty> {
Ok(PlayerProperty {
name: self.read_string()?,
value: self.read_string()?,
signature: {
let has_signature = self.read_bool()?;
if has_signature {
Some(self.read_string()?)
} else {
None
}
},
})
}
}

pub trait PacketEncoderExt: Write {
Expand Down Expand Up @@ -397,6 +419,18 @@ pub trait PacketEncoderExt: Write {
self.write_bool(false);
}
}

fn write_player_property(&mut self, player_property: &PlayerProperty)
where
Self: Sized,
{
self.write_string(32767, &player_property.name);
self.write_string(32767, &player_property.value);
self.write_bool(player_property.signature.is_some());
if let Some(signature) = &player_property.signature {
self.write_string(32767, signature);
}
}
}

impl PacketEncoderExt for Vec<u8> {}
Expand Down
25 changes: 3 additions & 22 deletions crates/network/src/packets/serverbound.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{DecodeResult, PacketDecoderExt, SlotData};
use super::{DecodeResult, PacketDecoderExt, PlayerProperty, SlotData};

pub trait ServerBoundPacketHandler {
// Handshaking
Expand Down Expand Up @@ -162,21 +162,14 @@ impl ServerBoundPacket for SLoginPluginResponse {
}
}

#[derive(Debug)]
pub struct VelocityGameProfileProperty {
pub name: String,
pub value: String,
pub signature: Option<String>,
}

#[derive(Debug)]
pub struct VelocityResponseData {
pub signature: Vec<u8>,
pub version: i32,
pub address: String,
pub uuid: u128,
pub username: String,
pub properties: Vec<VelocityGameProfileProperty>,
pub properties: Vec<PlayerProperty>,
}

impl VelocityResponseData {
Expand All @@ -197,19 +190,7 @@ impl VelocityResponseData {
let mut properties = Vec::new();
let len = decoder.read_varint()?;
for _ in 0..len {
let name = decoder.read_string()?;
let value = decoder.read_string()?;
let has_signature = decoder.read_bool()?;
let signature = if has_signature {
Some(decoder.read_string()?)
} else {
None
};
properties.push(VelocityGameProfileProperty {
name,
value,
signature,
});
properties.push(decoder.read_player_property()?);
}
properties
},
Expand Down

0 comments on commit 83e3803

Please sign in to comment.