oc2_hlapi_v2 0.2.0

A client for the new version of OpenComputers II's HLAPI
Documentation
//! The transposer device and its associated types

use std::{convert::Infallible, num::NonZeroU32};

use uuid::Uuid;

use crate::{
    client::{DeviceBase, HlapiClient, HlapiClientError},
    devices::{ItemStack, Side, TankContentInfo},
};

#[derive(Debug, Clone)]
/// A transposer device
///
/// [OC2 Docs](https://fnuecke.github.io/oc2/manual/en_us/device/transposer.html)
pub struct Transposer {
    client: HlapiClient,
    uuid: Uuid,
}

impl Transposer {
    /// Gets the contents of the given tank in the container to the specified side.
    /// Returns [`None`] if the tank is empty.
    pub fn get_fluid_in_tank(
        &self,
        side: Side,
        tank: u32,
    ) -> Result<Option<TankContentInfo>, HlapiClientError> {
        Ok(self
            .call("getFluidInTank", &[&side as _, &tank as _], &[])?
            .0)
    }

    /// Gets the number of millibuckets the given tank in the container to the specified side can hold.
    pub fn get_fluid_tank_capacity(&self, side: Side, tank: u32) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("getFluidTankCapacity", &[&side as _, &tank as _], &[])?
            .0)
    }

    /// Gets the number of tanks in the fluid container to the specified side.
    /// Returns [`None`] if there is no container to that side.
    pub fn get_fluid_tank_count(&self, side: Side) -> Result<Option<NonZeroU32>, HlapiClientError> {
        let raw = self.call("getFluidTankCount", &[&side as _], &[])?.0;
        Ok(NonZeroU32::new(raw))
    }

    /// Gets the number of slots in the inventory to the specified side.
    /// Returns [`None`] if there is no inventory to that side.
    pub fn get_item_slot_count(&self, side: Side) -> 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 can hold.
    pub fn get_item_slot_limit(&self, side: Side, slot: u32) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("getItemSlotLimit", &[&side as _, &slot as _], &[])?
            .0)
    }

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

    /// Moves fluid from one container to another, up to the maximum amount in millibuckets given.
    /// If there is no container on the source side, it will try to drain fluid from the world in
    /// increments of one bucket, and if there is no container on the destination side, it will try
    /// to dump fluid into the world in increments of one bucket. Returns the actual amount moved.
    pub fn move_fluid(
        &self,
        source: Side,
        destination: Side,
        max_amount: u32,
    ) -> Result<u32, HlapiClientError> {
        Ok(self
            .call(
                "moveFluid",
                &[&source as _, &destination as _, &max_amount as _],
                &[],
            )?
            .0)
    }

    /// Moves items from one container to another, up to the maximum count given.
    /// If there is no inventory on the source side, it will try to pick up items from the world,
    /// and if there is no inventory on the destination side, it will drop items into the world.
    /// Returns the actual count moved.
    pub fn move_items(
        &self,
        source: Side,
        destination: Side,
        max_count: u32,
    ) -> Result<u32, HlapiClientError> {
        Ok(self
            .call(
                "moveItems",
                &[&source as _, &destination as _, &max_count as _],
                &[],
            )?
            .0)
    }

    /// Moves items from one slot in a container to another slot in a possibly different container,
    /// up to the maximum count given. Returns the actual count moved.
    pub fn move_items_slot(
        &self,
        source: Side,
        source_slot: u32,
        destination: Side,
        destination_slot: u32,
        max_count: u32,
    ) -> Result<u32, HlapiClientError> {
        Ok(self
            .call(
                "moveItems",
                &[
                    &source as _,
                    &source_slot as _,
                    &destination as _,
                    &destination_slot as _,
                    &max_count as _,
                ],
                &[],
            )?
            .0)
    }
}

impl DeviceBase for Transposer {
    const TYPE: &'static str = "transposer";

    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
    }
}