From b90fd4c62d4e1eab3343517dbf38b7021dc71023 Mon Sep 17 00:00:00 2001 From: Edvin Bryntesson Date: Mon, 26 Aug 2024 08:48:35 +0200 Subject: [PATCH] add debug of mouse_click --- pumpkin-inventory/src/container_click.rs | 11 ++++-- pumpkin-inventory/src/lib.rs | 24 ++++++++++++ pumpkin/src/client/container.rs | 50 +++++++++++++++++++++++- pumpkin/src/entity/player.rs | 12 ++++-- 4 files changed, 89 insertions(+), 8 deletions(-) diff --git a/pumpkin-inventory/src/container_click.rs b/pumpkin-inventory/src/container_click.rs index bcc4647ca..0267c8874 100644 --- a/pumpkin-inventory/src/container_click.rs +++ b/pumpkin-inventory/src/container_click.rs @@ -10,13 +10,12 @@ pub struct Click { pub mode: ClickMode, pub state_id: u32, pub window_type: WindowType, - pub changed_items: Vec<(usize, Item)>, + pub changed_items: Vec, pub carried_item: Option, } impl ClickMode { - fn new(mode: u8, button: i8, slot: i16, total_slots: usize) -> Self { - assert!(slot < total_slots.try_into().unwrap()); + pub fn new(mode: u8, button: i8, slot: i16) -> Self { match mode { 0 => Self::new_normal_click(button, slot), // Both buttons do the same here, so we omit it @@ -167,6 +166,7 @@ pub enum ClickType { }, DoubleClick, } +#[derive(Debug)] pub enum MouseClick { Left, Right, @@ -199,3 +199,8 @@ pub enum MouseDragState { AddSlot, End, } + +pub enum ItemChange { + Remove { slot: usize }, + Add { slot: usize, item: Item }, +} diff --git a/pumpkin-inventory/src/lib.rs b/pumpkin-inventory/src/lib.rs index 3c6bc7fc6..45084c55e 100644 --- a/pumpkin-inventory/src/lib.rs +++ b/pumpkin-inventory/src/lib.rs @@ -53,3 +53,27 @@ impl WindowType { "WINDOW TITLE" } } + +pub trait Container { + const SLOTS: usize; + + fn item_mut(&mut self, slot: usize) -> &mut Item; +} + +pub struct ContainerStruct { + slots: [Option; SLOTS], + state_id: usize, + open_by: Option>, +} + +impl ContainerStruct { + pub fn take_item(&mut self, slot: usize) -> Item { + assert!(slot < T); + let Some(item) = self.slots[slot] else { + panic!() + }; + self.slots[slot] = None; + self.state_id += 1; + item + } +} diff --git a/pumpkin/src/client/container.rs b/pumpkin/src/client/container.rs index db27221e4..d54844b52 100644 --- a/pumpkin/src/client/container.rs +++ b/pumpkin/src/client/container.rs @@ -1,8 +1,8 @@ use pumpkin_core::text::TextComponent; -use pumpkin_inventory::container_click; -use pumpkin_inventory::container_click::KeyClick; +use pumpkin_inventory::container_click::MouseClick; use pumpkin_inventory::window_property::{WindowProperty, WindowPropertyTrait}; use pumpkin_inventory::WindowType; +use pumpkin_inventory::{container_click, ContainerStruct}; use pumpkin_protocol::client::play::{ CCloseContainer, COpenScreen, CSetContainerContent, CSetContainerProperty, CSetContainerSlot, }; @@ -11,6 +11,7 @@ use pumpkin_protocol::slot::Slot; use pumpkin_world::item::Item; use crate::entity::player::Player; +use crate::server::Server; impl Player { pub fn open_container( @@ -104,4 +105,49 @@ impl Player { self.client .send_packet(&CSetContainerProperty::new(window_type as u8, id, value)); } + + pub fn handle_click_container(&mut self, packet: SClickContainer) { + use container_click::*; + let click = Click { + state_id: packet.state_id.0.try_into().unwrap(), + changed_items: packet + .array_of_changed_slots + .into_iter() + .map(|(slot, item)| { + let slot = slot.try_into().unwrap(); + if let Some(item) = item.to_item() { + ItemChange::Add { slot, item } + } else { + ItemChange::Remove { slot } + } + }) + .collect::>(), + window_type: WindowType::from_u8(packet.window_id).unwrap(), + carried_item: packet.carried_item.to_item(), + mode: ClickMode::new( + packet + .mode + .0 + .try_into() + .expect("Mode can only be between 0-6"), + packet.button, + packet.slot, + ), + }; + + match click.mode.click_type { + ClickType::MouseClick(mouse_click) => self.mouse_click(mouse_click), + _ => todo!(), + } + } + + pub fn mouse_click(&mut self, mouse_click: MouseClick) { + dbg!(mouse_click); + } } + +/*impl ContainerStruct { + pub fn opened_by_players(&mut self, server: Server) -> Vec<&Player> { + + } +}*/ diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index 935ba7cd4..cb5ecf52a 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -9,9 +9,10 @@ use pumpkin_protocol::{ bytebuf::packet_id::Packet, client::play::{CGameEvent, CPlayDisconnect, CSyncPlayerPosition, CSystemChatMessage}, server::play::{ - SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SInteract, - SPlayPingRequest, SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation, - SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItemOn, + SChatCommand, SChatMessage, SClickContainer, SClientInformationPlay, SConfirmTeleport, + SInteract, SPlayPingRequest, SPlayerAction, SPlayerCommand, SPlayerPosition, + SPlayerPositionRotation, SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, + SUseItemOn, }, ConnectionState, RawPacket, ServerPacket, VarInt, }; @@ -30,6 +31,7 @@ pub struct Player { pub food: i32, pub food_saturation: f32, pub inventory: PlayerInventory, + pub open_container: Option<()>, // Client side value, Should be not trusted pub on_ground: bool, @@ -63,6 +65,7 @@ impl Player { current_block_destroy_stage: 0, velocity: Vector3::new(0.0, 0.0, 0.0), inventory: PlayerInventory::new(), + open_container: None, gamemode, } } @@ -195,6 +198,9 @@ impl Player { SPlayPingRequest::PACKET_ID => { self.handle_play_ping_request(server, SPlayPingRequest::read(bytebuf).unwrap()) } + SClickContainer::PACKET_ID => { + self.handle_click_container(SClickContainer::read(bytebuf).unwrap()) + } _ => log::error!("Failed to handle player packet id {:#04x}", packet.id.0), } }