Advertisement
SuperMcBrent

EssenceCounter

Jan 22nd, 2022 (edited)
910
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- load libraries
  2. os.loadAPI("dataLib")
  3. local pretty = require "cc.pretty"
  4.  
  5. -- peripheral setup
  6. local import = peripheral.wrap("right")
  7. local export = peripheral.wrap("left")
  8. local modem = peripheral.wrap("top")
  9. modem.open(1)
  10.  
  11. -- variables setup
  12. local items = {}
  13.  
  14. function stripModName(itemname)
  15.     local parts= {}
  16.     for part in itemname:gmatch("[^:]+") do
  17.         parts[ #parts + 1 ] = part
  18.     end
  19.     return parts[2]
  20. end
  21.  
  22. function stripEssence(itemname)
  23.     return itemname:gsub("_essence", "")
  24. end
  25.  
  26. function addItem(item)
  27.     local name = stripModName(item.name)
  28.     name = stripEssence(name)
  29.     if items[name] then
  30.         items[name] = items[name] + item.count
  31.     else
  32.         items[name] = item.count
  33.     end
  34. end
  35.  
  36. function waitForRequest()
  37.     parallel.waitForAny(wait,getRequest)
  38. end
  39.  
  40. function wait()
  41.     sleep(0.5)
  42. end
  43.  
  44. function getRequest()
  45.     local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  46.     if message == "provideEssenceCount" then
  47.         modem.transmit(2,1,items)
  48.     end
  49. end
  50.  
  51. while true do
  52.     for i=1,27 do
  53.         local item = import.getItemDetail(i)
  54.         if item then
  55.             addItem(item)
  56.             import.pushItems("left", i, item.count)
  57.         end
  58.     end
  59.     write(os.time() .. ": ")
  60.     waitForRequest()
  61.     pretty.pretty_print(items)
  62. end
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement