-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from LogicParrot/worldBorder
Fixed WorldLimiter jitter
- Loading branch information
Showing
1 changed file
with
51 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |