1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-06-30 11:47:45 +00:00

Further reduce boilerplate in various scripts

-Introduce a printf helper command which can print the sending command's name for you,
and treats the message as a format string if additional params follow
This commit is contained in:
The Dax 2025-06-25 16:52:37 -04:00 committed by Joshua Goins
parent d292e1d676
commit 0f7e068ba3
10 changed files with 51 additions and 56 deletions

View file

@ -24,6 +24,21 @@ function getTableSize(tbl)
return count return count
end end
function printf(player, fmt_str, ...)
-- Sender would be defined elsewhere, if at all
if sender == nil then
sender = ""
end
if ... ~= nil then
player:send_message(sender..fmt_str:format(...))
else
player:send_message(sender..fmt_str)
end
sender = nil -- Reset the sender, it's not required to have for printf to work, and not all users of printf will be commands, most likely.
end
-- Constants -- Constants
GM_RANK_NORMALUSER = 0 GM_RANK_NORMALUSER = 0
GM_RANK_GAMEMASTER = 1 GM_RANK_GAMEMASTER = 1

View file

@ -18,10 +18,9 @@ end
function onCommandRequiredRankMissingError(additional_information, player) function onCommandRequiredRankMissingError(additional_information, player)
local error_msg = "Your script does not define the required_rank variable. Please define it in your script for it to run." local error_msg = "Your script does not define the required_rank variable. Please define it in your script for it to run."
printf(player, "%s\nAdditional information: %s", error_msg, additional_information)
player:send_message(string.format("%s\nAdditional information: %s", error_msg, additional_information))
end end
function onUnknownCommandError(command_name, player) function onUnknownCommandError(command_name, player)
player:send_message(string.format("Unknown command %s", command_name)) printf(player, "Unknown command %s", command_name)
end end

View file

@ -1,22 +1,23 @@
required_rank = GM_RANK_DEBUG required_rank = GM_RANK_DEBUG
sender = "[teri] "
function onCommand(args, player) function onCommand(args, player)
local parts = split(args) local parts = split(args)
local argc = #parts local argc = #parts
local sender = "[teri] "
local usage = "\nThis command moves the user to a new zone/territory.\nUsage: !teri <id>" local usage = "\nThis command moves the user to a new zone/territory.\nUsage: !teri <id>"
if argc == 0 then if argc == 0 then
player:send_message(sender.."A territory id is required to use this command."..usage) printf(player, "A territory id is required to use this command."..usage)
return
end end
local id = tonumber(parts[1]) local id = tonumber(parts[1])
if not id or id < 0 or id > 65535 then -- Must be in range of unsigned 16-bit value if not id or id < 0 or id > 65535 then -- Must be in range of unsigned 16-bit value
player:send_message(sender.."Error parsing territory id! Make sure your input is an integer between 0 and 65535."..usage) printf(player, "Error parsing territory id! Make sure your input is an integer between 0 and 65535."..usage)
return return
end end
player:change_territory(id) player:change_territory(id)
player:send_message(string.format("%s Changing territory to %s.", sender, id)) printf(player, "Changing territory to %s.", id)
end end

View file

@ -1,35 +1,18 @@
-- A list of festival ids can be found in Hyperborea's source tree: -- A list of festival ids can be found in Hyperborea's source tree:
-- https://github.com/kawaii/Hyperborea/blob/main/Hyperborea/festivals.yaml -- https://github.com/kawaii/Hyperborea/blob/main/Hyperborea/festivals.yaml
required_rank = GM_RANK_DEBUG required_rank = GM_RANK_DEBUG
sender = "[festival] "
function onCommand(args, player) function onCommand(args, player)
local parts = split(args) local parts = split(args)
local argc = #parts local argc = #parts
local usage = "\nUsage: !festival <id1> <id2> <id3> <id4>" local usage = "\nUsage: !festival <id1> <id2> <id3> <id4>"
local sender = "[festival] "
local id1 = tonumber(parts[1]) local id1 = tonumber(parts[1]) or 0
local id2 = tonumber(parts[2]) local id2 = tonumber(parts[2]) or 0
local id3 = tonumber(parts[3]) local id3 = tonumber(parts[3]) or 0
local id4 = tonumber(parts[4]) local id4 = tonumber(parts[4]) or 0
if not id1 then
id1 = 0
end
if not id2 then
id2 = 0
end
if not id3 then
id3 = 0
end
if not id4 then
id4 = 0
end
player:set_festival(id1, id2, id3, id4) player:set_festival(id1, id2, id3, id4)
local message = string.format("Festival(s) changed to %s, %s, %s and %s.", id1, id2, id3, id4) printf(player, "Festival(s) changed to %s, %s, %s and %s.", id1, id2, id3, id4)
player:send_message(message)
end end

