Skip to content

Commit

Permalink
Add Block Breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 20, 2024
1 parent 7d89a4e commit 66ce63b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 24 deletions.
12 changes: 7 additions & 5 deletions pumpkin-protocol/src/client/play/c_block_destroy_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ pub struct CSetBlockDestroyStage {
}

impl CSetBlockDestroyStage {
pub fn new(entity_id: VarInt,
location: WorldPosition,
destroy_stage: u8) -> Self {
Self { entity_id, location, destroy_stage }
pub fn new(entity_id: VarInt, location: WorldPosition, destroy_stage: u8) -> Self {
Self {
entity_id,
location,
destroy_stage,
}
}
}
}
25 changes: 16 additions & 9 deletions pumpkin-protocol/src/client/play/c_worldevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ use crate::position::WorldPosition;

#[derive(Serialize)]
#[packet(0x28)]
pub struct CWorldEvent {
pub struct CWorldEvent<'a> {
event: i32,
location: WorldPosition,
location: &'a WorldPosition,
data: i32,
disable_relative_volume: bool,
}

impl CWorldEvent {
pub fn new(event: i32,
location: WorldPosition,
data: i32,
disable_relative_volume: bool) -> Self {
Self { event, location, data, disable_relative_volume }
impl<'a> CWorldEvent<'a> {
pub fn new(
event: i32,
location: &'a WorldPosition,
data: i32,
disable_relative_volume: bool,
) -> Self {
Self {
event,
location,
data,
disable_relative_volume,
}
}
}
}
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod c_actionbar;
mod c_block_destroy_stage;
mod c_block_update;
mod c_center_chunk;
mod c_change_difficulty;
Expand Down Expand Up @@ -29,11 +30,10 @@ mod c_update_entitiy_pos_rot;
mod c_update_entity_pos;
mod c_update_entity_rot;
mod c_worldevent;
mod c_block_destroy_stage;
mod player_action;

pub use c_block_destroy_stage::*;
pub use c_actionbar::*;
pub use c_block_destroy_stage::*;
pub use c_block_update::*;
pub use c_center_chunk::*;
pub use c_change_difficulty::*;
Expand Down
29 changes: 24 additions & 5 deletions pumpkin-protocol/src/server/play/s_player_action.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
use num_derive::FromPrimitive;
use pumpkin_macros::packet;

use crate::{position::WorldPosition, VarInt};

#[derive(serde::Deserialize)]
#[allow(dead_code)]
#[packet(0x24)]
pub struct SPlayerAction {
status: VarInt,
location: WorldPosition,
face: u8,
sequence: VarInt,
pub status: VarInt,
pub location: WorldPosition,
pub face: u8,
pub sequence: VarInt,
}

#[derive(FromPrimitive)]
pub enum Status {
/// Sent when the player starts digging a block. If the block was instamined or the player is in creative mode, the client will not send Status = Finished digging, and will assume the server completed the destruction. To detect this, it is necessary to calculate the block destruction speed server-side.
StartedDigging = 0,
/// Sent when the player lets go of the Mine Block key (default: left click). Face is always set to -Y.
CancelledDigging,
/// Sent when the client thinks it is finished.
FinishedDigging,
/// Triggered by using the Drop Item key (default: Q) with the modifier to drop the entire selected stack (default: Control or Command, depending on OS). Location is always set to 0/0/0, Face is always set to -Y. Sequence is always set to 0.
DropItemStack,
/// Triggered by using the Drop Item key (default: Q). Location is always set to 0/0/0, Face is always set to -Y. Sequence is always set to 0.
DropItem,
/// I didn't make that up
/// Indicates that the currently held item should have its state updated such as eating food, pulling back bows, using buckets, etc. Location is always set to 0/0/0, Face is always set to -Y. Sequence is always set to 0.
ShootArrowOrFinishEating,
/// Used to swap or assign an item to the second hand. Location is always set to 0/0/0, Face is always set to -Y. Sequence is always set to 0.
SwapItem,
}
46 changes: 43 additions & 3 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ use pumpkin_entity::EntityId;
use pumpkin_protocol::{
client::play::{
Animation, CBlockUpdate, CEntityAnimation, CEntityVelocity, CHeadRot, CHurtAnimation,
CPlayerChatMessage, CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot, FilterType,
CPlayerChatMessage, CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot, CWorldEvent,
FilterType,
},
position::WorldPosition,
server::play::{
Action, ActionType, SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport,
SInteract, SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItemOn,
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItemOn, Status,
},
};
use pumpkin_text::TextComponent;
Expand Down Expand Up @@ -315,7 +316,46 @@ impl Client {
}
}
}
pub fn handle_player_action(&mut self, _server: &mut Server, _player_action: SPlayerAction) {}
pub fn handle_player_action(&mut self, server: &mut Server, player_action: SPlayerAction) {
match Status::from_i32(player_action.status.0).unwrap() {
Status::StartedDigging => {
let player = self.player.as_mut().unwrap();
// TODO: Config
if player.gamemode == GameMode::Creative {
let location = player_action.location;
// Block break & block break sound
// TODO: currently this is always dirt replace it
server.broadcast_packet(self, &CWorldEvent::new(2001, &location, 11, false));
// AIR
server.broadcast_packet(self, &CBlockUpdate::new(location, 0.into()));
}
}
Status::CancelledDigging => {
let player = self.player.as_mut().unwrap();
player.current_block_destroy_stage = 0;
}
Status::FinishedDigging => {
let location = player_action.location;
// Block break & block break sound
// TODO: currently this is always dirt replace it
server.broadcast_packet(self, &CWorldEvent::new(2001, &location, 11, false));
// AIR
server.broadcast_packet(self, &CBlockUpdate::new(location, 0.into()));
}
Status::DropItemStack => {
dbg!("todo");
}
Status::DropItem => {
dbg!("todo");
}
Status::ShootArrowOrFinishEating => {
dbg!("todo");
}
Status::SwapItem => {
dbg!("todo");
}
}
}

pub fn handle_use_item_on(&mut self, server: &mut Server, use_item_on: SUseItemOn) {
let location = use_item_on.location;
Expand Down
4 changes: 4 additions & 0 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub struct Player {
pub sneaking: bool,
pub sprinting: bool,

// TODO: This is currently unused, We have to calculate the block breaking speed our own and then break the block our own if its done
pub current_block_destroy_stage: u8,

// TODO: prbly should put this into an Living Entitiy or something
pub velocity: Vector3<f64>,

Expand All @@ -42,6 +45,7 @@ impl Player {
health: 20.0,
food: 20,
food_saturation: 20.0,
current_block_destroy_stage: 0,
velocity: Vector3::new(0.0, 0.0, 0.0),
inventory: PlayerInventory::new(),
gamemode,
Expand Down

0 comments on commit 66ce63b

Please sign in to comment.