skypop

CC pocket.getEntity.lua

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