class Rosegold::Inventory

Overview

Utility methods for interacting with the open window.

Defined in:

rosegold/control/inventory.cr

Constructors

Macro Summary

Instance Method Summary

Constructor Detail

def self.new(client : Rosegold::Client) #

Creates an inventory facade over the client's currently active menu.


[View source]

Macro Detail

macro method_missing(call) #

Forwards unrecognized menu accessors to the active Client#container_menu.

This keeps the inventory DSL available for both the player inventory and an open container. #slots covers the whole active window; #inventory and #hotbar are the player's storage slots, while #content is the container portion of a non-player menu. These accessors are live menu views, not independently enumerable collections owned by Inventory.


[View source]

Instance Method Detail

def boots(*args, **options) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def boots(*args, **options, &) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def chestplate(*args, **options) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def chestplate(*args, **options, &) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def count(spec, slots = player_inventory_slots) #

Returns the number of matching items in the player inventory (inventory + hotbar), or in the given slots range.

Example:

inventory.count "diamond_pickaxe"                                                 # => 2
inventory.count { |slot| slot.name.ends_with?("_pickaxe") }                       # sum of matching stacks
inventory.count { |slot| slot.name == "diamond_pickaxe" && slot.efficiency >= 4 } # => 1
inventory.count "stone", slots                                                    # => 5 (count in entire window including container)

[View source]
def count(&spec : Slot -> _) #

Sums item counts in player-inventory stacks selected by the predicate. Pass an explicit slot range to the non-block overload to count another portion of the active menu.


[View source]
def deposit_at_least(count, spec) #

Tries to transfer at least #count matching items from the player inventory to the container, using shift-clicking. Returns the number of actually transferred items.

Example:

inventory.deposit_at_least 5, "diamond_pickaxe"                                                 # => 3
inventory.deposit_at_least 5, &.empty?                                                          # => 1
inventory.deposit_at_least(5) { |slot| slot.name == "diamond_pickaxe" && slot.efficiency >= 4 } # => 2

[View source]
def deposit_at_least(count, &spec : Slot -> _) #

Deposits matching whole stacks until at least count items have moved or no progress is possible. Uses the predicate to select stacks.


[View source]
def helmet(*args, **options) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def helmet(*args, **options, &) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def leggings(*args, **options) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def leggings(*args, **options, &) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def off_hand(*args, **options) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def off_hand(*args, **options, &) #

Equipment slots from the player's inventory menu: helmet, chestplate, leggings, boots, and off-hand.


[View source]
def pick(spec) #

Selects a matching item into the main hand, if one exists in the inventory. Returns true if an item was picked, false otherwise.

Example:

inventory.pick "diamond_pickaxe"                                                 # => true
inventory.pick &.empty?                                                          # => true
inventory.pick { |slot| slot.name == "diamond_pickaxe" && slot.efficiency >= 4 } # => false

Durability-aware selection

#pick is smarter than a plain find-and-equip. For each call it:

  1. Keeps the main hand if it already matches and is not close to breaking.
  2. Otherwise searches the hotbar, picking the most-damaged usable tool first so tools wear out evenly instead of leaving partially-damaged leftovers.
  3. Falls back to the rest of the inventory, swapping the chosen slot into the hotbar.

Because the most-damaged usable tool is chosen each time, calling #pick regularly in a loop will automatically rotate in fresh tools from the inventory as the hotbar wears down — no manual reselection needed.

Avoiding tool destruction

Slots that needs_repair? are treated as if they don't match, so #pick will not hand you a tool that's about to shatter. "Needs repair" means an enchanted diamond or netherite tool with fewer than 12 uses left whose repair_cost is still below the cutoff (see below). Such tools are preserved for the anvil.

Tools that are not worth repairing — unenchanted tools, wood/stone/ iron/gold tools, or diamond/netherite tools whose repair cost has climbed above the cutoff — are still selected and will be used until they break. The rationale: if repairing costs too many levels, it's cheaper to craft a fresh one.

Tuning the repair cutoff

The cutoff is Rosegold::Slot.max_repair_cost (default 31). Raise it to keep babying heavily-repaired tools, or lower it to retire them sooner:

Rosegold::Slot.max_repair_cost = 15 # retire tools past "Prior Work: 15" Rosegold::Slot.max_repair_cost = 0 # never preserve — use everything to breakage


[View source]
def pick(&spec : Slot -> _) #

Selects the first usable item matched by the predicate into the main hand. Returns true on selection and false when no eligible slot exists.


[View source]
def pick!(spec) #

Selects a matching usable item, raising ItemNotFoundError if none exists.


[View source]
def pick!(&spec : Slot -> _) #

Selects an item matched by the predicate, raising ItemNotFoundError when no eligible slot exists.


[View source]
def refill_hand #

Refills the main hand to its maximum stack size by manually combining stacks. Only works when no container is open (player inventory only). Returns the final quantity in the main hand after refilling.

Example:

inventory.refill_hand # => 64 (if main hand was stone and got filled to max stack)
inventory.refill_hand # => 32 (if only 32 items were available)
inventory.refill_hand # => 0 (if main hand is empty)

[View source]
def replenish(count, spec) #

Ensures the player has at least #count items of the specified type in their inventory. If there are already enough items, returns the current count. If not enough items are present, attempts to withdraw more from a container. Returns the total count after replenishment attempt.

Example:

inventory.replenish 10, "stone"                                                         # => 10 (if successful)
inventory.replenish 5, "diamond"                                                        # => 3 (if only 3 available)
inventory.replenish 3 { |slot| slot.name == "diamond_pickaxe" && slot.efficiency >= 4 } # => 2

[View source]
def replenish(count, &spec : Slot -> _) #

Withdraws predicate-matching stacks until the player holds at least count. Returns the resulting item count, which can be below or above the target.


[View source]
def throw_all_of(name) #

Drops every stack named name from the active menu and returns the item count queued for dropping. The server applies the click packets asynchronously.


[View source]
def withdraw_at_least(count, spec) #

Tries to transfer at least #count matching items from the container to the player inventory, using shift-clicking. Returns the number of actually transferred items.

Example:

inventory.withdraw_at_least 5, "diamond_pickaxe"                                                 # => 3
inventory.withdraw_at_least 5, &.empty?                                                          # => 1
inventory.withdraw_at_least(5) { |slot| slot.name == "diamond_pickaxe" && slot.efficiency >= 4 } # => 2

[View source]
def withdraw_at_least(count, &spec : Slot -> _) #

Withdraws matching whole stacks until at least count items have moved or no progress is possible. Uses the predicate to select stacks.


[View source]