ghast-client 0.4.9

Headless Minecraft Java Edition client for the ghast library: connection, login, fixed 50 ms tick loop and a version-agnostic bot API (1.8.9, 1.12.2, 1.16.5, 26.1 and 26.2)
Documentation
//! Creative mode inventory actions of the behaviour API on the legacy versions: the creative
//! inventory screen's tabs and item menu, and the pick block key (see
//! `ghast_inventory::Inventory::creative_menu_click` / `pick_block_legacy`; window 0 clicks
//! in the creative screen go through [`TickContext::click`]).

use super::{ClickRefused, Outbound, TickContext};
use ghast_inventory::{Click, ClickError, CreativeTab, Stack};

impl TickContext<'_> {
    /// Selects the tab of the creative inventory screen (1.8.9 – 1.16.5 creative mode; nothing
    /// is sent). The tab also stays for the next opening (vanilla keeps it in a static); the
    /// first opening shows an item tab. On the inventory tab [`TickContext::click`] uses window
    /// 0's slot numbers (5 ..) plus the delete slot
    /// ([`ghast_inventory::Inventory::creative_delete_slot`]); on an item tab the hotbar row is
    /// slots 45 … 53 and the menu is clicked with [`TickContext::creative_menu_click`].
    pub fn set_creative_tab(&mut self, tab: CreativeTab) {
        self.core.inv.set_creative_tab_legacy(tab);
    }

    /// A click on the menu of the open creative item tab, over a menu slot showing `menu` (a
    /// stack of the tab's item list): pick it up onto the cursor (no packet), put a full stack
    /// into the hotbar with a number key ([`Click::Swap`]: window 0 then sends its changed
    /// slots), clone it ([`Click::Clone`]) or drop a copy ([`Click::Drop`]). Refused like
    /// [`TickContext::click`] while dead or turning, or when no creative item tab is open.
    pub fn creative_menu_click(
        &mut self,
        menu: Option<Stack>,
        click: Click,
    ) -> Result<(), ClickRefused> {
        self.still()?;
        let o = self
            .core
            .inv
            .creative_menu_click(menu, click)
            .map_err(ClickRefused::Inventory)?;
        self.core.out.push(Outbound::Inventory(o));
        Ok(())
    }

    /// The pick block key, aimed at something whose pick stack is `pick` (1.8: its item and
    /// damage, `has_data`: the damage must match, as for items with subtypes; 1.12.2: the
    /// whole stack, NBT included), with no screen open:
    /// the client selects a hotbar slot holding it or, in creative mode, puts it into the
    /// selected (or first empty hotbar) slot and sends that slot (`PlayerInventory.addPickBlock`
    /// and `MinecraftClient.doPick`'s creative packet), written now like the other actions; a
    /// changed selected slot is sent by this tick's slot sync, after the behaviour (as a client
    /// mod picking in the tick start event does; the key itself would be read after the attack
    /// and use keys and its slot synced on the next tick). Which item a block or entity picks
    /// is the caller's (vanilla resolves it from the crosshair target).
    ///
    /// Refused ([`ClickError::Unpredictable`]) on versions whose pick block is not modelled
    /// (capability `legacy_pick_block`), with a screen open, and while dead.
    pub fn pick_block(&mut self, pick: &Stack, has_data: bool) -> Result<(), ClickRefused> {
        if self.core.dead {
            return Err(ClickRefused::Dead);
        }
        if !self.core.opts.caps.legacy_pick_block {
            return Err(ClickRefused::Inventory(ClickError::Unpredictable));
        }
        let c = &mut *self.core;
        if let Some(o) = c
            .inv
            .pick_block_legacy(pick, has_data)
            .map_err(ClickRefused::Inventory)?
        {
            c.out.push(Outbound::Inventory(o));
        }
        // `selectedSlot` changed on the client; the slot sync sends it
        c.held_slot = c.inv.selected();
        Ok(())
    }

    /// 1.12.2 creative mode: loads a saved toolbar with its key (hotbar key + the load key, no
    /// screen open): the hotbar becomes `saved` (the toolbar the client stored, which the
    /// caller keeps) and each slot is sent. Refused outside creative mode, with a screen open,
    /// on other versions and while dead.
    pub fn creative_load_hotbar(&mut self, saved: &[Option<Stack>; 9]) -> Result<(), ClickRefused> {
        if self.core.dead {
            return Err(ClickRefused::Dead);
        }
        let o = self
            .core
            .inv
            .creative_load_hotbar_legacy(saved)
            .map_err(ClickRefused::Inventory)?;
        self.core.out.push(Outbound::Inventory(o));
        Ok(())
    }
}