use std::convert::Infallible;
use thiserror::Error;
use uuid::Uuid;
use crate::{
client::{DeviceBase, HlapiClient, HlapiClientError},
devices::ItemStack,
};
#[derive(Debug, Clone)]
pub struct ItemHandler {
client: HlapiClient,
uuid: Uuid,
slot_count: u32,
}
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum ItemHandlerError {
#[error("HLAPI client error: {0}")]
HlapiClientError(#[from] HlapiClientError),
#[error("Slot index out of range")]
SlotIndexOutOfRange,
}
impl ItemHandler {
#[must_use]
pub fn get_item_slot_count(&self) -> u32 {
self.slot_count
}
pub fn get_item_slot_limit(&self, slot: u32) -> Result<u32, ItemHandlerError> {
if slot >= self.slot_count {
return Err(ItemHandlerError::SlotIndexOutOfRange);
}
Ok(self.call("getItemSlotLimit", &[&slot as _], &[])?.0)
}
pub fn get_item_stack_in_slot(&self, slot: u32) -> Result<Option<ItemStack>, ItemHandlerError> {
if slot >= self.slot_count {
return Err(ItemHandlerError::SlotIndexOutOfRange);
}
Ok(self.call("getItemStackInSlot", &[&slot as _], &[])?.0)
}
}
impl DeviceBase for ItemHandler {
const TYPE: &'static str = "item_handler";
type Event = Infallible;
fn new(client: HlapiClient, uuid: Uuid) -> Result<Self, HlapiClientError> {
let mut slf = Self {
client,
uuid,
slot_count: 0,
};
slf.slot_count = slf.call("getItemSlotCount", &[], &[])?.0;
Ok(slf)
}
fn uuid_base(&self) -> Uuid {
self.uuid
}
fn client(&self) -> &HlapiClient {
&self.client
}
}