Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------
- -- GetEntity
- -- by SukaiPoppuGo
- -- player data watcher
- -- Params
- local debug = false
- local maxDigits = 6
- local delay = .15
- local playerData = {}
- -- Pixel API Wizard
- os.loadAPI("/api/pixel.lua")
- if not pixel then
- local confirm
- repeat
- print("Pixel API required")
- print("pastebin get Fa9KJqS4 /api/pixel.lua")
- print("Download ? Y/N")
- confirm = string.lower(read())
- if confirm ~= "y" and confirm ~= "n" then
- printError(confirm, "Invalid\n")
- end
- until confirm == "y" or confirm == "n"
- if confirm == "y" then
- shell.run("pastebin get Fa9KJqS4 /api/pixel.lua")
- sleep(1)
- os.loadAPI("/api/pixel.lua")
- if not pixel then
- printError("Setup Pixel API failed..")
- return
- end
- else
- print("Abort")
- return
- end
- end
- -- End Wizard
- --Pocket screen
- --local screenW, screenH = term.getSize()
- local screenW, screenH = 26, 20
- -- With pixel API : 26 x 30 borderless -> 24 x 28 framed -> 108 outline borderless -> 100 outline framed
- -- With blittle API : 52 x 60 borderless -> 48 x 54 framed -> 220 outline borderless -> 200 outline framed
- local _n = {
- {"000","ff0","000","000","0f0","000","000","000","000","000",},
- {"0f0","ff0","ff0","ff0","0f0","0ff","0ff","ff0","0f0","0f0",},
- {"0f0","ff0","000","000","000","000","000","ff0","000","000",},
- {"0f0","ff0","0ff","ff0","ff0","ff0","0f0","ff0","0f0","ff0",},
- {"000","ff0","000","000","ff0","000","000","ff0","000","000",},
- }
- local tNum = {}
- for i=0,9 do
- tNum[i] = pixel.compute({_n[1][i],_n[2][i],_n[3][i],_n[4][i],_n[5][i]})
- end
- function test()
- term.clear()
- local x,y = 4,6
- for i=0,9 do
- term.setCursorPos(x + ((i%5)*4), i > 4 and y+6 or y)
- pixel.draw(tNum[i])
- end
- term.setCursorPos(3,18)
- read()
- end
- function display(num, x, y)
- assert(tonumber(num), string.format([[! "%s" NaN]], num))
- assert(string.len(tostring(num)) <= maxDigits, string.format([[! "%s" too long (%s digits)]], num, maxDigits))
- --debug
- if debug then
- local _color = term.getTextColor()
- term.setTextColor(colors.gray)
- term.setCursorPos(1,y < 10 and 2 or 17)
- term.clearLine()
- print("num:", num, "s:", tostring(num), "n:", tonumber(num))
- term.setCursorPos(1,y < 10 and 3 or 18)
- term.clearLine()
- term.write("split: ")
- term.setTextColor(_color)
- end
- num = string.sub(tostring(num)..string.rep(" ",maxDigits),1,maxDigits)
- local line = {"","","","",""}
- for i=1,string.len(num) do
- if string.sub(num,i,i) == " " then
- local pad = i == string.len(num) and "" or " "
- line[1] = line[1].." "..pad
- line[2] = line[2].." "..pad
- line[3] = line[3].." "..pad
- line[4] = line[4].." "..pad
- line[5] = line[5].." "..pad
- elseif string.sub(num,i,i) == "." then
- local pad = i == string.len(num) and "" or " "
- line[1] = line[1].." "..pad
- line[2] = line[2].." "..pad
- line[3] = line[3].." "..pad
- line[4] = line[4].." "..pad
- line[5] = line[5].."0"..pad
- elseif i == 1 and tonumber(num) < 0 then --nombre négatif
- line = string.len(num) == maxLen and {" ff "," ff "," 00 "," ff "," ff "} or {"fff ","fff ","000 ","fff ","fff "}
- if debug then
- local _color = term.getTextColor()
- term.setTextColor(colors.gray)
- term.write("-")
- term.setTextColor(_color)
- end
- else
- if debug then
- local _color = term.getTextColor()
- term.setTextColor(colors.gray)
- term.write(tonumber(string.sub(num,i,i)).." ")
- term.setTextColor(_color)
- end
- local n = tonumber(string.sub(num,i,i))
- local pad = i == string.len(num) and "" or " "
- line[1] = line[1].._n[1][n+1]..pad
- line[2] = line[2].._n[2][n+1]..pad
- line[3] = line[3].._n[3][n+1]..pad
- line[4] = line[4].._n[4][n+1]..pad
- line[5] = line[5].._n[5][n+1]..pad
- end
- end
- term.setCursorPos(x, y)
- pixel.draw(pixel.compute(line))
- end
- function alignLeft()
- return margin
- end
- function alignCenter(v)
- local w = string.len(tostring(v))*4
- return math.max(2, math.floor((screenW - w) / 2))
- end
- function alignRight(v, margin)
- local w = string.len(tostring(v))*4
- return screenW - w - margin
- end
- --List properties and numeric/table values
- local _list = {}
- local isNumeric = {}
- local isTable = {}
- for k,v in pairs(pocket.getEntity()) do
- table.insert(_list, k)
- if type(v) == "number" then
- isNumeric[k] = true
- elseif type(v) == "table" then
- isTable[k] = true
- end
- end
- table.sort(_list)
- function formatNum(num)
- num = tonumber(num)
- if not num
- or math.abs(num) > tonumber(string.rep("9",maxDigits))
- then
- return false
- elseif string.len(tostring(num)) > maxDigits then
- num = tonumber(string.sub(tostring(num),1,maxDigits))
- end
- return num
- end
- local function displayTable(name, value)
- local day, time = os.day(), os.time()
- local fileName = string.format("/log/getEntity/%s_%s_%s",name,day,time*1000)
- local fh = fs.open(fileName, "w")
- fh.write(string.format([[%s
- -- Log pocket.getEntity()
- -- value: %s
- -- Day: %s, Time: %s (MC)
- -- Results:
- %s
- ]], string.rep("-",26), name, day, time, textutils.serialize(value)))
- fh.close()
- shell.run("edit",fileName)
- sleep(.5)
- end
- --List position display
- local scroll = 1
- --List selected item
- local sel = 0
- --Get the first numeric value
- for i,v in ipairs(_list) do
- if isNumeric[v] then
- sel = i
- break
- end
- end
- local function menu(clickY)
- --Params
- local height = 8
- local x,y = 1, screenH - height
- --List constraint
- scroll = math.min(scroll, #_list-height)
- scroll = math.max(scroll, 1)
- --Convert clickY
- local click = false
- if clickY then
- click = clickY - y + scroll
- end
- --Save Term settings
- local _color = term.getTextColor()
- --Display list
- for i=scroll, scroll+height do
- --Selected item
- if i == sel then
- term.setTextColor(colors.yellow)
- --Clicked item
- elseif click and i == click then
- term.setTextColor(colors.white)
- --Select only numeric values
- if isNumeric[_list[i]] then
- sel = i
- elseif isTable[_list[i]] then
- sleep(.1)
- displayTable(_list[i], playerData[_list[i]])
- end
- --Other items
- else
- term.setTextColor(colors.lightGray)
- end
- if _list[i] then
- term.setCursorPos(x,y)
- term.clearLine()
- term.write(_list[i])
- if playerData[_list[i]] then
- local value = playerData[_list[i]]
- if type(value) == "table" then
- value = string.gsub(textutils.serialise(value),"([%s|\n]+)","")
- elseif type(value) ~= "number"
- and type(value) ~= "string"
- and type(value) ~= "boolean"
- then
- value = "("..type(value)..")"
- else
- value = tostring(value)
- end
- term.blit(
- ":"..string.sub(value..string.rep(" ",screenW),1,screenW-1),
- "8"..string.rep("7",screenW-1),
- "f"..string.rep("f",screenW-1)
- )
- else
- term.blit(":nil","8777","ffff")
- end
- y = y+1
- end
- end
- --Term reset
- term.setTextColor(_color)
- end
- term.clear()
- repeat
- playerData = pocket.getEntity()
- local data = formatNum(playerData[_list[sel]])
- term.clear()
- term.setCursorPos(1,1)
- term.clearLine()
- term.write(_list[sel])
- menu()
- term.setCursorPos(1,2)
- term.clearLine()
- print("scroll:",scroll.."/"..#_list,"sel:",sel)
- display(data, alignCenter(data, 4), 6)
- local tick = os.startTimer(delay)
- local e = {os.pullEventRaw()}
- os.cancelTimer(tick)
- if e[1] == "mouse_click" and e[2] == 1 then
- menu(e[4])
- sleep(.1)
- elseif e[1] == "mouse_scroll" then
- scroll = scroll + e[2]
- end
- until e[1] == "terminate"
- --Term reset
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- printError("Terminated")
- sleep(.2)
Add Comment
Please, Sign In to add comment