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