Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Convert world command to Lua and improve help command and subcommands…
Browse files Browse the repository at this point in the history
… listings
  • Loading branch information
vexyl committed Jul 23, 2018
1 parent 9f0407e commit 9661337
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 240 deletions.
2 changes: 1 addition & 1 deletion plugins/core/permissions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ PermissionsPlugin.CheckPermissionNotify = function(client, permission)
end

-- Operators bypass permissions
if (client:GetUserType() == 0x64) then -- operator
if (client:GetUserType() >= 0x64) then -- operator or higher
return true
end

Expand Down
4 changes: 4 additions & 0 deletions plugins/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ end
WORLD_NOT_ACTIVE = function(name)
return "&cWorld &f" .. name .. " &cnot active (unloaded)"
end

WORLD_ALREADY_EXISTS = function(name)
return "&cWorld &f" .. name .. " &calready exists"
end
58 changes: 45 additions & 13 deletions plugins/essentials/world.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
EssentialsPlugin.World = {
Init = function()
local worldCmd = Server.AddCommand("luaworld", "lw", function() end, "world - various commands related to worlds", 1, 0)
local worldListCmd = worldCmd:AddSubcommand("list", EssentialsPlugin.World.WorldCommand_List, "list - list all available worlds; active=green, inactive=red", 0, 0)
worldCmd:AddSubcommand("set", EssentialsPlugin.World.WorldCommand_Set, "set [option] [value] - sets world options; leave off arguments to see list of options; leave off value to see current value", 0, 1)
worldCmd:AddSubcommand("save", EssentialsPlugin.World.WorldCommand_Save, "save - saves world options and map data to file", 0, 1)
worldCmd:AddSubcommand("load", EssentialsPlugin.World.WorldCommand_Load, "load <world name> - loads world map into memory", 1, 0)
worldCmd:AddSubcommand("new", EssentialsPlugin.World.WorldCommand_New, "new <world name> <x> <y> <z>", 4, 0)
local worldCmd = Server.AddCommand("world", "w map", function() end, "world - various commands related to worlds", 1, 0)
local worldListCmd = worldCmd:AddSubcommand("list", EssentialsPlugin.World.Command_List, "list - list all available worlds; active=green, inactive=red", 0, 0)
worldCmd:AddSubcommand("set", EssentialsPlugin.World.Command_Set, "set [option] [value] - sets world options; leave off arguments to see list of options; leave off value to see current value", 0, 1)
worldCmd:AddSubcommand("save", EssentialsPlugin.World.Command_Save, "save - saves world options and map data to file", 0, 1)
worldCmd:AddSubcommand("load", EssentialsPlugin.World.Command_Load, "load <world name> - loads world map into memory", 1, 0)
worldCmd:AddSubcommand("new", EssentialsPlugin.World.Command_New, "new <world name> <x> <y> <z>", 4, 0)

Server.AddCommand("worlds", "", function(client, args) EssentialsPlugin.World.WorldCommand_List(client, args) end, "worlds - shortcut for /world list", 0, 0)
Server.AddCommand("worlds", "", function(client, args) EssentialsPlugin.World.Command_List(client, args) end, "worlds - shortcut for /world list", 0, 0)

PermissionsPlugin.RequirePermission("essentials.world")
end,

WorldCommand_List = function(client, args)
Command_List = function(client, args)
local message = "&eWorlds: "
local worlds = Server.GetWorlds()

Expand All @@ -33,7 +33,7 @@ WorldCommand_List = function(client, args)
Server.SendMessage(client, message)
end,

WorldCommand_Set = function(client, args)
Command_Set = function(client, args)
if (not PermissionsPlugin.CheckPermissionNotify(client, "essentials.world")) then
return
end
Expand Down Expand Up @@ -73,7 +73,7 @@ WorldCommand_Set = function(client, args)
Server.SendMessage(client, "&eSet option " .. option .. " to " .. value)
end,

WorldCommand_Save = function(client, args)
Command_Save = function(client, args)
if (not PermissionsPlugin.CheckPermissionNotify(client, "essentials.world")) then
return
end
Expand All @@ -83,7 +83,7 @@ WorldCommand_Save = function(client, args)
Server.SendMessage(client, "&eWorld saved")
end,

WorldCommand_Load = function(client, args)
Command_Load = function(client, args)
local targetName = string.lower(args[1])

local targetWorld = Server.GetWorldByName(targetName)
Expand All @@ -104,7 +104,39 @@ WorldCommand_Load = function(client, args)
Server.SendMessage(client, "&eLoaded world &a" .. worldName)
end,

WorldCommand_New = function(client, args)
Server.SendMessage(client, "&cNot yet implemented")
Command_New = function(client, args)
if (not PermissionsPlugin.CheckPermissionNotify(client, "essentials.world")) then
return
end

local worldName = string.lower(args[1])

if (string.len(worldName) > 10) then
Server.SendMessage(client, "&cInvalid name")
return
end

local x = tonumber(args[2])
local y = tonumber(args[3])
local z = tonumber(args[4])

if (type(x) ~= 'number' or type(y) ~= 'number' or type(z) ~= 'number') then
Server.SendMessage(client, "&cMalformed input")
return
end

if ((x > 512 or x <= 0) or (y > 64 or y <=0) or (z > 512 or z <= 0)) then
Server.SendMessage(client, "&cInvalid size. Max map size is &f512&cx&f64&cx&f512&c.")
return
end

if (Server.GetWorldByName(worldName) ~= nil) then
Server.SendMessage(client, WORLD_ALREADY_EXISTS(worldName))
return
end

Server.CreateWorld(worldName, x, y, z)

Server.SendMessage(client, "&eWorld '" .. worldName .. "' with size " .. x .. "x" .. y .. "x" .. z .. " created successfully.")
end
}
18 changes: 16 additions & 2 deletions src/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ Command* CommandHandler::GetCommand(std::string name)
return iter->second;
}

