Advertisement
TangentFox

Item counter (for minetest luacontroller)

May 26th, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. -- mem.count: count per item type
  2. -- mem.list: list of item types
  3. -- mem.total: total number of items counted
  4. local ControlPanel = "panel"
  5.  
  6. local send = function()
  7.   local line = ""
  8.   for i = 1, 5 do
  9.     local name = mem.list[mem.pointer + i]
  10.     if not name then break end
  11.     line = line .. tostring(mem.count[name]) .. " " .. name .. "\n"
  12.   end
  13.   digiline_send(ControlPanel, line .. "Page: " .. tostring(math.floor(mem.pointer / 5)) .. "/" .. tostring(math.floor(#mem.list / 5)) .. "   Heat: " .. tostring(heat))
  14. end
  15.  
  16. if event.type == "digiline" then
  17.   local msg = event.msg
  18.  
  19.   if event.channel == "incoming" then
  20.     if mem.count[msg] then
  21.       mem.count[msg] = mem.count[msg] + 1
  22.     else
  23.       mem.count[msg] = 1
  24.       table.insert(mem.list, msg)
  25.     end
  26.     mem.total = mem.total + 1
  27.     send()
  28.  
  29.   elseif event.channel == ControlPanel then
  30.     if msg == "up" then
  31.       mem.pointer = math.max(mem.pointer - 1, 0)
  32.       send()
  33.     elseif msg == "down" then
  34.       mem.pointer = math.min(mem.pointer + 1, #mem.list)
  35.       send()
  36.     end
  37.  
  38.   end
  39.  
  40. elseif event.type == "program" then
  41.   mem.count = mem.count or {}
  42.   mem.list = mem.list or {}
  43.   mem.pointer = mem.pointer or 0
  44.   mem.total = mem.total or 0
  45.   send()
  46.  
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement