Create: Computing AgesBrass Docs
Built-in programs

stock

watches a chest, alarm

Runs on All All computers · 16 lines
Type this at the prompt of a computer to copy it onto the medium:
Terminal
> examples stock
> edit stock

Watches a chest (or a Create Item Vault, a barrel, a depot...) and turns on an alarm when it holds fewer than 64 items. The total also goes to the screen and to Display Links. The heart of every factory: know what you have, and react when it runs out.

What to build

  • On the left of the computer (seen facing the screen), a block with an inventory: chest, barrel, Create Item Vault, depot...
  • On top, a Redstone Lamp for the alarm, or anything driven by redstone.
  • If you like, a Create Display Link pointed at the computer, to show Stock: 123 on a Display Board.

If no peripheral at all touches the left side, the program stops at once with no chest on the left.

How it works

Brass
local chest = peripheral.wrap("left")
if chest == nil then
  error("no chest on the left")
end

peripheral.wrap() gives an object to control the block on a side, or nil when there is none. error() stops the program with a message, which is far clearer than a crash a few lines later.

Brass
local total = chest.count()
rs.set("top", total < threshold)

inventory.count() adds up the items of every slot; chest.count("minecraft:iron_ingot") counts only one kind of item. total < threshold is true or false, which rs.set() takes directly: the lamp is on exactly while the stock is low.

Brass
display.set({"Stock: " .. total})
print("Items: " .. total)
sleep(5)

An inventory sends no event when it changes, so the program looks again every 5 seconds. Counting costs a few instructions per slot of the inventory: a big vault is fine every few seconds, but not twenty times a second.

Tip

To drive a production line instead of a lamp, put a Create Clutch on top and write total >= threshold: a powered clutch disconnects, so the machines stop when the chest is full and start again when it empties.

Ideas

  • Count only what matters: chest.count("minecraft:iron_ingot"). The item ids are shown in the advanced tooltips (F3+H).
  • Stop the lamp from flickering around the threshold with two limits: the alarm starts below 64 and only stops above 256.

    Brass
    local LOW, HIGH = 64, 256
    local alarm = false
    for _, total in ipairs({300, 200, 60, 100, 250, 260}) do
      if total < LOW then
        alarm = true
      elseif total > HIGH then
        alarm = false
      end
      print(total, alarm)
    end
    Screen
    300 false
    200 false
    60  true
    100 true
    250 true
    260 false
  • Watch several chests: from the Minicomputer on, chests along a Data Cable are peripherals too, found with peripheral.find() or peripheral.list().
  • Move items when the stock is high with inventory.push().
  • The stock alarm recipe builds a complete version with a screen and several items.

The program

stock
-- Watches the chest (or Create vault, depot...) on the left.
-- Below the threshold, the top output turns on: an alarm lamp.
-- To run a production line through a Create clutch instead, write
-- total >= threshold: a powered clutch disconnects.
local chest = peripheral.wrap("left")
if chest == nil then
  error("no chest on the left")
end
local threshold = 64
while true do
  local total = chest.count()  -- or chest.count("minecraft:iron_ingot")
  rs.set("top", total < threshold)
  display.set({"Stock: " .. total})
  print("Items: " .. total)
  sleep(5)
end