Command* CommandHandler::GetCommandByAlias(std::string name)
{
auto aliasIter = m_aliases.find(name);
if (aliasIter != m_aliases.end()) {
auto iter = m_commands.find(aliasIter->second);
if (iter != m_commands.end())
return iter->second;
}

return nullptr;
}

void CommandHandler::Register(std::string name, Command* command, std::string aliases)
{
std::pair<CommandMap::iterator, bool> res = m_commands.insert(std::make_pair(name, command));
Expand Down Expand Up @@ -111,8 +123,10 @@ void CommandHandler::Execute(Client* sender, std::string name, const CommandArgs
// Not found, check aliases
if (iter == m_commands.end()) {
auto aliasIter = m_aliases.find(name);
if (aliasIter != m_aliases.end())
iter = m_commands.find(aliasIter->second);
if (aliasIter != m_aliases.end()) {
name = aliasIter->second;
iter = m_commands.find(name);
}
}

if (iter == m_commands.end()) {
Expand Down
3 changes: 2 additions & 1 deletion src/CommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class CommandHandler {
void Reset();

Command* GetCommand(std::string name);
const CommandMap& GetCommandList() const { return m_commands; }
Command* GetCommandByAlias(std::string name);
CommandMap GetCommandList() const { return m_commands; }

void Register(std::string name, Command* command, std::string aliases="");
void Execute(Client* sender, std::string name, const CommandArgs& args);
Expand Down
11 changes: 9 additions & 2 deletions src/Commands/HelpCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ class HelpCommand : public Command {

// Does this command exist?
auto iter = commands.find(commandName);
if (iter != commands.end()) {

Command* testCommand = nullptr;
if (iter != commands.end())
testCommand = iter->second;
else
testCommand = server->GetCommandHandler().GetCommandByAlias(commandName);

if (testCommand != nullptr) {
// issued /help commandName
Command* subcheck = iter->second;
Command* subcheck = testCommand;
std::string docStringPrefix = "/";

if (args.size() > 1) {
Expand Down
219 changes: 0 additions & 219 deletions src/Commands/WorldCommand.hpp

This file was deleted.

Loading

0 comments on commit 9661337

Please sign in to comment.