Skip to content

Commit

Permalink
More cleanups (#230)
Browse files Browse the repository at this point in the history
* More cleanups

* Use index instead of table.insert

* Address comments
  • Loading branch information
mathiascode committed Jun 29, 2020
1 parent 445dbc7 commit 5dbe68f
Show file tree
Hide file tree
Showing 24 changed files with 528 additions and 506 deletions.
56 changes: 54 additions & 2 deletions Info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ g_PluginInfo =
HelpString = "Shows the message of the day.",
},

["/numchunks"] =
{
Permission = "core.numchunks",
Handler = HandleNumChunksCommand,
HelpString = "Shows number of chunks currently loaded.",
},

["/plugins"] =
{
Alias = "/pl",
Expand All @@ -230,6 +237,7 @@ g_PluginInfo =

["/portal"] =
{
Alias = "/world",
Permission = "core.portal",
Handler = HandlePortalCommand,
HelpString = "Moves your player to a different world.",
Expand All @@ -256,6 +264,13 @@ g_PluginInfo =
HelpString = "Add a player to the administrator rank.",
},

["/players"] =
{
Permission = "core.players",
Handler = HandlePlayersCommand,
HelpString = "Shows a list of all connected players.",
},

["/regen"] =
{
Permission = "core.regen",
Expand Down Expand Up @@ -492,6 +507,13 @@ g_PluginInfo =
HelpString = "Unbans a player.",
},

["/unloadchunks"] =
{
Permission = "core.unloadchunks",
Handler = HandleUnloadChunksCommand,
HelpString = "Unloads all unused chunks.",
},

["/unrank"] =
{
Permission = "core.unrank",
Expand Down Expand Up @@ -828,6 +850,18 @@ g_PluginInfo =
HelpString = "Add a player to the Admin rank.",
},

["msg"] =
{
Handler = HandleConsoleTell,
HelpString = "Sends a private message to a player.",
},

["r"] =
{
Handler = HandleConsoleR,
HelpString = "Replies to the latest private message you received.",
},

["ranks"] =
{
Handler = HandleConsoleListRanks,
Expand All @@ -852,6 +886,12 @@ g_PluginInfo =
},
}, -- regen

["tell"] =
{
Handler = HandleConsoleTell,
HelpString = "Sends a private message to a player.",
},

["save-all"] =
{
Handler = HandleConsoleSaveAll,
Expand Down Expand Up @@ -882,6 +922,12 @@ g_PluginInfo =
HelpString = "Returns a player to the spawn point.",
},

["summon"] =
{
Handler = HandleConsoleSummon,
HelpString = "Summons an entity in the world.",
},

["time"] =
{
HelpString = "Sets or displays the time.",
Expand Down Expand Up @@ -1031,7 +1077,7 @@ g_PluginInfo =

["unloadchunks"] =
{
Handler = HandleConsoleUnload,
Handler = HandleConsoleUnloadChunks,
HelpString = "Unloads all unused chunks.",
},

Expand Down Expand Up @@ -1068,7 +1114,7 @@ g_PluginInfo =

["weather"] =
{
Handler = HandleConsoleWeather,
Handler = HandleConsoleWeather,
HelpString = "Changes the world's weather.",
ParameterCombinations =
{
Expand Down Expand Up @@ -1137,6 +1183,12 @@ g_PluginInfo =
},
}, -- Subcommands
}, -- whitelist