View file

@ -1,10 +1,7 @@
-- Ported from Ioncannon's Project Meteor Server -- Ported from Ioncannon's Project Meteor Server
-- https://bitbucket.org/Ioncannon/project-meteor-server/src/develop/Data/scripts/commands/gm/nudge.lua -- https://bitbucket.org/Ioncannon/project-meteor-server/src/develop/Data/scripts/commands/gm/nudge.lua
required_rank = GM_RANK_DEBUG required_rank = GM_RANK_DEBUG
sender = "[nudge] "
function send_msg(message, player)
player:send_message(string.format("[nudge] %s", message))
end
function onCommand(args, player) function onCommand(args, player)
local parts = split(args) local parts = split(args)
@ -33,7 +30,7 @@ function onCommand(args, player)
if checkArg1 then if checkArg1 then
distance = checkArg1 distance = checkArg1
else else
send_msg("Error parsing distance! \n"..usage, player) printf(player, "Error parsing distance!\n"..usage)
return return
end end
end end
@ -46,11 +43,11 @@ function onCommand(args, player)
if vertical[string.upper(arg2)] then -- Check vertical direction on string, otherwise throw param error if vertical[string.upper(arg2)] then -- Check vertical direction on string, otherwise throw param error
direction = vertical[string.upper(arg2)] direction = vertical[string.upper(arg2)]
else else
send_msg("Error parsing direction! \n"..usage, player) printf(player, "Error parsing direction! \n"..usage)
return return
end end
else else
send_msg("Error parsing parameters! \n"..usage, player) printf(player, "Error parsing parameters! \n"..usage)
return return
end end
end end
@ -73,6 +70,5 @@ function onCommand(args, player)
end end
player:set_position(new_position, player.rotation) player:set_position(new_position, player.rotation)
local message = string.format("Positioning %s %s yalms.", direction_str, distance) printf(player, "Positioning %s %s yalms.", direction_str, distance)
send_msg(message, player)
end end

View file

@ -1,15 +1,15 @@
required_rank = GM_RANK_DEBUG required_rank = GM_RANK_DEBUG
sender = "[setspeed] "
function onCommand(args, player) function onCommand(args, player)
local parts = split(args) local parts = split(args)
local argc = #parts local argc = #parts
local sender = "[setspeed] "
local usage = "\nThis command sets the user's speed to a desired multiplier.\nUsage: !setspeed <multiplier>" local usage = "\nThis command sets the user's speed to a desired multiplier.\nUsage: !setspeed <multiplier>"
local SPEED_MAX = 10 -- Arbitrary, but it's more or less unplayable even at this amount local SPEED_MAX = 10 -- Arbitrary, but it's more or less unplayable even at this amount
local speed_multiplier = tonumber(parts[1]) local speed_multiplier = tonumber(parts[1])
if argc == 1 and not speed_multiplier then if argc == 1 and not speed_multiplier then
player:send_message(sender.."Error parsing speed multiplier! Make sure the multiplier is an integer."..usage) printf(player, "Error parsing speed multiplier! Make sure the multiplier is an integer."..usage)
return return
elseif argc == 0 then elseif argc == 0 then
speed_multiplier = 1 speed_multiplier = 1
@ -22,5 +22,5 @@ function onCommand(args, player)
end end
player:set_speed(speed_multiplier) player:set_speed(speed_multiplier)
player:send_message(string.format("%sSpeed multiplier set to %s.", sender, speed_multiplier)) printf(player, "Speed multiplier set to %s.", speed_multiplier)
end end

View file

