Advertisement
fatboychummy

Item caching thing for reddit

Aug 9th, 2024 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. -- Context: https://www.reddit.com/r/ComputerCraft/comments/1enbvku/comment/lhd6j9h/
  2.  
  3. local cache = {}
  4.  
  5. --- Cache info about a single slot in the inventory.
  6. ---@param inventory Inventory The inventory to cache from.
  7. ---@param slot number The slot to cache.
  8. ---@param item item The item in the slot. This allows the function to skip if the item is already cached.
  9. local function cache_slot(inventory, slot, item)
  10.   -- If we have already cached info about this item...
  11.   if cache[item.name] then
  12.     -- ...then we don't need to do it again.
  13.     return
  14.   end
  15.  
  16.   local data = inventory.getItemDetail(slot)
  17.  
  18.   if data then
  19.     cache[data.name] = data
  20.   end
  21. end
  22.  
  23. -- Initialize the peripherals
  24. local chest = peripheral.find("minecraft:chest") --[[@as Inventory?]]
  25. local monitor = peripheral.find("monitor") --[[@as Monitor?]]
  26.  
  27. -- Throw an error if the peripherals are not found
  28. if not chest then
  29.   error("No chest found.", 0)
  30. end
  31.  
  32. if not monitor then
  33.   error("No monitor found.", 0)
  34. end
  35.  
  36. -- Get the size of the chest
  37. local size = chest.size()
  38.  
  39. -- and set up the monitor.
  40. monitor.setTextScale(0.5)
  41. monitor.setBackgroundColor(colors.white)
  42. monitor.setTextColor(colors.black)
  43. monitor.clear()
  44.  
  45. local w, h = monitor.getSize()
  46. local spaces = (' '):rep(w)
  47.  
  48. -- Main loop:
  49. while true do
  50.   -- First, get the list of items in the chest.
  51.   local list = chest.list()
  52.  
  53.   -- Initialize the items counter
  54.   local items = 0
  55.  
  56.   -- For each slot in the chest...
  57.   for i = 1, size do
  58.     -- Get the item in the slot.
  59.     local item = list[i]
  60.  
  61.     -- If there is an item in the slot...
  62.     if item then
  63.       -- Cache the item info.
  64.       cache_slot(chest, i, item)
  65.  
  66.       -- Then increment our items counter, and set the cursor position to the start of the correct line.
  67.       items = items + 1
  68.       monitor.setCursorPos(1, items)
  69.  
  70.       -- Set the color depending on how full the slot is.
  71.       if item.count >= 64 then
  72.         monitor.setTextColor(colors.red)
  73.       elseif item.count >= 32 then
  74.         monitor.setTextColor(colors.pink)
  75.       else
  76.         monitor.setTextColor(colors.white)
  77.       end
  78.  
  79.  
  80.       -- Before finally writing the item name and count to the monitor.
  81.       monitor.write(("%d x %s in slot %d%s"):format(item.count, cache[item.name].displayName, i, spaces))
  82.     end
  83.   end
  84.  
  85.   sleep() -- Wait for the next tick.
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement