From 8a34ea80553b51d94e2cb2243501f2df71ebe4aa Mon Sep 17 00:00:00 2001 From: Beha Date: Sat, 4 Apr 2020 13:46:41 -0400 Subject: [PATCH 1/6] Add awards.add_defaults setting to control adding default awards. Useful for games not based on MTG but using some of its mods. --- init.lua | 6 +++++- settingtypes.txt | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 settingtypes.txt diff --git a/init.lua b/init.lua index 98f054a..25dc07f 100644 --- a/init.lua +++ b/init.lua @@ -16,7 +16,11 @@ dofile(minetest.get_modpath("awards").."/src/api_triggers.lua") dofile(minetest.get_modpath("awards").."/src/chat_commands.lua") dofile(minetest.get_modpath("awards").."/src/gui.lua") dofile(minetest.get_modpath("awards").."/src/triggers.lua") -dofile(minetest.get_modpath("awards").."/src/awards.lua") + +-- Optionally add default awards. +if minetest.settings:get_bool("awards.add_defaults", true) then + dofile(minetest.get_modpath("awards").."/src/awards.lua") +end awards.load() minetest.register_on_shutdown(awards.save) diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..1133d52 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,2 @@ +# Add default achievements from the awards mod +awards.add_defaults (Add Default Achievements) bool true From 9d98f950b8252a40ff3219ca0aa95235082257b2 Mon Sep 17 00:00:00 2001 From: Beha Date: Sat, 4 Apr 2020 15:35:14 -0400 Subject: [PATCH 2/6] Fix `eat` trigger to actually notify awards on item eat. --- src/triggers.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/triggers.lua b/src/triggers.lua index 32a0b85..33f779a 100644 --- a/src/triggers.lua +++ b/src/triggers.lua @@ -137,5 +137,5 @@ minetest.register_on_item_eat(function(_, _, itemstack, player, _) local itemname = itemstack:get_name() itemname = minetest.registered_aliases[itemname] or itemname - awards.notify_craft(player, itemname, itemstack:get_count()) + awards.notify_eat(player, itemname, itemstack:get_count()) end) From 7e3064ef934173b420c97106692c108d5d4047e6 Mon Sep 17 00:00:00 2001 From: Beha Date: Sat, 4 Apr 2020 16:34:25 -0400 Subject: [PATCH 3/6] Check for valid players in default triggers instead of crashing. --- src/triggers.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/triggers.lua b/src/triggers.lua index 33f779a..2daf6d3 100644 --- a/src/triggers.lua +++ b/src/triggers.lua @@ -14,6 +14,10 @@ -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -- +-- Check if a player object is valid for awards. +local function player_ok(player) + return player and player.is_player and player:is_player() and not player.is_fake_player +end awards.register_trigger("chat", { type = "counted", @@ -22,7 +26,7 @@ awards.register_trigger("chat", { }) minetest.register_on_chat_message(function(name, message) local player = minetest.get_player_by_name(name) - if not player or string.find(message, "/") then + if not player_ok(player) or string.find(message, "/") then return end @@ -68,7 +72,7 @@ awards.register_trigger("dig", { key_is_item = true, }) minetest.register_on_dignode(function(pos, node, player) - if not player or not pos or not node then + if not player_ok(player) or not pos or not node then return end @@ -89,7 +93,7 @@ awards.register_trigger("place", { key_is_item = true, }) minetest.register_on_placenode(function(pos, node, player) - if not player or not pos or not node then + if not player_ok(player) or not pos or not node then return end @@ -110,7 +114,7 @@ awards.register_trigger("craft", { key_is_item = true, }) minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) - if not player or itemstack:is_empty() then + if not player_ok(player) or itemstack:is_empty() then return end @@ -131,7 +135,7 @@ awards.register_trigger("eat", { key_is_item = true, }) minetest.register_on_item_eat(function(_, _, itemstack, player, _) - if not player or itemstack:is_empty() then + if not player_ok(player) or itemstack:is_empty() then return end From d542042a5002c3c4737d55a0ca0bb7a3539a3766 Mon Sep 17 00:00:00 2001 From: Beha Date: Sat, 4 Apr 2020 17:38:25 -0400 Subject: [PATCH 4/6] Interpret group ratings of zero as not in group. The MT API docs instruct to interpret `nil` and `0` as the same rating. --- src/api_triggers.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api_triggers.lua b/src/api_triggers.lua index 454c79e..d763a05 100644 --- a/src/api_triggers.lua +++ b/src/api_triggers.lua @@ -144,8 +144,8 @@ function awards.register_trigger(tname, tdef) if tdef.key_is_item and key:sub(1, 6) ~= "group:" then local itemdef = minetest.registered_items[key] if itemdef then - for groupname, _ in pairs(itemdef.groups or {}) do - if tdef.watched_groups[groupname] then + for groupname,rating in pairs(itemdef.groups or {}) do + if rating ~= 0 and tdef.watched_groups[groupname] then tdef.notify(player, "group:" .. groupname, n) end end From ce58720493a70b3eff28b6a9f1afe1ebaee56a46 Mon Sep 17 00:00:00 2001 From: Beha Date: Sun, 5 Apr 2020 16:00:33 +0000 Subject: [PATCH 5/6] Fix group total, fix item increment count argument, add player checks, resize award icon, fix log output --- src/api_awards.lua | 9 +++++++-- src/api_triggers.lua | 6 +++--- src/triggers.lua | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/api_awards.lua b/src/api_awards.lua index 5385d1b..c3cae5a 100644 --- a/src/api_awards.lua +++ b/src/api_awards.lua @@ -43,6 +43,11 @@ end -- name - the name of the player -- award - the name of the award to give function awards.unlock(name, award) + -- Ensure the player is online. + if not minetest.get_player_by_name(name) then + return + end + -- Access Player Data local data = awards.player(name) local awdef = awards.registered_awards[award] @@ -60,7 +65,7 @@ function awards.unlock(name, award) end -- Unlock Award - minetest.log("action", name.." has unlocked award "..name) + minetest.log("action", name.." has unlocked award "..award) data.unlocked[award] = award awards.save() @@ -91,7 +96,7 @@ function awards.unlock(name, award) local title = awdef.title or award local desc = awdef.description or "" local background = awdef.background or "awards_bg_default.png" - local icon = awdef.icon or "awards_unknown.png" + local icon = (awdef.icon or "awards_unknown.png") .. "^[resize:32x32" local sound = awdef.sound if sound == nil then -- Explicit check for nil because sound could be `false` to disable it diff --git a/src/api_triggers.lua b/src/api_triggers.lua index d763a05..11a1391 100644 --- a/src/api_triggers.lua +++ b/src/api_triggers.lua @@ -160,8 +160,9 @@ function awards.register_trigger(tname, tdef) data[tname] = data[tname] or {} local currentVal = (data[tname][key] or 0) + n data[tname][key] = currentVal + data[tname].__total = (data[tname].__total or 0) if key:sub(1, 6) ~= "group:" then - data[tname].__total = (data[tname].__total or 0) + n + data[tname].__total = data[tname].__total + n end tdef:run_callbacks(player, data, function(entry) @@ -173,7 +174,6 @@ function awards.register_trigger(tname, tdef) else return end - if current >= entry.target then return entry.award end @@ -201,7 +201,7 @@ end function awards.increment_item_counter(data, field, itemname, count) itemname = minetest.registered_aliases[itemname] or itemname - data[field][itemname] = (data[field][itemname] or 0) + 1 + data[field][itemname] = (data[field][itemname] or 0) + (count or 1) end function awards.get_item_count(data, field, itemname) diff --git a/src/triggers.lua b/src/triggers.lua index 2daf6d3..fc260b5 100644 --- a/src/triggers.lua +++ b/src/triggers.lua @@ -141,5 +141,5 @@ minetest.register_on_item_eat(function(_, _, itemstack, player, _) local itemname = itemstack:get_name() itemname = minetest.registered_aliases[itemname] or itemname - awards.notify_eat(player, itemname, itemstack:get_count()) + awards.notify_eat(player, itemname) end) From c7b60be88061dc7f9f06ef5c86bd6c2859d01312 Mon Sep 17 00:00:00 2001 From: "Alexand(er|ra) Yst" Date: Sun, 19 Apr 2020 19:05:06 +0000 Subject: [PATCH 6/6] player:getpos() is deprecated and calling it causes the game to halt if deprecated_lua_api_handling is set to "error" in minetest.conf. Call player:get_pos() instead to prevent the game from halting. --- src/awards.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/awards.lua b/src/awards.lua index bc34167..a1500ca 100644 --- a/src/awards.lua +++ b/src/awards.lua @@ -89,7 +89,7 @@ if minetest.get_modpath("fire") then secret = true, }) awards.register_on_death(function(player,data) - local pos = player:getpos() + local pos = player:get_pos() if pos and minetest.find_node_near(pos, 2, "fire:basic_flame") ~= nil then return "award_burn" end @@ -115,7 +115,7 @@ awards.register_award("award_deep_down", { secret = true, }) awards.register_on_death(function(player,data) - local pos = player:getpos() + local pos = player:get_pos() if pos and pos.y < -10000 then return "award_deep_down" end @@ -129,7 +129,7 @@ awards.register_award("award_no_screen", { secret = true, }) awards.register_on_death(function(player,data) - local pos = player:getpos() + local pos = player:get_pos() if pos and pos.y > 10000 then return "award_no_screen" end @@ -767,7 +767,7 @@ if minetest.get_modpath("default") then secret = true, }) awards.register_on_death(function(player,data) - local pos = player:getpos() + local pos = player:get_pos() if pos and (minetest.find_node_near(pos, 2, "default:lava_flowing") ~= nil or minetest.find_node_near(pos, 2, "default:lava_source") ~= nil) then return "award_in_the_flow" @@ -782,7 +782,7 @@ if minetest.get_modpath("default") then secret = true, }) awards.register_on_death(function(player,data) - local pos = player:getpos() + local pos = player:get_pos() if pos and minetest.find_node_near(pos, 5, "default:stone_with_diamond") ~= nil then return "award_this_is_sad" end @@ -798,7 +798,7 @@ if minetest.get_modpath("bones") then secret = true, }) awards.register_on_death(function(player,data) - local pos = player:getpos() + local pos = player:get_pos() if pos and minetest.find_node_near(pos, 5, "bones:bones") ~= nil then return "award_the_stack" end