Advertisement
posicat

/cattech/serverops.lua

Oct 15th, 2024 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | None | 0 0
  1. --File: /cattech/serverops.lua
  2.  
  3. chunkDataHash={}
  4.  
  5. function coordinatesToChunkColumn(x, y, z)
  6.     local chunkX = math.floor(x / 16)
  7.     local chunkZ = math.floor(z / 16)
  8.     return chunkX, chunkZ
  9. end
  10.  
  11. function roundCoordinatesToChunkColumn(x,y,z)
  12.     local rX = math.floor(x / 16) * 16
  13.     local rZ = math.floor(z / 16) * 16
  14.     return rX, rZ
  15. end
  16.  
  17. function deltaTime(seconds)
  18.     local currentTime = os.time()
  19.     local newTime = currentTime + seconds/72
  20.     return newTime
  21. end
  22.  
  23. function deltaGameTime(seconds)
  24.     local currentTime = os.time()
  25.     local newTime = currentTime + seconds
  26.     return newTime
  27. end
  28.  
  29. function handleChunkLoad()
  30.     for key, chnk in pairs(chunkDataHash) do
  31.         local coord = chnk.chunkX .. " " .. chnk.chunkZ
  32.         log("debug",key .. " | " .. chnk.added .. " " .. chnk.expires .. "<=?" .. deltaTime(0))
  33.  
  34.         if chnk.expires <= deltaTime(0) then
  35.             executeCommand("/forceload remove " .. coord)
  36.             chunkDataHash[key]=nil
  37.         else
  38.             if (chnk.added == 0) then
  39.                 chunkDataHash[key]["added"] = 1
  40.                 executeCommand("/forceload add " .. coord)
  41.             end
  42.         end
  43.     end
  44. end
  45.  
  46. function addChunkLoad(x, y, z, minutes)
  47.     local chunkX, chunkZ = roundCoordinatesToChunkColumn(x, y, z)
  48.     local coord = chunkX .. "x" .. chunkZ
  49.  
  50.     if not chunkDataHash[coord] then
  51.         chunkDataHash[coord] = {}
  52.     end
  53.  
  54.     chunkDataHash[coord].chunkX = chunkX
  55.     chunkDataHash[coord].chunkZ = chunkZ
  56.     chunkDataHash[coord].added = chunkDataHash[coord].added or 0
  57.     chunkDataHash[coord].expires = deltaTime(minutes * 60)
  58. end
  59.  
  60. function executeCommand(command)
  61.     local success, err = pcall(function()
  62.         commands.exec(command)
  63.     end)
  64.  
  65.     if success then
  66.         log("debug","Command: " .. command)
  67.     else
  68.         log("error","Error: " .. err)
  69.     end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement