Guest User

getEntity

a guest
Aug 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.55 KB | None | 0 0
  1. ----------------------------
  2. -- GetEntity
  3. -- by SukaiPoppuGo
  4. -- player data watcher
  5.  
  6. -- Params
  7. local debug = false
  8. local maxDigits = 6
  9. local delay = .15
  10. local playerData = {}
  11. -- Pixel API
  12. os.loadAPI("/api/pixel.lua")
  13.  
  14. --Pocket screen
  15. --local screenW, screenH = term.getSize()
  16. local screenW, screenH = 26, 20
  17. -- With pixel API   : 26 x 30 borderless -> 24 x 28 framed -> 108 outline borderless -> 100 outline framed
  18. -- With blittle API : 52 x 60 borderless -> 48 x 54 framed -> 220 outline borderless -> 200 outline framed
  19.  
  20. local _n = {
  21. {"000","ff0","000","000","0f0","000","000","000","000","000",},
  22. {"0f0","ff0","ff0","ff0","0f0","0ff","0ff","ff0","0f0","0f0",},
  23. {"0f0","ff0","000","000","000","000","000","ff0","000","000",},
  24. {"0f0","ff0","0ff","ff0","ff0","ff0","0f0","ff0","0f0","ff0",},
  25. {"000","ff0","000","000","ff0","000","000","ff0","000","000",},
  26. }
  27. local tNum = {}
  28. for i=0,9 do
  29.     tNum[i] = pixel.compute({_n[1][i],_n[2][i],_n[3][i],_n[4][i],_n[5][i]})
  30. end
  31.  
  32. function test()
  33.     term.clear()
  34.     local x,y = 4,6
  35.     for i=0,9 do
  36.         term.setCursorPos(x + ((i%5)*4), i > 4 and y+6 or y)
  37.         pixel.draw(tNum[i])
  38.     end
  39.     term.setCursorPos(3,18)
  40.     read()
  41. end
  42.  
  43. function display(num, x, y)
  44.     assert(tonumber(num), string.format([[! "%s" NaN]], num))
  45.     assert(string.len(tostring(num)) <= maxDigits, string.format([[! "%s" too long (%s digits)]], num, maxDigits))
  46.    
  47.     --debug
  48.  if debug then
  49.   local _color = term.getTextColor()
  50.   term.setTextColor(colors.gray)
  51.     term.setCursorPos(1,y < 10 and 2 or 17)
  52.     term.clearLine()
  53.     print("num:", num, "s:", tostring(num), "n:", tonumber(num))
  54.     term.setCursorPos(1,y < 10 and 3 or 18)
  55.     term.clearLine()
  56.     term.write("split: ")
  57.   term.setTextColor(_color)
  58.  end
  59.  
  60.     num = string.sub(tostring(num)..string.rep(" ",maxDigits),1,maxDigits)
  61.     local line = {"","","","",""}
  62.     for i=1,string.len(num) do
  63.         if string.sub(num,i,i) == " " then
  64.             local pad = i == string.len(num) and "" or " "
  65.             line[1] = line[1].." "..pad
  66.             line[2] = line[2].." "..pad
  67.             line[3] = line[3].." "..pad
  68.             line[4] = line[4].." "..pad
  69.             line[5] = line[5].." "..pad
  70.   elseif string.sub(num,i,i) == "." then
  71.    local pad = i == string.len(num) and "" or " "
  72.    line[1] = line[1].." "..pad
  73.    line[2] = line[2].." "..pad
  74.    line[3] = line[3].." "..pad
  75.    line[4] = line[4].." "..pad
  76.    line[5] = line[5].."0"..pad
  77.         elseif i == 1 and tonumber(num) < 0 then --nombre négatif
  78.             line = string.len(num) == maxLen and {" ff "," ff "," 00 "," ff "," ff "} or {"fff ","fff ","000 ","fff ","fff "}
  79.    if debug then
  80.     local _color = term.getTextColor()
  81.     term.setTextColor(colors.gray)
  82.              term.write("-")
  83.     term.setTextColor(_color)
  84.    end
  85.         else
  86.    if debug then
  87.     local _color = term.getTextColor()
  88.     term.setTextColor(colors.gray)
  89.             term.write(tonumber(string.sub(num,i,i)).." ")
  90.     term.setTextColor(_color)
  91.    end
  92.             local n = tonumber(string.sub(num,i,i))
  93.             local pad = i == string.len(num) and "" or " "
  94.             line[1] = line[1].._n[1][n+1]..pad
  95.             line[2] = line[2].._n[2][n+1]..pad
  96.             line[3] = line[3].._n[3][n+1]..pad
  97.             line[4] = line[4].._n[4][n+1]..pad
  98.             line[5] = line[5].._n[5][n+1]..pad
  99.         end
  100.     end
  101.     term.setCursorPos(x, y)
  102.     pixel.draw(pixel.compute(line))
  103. end
  104.  
  105. function alignLeft()
  106.     return margin
  107. end
  108. function alignCenter(v)
  109.     local w = string.len(tostring(v))*4
  110.     return math.max(2, math.floor((screenW - w) / 2))
  111. end
  112. function alignRight(v, margin)
  113.     local w = string.len(tostring(v))*4
  114.     return screenW - w - margin
  115. end
  116.  
  117. --List properties and numeric/table values
  118. local _list = {}
  119. local isNumeric = {}
  120. local isTable = {}
  121. for k,v in pairs(pocket.getEntity()) do
  122.     table.insert(_list, k)
  123.     if type(v) == "number" then
  124.         isNumeric[k] = true
  125.     elseif type(v) == "table" then
  126.         isTable[k] = true
  127.     end
  128. end
  129. table.sort(_list)
  130.  
  131. function formatNum(num)
  132.     num = tonumber(num)
  133.     if not num
  134.     or math.abs(num) > tonumber(string.rep("9",maxDigits))
  135.     then
  136.         return false
  137.     elseif string.len(tostring(num)) > maxDigits then
  138.         num = tonumber(string.sub(tostring(num),1,maxDigits))
  139.     end
  140.     return num
  141. end
  142.  
  143. local function displayTable(name, value)
  144.     local day, time = os.day(), os.time()
  145.     local fileName = string.format("/log/getEntity/%s_%s_%s",name,day,time*1000)
  146.     local fh = fs.open(fileName, "w")
  147.     fh.write(string.format([[%s
  148. -- Log pocket.getEntity()
  149. -- value: %s
  150. -- Day: %s, Time: %s (MC)
  151. -- Results:
  152. %s
  153. ]], string.rep("-",26), name, day, time, textutils.serialize(value)))
  154.     fh.close()
  155.     shell.run("edit",fileName)
  156.     sleep(.5)
  157. end
  158.  
  159. --List position display
  160. local scroll = 1
  161. --List selected item
  162. local sel = 0
  163. --Get the first numeric value
  164. for i,v in ipairs(_list) do
  165.     if isNumeric[v] then
  166.         sel = i
  167.         break
  168.     end
  169. end
  170. local function menu(clickY)
  171.     --Params
  172.     local height = 8
  173.     local x,y = 1, screenH - height
  174.     --List constraint
  175.     scroll = math.min(scroll, #_list-height)
  176.     scroll = math.max(scroll, 1)
  177.     --Convert clickY
  178.     local click = false
  179.     if clickY then
  180.         click = clickY - y + scroll
  181.     end
  182.    
  183.     --Save Term settings
  184.     local _color = term.getTextColor()
  185.    
  186.     --Display list
  187.     for i=scroll, scroll+height do
  188.         --Selected item
  189.         if i == sel then
  190.             term.setTextColor(colors.yellow)
  191.         --Clicked item
  192.         elseif click and i == click then
  193.             term.setTextColor(colors.white)
  194.             --Select only numeric values
  195.             if isNumeric[_list[i]] then
  196.                 sel = i
  197.             elseif isTable[_list[i]] then
  198.                 sleep(.1)
  199.                 displayTable(_list[i], playerData[_list[i]])
  200.             end
  201.         --Other items
  202.         else
  203.             term.setTextColor(colors.lightGray)
  204.         end
  205.         if _list[i] then
  206.             term.setCursorPos(x,y)
  207.             term.clearLine()
  208.             term.write(_list[i])
  209.             if playerData[_list[i]] then
  210.                 local value = playerData[_list[i]]
  211.                 if type(value) == "table" then
  212.                     value = string.gsub(textutils.serialise(value),"([%s|\n]+)","")
  213.                 elseif type(value) ~= "number"
  214.                 and type(value) ~= "string"
  215.                 and type(value) ~= "boolean"
  216.                 then
  217.                     value = "("..type(value)..")"
  218.                 else
  219.                     value = tostring(value)
  220.                 end
  221.                 term.blit(
  222.                     ":"..string.sub(value..string.rep(" ",screenW),1,screenW-1),
  223.                     "8"..string.rep("7",screenW-1),
  224.                     "f"..string.rep("f",screenW-1)
  225.                 )
  226.             else
  227.                 term.blit(":nil","8777","ffff")
  228.             end
  229.             y = y+1
  230.         end
  231.     end
  232.     --Term reset
  233.     term.setTextColor(_color)
  234. end
  235.  
  236. term.clear()
  237. repeat
  238.     playerData = pocket.getEntity()
  239.  local data = formatNum(playerData[_list[sel]])
  240.  
  241.     term.clear()
  242.  term.setCursorPos(1,1)
  243.  term.clearLine()
  244.  term.write(_list[sel])
  245.  
  246.  menu()
  247.  
  248.  term.setCursorPos(1,2)
  249.  term.clearLine()
  250.  print("scroll:",scroll.."/"..#_list,"sel:",sel)
  251.  
  252.     display(data, alignCenter(data, 4), 6)
  253.  
  254.     local tick = os.startTimer(delay)
  255.  local e = {os.pullEventRaw()}
  256.  os.cancelTimer(tick)
  257.  if e[1] == "mouse_click" and e[2] == 1 then
  258.      menu(e[4])
  259.      sleep(.1)
  260.  elseif e[1] == "mouse_scroll" then
  261.      scroll = scroll + e[2]
  262.  end
  263. until e[1] == "terminate"
  264.  
  265. --Term reset
  266. term.setCursorPos(1,1)
  267. term.setTextColor(colors.white)
  268. term.setBackgroundColor(colors.black)
  269. term.clear()
  270. printError("Terminated")
  271. sleep(.2)
Advertisement
Add Comment
Please, Sign In to add comment