From d8cb7a7164586490f2b7f3c5df3a172de921ff91 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Mon, 30 Mar 2020 21:02:13 +0200 Subject: [PATCH] Add achievement chat command --- mods/HUD/awards/api.lua | 5 +++ mods/HUD/awards/chat_commands.lua | 47 +++++++++++++++++++++++++++++ mods/HUD/awards/locale/awards.de.tr | 9 ++++++ mods/HUD/awards/locale/template.txt | 9 ++++++ mods/HUD/awards/readme.md | 4 ++- 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/mods/HUD/awards/api.lua b/mods/HUD/awards/api.lua index 707595096..c4367ba8f 100644 --- a/mods/HUD/awards/api.lua +++ b/mods/HUD/awards/api.lua @@ -178,6 +178,11 @@ function awards.clear_player(name) awards.players[name] = {} end +-- Returns true if award exists, false otherwise +function awards.exists(award) + return awards.def[award] ~= nil +end + -- This function is called whenever a target condition is met. -- It checks if a player already has that achievement, and if they do not, -- it gives it to them diff --git a/mods/HUD/awards/chat_commands.lua b/mods/HUD/awards/chat_commands.lua index fab2d0325..88e799dfe 100644 --- a/mods/HUD/awards/chat_commands.lua +++ b/mods/HUD/awards/chat_commands.lua @@ -38,3 +38,50 @@ minetest.register_chatcommand("awards", { end }) +minetest.register_privilege("achievements", { + description = S("Can give achievements to any player"), + give_to_singleplayer = false, + give_to_admin = false, +}) + +minetest.register_chatcommand("achievement", { + params = S("(grant ( | all)) | list"), + privs = { achievements = true }, + description = S("Give achievement to player or list all achievements"), + func = function(name, param) + if param == "list" then + local list = {} + for k,_ in pairs(awards.def) do + table.insert(list, k) + end + table.sort(list) + for a=1, #list do + minetest.chat_send_player(name, S("@1 (@2)", awards.def[list[a]].title, list[a])) + end + return true + end + local keyword, playername, achievement = string.match(param, "([^ ]+) (.+) (.+)") + if not keyword or not playername or not achievement then + return false, S("Invalid syntax.") + end + if keyword ~= "grant" then + return false, S("Invalid action.") + end + local player = minetest.get_player_by_name(playername) + if not player then + return false, S("Player is not online.") + end + if achievement == "all" then + for k,_ in pairs(awards.def) do + awards.unlock(playername, k) + end + return true, S("Done.") + elseif awards.exists(achievement) then + awards.unlock(playername, achievement) + return true, S("Done.") + else + return false, S("Achievement “@1” does not exist.", achievement) + end + end +}) + diff --git a/mods/HUD/awards/locale/awards.de.tr b/mods/HUD/awards/locale/awards.de.tr index 49663d119..2fb04c4ca 100644 --- a/mods/HUD/awards/locale/awards.de.tr +++ b/mods/HUD/awards/locale/awards.de.tr @@ -49,3 +49,12 @@ Place @1 block(s).=Platzieren Sie @1 Blöcke. Dig @1 block(s).=Bauen Sie @1 Blöcke ab. Eat @1 item(s).=Essen Sie @1 Dinge. Craft @1 item(s).=Fertigen Sie @1 Gegenstände. +Can give achievements to any player=Kann Spielern Auszeichnungen vergeben +(grant ( | all)) | list=(grant ( | all)) | list +Give achievement to player or list all achievements=Auszeichnung an Spieler vergeben oder alle Auszeichnungen auflisten +@1 (@2)=@1 (@2) +Invalid syntax.=Ungültige Syntax. +Invalid action.=Ungültige Aktion. +Player is not online.=Spieler ist nicht online. +Done.=Fertig. +Achievement “@1” does not exist.=Auszeichnung »@1« existiert nicht. diff --git a/mods/HUD/awards/locale/template.txt b/mods/HUD/awards/locale/template.txt index bcfb67acd..529d524c0 100644 --- a/mods/HUD/awards/locale/template.txt +++ b/mods/HUD/awards/locale/template.txt @@ -50,3 +50,12 @@ Place @1 block(s).= Dig @1 block(s).= Eat @1 item(s).= Craft @1 item(s).= +Can give achievements to any player= +(grant ( | all)) | list= +Give achievement to player or list all achievements= +@1 (@2)= +Invalid syntax.= +Invalid action.= +Player is not online.= +Done.= +Achievement “@1” does not exist.= diff --git a/mods/HUD/awards/readme.md b/mods/HUD/awards/readme.md index 83235130a..70c650c84 100644 --- a/mods/HUD/awards/readme.md +++ b/mods/HUD/awards/readme.md @@ -11,7 +11,7 @@ old fork in Carbone, under same license. # Basic API * awards.register_achievement(name, def) - * name + * name: Unique identifier for achievement. You can use anything except "all" * desciption * sound [optional] - set a custom sound (SimpleSoundSpec) or `false` to play no sound. If not specified, a default sound is played @@ -59,6 +59,8 @@ old fork in Carbone, under same license. * awards.unlock(name, award) * gives an award to a player * name is the player name +* awards.exists(award) + * returns true if award exists, false otherwise # Included in the Mod