maca134

CC: Tweaked - Item Counter/Stats

Sep 26th, 2021 (edited)
559
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- An item counter for CC: Tweaked - https://pastebin.com/FBNVFw3A
  2. -- https://dl.dropboxusercontent.com/s/hwxp3jkfopy51n3/xhW2PxsI4XKads0Utgeg.gif
  3. --
  4. -- Input chest is on the left of the computer
  5. -- Output chest is on the right of the computer
  6. -- A redstone signal on the front of the computer resets the counters
  7. --
  8. -- To install run the follow in a CC: Tweaked computer:
  9. --      pastebin get FBNVFw3A startup
  10. --      reboot
  11. --
  12.  
  13. local inputChest = peripheral.wrap("left")
  14. local outputChest = peripheral.wrap("right")
  15. local monitor = peripheral.find("monitor")
  16.  
  17. local startTime = os.clock();
  18. local items = {}
  19.  
  20. local function shortNumberFormat(n)
  21.     if n >= 10^12 then
  22.         return string.format("%.2fg", n / 10^12)
  23.     elseif n >= 10^6 then
  24.         return string.format("%.2fm", n / 10^6)
  25.     elseif n >= 10^3 then
  26.         return string.format("%.2fk", n / 10^3)
  27.     else
  28.         return tostring(n)
  29.     end
  30. end
  31.  
  32. local function trimString(str, length)
  33.     if #str <= length then
  34.         return str .. string.rep(" ", length - #str)
  35.     else
  36.         return string.sub(str, 0, length - 3) .. "..."
  37.     end
  38. end
  39.  
  40. local function round(val, decimal)
  41.     if (decimal) then
  42.         return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  43.     else
  44.         return math.floor(val+0.5)
  45.     end
  46. end
  47.  
  48. local function renderText(text, line, color, pos)
  49.     local monX, monY = monitor.getSize()
  50.     monitor.setBackgroundColor(colors.black)
  51.     monitor.setTextColor(color)
  52.     local length = string.len(text)
  53.     local dif = math.floor(monX - length)
  54.     local x = math.floor(dif / 2)
  55.  
  56.     if pos == "center" then
  57.         monitor.setCursorPos(x+1, line)
  58.         monitor.write(text)
  59.     elseif pos == "left" then
  60.         monitor.setCursorPos(2, line)
  61.         monitor.write(text)
  62.     elseif pos == "right" then
  63.         monitor.setCursorPos(monX-length, line)
  64.         monitor.write(text)
  65.     end
  66. end
  67.  
  68. local function clearScreen()
  69.     monitor.setBackgroundColor(colors.black)
  70.     monitor.clear()
  71.     monitor.setCursorPos(1,1)
  72.     line = 1
  73. end
  74.  
  75. local function render()
  76.     clearScreen()
  77.     local duration = os.clock() - startTime;
  78.     renderText("Item Counter", 1, colors.red, "center")
  79.     local sortedItems = {}
  80.     for key, val in pairs(items) do
  81.         table.insert(sortedItems, val)
  82.     end
  83.     table.sort(sortedItems, function(a, b) return a["count"] > b["count"] end)
  84.     local monX, monY = monitor.getSize()
  85.     monX = monX - 5
  86.     local rows = {}
  87.     for i, val in ipairs(sortedItems) do
  88.         if i + 2 > monY then break end
  89.         local rate = val["count"] / duration
  90.         local unit = "sec"
  91.         if (rate < 1) then
  92.             rate = rate * 60
  93.             unit = "min"
  94.         end
  95.         if (rate < 1) then
  96.             rate = rate * 60
  97.             unit = "hr"
  98.         end
  99.         rows[i] = {
  100.             shortNumberFormat(val["count"]),
  101.             val["name"],
  102.             shortNumberFormat(round(rate, 2)) .. "/" .. unit
  103.         }
  104.     end
  105.     local sizes = {}
  106.     for i, row in ipairs(rows) do
  107.         for j, col in ipairs(row) do
  108.             if not sizes[j] then
  109.                 sizes[j] = 0
  110.             end
  111.             if sizes[j] < #col then
  112.                 sizes[j] = #col
  113.             end
  114.         end
  115.     end
  116.     local sum = 0
  117.     for i, size in ipairs(sizes) do
  118.         sum = sum + size
  119.     end
  120.     if sum == 0 then
  121.         return
  122.     end
  123.     sizes[2] = sizes[2] + (monX - sum)
  124.     for i, row in ipairs(rows) do
  125.         renderText(trimString(row[1], sizes[1]) .. "|" .. trimString(row[2], sizes[2]), i+2, colors.yellow, "left")
  126.         renderText(row[3], i+2, colors.green, "right")
  127.     end
  128. end
  129.  
  130. local function countItems()
  131.     while true do
  132.         for slot, item in pairs(inputChest.list()) do
  133.             if not items[item.name] then
  134.                 local itemDetails = inputChest.getItemDetail(slot)
  135.                 items[item.name] = {
  136.                     ["name"] = itemDetails.displayName,
  137.                     ["count"] = 0
  138.                 }
  139.             end
  140.             items[item.name]["count"] = items[item.name]["count"] + item.count
  141.             inputChest.pushItems(peripheral.getName(outputChest), slot)
  142.         end
  143.         render()
  144.         os.sleep(1)
  145.     end
  146. end
  147.  
  148. local function reset()
  149.     while true do
  150.         local event = os.pullEvent("redstone")
  151.         if redstone.getInput("front") then
  152.             startTime = os.clock()
  153.             items = {}
  154.             render()
  155.         end
  156.     end
  157. end
  158.  
  159. clearScreen()
  160. print("running")
  161. parallel.waitForAny(countItems, reset)
RAW Paste Data