forked from cuberite/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgm.lua
More file actions
121 lines (95 loc) · 3.33 KB
/
gm.lua
File metadata and controls
121 lines (95 loc) · 3.33 KB
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
-- gm.lua
-- Implements gamemode and gm commands and console commands
-- Modified for the Aedificium platform.
-- Used to translate gamemodes into strings
local GameModeNameTable =
{
[gmSurvival] = "survival",
[gmCreative] = "creative",
[gmAdventure] = "adventure",
[gmSpectator] = "spectator",
}
-- Translate strings to their representative gamemodes
-- All options from vanilla minecraft
local GameModeTable =
{
["0"] = gmSurvival,
["survival"] = gmSurvival,
["s"] = gmSurvival,
["1"] = gmCreative,
["creative"] = gmCreative,
["c"] = gmCreative,
["2"] = gmAdventure,
["adventure"] = gmAdventure,
["a"] = gmAdventure,
["3"] = gmSpectator,
["spectator"] = gmSpectator,
["sp"] = gmSpectator,
}
local MessageFailure = "Couldn't find that player."
--- Changes the gamemode of the given player
--
-- @param GameMode The gamemode to change to
-- @param PlayerName The player name of the player to change the gamemode of
--
-- @return true if player was found and gamemode successfully changed, false otherwise
--
local function ChangeGameMode( GameMode, PlayerName )
local GMChanged = false
local lcPlayerName = string.lower(PlayerName)
-- Search through online players and if one matches
-- the given PlayerName then change their gamemode
cRoot:Get():FindAndDoWithPlayer(PlayerName,
function(PlayerMatch)
if string.lower(PlayerMatch:GetName()) == lcPlayerName then
PlayerMatch:SetGameMode(GameMode)
SendMessage(PlayerMatch, cChatColor.LightGray .. "Set your gamemode to " .. GameModeNameTable[GameMode] .. ".")
GMChanged = true
end
return true
end
)
return GMChanged
end
--- Handles the `/gamemode <survival|creative|adventure|spectator> [player]` in-game command
--
function HandleChangeGMCommand(Split, Player)
-- Check params, translate into gamemode and player name:
local GameMode = GameModeTable[Split[2]]
if not GameMode then
SendMessage(Player, cChatColor.LightGray .. "Usage: " .. Split[1] .. " <survival|creative|adventure|spectator> [player]")
return true
end
local PlayerToChange = Split[3] or Player:GetName()
-- Report success or failure:
if ChangeGameMode( GameMode, PlayerToChange ) then
local Message = PlayerToChange .. "'s gamemode was set to " .. GameModeNameTable[GameMode]
local MessageTail = " by the player " .. Player:GetName()
if PlayerToChange ~= Player:GetName() then
SendMessageSuccess(Player, cChatColor.LightGray .. Message .. ".")
end
LOGINFO(Message .. MessageTail .. ".")
else
SendMessageFailure(Player, cChatColor.LightGray .. MessageFailure)
end
return true
end
--- Handles the `gamemode <survival|creative|adventure|spectator> [player]` console command
--
function HandleConsoleGamemode( a_Split )
-- Check params, translate into gamemode and player name:
local GameMode = GameModeTable[a_Split[2]]
local PlayerToChange = a_Split[3]
if not PlayerToChange or not GameMode then
return true, "Usage: " .. a_Split[1] .. " <survival|creative|adventure|spectator> <player>"
end
-- Report success or failure:
if ChangeGameMode( GameMode, PlayerToChange ) then
local Message = PlayerToChange .. "'s gamemode was set to " .. GameModeNameTable[GameMode]
local MessageTail = " by the " .. "console"
LOGINFO(Message .. MessageTail .. ".")
else
LOG(MessageFailure)
end
return true
end