add error handling to Click functions
This commit is contained in:
parent
814b48a485
commit
3b6fc48a0f
@ -1,3 +1,4 @@
|
||||
use crate::InventoryError;
|
||||
use pumpkin_world::item::ItemStack;
|
||||
|
||||
pub struct Click {
|
||||
@ -6,104 +7,95 @@ pub struct Click {
|
||||
}
|
||||
|
||||
impl Click {
|
||||
pub fn new(mode: u8, button: i8, slot: i16) -> Self {
|
||||
pub fn new(mode: u8, button: i8, slot: i16) -> Result<Self, InventoryError> {
|
||||
match mode {
|
||||
0 => Self::new_normal_click(button, slot),
|
||||
// Both buttons do the same here, so we omit it
|
||||
1 => Self::new_shift_click(slot),
|
||||
2 => Self::new_key_click(button, slot),
|
||||
3 => Self {
|
||||
3 => Ok(Self {
|
||||
click_type: ClickType::CreativePickItem,
|
||||
slot: Slot::Normal(slot.try_into().unwrap()),
|
||||
},
|
||||
slot: Slot::Normal(slot.try_into().or(Err(InventoryError::InvalidSlot))?),
|
||||
}),
|
||||
4 => Self::new_drop_item(button),
|
||||
5 => Self::new_drag_item(button, slot),
|
||||
6 => Self {
|
||||
6 => Ok(Self {
|
||||
click_type: ClickType::DoubleClick,
|
||||
slot: Slot::Normal(slot.try_into().unwrap()),
|
||||
},
|
||||
// TODO: Error handling
|
||||
_ => unreachable!(),
|
||||
slot: Slot::Normal(slot.try_into().or(Err(InventoryError::InvalidSlot))?),
|
||||
}),
|
||||
_ => Err(InventoryError::InvalidPacket),
|
||||
}
|
||||
}
|
||||
|
||||
fn new_normal_click(button: i8, slot: i16) -> Self {
|
||||
fn new_normal_click(button: i8, slot: i16) -> Result<Self, InventoryError> {
|
||||
let slot = match slot {
|
||||
-999 => Slot::OutsideInventory,
|
||||
_ => {
|
||||
// TODO: Error here
|
||||
let slot = slot.try_into();
|
||||
if let Ok(slot) = slot {
|
||||
let slot = slot.try_into().or(Err(InventoryError::InvalidSlot))?;
|
||||
Slot::Normal(slot)
|
||||
} else {
|
||||
Slot::OutsideInventory
|
||||
}
|
||||
}
|
||||
};
|
||||
let button = match button {
|
||||
0 => MouseClick::Left,
|
||||
1 => MouseClick::Right,
|
||||
// TODO: Error here
|
||||
_ => unreachable!(),
|
||||
_ => Err(InventoryError::InvalidPacket)?,
|
||||
};
|
||||
Self {
|
||||
Ok(Self {
|
||||
click_type: ClickType::MouseClick(button),
|
||||
slot,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn new_shift_click(slot: i16) -> Self {
|
||||
Self {
|
||||
// TODO: Error handle this
|
||||
slot: Slot::Normal(slot.try_into().unwrap()),
|
||||
fn new_shift_click(slot: i16) -> Result<Self, InventoryError> {
|
||||
Ok(Self {
|
||||
slot: Slot::Normal(slot.try_into().or(Err(InventoryError::InvalidSlot))?),
|
||||
click_type: ClickType::ShiftClick,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn new_key_click(button: i8, slot: i16) -> Self {
|
||||
fn new_key_click(button: i8, slot: i16) -> Result<Self, InventoryError> {
|
||||
let key = match button {
|
||||
0..9 => KeyClick::Slot(button.try_into().unwrap()),
|
||||
0..9 => KeyClick::Slot(button.try_into().or(Err(InventoryError::InvalidSlot))?),
|
||||
40 => KeyClick::Offhand,
|
||||
// TODO: Error handling here
|
||||
_ => unreachable!(),
|
||||
_ => Err(InventoryError::InvalidSlot)?,
|
||||
};
|
||||
|
||||
Self {
|
||||
Ok(Self {
|
||||
click_type: ClickType::KeyClick(key),
|
||||
slot: Slot::Normal(slot.try_into().unwrap()),
|
||||
}
|
||||
slot: Slot::Normal(slot.try_into().or(Err(InventoryError::InvalidSlot))?),
|
||||
})
|
||||
}
|
||||
|
||||
fn new_drop_item(button: i8) -> Self {
|
||||
fn new_drop_item(button: i8) -> Result<Self, InventoryError> {
|
||||
let drop_type = match button {
|
||||
0 => DropType::SingleItem,
|
||||
1 => DropType::FullStack,
|
||||
// TODO: Error handling
|
||||
_ => unreachable!(),
|
||||
_ => Err(InventoryError::InvalidPacket)?,
|
||||
};
|
||||
Self {
|
||||
Ok(Self {
|
||||
click_type: ClickType::DropType(drop_type),
|
||||
slot: Slot::OutsideInventory,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn new_drag_item(button: i8, slot: i16) -> Self {
|
||||
fn new_drag_item(button: i8, slot: i16) -> Result<Self, InventoryError> {
|
||||
let state = match button {
|
||||
0 => MouseDragState::Start(MouseDragType::Left),
|
||||
4 => MouseDragState::Start(MouseDragType::Right),
|
||||
8 => MouseDragState::Start(MouseDragType::Middle),
|
||||
1 | 5 | 9 => MouseDragState::AddSlot(slot.try_into().unwrap()),
|
||||
1 | 5 | 9 => {
|
||||
MouseDragState::AddSlot(slot.try_into().or(Err(InventoryError::InvalidSlot))?)
|
||||
}
|
||||
2 | 6 | 10 => MouseDragState::End,
|
||||
// TODO: Error handling
|
||||
_ => unreachable!(),
|
||||
_ => Err(InventoryError::InvalidPacket)?,
|
||||
};
|
||||
Self {
|
||||
Ok(Self {
|
||||
slot: match &state {
|
||||
MouseDragState::AddSlot(slot) => Slot::Normal(*slot),
|
||||
_ => Slot::OutsideInventory,
|
||||
},
|
||||
click_type: ClickType::MouseDrag { drag_state: state },
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -134,7 +134,7 @@ impl Player {
|
||||
.expect("Mode can only be between 0-6"),
|
||||
packet.button,
|
||||
packet.slot,
|
||||
);
|
||||
)?;
|
||||
let mut update_whole_container = false;
|
||||
|
||||
match click.click_type {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user