commands.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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(
  21. vMod.modname,
  22. {
  23. description = vMod.locate("Allow player to alter all players stored power (burntime)"),
  24. give_to_singleplayer = false,
  25. give_to_admin = false
  26. }
  27. )
  28. MT.register_chatcommand(
  29. vMod.modname,
  30. {
  31. params = "[set|add|remove] [name|me] [amount]",
  32. description = "performs the given action on the given players name to alter stored power (burntime)",
  33. privs = {
  34. vm_lighting_wand = true
  35. },
  36. func = function(name, param)
  37. if not param or param == "" then
  38. return false, ""
  39. end
  40. local str = split(param, " ")
  41. -- check and set action if valid
  42. local action = str[1]
  43. if not action == "set" or not action == "add" or not action == "remove" then
  44. return false, "[" .. vMod.modname .. "]Error:\nInvalid action\n\tMust be one of \n\t\tset\n\t\tadd\n\t\tremove"
  45. end
  46. -- check the player
  47. local who = str[2]
  48. local who_name = who
  49. -- me or self == current player
  50. if who == "me" or who == "self" then
  51. who_name = name
  52. end
  53. who = MT.get_player_by_name(who_name) or nil
  54. if not who then
  55. return false, "[" .. vMod.modname .. "]Error:\n\tUnknown player name - " .. (who_name or "unknown")
  56. end
  57. -- check the given amount is valid
  58. local amount = vMod.round(tonumber(str[3] or 0))
  59. if not amount or amount <= 0 or amount > vMod.get_max_power() then
  60. return false, "[" .. vMod.modname .. "]Error:\nInvalid amount given: range = 0 > " .. vMod.get_max_power()
  61. end
  62. -- Just pass the amount to the correct function to handle it
  63. action_func[action](who, amount)
  64. local power = vMod.get_power(who)
  65. vMod.logger(
  66. name .. " (" .. action .. ") Power for " .. who_name .. " - amount:" .. amount .. " new power:" .. power,
  67. "action"
  68. )
  69. -- Success
  70. return true, "[" ..
  71. vMod.modname ..
  72. "]Success!\n" ..
  73. " amount: " .. amount .. " action: " .. action .. " for: " .. who_name .. " new power is now " .. power
  74. end
  75. }
  76. )