From 34b3c13404c466c8f64d198dce914a1e3fa094c2 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Fri, 15 Feb 2013 13:00:59 +0000 Subject: Plugins can now bind console commands FS #300 Most console commands are now implemented in the Core plugin. git-svn-id: http://mc-server.googlecode.com/svn/trunk@1214 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- MCServer/Plugins/Core/console.lua | 188 ++++++++++++++++++++++++++++++++++++++ MCServer/Plugins/Core/main.lua | 14 +-- 2 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 MCServer/Plugins/Core/console.lua (limited to 'MCServer') diff --git a/MCServer/Plugins/Core/console.lua b/MCServer/Plugins/Core/console.lua new file mode 100644 index 000000000..398b70eb4 --- /dev/null +++ b/MCServer/Plugins/Core/console.lua @@ -0,0 +1,188 @@ + +-- console.lua + +-- Implements things related to console commands + + + + + +function InitConsoleCommands() + local PluginMgr = cPluginManager:Get(); + PluginMgr:BindConsoleCommand("help", HandleConsoleHelp, "Lists all commands"); + PluginMgr:BindConsoleCommand("numchunks", HandleConsoleNumChunks, "Shows number of chunks currently loaded"); + PluginMgr:BindConsoleCommand("players", HandleConsolePlayers, "Lists all connected players"); + PluginMgr:BindConsoleCommand("primaryserverversion", HandleConsolePrimaryServerVersion, "Gets or sets server version reported to 1.4+ clients"); + PluginMgr:BindConsoleCommand("reload", HandleConsoleReload, "Reloads all plugins"); + PluginMgr:BindConsoleCommand("save-all", HandleConsoleSaveAll, "Saves all chunks"); + PluginMgr:BindConsoleCommand("say", HandleConsoleSay, "Sends a chat message to all players"); + PluginMgr:BindConsoleCommand("unload", HandleConsoleUnload, "Unloads all unused chunks"); +end + + + + + +function HandleConsoleHelp(Split) + local Commands = {}; -- {index => {"Command", "HelpString"} } + local MaxLength = 0; + local AddToTable = function(Command, HelpString) + table.insert(Commands, { Command, HelpString }); + local CmdLen = Command:len(); + if (CmdLen > MaxLength) then + MaxLength = CmdLen; + end + end + + cPluginManager:Get():ForEachConsoleCommand(AddToTable); + + -- Sort the table: + local CompareCommands = function(a, b) + return a[1] < b[1]; -- compare command strings + end + table.sort(Commands, CompareCommands); + + for i, Command in ipairs(Commands) do + local Cmd = Command[1] .. string.rep(" ", MaxLength - Command[1]:len()); -- Align to a table + LOG(Cmd .. " - " .. Command[2]); + end + return true; +end + + + + + +function HandleConsoleNumChunks(Split) + local Output = {}; + local AddNumChunks = function(World) + Output[World:GetName()] = World:GetNumChunks(); + end; + + cRoot:Get():ForEachWorld(AddNumChunks); + + local Total = 0; + for name, num in pairs(Output) do + LOG(" " .. name .. ": " .. num .. " chunks"); + Total = Total + num; + end + LOG("Total: " .. Total .. " chunks"); + + return true; +end + + + + + +function HandleConsolePlayers(Split) + local PlayersInWorlds = {}; -- "WorldName" => [players array] + local AddToTable = function(Player) + local WorldName = Player:GetWorld():GetName(); + if (PlayersInWorlds[WorldName] == nil) then + PlayersInWorlds[WorldName] = {}; + end + table.insert(PlayersInWorlds[WorldName], Player:GetName()); + end + + cRoot:Get():ForEachPlayer(AddToTable); + + for WorldName, Players in pairs(PlayersInWorlds) do + LOG("World " .. WorldName .. ":"); + for i, PlayerName in ipairs(Players) do + LOG(" " .. PlayerName); + end + end + + return true; +end + + + + + +function HandleConsolePrimaryServerVersion(Split) + if (#Split == 1) then + -- Display current version: + local Version = cRoot:Get():GetPrimaryServerVersion(); + LOG("Primary server version: #" .. Version .. ", " .. cRoot:GetProtocolVersionTextFromInt(Version)); + return true; + end + + -- Set new value as the version: + cRoot:Get():SetPrimaryServerVersion(tonumber(Split[2])); + local Version = cRoot:Get():GetPrimaryServerVersion(); + LOG("Primary server version is now #" .. Version .. ", " .. cRoot:GetProtocolVersionTextFromInt(Version)); + return true; +end + + + + + +function HandleConsoleReload(Split) + Server = cRoot:Get():GetServer(); + Server:SendMessage(cChatColor.Green .. "Reloading all plugins."); + cPluginManager:Get():ReloadPlugins(); + return true; +end + + + + + +function HandleConsoleSaveAll(Split) + cRoot:Get():SaveAllChunks(); + return true; +end + + + + + +function HandleConsoleSay(Split) + table.remove(Split, 1); + local Message = ""; + for i, Text in ipairs(Split) do + Message = Message .. " " .. Text; + end + Message = Message:sub(2); -- Cut off the first space + cRoot:Get():GetServer():BroadcastChat(cChatColor.Purple .. "[SERVER] " .. Message); + return true; +end + + + + + +function HandleConsoleUnload(Split) + local UnloadChunks = function(World) + World:UnloadUnusedChunks(); + end + + LOGINFO("Num loaded chunks before: " .. cRoot:Get():GetTotalChunkCount()); + cRoot:Get():ForEachWorld(UnloadChunks); + LOGINFO("Num loaded chunks after: " .. cRoot:Get():GetTotalChunkCount()); + return true; +end + + + + + +function HandleConsole(Split) + return true; +end + + + + + +function HandleConsole(Split) + return true; +end + + + + + diff --git a/MCServer/Plugins/Core/main.lua b/MCServer/Plugins/Core/main.lua index 4682333fd..d9049bfda 100644 --- a/MCServer/Plugins/Core/main.lua +++ b/MCServer/Plugins/Core/main.lua @@ -49,6 +49,8 @@ function Initialize(Plugin) PluginManager:BindCommand("/regeneratechunk", "core.regeneratechunk", HandleRegenerateChunkCommand, " <[X] [Z]> - Regenerates a chunk, current or specified"); PluginManager:BindCommand("/viewdistance", "core.viewdistance", HandleViewDistanceCommand, " [".. cClientHandle.MIN_VIEW_DISTANCE .."-".. cClientHandle.MAX_VIEW_DISTANCE .."] - Change your view distance") + InitConsoleCommands(); + local IniFile = cIniFile("settings.ini") if ( IniFile:ReadFile() == true ) then SHOW_PLUGIN_NAMES = IniFile:GetValueB("HelpPlugin", "ShowPluginNames", true ) @@ -89,12 +91,12 @@ function Initialize(Plugin) end end - Plugin:AddWebTab( "Server Settings", HandleRequest_ServerSettings ) - Plugin:AddWebTab( "Chat", HandleRequest_Chat ) - Plugin:AddWebTab( "Playerlist", HandleRequest_PlayerList ) - Plugin:AddWebTab( "Whitelist", HandleRequest_WhiteList ) - Plugin:AddWebTab( "Permissions", HandleRequest_Permissions ) - Plugin:AddWebTab( "Manage Plugins", HandleRequest_ManagePlugins ) + Plugin:AddWebTab("Server Settings", HandleRequest_ServerSettings); + Plugin:AddWebTab("Chat", HandleRequest_Chat); + Plugin:AddWebTab("Playerlist", HandleRequest_PlayerList); + Plugin:AddWebTab("Whitelist", HandleRequest_WhiteList); + Plugin:AddWebTab("Permissions", HandleRequest_Permissions); + Plugin:AddWebTab("Manage Plugins", HandleRequest_ManagePlugins); LOG( "Initialized " .. Plugin:GetName() .. " v." .. Plugin:GetVersion() ) return true -- cgit v1.2.3