forked from cuberite/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.lua
99 lines (75 loc) · 2.66 KB
/
help.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
-- help.lua
-- Implements the /help in-game command
--- How many commands to put into one help page
local g_CommandsPerPage = 8
--- Displays one page of help for commands beginning with the specified string
-- If a_Beginning is not given, all commands are displayed
local function handleHelpPage(a_Player, a_PageNumber, a_Beginning)
-- Check params:
assert(type(a_PageNumber) == "number")
assert(tolua.type(a_Player) == "cPlayer")
a_Beginning = a_Beginning or ""
assert(type(a_Beginning) == "string")
-- Collect all commands into a table:
local output = {}
local beginLen = a_Beginning:len()
cPluginManager:Get():ForEachCommand(
function(a_CBCommand, a_CBPermission, a_CBHelpString)
if not (a_Player:HasPermission(a_CBPermission)) then
-- Do not display commands for which the player has no permission
return false
end
if (a_CBHelpString == "") then
-- Do not display commands without a help string
return false
end
-- Check that the command contains the wanted string:
if (a_CBCommand:sub(1, beginLen) == a_Beginning) then
table.insert(output, a_CBCommand .. " " .. a_CBHelpString)
end
end
)
-- Check the page count:
local numCommands = #output
if (numCommands == 0) then
a_Player:SendMessageFailure("No commands available")
return true
end
local firstLine = (a_PageNumber - 1) * g_CommandsPerPage + 1
local lastLine = firstLine + g_CommandsPerPage
local maxPages = math.ceil(numCommands / g_CommandsPerPage)
if (firstLine > numCommands) then
a_Player:SendMessageFailure("The requested page is not available. Only pages 1 - " .. maxPages .. " are available.")
return true
end
-- Display only the requested page:
table.sort(output)
a_Player:SendMessageInfo("Displaying page " .. a_PageNumber .. " of " .. maxPages .. ":")
for idx, txt in ipairs(output) do
if ((idx >= firstLine) and (idx < lastLine)) then
a_Player:SendMessage(txt)
end
end
return true
end
--- Decides what help to show based on the parameters, calls the appropriate worker function
function HandleHelpCommand(a_Split, a_Player)
-- Handles the "/help [<PageNum>] [<CommandText>]" in-game command
-- If there's no param, display the first page of help:
local numSplit = #a_Split
if (numSplit == 1) then
return handleHelpPage(a_Player, 1)
end
-- If there is a number as the first param, show that page:
local pageRequested = tonumber(a_Split[2])
local filterStart = 3
if (pageRequested == nil) then
filterStart = 2
pageRequested = 1
end
local beginningWanted
if (numSplit >= filterStart) then
beginningWanted = "/" .. table.concat(a_Split, " ", filterStart)
end
return handleHelpPage(a_Player, pageRequested, beginningWanted)
end