commands.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. local MT = minetest
  2. local vMod = vm_lighting_wand
  3. string.split = string.split
  4. local split = function(s, delimiter)
  5. local result = {}
  6. for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
  7. match:gsub("%s+", "")
  8. if match ~= "" then
  9. table.insert(result, match)
  10. end
  11. end
  12. return result
  13. end
  14. local action_func = {
  15. set = vMod.set_power,
  16. add = vMod.add_power,
  17. remove = vMod.remove_power
  18. }
  19. -- Add a privi to use this command
  20. MT.register_privilege(vMod.modname, {
  21. description = vMod.locale("Allow player to alter all players stored power (burntime)"),
  22. give_to_singleplayer = false,
  23. give_to_admin = false
  24. })
  25. MT.register_chatcommand(vMod.modname, {
  26. params = "[set|add|remove] [name|me] [amount]",
  27. description = "performs the given action on the given players name to alter stored power (burntime)",
  28. privs = {
  29. vm_lighting_wand = true
  30. },
  31. func = function(name, param)
  32. if not param or param == "" then
  33. return false, "Error:\nmissing params, please see help"
  34. end
  35. local str = split(param, " ")
  36. -- check and set action if valid
  37. local action = str[1]
  38. if not action == "set" or not action == "add" or not action == "remove" then
  39. return false,
  40. "[" .. vMod.modname .. "]Error:\nInvalid action\n\tMust be one of \n\t\tset\n\t\tadd\n\t\tremove"
  41. end
  42. -- check the player
  43. local who = str[2]
  44. local who_name = who
  45. -- me or self == current player
  46. if who == "me" or who == "self" then
  47. who_name = name
  48. end
  49. who = MT.get_player_by_name(who_name) or nil
  50. if not who then
  51. return false, "[" .. vMod.modname .. "]Error:\n\tUnknown player name - " .. (who_name or "unknown")
  52. end
  53. -- check the given amount is valid
  54. local amount = vMod.round(tonumber(str[3] or 0))
  55. if not amount or amount <= 0 or amount > vMod.get_max_power() then
  56. return false, "[" .. vMod.modname .. "]Error:\nInvalid amount given: range = 0 > " .. vMod.get_max_power()
  57. end
  58. -- Just pass the amount to the correct function to handle it
  59. action_func[action](who, amount)
  60. local power = vMod.get_power(who)
  61. vMod.logger(name .. " (" .. action .. ") Power for " .. who_name .. " - amount:" .. amount .. " new power:" ..
  62. power)
  63. -- Success
  64. return true,
  65. "[" .. vMod.modname .. "]Success!\n" .. " amount: " .. amount .. " action: " .. action .. " for: " ..
  66. who_name .. " new power is now " .. power
  67. end
  68. })