forked from cuberite/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gm.lua
126 lines (94 loc) · 3.16 KB
/
gm.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
-- gm.lua
-- Implements gamemode and gm commands and console commands
-- 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 = "Player not found"
--- 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, "Gamemode set 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, "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 = "Gamemode of " .. PlayerToChange .. " set to " .. GameModeNameTable[GameMode]
local MessageTail = " by: " .. Player:GetName()
if PlayerToChange ~= Player:GetName() then
SendMessageSuccess( Player, Message )
end
LOG( Message .. MessageTail )
else
SendMessageFailure(Player, 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 = "Gamemode of " .. PlayerToChange .. " set to " .. GameModeNameTable[GameMode]
local MessageTail = " by: " .. "console"
LOG( Message .. MessageTail )
else
LOG( MessageFailure )
end
return true
end