@ -1,9 +1,9 @@
required_rank = GM_RANK_DEBUG required_rank = GM_RANK_DEBUG
sender = "[invis] "
function onCommand(args, player) function onCommand(args, player)
local sender = "[invis] "
local usage = "\nThis command makes the user invisible to all other actors." local usage = "\nThis command makes the user invisible to all other actors."
player:toggle_invisibility() player:toggle_invisibility()
player:send_message(sender.."Invisibility toggled.") printf(player, "Invisibility toggled.")
end end

View file

@ -1,9 +1,9 @@
required_rank = GM_RANK_DEBUG required_rank = GM_RANK_DEBUG
sender = "[wireframe] "
function onCommand(args, player) function onCommand(args, player)
local sender = "[wireframe] "
local usage = "\nThis command allows the user to view the world in wireframe mode." local usage = "\nThis command allows the user to view the world in wireframe mode."
player:toggle_wireframe() player:toggle_wireframe()
player:send_message(sender.."Wireframe mode toggled.") printf(player, "Wireframe mode toggled.")
end end

View file

@ -1,13 +1,14 @@
required_rank = GM_RANK_DEBUG required_rank = GM_RANK_DEBUG
sender = "[unlockaction] "
function onCommand(args, player) function onCommand(args, player)
local parts = split(args) local parts = split(args)
local argc = #parts local argc = #parts
local sender = "[unlockaction] "
local usage = "\nThis command teaches the user an action, emote, etc.\nUsage: !useaction <id/all>" local usage = "\nThis command teaches the user an action, emote, etc.\nUsage: !useaction <id/all>"
if argc < 1 then if argc < 1 then
player:send_message(sender.."This command requires 1 parameter."..usage) printf(player, "This command requires 1 parameter."..usage)
return return
end end
@ -15,16 +16,16 @@ function onCommand(args, player)
for i = 0, 1000, 1 do for i = 0, 1000, 1 do
player:unlock_action(i) player:unlock_action(i)
end end
player:send_message(string.format("%s Everything is unlocked!", sender, id)) printf(player, "Everything is unlocked!", id)
else else
local id = tonumber(parts[1]) local id = tonumber(parts[1])
if not id then if not id then
player:send_message(sender.."Error parsing action id! Make sure the id is an integer."..usage) printf(player, "Error parsing action id! Make sure the id is an integer."..usage)
return return
end end
player:unlock_action(id) player:unlock_action(id)
player:send_message(string.format("%s Action %s unlocked!", sender, id)) printf(player, "Action %s unlocked!", id)
end end
end end

View file

@ -1,13 +1,13 @@
required_rank = GM_RANK_DEBUG required_rank = GM_RANK_DEBUG
sender = "[unlockaetheryte] "
function onCommand(args, player) function onCommand(args, player)
local parts = split(args) local parts = split(args)
local argc = #parts local argc = #parts
local sender = "[unlockaetheryte] "
local usage = "\nThis command unlocks an aetheryte for the user.\nUsage: !unlockaetheryte <on/off> <id>" local usage = "\nThis command unlocks an aetheryte for the user.\nUsage: !unlockaetheryte <on/off> <id>"
if argc < 2 then if argc < 2 then
player:send_message(sender.."This command requires two parameters."..usage) printf(player, "This command requires two parameters."..usage)
return return
end end
@ -18,7 +18,7 @@ function onCommand(args, player)
elseif on == "off" then elseif on == "off" then
on = 1 on = 1
else else
player:send_message(sender.."Error parsing first parameter. Must be either of the words: 'on' or 'off'."..usage) printf(player, "Error parsing first parameter. Must be either of the words: 'on' or 'off'."..usage)
return return
end end
@ -29,11 +29,11 @@ function onCommand(args, player)
if id == "all" then if id == "all" then
id = 0 id = 0
else else
player:send_message(sender.."Error parsing id parameter. Must be an aetheryte id or the word 'all'."..usage) printf(player, "Error parsing id parameter. Must be an aetheryte id or the word 'all'."..usage)
return return
end end
end end
player:unlock_aetheryte(on, id) player:unlock_aetheryte(on, id)
player:send_message(string.format("%s Aetheryte(s) %s had their unlocked status changed!", sender, id)) printf(player, "Aetheryte(s) %s had their unlocked status changed!", id)
end end