Larvix

map/query

Dec 15th, 2023 (edited)
990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. mapPath = "./data/map"
  2. logPath = "./data/queries/"
  3.  
  4. myX, myY, myZ = gps.locate()
  5. myX = math.floor(myX)
  6. myY = math.floor(myY)
  7. myZ = math.floor(myZ)
  8.  
  9. args = {...}
  10. if args[1] == "me" then
  11.     print("I am at",myX,myY,myZ)
  12. elseif args[1] == "type" then
  13.     getType = tostring(args[2])
  14. elseif args[1] == "pos" then
  15.     if args[4] then
  16.         getX = tonumber(args[2])
  17.         getY = tonumber(args[3])
  18.         getZ = tonumber(args[4])
  19.     end
  20. else print("Usage:\n'query me'\n'query pos <x> <y> <z>'\n'query type <block_name>'") end
  21.  
  22. if myX == getX and myY == getY and myZ == getZ then
  23.     print("That's me! \2")
  24. elseif (getX ~= nil) or (getType ~= nil) then
  25.     if not fs.exists(mapPath) then
  26.         print("Requires map to exist")
  27.     else
  28.         mapFile = fs.open(mapPath, "r")
  29.         map = mapFile.readAll()
  30.         map = textutils.unserialise(map)
  31.         mapFile.close()
  32.         found = false
  33.         qResult = {}
  34.         for i = 1,#map do
  35.             if getX and (found == false) then
  36.                 if map[i].x == getX and map[i].y == getY and map[i].z == getZ then
  37.                     name = map[i].name
  38.                     found = true
  39.                 end
  40.             elseif getType ~= nil then
  41.                 name = map[i].name
  42.                 name = name:sub((string.find(name, ":"))+1,#name)
  43.                 if name == getType then
  44.                     found = true
  45.                     table.insert(qResult, {x=map[i].x,y=map[i].y,z=map[i].z})
  46.                 end
  47.             end
  48.         end
  49.         if getType and found then
  50.             print(found,#qResult)
  51.             if #qResult < 2 then
  52.                 for res = 1,#qResult do
  53.                     write("Found @ "..qResult[res].x.." "..qResult[res].y.." "..qResult[res].z.."\n")
  54.                 end
  55.             elseif #qResult > 1 then
  56.                 print("Save query result to file? <y/n>")
  57.                 evt, key = os.pullEvent("key")
  58.                 sleep(0)
  59.                 if key == keys.y then
  60.                     fName = getType.."_query"
  61.                     file = fs.open(logPath..fName, "w")
  62.                     for save = 1,#qResult do
  63.                         file.writeLine(qResult[save])
  64.                     end
  65.                     file.close()
  66.                 end
  67.             end
  68.         elseif getX and found then
  69.             print(found,name)
  70.         else print(found) end
  71.     end
  72. end
Advertisement
Add Comment
Please, Sign In to add comment