add debug of mouse_click

This commit is contained in:
Edvin Bryntesson 2024-08-26 08:48:35 +02:00
parent 7b05ded0b3
commit b90fd4c62d
No known key found for this signature in database
4 changed files with 89 additions and 8 deletions

View File

@ -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<ItemChange>,
pub carried_item: Option<Item>,
}
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 },
}

View File

@ -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<const SLOTS: usize> {
slots: [Option<Item>; SLOTS],
state_id: usize,
open_by: Option<Vec<i32>>,
}
impl<const T: usize> ContainerStruct<T> {
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
}
}

View File

@ -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::<Vec<_>>(),
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<const SLOTS: usize> ContainerStruct<SLOTS> {
pub fn opened_by_players(&mut self, server: Server) -> Vec<&Player> {
}
}*/

View File

@ -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),
}
}