Skip to content

Commit

Permalink
feat(player): accept item use events
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspeedfpv committed Nov 26, 2024
1 parent b8078fc commit 266bba6
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/net/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
ConfirmTeleportS, GameEvent, GameEventC, Gamemode, KeepAliveC, LoginPlayC,
PlayerInfoUpdateC, PlayerStatus, SetBorderCenterC, SetBorderSizeC, SetCenterChunkC,
SetPlayerPositionAndRotationS, SetPlayerPositionS, SetTickingStateC, StepTicksC,
SynchronisePositionC,
SynchronisePositionC, UseItemOnS,
},
},
Frame, Packet, PacketState,
Expand Down Expand Up @@ -506,6 +506,11 @@ impl SharedPlayer {
self.check_teleports(Some(packet)).await?;
}

UseItemOnS::ID => {
let packet: UseItemOnS = frame.decode()?;
debug!("{:?}", packet);
}

id => {
debug!(
"Got packet with id {id} from player {}, ignoring",
Expand Down
29 changes: 27 additions & 2 deletions src/protocol/datatypes/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/

use bitfield_struct::bitfield;
use byteorder::{BigEndian, WriteBytesExt};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use color_eyre::eyre::Result;
use thiserror::Error;

use crate::protocol::Encode;
use crate::protocol::{Decode, Encode};

#[derive(Debug)]
pub struct Position {
Expand Down Expand Up @@ -70,13 +70,38 @@ impl TryFrom<&Position> for PackedPosition {
}
}

impl From<PackedPosition> for Position {
fn from(value: PackedPosition) -> Self {
Self {
x: value.x(),
y: value.y(),
z: value.z(),
}
}
}

impl Encode for Position {
fn encode(&self, w: impl std::io::Write) -> Result<()> {
let encoded: PackedPosition = self.try_into()?;
encoded.encode(w)
}
}

impl Decode<'_> for Position {
fn decode(r: &mut &'_ [u8]) -> Result<Self>
where
Self: Sized,
{
let bytes = r.read_i64::<BigEndian>()?;

Ok(Self {
x: (bytes >> 38) as i32,
y: (bytes << 52 >> 52) as i32,
z: (bytes << 26 >> 38) as i32,
})
}
}

impl Encode for PackedPosition {
fn encode(&self, mut w: impl std::io::Write) -> Result<()> {
Ok(w.write_u64::<BigEndian>(self.0)?)
Expand Down
2 changes: 2 additions & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub mod packets {

pub mod play {
mod game_event;
mod interactions;
mod keepalive;
mod login;
mod position;
Expand All @@ -56,6 +57,7 @@ pub mod packets {
mod world;

pub use game_event::*;
pub use interactions::*;
pub use keepalive::*;
pub use login::*;
pub use position::*;
Expand Down
115 changes: 115 additions & 0 deletions src/protocol/packets/play/interactions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2024 Andrew Brower.
* This file is part of Crawlspace.
*
* Crawlspace is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* Crawlspace is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Crawlspace. If not, see
* <https://www.gnu.org/licenses/>.
*/

use byteorder::{BigEndian, ReadBytesExt};

use crate::protocol::{
datatypes::{Position, VarInt},
Decode, Packet,
};

#[derive(Debug)]
pub struct UseItemOnS {
pub hand: Hand,
pub location: Position,
pub face: Face,
pub cursor_x: f32,
pub cursor_y: f32,
pub cursor_z: f32,
pub inside_block: bool,
pub sequence: VarInt,
}

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

#[derive(thiserror::Error, Debug)]
pub enum HandParseError {
#[error("Got unexpected hand index {0}")]
Unexpected(i32),
}

impl TryFrom<VarInt> for Hand {
type Error = HandParseError;

fn try_from(value: VarInt) -> Result<Self, Self::Error> {
match value.0 {
0 => Ok(Hand::Main),
1 => Ok(Hand::Off),
i => Err(HandParseError::Unexpected(i)),
}
}
}

#[derive(Debug)]
pub enum Face {
Bottom,
Top,
North,
South,
East,
West,
}

#[derive(thiserror::Error, Debug)]
pub enum FaceParseError {
#[error("Got unexpected face index {0}")]
Unexpected(i32),
}

impl TryFrom<VarInt> for Face {
type Error = FaceParseError;

fn try_from(value: VarInt) -> Result<Self, Self::Error> {
match value.0 {
0 => Ok(Face::Bottom),
1 => Ok(Face::Top),
2 => Ok(Face::North),
3 => Ok(Face::South),
4 => Ok(Face::East),
5 => Ok(Face::West),
i => Err(FaceParseError::Unexpected(i)),
}
}
}

impl Packet for UseItemOnS {
const ID: i32 = 0x38;
}

impl Decode<'_> for UseItemOnS {
fn decode(r: &mut &'_ [u8]) -> color_eyre::eyre::Result<Self>
where
Self: Sized,
{
Ok(Self {
hand: VarInt::decode(r)?.try_into()?,
location: Position::decode(r)?,
face: VarInt::decode(r)?.try_into()?,
cursor_x: r.read_f32::<BigEndian>()?,
cursor_y: r.read_f32::<BigEndian>()?,
cursor_z: r.read_f32::<BigEndian>()?,
inside_block: r.read_u8()? == 1,
sequence: VarInt::decode(r)?,
})
}
}

0 comments on commit 266bba6

Please sign in to comment.