["worlds"] =
{
Handler = HandleConsoleWorlds,
HelpString = "Shows a list of all the worlds.",
},
}, -- ConsoleCommands
Permissions =
{
Expand Down
24 changes: 24 additions & 0 deletions cmd_numchunks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function HandleNumChunksCommand(Split, Player)
-- List each world's chunk count into a table, sum the total chunk count:
local Response = {}
local Total = 0

local GetWorldChunks = function(World)
local numchunks = World:GetNumChunks()
table.insert(Response, World:GetName() .. ": " .. numchunks .. " chunks")
Total = Total + numchunks
end

table.insert(Response, "Number of loaded chunks:")
cRoot:Get():ForEachWorld(GetWorldChunks)
table.sort(Response)

table.insert(Response, "Total: " .. Total .. " chunks")

-- Return the complete report:
return true, SendMessage(Player, table.concat(Response, "\n"))
end

function HandleConsoleNumChunks(Split)
return HandleNumChunksCommand(Split)
end
18 changes: 15 additions & 3 deletions cmd_op.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
local function GetAdminRankName()
local Ranks = cRankManager:GetAllRanks()
for _, Rank in ipairs(Ranks) do
local Permissions = cRankManager:GetRankPermissions(Rank)
for _, Permission in ipairs(Permissions) do
if Permission == "*" then
return Rank
end
end
end
end

function HandleOpCommand(Split, Player)
local Response

if not Split[2] then
Response = SendMessage(Player, "Usage: " .. Split[1] .. " <player>")
else
local PlayerName = Split[2]
local AdminRank = GetAdminRank()
local AdminRankName = GetAdminRankName()

if not AdminRank then
if not AdminRankName then
Response = SendMessage(Player, "No admin rank found, missing * permission")
else
return HandleRankCommand({"rank", PlayerName, AdminRank}, Player)
return HandleRankCommand({"rank", PlayerName, AdminRankName}, Player)
end
end
return true, Response
Expand Down
21 changes: 21 additions & 0 deletions cmd_players.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function HandlePlayersCommand(Split, Player)
local Response = {}

local AddToResponse = function(OtherPlayer)
table.insert(Response, " " .. OtherPlayer:GetName() .. " @ " .. OtherPlayer:GetIP())
end

local ForEachPlayer = function(World)
table.insert(Response, "World " .. World:GetName() .. ":")
World:ForEachPlayer(AddToResponse)
end

table.insert(Response, "Players online:")
cRoot:Get():ForEachWorld(ForEachPlayer)

return true, SendMessage(Player, table.concat(Response, "\n"))
end

function HandleConsolePlayers(Split)
return HandlePlayersCommand(Split)
end
19 changes: 19 additions & 0 deletions cmd_portal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function HandlePortalCommand(Split, Player)
local WorldName = Split[2]

if Split[3] then
SendMessage(Player, "Usage: " .. Split[1] .. " [world]")
elseif not WorldName then
SendMessage(Player, "You are in world \"" .. Player:GetWorld():GetName() .. "\"")
elseif Player:GetWorld():GetName() == WorldName then
SendMessageFailure(Player, "You are already in world \"" .. Split[2] .. "\"!")
elseif not cRoot:Get():GetWorld(WorldName) then
SendMessageFailure(Player, "Could not find world \"" .. Split[2] .. "\"!")
elseif not Player:MoveToWorld(WorldName) then
SendMessageFailure(Player, "Could not move to world \"" .. Split[2] .. "\"!")
else
SendMessageSuccess(Player, "Successfully moved to world \"" .. Split[2] .. "\"! :D")
end

return true
end
62 changes: 32 additions & 30 deletions summon.lua → cmd_summon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ local Mobs =
["villager"] = mtVillager,
["witch"] = mtWitch,
["wither"] = mtWither,
["wither_skeleton"] = mtWitherSkeleton,
["wolf"] = mtWolf,
["zombie"] = mtZombie,
["zombie_pigman"] = mtZombiePigman,
Expand Down Expand Up @@ -78,6 +79,7 @@ local Mobs =
["VillagerGolem"] = mtIronGolem,
["Witch"] = mtWitch,
["Wither"] = mtWither,
["WitherSkeleton"] = mtWitherSkeleton,
["Wolf"] = mtWolf,
["Zombie"] = mtZombie,
["PigZombie"] = mtZombiePigman,
Expand Down Expand Up @@ -141,46 +143,46 @@ local function SpawnEntity(EntityName, World, X, Y, Z, Player)
end

function HandleSummonCommand(Split, Player)
if not Split[2] then
Player:SendMessageInfo("Usage: " .. Split[1] .. " <entityname> [x] [y] [z]")
else
local X = Player:GetPosX()
local Y = Player:GetPosY()
local Z = Player:GetPosZ()
local World = Player:GetWorld()

if Split[3] then
X = RelativeCommandCoord(Split[3], X)
end
local Response

if Split[4] then
Y = RelativeCommandCoord(Split[4], Y)
if not Split[2] or (not Player and not Split[5]) then
Response = SendMessage(Player, "Usage: " .. Split[1] .. " <entityname> [x] [y] [z]")
else
local X
local Y
local Z
local World = cRoot:Get():GetDefaultWorld()

if Player then
X = Player:GetPosX()
Y = Player:GetPosY()
Z = Player:GetPosZ()
World = Player:GetWorld()
end

if Split[5] then
X = RelativeCommandCoord(Split[3], X)
Y = RelativeCommandCoord(Split[4], Y)
Z = RelativeCommandCoord(Split[5], Z)
end

if not X then
Player:SendMessageFailure("'" .. Split[3] .. "' is not a valid number")
return true
end

if not Y then
Player:SendMessageFailure("'" .. Split[4] .. "' is not a valid number")
return true
end

if not Z then
Player:SendMessageFailure("'" .. Split[5] .. "' is not a valid number")
return true
if not X then
return true, SendMessageFailure(Player, "'" .. Split[3] .. "' is not a valid number")
elseif not Y then
return true, SendMessageFailure(Player, "'" .. Split[4] .. "' is not a valid number")
elseif not Z then
return true, SendMessageFailure(Player, "'" .. Split[5] .. "' is not a valid number")
end
end

if SpawnEntity(Split[2], World, X, Y, Z, Player) then
Player:SendMessageSuccess("Successfully summoned entity at [X:" .. math.floor(X) .. " Y:" .. math.floor(Y) .. " Z:" .. math.floor(Z) .. "]")
Response = SendMessageSuccess(Player, "Successfully summoned entity at [X:" .. math.floor(X) .. " Y:" .. math.floor(Y) .. " Z:" .. math.floor(Z) .. "]")
else
Player:SendMessageFailure("Unknown entity '" .. Split[2] .. "'")
Response = SendMessageFailure(Player, "Unknown entity '" .. Split[2] .. "'")
end
end
return true
return true, Response
end

function HandleConsoleSummon(Split)
return HandleSummonCommand(Split)
end
66 changes: 66 additions & 0 deletions cmd_tell.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local LastSender = {}

function HandleTellCommand(Split, Player)
local Response

local SenderName = "Server"
local SenderUUID = "CuberiteServerConsoleSender"

if Player then
SenderName = Player:GetName()
SenderUUID = Player:GetUUID()
end

local SendPrivateMessage = function(OtherPlayer)
local Message = table.concat(Split, " ", 2)

local ReceiverName = "Server"
local ReceiverUUID = "CuberiteServerConsoleSender"

if OtherPlayer then
ReceiverName = OtherPlayer:GetName()
ReceiverUUID = OtherPlayer:GetUUID()

OtherPlayer:SendMessagePrivateMsg(Message, SenderName)
else
LOG("[MSG:" .. SenderName .. "] " .. Message)
end
LastSender[ReceiverUUID] = SenderUUID

Response = SendMessageSuccess(Player, "Message to \"" .. ReceiverName .. "\" sent!")
end

if Split[1] == "r" or Split[1] == "/r" then
if not Split[2] then
Response = SendMessage(Player, "Usage: " .. Split[1] .. " <message ...>")
elseif not LastSender[SenderUUID] then
Response = SendMessageFailure(Player, "No last sender found")
elseif "CuberiteServerConsoleSender" == LastSender[SenderUUID] then
SendPrivateMessage()
else
local ReceiverName = cMojangAPI:GetPlayerNameFromUUID(LastSender[SenderUUID], true)
if not cRoot:Get():FindAndDoWithPlayer(ReceiverName, SendPrivateMessage) then
Response = SendMessageFailure(Player, "Player \"" .. ReceiverName .. "\" not found")
end
end
else
if not Split[3] then
Response = SendMessage(Player, "Usage: " .. Split[1] .. " <player> <message ...>")
elseif Split[2] == "" or not cRoot:Get():FindAndDoWithPlayer(Split[2], SendPrivateMessage) then
Response = SendMessageFailure(Player, "Player \"" .. Split[2] .. "\" not found")
end
end
return true, Response
end

function HandleConsoleTell(Split)
return HandleTellCommand(Split)
end

function HandleRCommand(Split, Player)
return HandleTellCommand(Split, Player)
end

function HandleConsoleR(Split)
return HandleRCommand(Split)
end
6 changes: 3 additions & 3 deletions cmd_tps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ end
function HandleTpsCommand(Split, Player)
local Response = {}

table.insert(Response, SendMessage(Player, "Global TPS: " .. GetAverageNum(GlobalTps)))
table.insert(Response, "Global TPS: " .. GetAverageNum(GlobalTps))
for WorldName, WorldTps in pairs(TpsCache) do
table.insert(Response, SendMessage(Player, "World \"" .. WorldName .. "\": " .. GetAverageNum(WorldTps) .. " TPS"))
table.insert(Response, "World \"" .. WorldName .. "\": " .. GetAverageNum(WorldTps) .. " TPS")
end
return true, table.concat(Response, "")
return true, SendMessage(Player, table.concat(Response, "\n"))
end

function HandleConsoleTps(Split)
Expand Down

0 comments on commit 5dbe68f

Please sign in to comment.