lunacanis

SmartyQuarryDB

Sep 19th, 2023 (edited)
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. local database = {}
  2. local Coords = {}
  3. local isPosSaved = true
  4.  
  5. --returns xCoord as int
  6. function database.GetXCoord()
  7.     if(Coords.xCoord == nil) then
  8.         printError("x co-ordinate is nil, did you run PopulateDB first?")
  9.     end
  10.     return Coords.xCoord
  11. end
  12.  
  13. function database.SetXCoord(xCoord)
  14.     if(Coords.xCoord == nil) then
  15.         printError("x co-ordinate is nil, did you run PopulateDB first?")
  16.         return
  17.     end
  18.     Coords.xCoord = xCoord
  19.     database.StorePosition()
  20. end
  21.  
  22. --returns yCoord as int
  23. function database.GetYCoord()
  24.     if(Coords.yCoord == nil) then
  25.         printError("y co-ordinate is nil, did you run PopulateDB first?")
  26.     end
  27.     return Coords.yCoord
  28. end
  29.  
  30. function database.SetYCoord(yCoord)
  31.     if(Coords.yCoord == nil) then
  32.         printError("y co-ordinate is nil, did you run PopulateDB first?")
  33.         return
  34.     end
  35.     Coords.yCoord = yCoord
  36.     database.StorePosition()
  37. end
  38.  
  39. --returns zCoord as int
  40. function database.GetZCoord()
  41.     if(Coords.zCoord == nil) then
  42.         printError("z co-ordinate is nil, did you run PopulateDB first?")
  43.     end
  44.     return Coords.zCoord
  45. end
  46.  
  47. function database.SetZCoord(zCoord)
  48.     if(Coords.zCoord == nil) then
  49.         printError("z co-ordinate is nil, did you run PopulateDB first?")
  50.         return
  51.     end
  52.     Coords.zCoord = zCoord
  53.     database.StorePosition()
  54. end
  55.  
  56. --function returns table of x,y,z coordinates indexed by xCoord, yCoord, and zCoord
  57. function database.GetCoords()
  58.     if(Coords.xCoord == nil or Coords.y == nil or Coords.z == nil ) then
  59.         printError("one or more co-ordinates in the table are nil, did you run PopulateDB first?")
  60.     end
  61.     return Coords
  62. end
  63.  
  64. --reads from the position.txt file and saves the coordinates, creates the position file at /home/smart_quarry/database/position.txt
  65. --returns true if succeeds and false if fails, prints error messages to console
  66. function database.PopulateDB()
  67.     local pos = fs.open("smart_quarry/database/position.txt","r")
  68.     local dbPopulatedCorrectly = true
  69.  
  70.     if(pos == nil) then
  71.         printError("position.txt file does not exist, creating one now at home/smart_quarry/database/position.txt, please add turtle start position to file with co-ordinate on a seperate line. It goes in order x, y, z. only the co-ordinates should be present no letters")
  72.         isPosSaved = false
  73.         pos = fs.open("smart_quarry/database/position.txt","w")
  74.         dbPopulatedCorrectly = false
  75.         return dbPopulatedCorrectly
  76.     end
  77.  
  78.     local x = pos.readLine()
  79.     local y = pos.readLine()
  80.     local z = pos.readLine()
  81.      
  82.     if(x == nil) then
  83.         printError("x value nil, check position.txt file for syntax errors")
  84.         dbPopulatedCorrectly = false
  85.     end
  86.      
  87.     if(y == nil) then
  88.         printError("y value nil, check position.txt file for syntax errors")
  89.         dbPopulatedCorrectly = false
  90.     end
  91.      
  92.     if(z == nil) then
  93.         printError("z value nil, check position.txt file for syntax errors")
  94.         dbPopulatedCorrectly = false
  95.     end
  96.  
  97.     if(dbPopulatedCorrectly) then
  98.         Coords.xCoord = tonumber(x)
  99.         Coords.yCoord = tonumber(y)
  100.         Coords.zCoord = tonumber(z)
  101.     end
  102.  
  103.     pos.close()
  104.     return dbPopulatedCorrectly
  105. end
  106.  
  107. --Writes the positions to the position.txt file in /home/smart_quarry/database/position.txt
  108. --This is necessary to properly save the database values
  109. function database.StorePosition()
  110.     local pos = fs.open("/smart_quarry/database/position.txt","w")
  111.     pos.writeLine(Coords.xCoord)
  112.     pos.writeLine(Coords.yCoord)
  113.     pos.writeLine(Coords.zCoord)
  114.     pos.close()
  115. end
  116.  
  117. --Updates positions in database for x y and z Coords, takes 3 seperate integer values for x,y,z coordinates respectively
  118. function database.UpdatePosition(xCoord, yCoord, zCoord)
  119.     Coords.xCoord = xCoord
  120.     Coords.yCoord = yCoord
  121.     Coords.zCoord = zCoord
  122. end
  123.  
  124.  
  125. return database
Advertisement
Add Comment
Please, Sign In to add comment