Skip to content

Commit

Permalink
Merge pull request #165 from LogicParrot/worldBorder
Browse files Browse the repository at this point in the history
Fixed WorldLimiter jitter
  • Loading branch information
Julian Laubstein committed Apr 20, 2016
2 parents c4afc0a + c639f3c commit 00b2463
Showing 1 changed file with 51 additions and 22 deletions.
73 changes: 51 additions & 22 deletions worldlimiter.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@
function OnPlayerMoving( Player )
local LimitWorldWidth = WorldsWorldLimit[Player:GetWorld():GetName()]
local WorldLimiter_Flag = false -- True when teleportation is about to occur, false otherwise
local WorldLimiter_LastMessage = -100 -- The last time the player was sent a message about reaching the border
function OnPlayerMoving(Player)
if (WorldLimiter_Flag == true) then
return
end

local LimitChunks = WorldsWorldLimit[Player:GetWorld():GetName()]

-- The world probably was created by an external plugin. Lets load the settings.
if not LimitWorldWidth then
-- The world probably was created by an external plugin. Let's load the settings.
if not LimitChunks then
LoadWorldSettings(Player:GetWorld())
LimitWorldWidth = WorldsWorldLimit[Player:GetWorld():GetName()]
LimitChunks = WorldsWorldLimit[Player:GetWorld():GetName()]
end
if LimitWorldWidth > 0 then

if (LimitChunks > 0) then
local World = Player:GetWorld()
local SpawnX = math.floor(World:GetSpawnX() / 16)
local SpawnZ = math.floor(World:GetSpawnZ() / 16)
local X = math.floor(Player:GetPosX() / 16)
local Z = math.floor(Player:GetPosZ() / 16)
if ( (SpawnX + LimitWorldWidth - 1) < X ) then
Player:TeleportToCoords(Player:GetPosX() - 1, Player:GetPosY(), Player:GetPosZ())
end
if ( (SpawnX - LimitWorldWidth + 1) > X ) then
Player:TeleportToCoords(Player:GetPosX() + 1, Player:GetPosY(), Player:GetPosZ())
end
if ( (SpawnZ + LimitWorldWidth - 1) < Z ) then
Player:TeleportToCoords(Player:GetPosX(), Player:GetPosY(), Player:GetPosZ() - 1)
local Limit = LimitChunks * 16 - 1
local SpawnX = math.floor(World:GetSpawnX())
local SpawnZ = math.floor(World:GetSpawnZ())
local X = math.floor(Player:GetPosX())
local Z = math.floor(Player:GetPosZ())
local NewX = X
local NewZ = Z

if (X > SpawnX + Limit) then
NewX = SpawnX + Limit
elseif (X < SpawnX - Limit) then
NewX = SpawnX - Limit
end
if ( (SpawnZ - LimitWorldWidth + 1) > Z ) then
Player:TeleportToCoords(Player:GetPosX(), Player:GetPosY(), Player:GetPosZ() + 1)

if (Z > SpawnZ + Limit) then
NewZ = SpawnZ + Limit
elseif (Z < SpawnZ - Limit) then
NewZ = SpawnZ - Limit
end

if (X ~= NewX) or (Z ~= NewZ) then
WorldLimiter_Flag = true

local UpTime = cRoot:Get():GetServerUpTime()
if UpTime - WorldLimiter_LastMessage > 30 then
WorldLimiter_LastMessage = UpTime
Player:SendMessageInfo("You have reached the world border")
end

local UUID = Player:GetUUID()
World:ScheduleTask(3, function(World)
World:DoWithPlayerByUUID(UUID, function(Player)
Player:TeleportToCoords(NewX, Player:GetPosY(), NewZ)
WorldLimiter_Flag = false
end)
end)
end


end
end
end

0 comments on commit 00b2463

Please sign in to comment.