Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Context: https://www.reddit.com/r/ComputerCraft/comments/1enbvku/comment/lhd6j9h/
- local cache = {}
- --- Cache info about a single slot in the inventory.
- ---@param inventory Inventory The inventory to cache from.
- ---@param slot number The slot to cache.
- ---@param item item The item in the slot. This allows the function to skip if the item is already cached.
- local function cache_slot(inventory, slot, item)
- -- If we have already cached info about this item...
- if cache[item.name] then
- -- ...then we don't need to do it again.
- return
- end
- local data = inventory.getItemDetail(slot)
- if data then
- cache[data.name] = data
- end
- end
- -- Initialize the peripherals
- local chest = peripheral.find("minecraft:chest") --[[@as Inventory?]]
- local monitor = peripheral.find("monitor") --[[@as Monitor?]]
- -- Throw an error if the peripherals are not found
- if not chest then
- error("No chest found.", 0)
- end
- if not monitor then
- error("No monitor found.", 0)
- end
- -- Get the size of the chest
- local size = chest.size()
- -- and set up the monitor.
- monitor.setTextScale(0.5)
- monitor.setBackgroundColor(colors.white)
- monitor.setTextColor(colors.black)
- monitor.clear()
- local w, h = monitor.getSize()
- local spaces = (' '):rep(w)
- -- Main loop:
- while true do
- -- First, get the list of items in the chest.
- local list = chest.list()
- -- Initialize the items counter
- local items = 0
- -- For each slot in the chest...
- for i = 1, size do
- -- Get the item in the slot.
- local item = list[i]
- -- If there is an item in the slot...
- if item then
- -- Cache the item info.
- cache_slot(chest, i, item)
- -- Then increment our items counter, and set the cursor position to the start of the correct line.
- items = items + 1
- monitor.setCursorPos(1, items)
- -- Set the color depending on how full the slot is.
- if item.count >= 64 then
- monitor.setTextColor(colors.red)
- elseif item.count >= 32 then
- monitor.setTextColor(colors.pink)
- else
- monitor.setTextColor(colors.white)
- end
- -- Before finally writing the item name and count to the monitor.
- monitor.write(("%d x %s in slot %d%s"):format(item.count, cache[item.name].displayName, i, spaces))
- end
- end
- sleep() -- Wait for the next tick.
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement