Player spawning works

This commit is contained in:
Garen Tyler 2021-03-17 21:16:20 -06:00
parent a0cebf6d44
commit 6fa9ee968e
3 changed files with 105 additions and 1 deletions

View File

@ -247,6 +247,66 @@ pub mod other {
Err(io_error("Cannot read MCChat from stream"))
}
}
// TODO: Actually make the MCPosition work.
#[derive(Debug, PartialEq, Clone)]
pub struct MCPosition {
x: MCLong,
y: MCLong,
z: MCLong,
}
impl Display for MCPosition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"({}, {}, {})",
self.x,
self.y,
self.z,
)
}
}
impl TryFrom<Vec<u8>> for MCPosition {
type Error = &'static str;
fn try_from(_bytes: Vec<u8>) -> Result<Self, Self::Error> {
Err("Cannot read MCPosition from bytes")
}
}
impl Into<Vec<u8>> for MCPosition {
fn into(self) -> Vec<u8> {
// Just output
// {"text": "<data>"}
let mut out = vec![];
let mut temp = vec![];
temp.extend_from_slice(
&(
(
((Into::<i64>::into(self.x) & 0x3FFFFFF) << 38)
| ((Into::<i64>::into(self.y) & 0xFFF) << 26)
| (Into::<i64>::into(self.z) & 0x3FFFFFF)
) as u64
).to_be_bytes()
);
// temp.extend_from_slice(&"{\"text\": \"".to_owned().into_bytes());
// temp.extend_from_slice(&self.text.value.into_bytes());
// temp.extend_from_slice(&"\"}".to_owned().into_bytes());
out.extend_from_slice(&Into::<Vec<u8>>::into(MCVarInt::from(temp.len() as i32)));
out.extend_from_slice(&temp);
out
}
}
impl MCPosition {
pub fn new() -> MCPosition {
MCPosition {
x: 0.into(),
y: 0.into(),
z: 0.into(),
}
}
pub async fn read(_t: &mut TcpStream) -> tokio::io::Result<Self> {
Err(io_error("Cannot read MCPosition from stream"))
}
}
}
/// All the numbers, from `i8` and `u8` to `i64` and `u64`, plus `VarInt`s.

View File

@ -225,7 +225,7 @@ impl NetworkClient {
// TODO: S->C Player Position and Look
let playerpositionandlook = PlayerPositionAndLook::new();
// TODO: Retrieve player position from storage.
// playerpositionandlook.write(&mut self.stream).await.unwrap();
playerpositionandlook.write(&mut self.stream).await.unwrap();
debug!("{:?}", playerpositionandlook);
// TODO: S->C Player Info (Add Player action) (1.16?)
// TODO: S->C Player Info (Update latency action) (1.16?)
@ -234,6 +234,9 @@ impl NetworkClient {
// TODO: S->C Chunk Data
// TODO: S->C World Border
// TODO: S->C Spawn Position
let spawnposition = SpawnPosition::new();
spawnposition.write(&mut self.stream).await.unwrap();
debug!("{:?}", spawnposition);
// TODO: S->C Player Position and Look
// TODO: C->S Teleport Confirm
// TODO: C->S Player Position and Look

View File

@ -391,3 +391,44 @@ impl PlayerPositionAndLook {
Ok(())
}
}
// TODO: Actually send the position.
#[derive(Debug, Clone)]
pub struct SpawnPosition {
position: MCPosition,
}
impl Into<Vec<u8>> for SpawnPosition {
fn into(self) -> Vec<u8> {
let mut out = vec![];
let mut temp: Vec<u8> = MCVarInt::from(0x05).into(); // 0x05 Spawn Position.
// temp.extend_from_slice(&Into::<Vec<u8>>::into(self.position));
temp.extend_from_slice(&0u64.to_be_bytes());
out.extend_from_slice(&Into::<Vec<u8>>::into(MCVarInt::from(temp.len() as i32)));
out.extend_from_slice(&temp);
out
}
}
impl TryFrom<Vec<u8>> for SpawnPosition {
type Error = &'static str;
fn try_from(_bytes: Vec<u8>) -> Result<Self, Self::Error> {
Err("unimplemented")
}
}
impl SpawnPosition {
pub fn new() -> Self {
SpawnPosition {
position: MCPosition::new(),
}
}
pub async fn read(t: &mut TcpStream) -> tokio::io::Result<Self> {
let mut spawnposition = SpawnPosition::new();
spawnposition.position = MCPosition::read(t).await?;
Ok(spawnposition)
}
pub async fn write(&self, t: &mut TcpStream) -> tokio::io::Result<()> {
for b in Into::<Vec<u8>>::into(self.clone()) {
write_byte(t, b).await?;
}
Ok(())
}
}