oc2_hlapi_v2 0.2.0

A client for the new version of OpenComputers II's HLAPI
Documentation
use std::{convert::Infallible, num::NonZeroU32};

use uuid::Uuid;

use crate::{
    client::{DeviceBase, HlapiClient, HlapiClientError},
    devices::{ItemStack, robot::RobotSide},
};

#[derive(Debug, Clone)]
/// The device for the inventory operations module
///
/// [OC2 Docs](https://fnuecke.github.io/oc2/manual/en_us/device/inventory_operations.html)
pub struct InventoryOperations {
    client: HlapiClient,
    uuid: Uuid,
}

impl InventoryOperations {
    /// Tries to drop the given number of items to the specified side, defaulting to [`RobotSide::Front`].
    /// Items are dropped into the world if there is no inventory to that side. Returns the actual number of
    /// items dropped.
    pub fn drop(&self, count: u32, side: Option<RobotSide>) -> Result<u32, HlapiClientError> {
        Ok(self.call("drop", &[&count as _, &side as _], &[])?.0)
    }

    /// Tries to drop the given number of items into the given slot of the inventory to the specified side,
    /// defaulting to [`RobotSide::Front`]. Returns the actual number of items dropped.
    pub fn drop_into(
        &self,
        slot: u32,
        count: u32,
        side: Option<RobotSide>,
    ) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("dropInto", &[&slot as _, &count as _, &side as _], &[])?
            .0)
    }

    /// Gets the number of slots in the inventory to the specified side, defaulting to [`RobotSide::Front`].
    /// Returns [`None`] if there is no inventory to that side.
    pub fn get_item_slot_count(
        &self,
        side: Option<RobotSide>,
    ) -> Result<Option<NonZeroU32>, HlapiClientError> {
        let raw = self.call("getItemSlotCount", &[&side as _], &[])?.0;
        Ok(NonZeroU32::new(raw))
    }

    /// Gets the number of items the given slot in the inventory to the specified side, defaulting to
    /// [`RobotSide::Front`], can hold.
    pub fn get_item_slot_limit(
        &self,
        slot: u32,
        side: Option<RobotSide>,
    ) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("getItemSlotLimit", &[&slot as _, &side as _], &[])?
            .0)
    }

    /// Gets the contents of the given slot in the inventory to the specified side, defaulting to
    /// [`RobotSide::Front`]. Returns [`None`] if the slot is empty.
    pub fn get_item_stack_in_slot(
        &self,
        slot: u32,
        side: Option<RobotSide>,
    ) -> Result<Option<ItemStack>, HlapiClientError> {
        Ok(self
            .call("getItemStackInSlot", &[&slot as _, &side as _], &[])?
            .0)
    }

    /// Moves up to the given number of items from one internal inventory slot to another, returning
    /// the actual count of items moved.
    pub fn move_internal(
        &self,
        from: u32,
        into: u32,
        max_count: u32,
    ) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("move", &[&from as _, &into as _, &max_count as _], &[])?
            .0)
    }

    /// Moves up to the given number of items from the container in the selected slot to the given
    /// internal slot, returning
    /// the actual count of items moved.
    pub fn move_from(&self, into: u32, max_count: u32) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("moveFrom", &[&into as _, &max_count as _], &[])?
            .0)
    }

    /// Moves up to the given number of items from the given internal slot to the container in the
    /// selected slot, returning the actual count of items moved.
    pub fn move_into(&self, from: u32, max_count: u32) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("moveInto", &[&from as _, &max_count as _], &[])?
            .0)
    }

    /// Tries to take up to the given number of items from the specified side, defaulting to
    /// [`RobotSide::Front`]. Items are taken from the world if there is no inventory to that side.
    /// Returns the actual count of items taken.
    pub fn take(&self, max_count: u32, side: Option<RobotSide>) -> Result<u32, HlapiClientError> {
        Ok(self.call("take", &[&max_count as _, &side as _], &[])?.0)
    }

    /// Tries to take up to the given number of items from the given slot of the inventory to the
    /// specified side, defaulting to [`RobotSide::Front`]. Returns the actual count of items taken.
    pub fn take_from(
        &self,
        slot: u32,
        max_count: u32,
        side: Option<RobotSide>,
    ) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("takeFrom", &[&slot as _, &max_count as _, &side as _], &[])?
            .0)
    }
}

impl DeviceBase for InventoryOperations {
    const TYPE: &'static str = "inventory_operations";

    type Event = Infallible;

    fn new(client: HlapiClient, uuid: Uuid) -> Result<Self, HlapiClientError> {
        Ok(Self { client, uuid })
    }

    fn uuid_base(&self) -> Uuid {
        self.uuid
    }

    fn client(&self) -> &HlapiClient {
        &self.client
    }
}