Advertisement
NickSProud

nsp_core.lua

Jun 26th, 2022
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. nsp_core = {}
  2.  
  3. function nsp_core.serialize(data, name)
  4.     if not fs.exists('/data') then
  5.         fs.makeDir('/data')
  6.     end
  7.     local f = fs.open('/data/'..name, 'w')
  8.     f.write(textutils.serialize(data))
  9.     f.close()
  10. end
  11.  
  12. function nsp_core.unserialize(name)
  13.     if fs.exists('/data/'..name) then
  14.         local f = fs.open('/data/'..name, 'r')
  15.         local data = textutils.unserialize(f.readAll())
  16.         f.close()
  17.         return data
  18.     else
  19.         return nil
  20.     end
  21. end
  22.  
  23. function nsp_core.serialize_location(data)
  24.     nsp_core.serialize(data, "location/".. data["name"])
  25. end
  26.  
  27. function nsp_core.unserialize_location(name)
  28.     return nsp_core.unserialize("location/" .. name)
  29. end
  30.  
  31. function nsp_core.serialize_chunk(data)
  32.     nsp_core.serialize(data, "chunk/".. data["x"] .. ",".. data["z"])
  33. end
  34.  
  35. function nsp_core.unserialize_chunk(x,z)
  36.     return nsp_core.unserialize("chunk/" .. x .. "," ..z)
  37. end
  38.  
  39. function nsp_core.is_direction(nsew)
  40.     if nsew == "N" or nsew == "S" or nsew == "E" or nsew == "W" then
  41.         return true
  42.     else
  43.         return false
  44.     end
  45. end
  46.  
  47. function nsp_core.set_location(name, x, z, y, ...)
  48.     local args = { ... }
  49.     local x_num = tonumber(x)
  50.     local y_num = tonumber(y)
  51.     local z_num = tonumber(z)
  52.     if type(x_num) == "number"
  53.     and type(z_num) == "number"
  54.     and type(y_num) == "number"
  55.     and type(name) == "string" then
  56.         local location = {["x"] = x_num,["z"] = z_num,["y"] = y_num, ["name"] = name}
  57.         if #args == 1 and nsp_core.is_direction(args[1]) then
  58.             location["f"] = args[1]
  59.         end
  60.         nsp_core.serialize_location(location)
  61.     else
  62.         print("Not applicable data to set location")
  63.     end
  64. end
  65. --local chunk_location = {["x"] = 0,["z"] = 0,["name"] = "unnamed"}
  66. function nsp_core.find_chunk(x, z)
  67.     local x_num = tonumber(x)
  68.     x_num = math.floor(x_num/16)
  69.     local z_num = tonumber(z)
  70.     z_num = math.floor(z_num/16)
  71.     local chunk_data = nil
  72.     if type(x_num) == "number" and type(z_num) == "number" then
  73.         chunk_data = nsp_core.unserialize_chunk(x_num,z_num)
  74.         if chunk_data == nil then
  75.             chunk_data = {["x"] = x_num,["z"] = z_num,["name"] = "unnamed"}
  76.         end
  77.         print(textutils.serialize(chunk_data))
  78.         return chunk_data
  79.     end
  80. end
  81.  
  82. return nsp_core
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement