alt.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. -- chat3/alt.lua
  2. chat3.alt = {}
  3. local storage = chat3.storage
  4. local MAX = chat3.settings.get_int("chat3.alt_max") or 3
  5. ---
  6. --- Functions (exposed API)
  7. ---
  8. -- [function] Get alt list
  9. function chat3.alt.get(name)
  10. local list = storage:get_string("alt_list_"..name)
  11. if list then
  12. list = minetest.deserialize(list)
  13. end
  14. return list or {}
  15. end
  16. -- [function] Set alt list
  17. function chat3.alt.set(name, list)
  18. storage:set_string("alt_list_"..name, minetest.serialize(list))
  19. end
  20. -- [function] Add alt
  21. function chat3.alt.add(name, alt)
  22. local list = chat3.alt.get(name)
  23. local count = 0
  24. for _, i in pairs(list) do
  25. count = count + 1
  26. end
  27. if count < MAX and not list[alt] then
  28. list[alt] = true
  29. chat3.alt.set(name, list)
  30. else
  31. return
  32. end
  33. return true
  34. end
  35. -- [function] Remove alt
  36. function chat3.alt.remove(name, alt)
  37. local list = chat3.alt.get(name)
  38. if list[alt] then
  39. list[alt] = nil
  40. else
  41. return
  42. end
  43. chat3.alt.set(name, list)
  44. return true
  45. end
  46. -- [function] List Alt Usernames (returns a table suitable for use with table.concat)
  47. function chat3.alt.list(name)
  48. local list, result = chat3.alt.get(name), {}
  49. if list then
  50. for alt, i in pairs(list) do
  51. table.insert(result, alt)
  52. end
  53. if #result > 0 then
  54. return result
  55. end
  56. end
  57. end
  58. ---
  59. --- Registrations
  60. ---
  61. -- [chatcommand] Alt
  62. minetest.register_chatcommand("alt", {
  63. description = "Manage your chat3 alternate usernames",
  64. params = "[list | add | del | rst] [<alternate username>]",
  65. func = function(name, params)
  66. params = params:split(" ")
  67. local operation, alt = params[1], params[2]
  68. if operation == "list" then
  69. local list = chat3.alt.list(name)
  70. if list then
  71. return true, "Your Alternate Usernames: "..table.concat(list, ", ")
  72. else
  73. return false, "You have not yet configured any alternate usernames."
  74. end
  75. elseif operation == "add" and alt and alt ~= "" then
  76. if chat3.alt.add(name, alt) then
  77. return true, "Added alternate username \""..alt.."\"."
  78. else
  79. return false, "You have either reached the max number ("..tostring(MAX)
  80. ..") of alternate usernames, or the alternate username already exists."
  81. end
  82. elseif operation == "del" and alt and alt ~= "" then
  83. if chat3.alt.remove(name, alt) then
  84. return true, "Removed alternate username \""..alt.."\"."
  85. else
  86. return false, "Alternate username already does not exist."
  87. end
  88. elseif operation == "rst" then
  89. chat3.alt.set(name, {})
  90. return true, "Reset your alternate username list."
  91. else
  92. return false, "Invalid parameters (see /help alt)"
  93. end
  94. end,
  95. })