forked from cuberite/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_functions.lua
127 lines (108 loc) · 3.67 KB
/
core_functions.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-- Returns the online mode UUID for a player name, if it exists
-- Otherwise returns an offline UUID
function GetPlayerUUID(PlayerName)
if cRoot:Get():GetServer():ShouldAuthenticate() then
-- The server is in online-mode, get the UUID from Mojang servers and check for validity:
return cMojangAPI:GetUUIDFromPlayerName(PlayerName)
end
-- The server is in offline mode, generate an offline-mode UUID, no validity check is possible:
return cClientHandle:GenerateOfflineUUID(PlayerName)
end
-- Returns the world object of the specified world name.
-- If a name isn't provided, the function returns the world of the specified player.
-- If a player isn't specified (e.g. console), the function returns the default world.
function GetWorld(WorldName, Player)
if not WorldName then
return Player and Player:GetWorld() or cRoot:Get():GetDefaultWorld()
end
return cRoot:Get():GetWorld(WorldName)
end
-- Kicks a player by name, with the specified reason; returns bool whether found
function KickPlayer(PlayerName, Reason)
if not Reason then
Reason = "You have been kicked"
end
local KickPlayer = function(Player)
Player:GetClientHandle():Kick(Reason)
end
if not cRoot:Get():FindAndDoWithPlayer(PlayerName, KickPlayer) then
-- Could not find player
return false
end
-- Player has been kicked
return true
end
function RelativeCommandCoord(Split, Coord)
if Split then
if string.sub(Split, 1, 1) == "~" then
local Relative = tonumber(string.sub(Split, 2, -1))
if Coord then
if Relative then
return Coord + Relative
end
return Coord
end
return Relative
end
return tonumber(Split)
end
return Split
end
-- Safer method to find players
function SafeDoWithPlayer(PlayerName, Function)
local DoWithPlayer = function(World)
World:DoWithPlayer(PlayerName, Function)
end
local QueueTask = function(World)
World:QueueTask(DoWithPlayer)
end
cRoot:Get():ForEachWorld(QueueTask);
end
-- If the target is a player, the SendMessage function takes care of sending the message to the player.
-- If the target is a command block or the console, the message is simply returned to the calling function,
-- which delivers it appropriately
function SendMessage(Player, Message)
if Player then
Player:SendMessageInfo(Message)
return nil
end
return Message
end
function SendMessageSuccess(Player, Message)
if Player then
Player:SendMessageSuccess(Message)
return nil
end
return Message
end
function SendMessageFailure(Player, Message)
if Player then
Player:SendMessageFailure(Message)
return nil
end
return Message
end
-- Teleports a_SrcPlayer to a player named a_DstPlayerName; if a_TellDst is true, will send a notice to the destination player
-- TODO: cleanup
function TeleportToPlayer( a_SrcPlayer, a_DstPlayerName, a_TellDst )
local teleport = function(a_DstPlayerName)
if a_DstPlayerName == a_SrcPlayer then
-- Asked to teleport to self?
SendMessageFailure( a_SrcPlayer, "Y' can't teleport to yerself" )
else
-- If destination player is not in the same world, move to the correct world
if a_SrcPlayer:GetWorld() ~= a_DstPlayerName:GetWorld() then
a_SrcPlayer:MoveToWorld( a_DstPlayerName:GetWorld(), true, Vector3d( a_DstPlayerName:GetPosX() + 0.5, a_DstPlayerName:GetPosY(), a_DstPlayerName:GetPosZ() + 0.5 ) )
else
a_SrcPlayer:TeleportToEntity( a_DstPlayerName )
end
SendMessageSuccess( a_SrcPlayer, "You teleported to " .. a_DstPlayerName:GetName() )
if (a_TellDst) then
SendMessage( a_DstPlayerName, a_SrcPlayer:GetName().." teleported to you" )
end
end
end
if not cRoot:Get():FindAndDoWithPlayer( a_DstPlayerName, teleport ) then
SendMessageFailure( a_SrcPlayer, "Player " .. a_DstPlayerName .. " not found" )
end
end