Advertisement
Guest User

anav_tracker

a guest
Nov 10th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. local a=http.get"https://raw.githubusercontent.com/rxi/json.lua/bee7ee3431133009a97257bde73da8a34e53c15c/json.lua"local b=fs.open("/libs/json.lua","w")b.write(a.readAll())a.close()b.close()
  2.  
  3. local json = require("/libs/json")
  4. local worldurl = "https://dynmap.switchcraft.pw/up/world/world/"
  5. local tX, tY = term.getSize()
  6. local reqdelay = 1
  7. local errdelay = 10
  8. local world = {}
  9.  
  10. local function writeAt(text, x, y, tc, bg)
  11.   term.setCursorPos(x, y)
  12.   term.setTextColor(tc or colors.white)
  13.   term.setBackgroundColor(bg or colors.black)
  14.   term.write(text)
  15. end
  16.  
  17. local function getMinecraftTime(servertime)
  18.     return {
  19.       hours = ((servertime / 1000) + 6) % 24,
  20.       minutes = ((servertime / 1000) % 1) * 60,
  21.       isDay = (servertime >= 0 and servertime < 13700),
  22.     }
  23. end
  24.  
  25. local function getWorld(world)
  26.   if world == "world" then return "Overworld"
  27.   elseif world == "-some-other-bogus-world-" then return "Nether"
  28.   elseif world == "DIM1" then return "The End"
  29.   else return "? "..world
  30.   end
  31. end
  32.  
  33. local function draw(world)
  34.   term.setBackgroundColor(colors.black)
  35.   term.clear()
  36.   term.setCursorPos(1, tY)
  37.   term.setTextColor(colors.red)
  38.   term.write('<---')
  39.  
  40.   local time = getMinecraftTime(world.servertime)
  41.   local weather = "Sunny"
  42.   if world.hasStorm then weather = "Raining"
  43.     if world.isThundering then weather = "Thunderstorm"
  44.     end
  45.   end
  46.  
  47.   writeAt(("%d players online | %#2d:%02d (%s) | %s"):format(world.currentcount, time.hours, time.minutes, time.isDay and "Day" or "Night", weather), 1, 1)
  48.  
  49.   for id, player in pairs(world.players) do
  50.     local bg = (id % 2 == 0) and colors.black or colors.gray
  51.     local cy = 2+id
  52.     term.setCursorPos(1, cy)
  53.     term.setBackgroundColor(bg)
  54.     term.clearLine()
  55.     writeAt("\16 "..player.name, 1, cy, nil, bg)
  56.     writeAt(("| \3%#2d \4%#2d |"):format(player.health, player.armor), 20, cy, nil, bg)
  57.     writeAt(getWorld(player.world), 32, cy, nil, bg)
  58.     writeAt(("| %#5d, %#3d, %#5d"):format(math.floor(player.x), math.floor(player.y), math.floor(player.z)), 42, cy, nil, bg)
  59.   end
  60. end
  61.  
  62. local timeout = os.startTimer(0)
  63. while true do
  64.   local event, url, data, data2 = os.pullEvent()
  65.   if event == "http_success" then
  66.     local recv = data.readAll()
  67.     data.close()
  68.     world = json.decode(recv)
  69.     if world then
  70.       draw(world)
  71.       timeout = os.startTimer(reqdelay)
  72.     else
  73.       printError("Could not decode json: "..recv)
  74.       timeout = os.startTimer(errdelay)
  75.     end
  76.   elseif event == "http_failure" then
  77.     printError("Could not connect to "..url)
  78.     timeout = os.startTimer(errdelay)
  79.  
  80.   elseif event == "timer" and url == timeout then http.request(worldurl)
  81.   elseif event == "term_resize" then
  82.     tX, tY = term.getSize()
  83.     draw(world)
  84.   elseif event == "monitor_touch" then
  85.       if data < 5 and data2 == tY then
  86.           break
  87.       end
  88.   elseif event == "key" then
  89.     if url == keys.q then
  90.       sleep(0)
  91.       break
  92.     end
  93.   end
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement