From 8b024accdd7b76229de87eb8b58ee688eb028beb Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 16:03:34 +0100 Subject: Add register_trigger_counted for homogenous events --- triggers.lua | 167 ++++++++++++++++++++--------------------------------------- 1 file changed, 55 insertions(+), 112 deletions(-) (limited to 'triggers.lua') diff --git a/triggers.lua b/triggers.lua index ac3afc8..7d028ee 100644 --- a/triggers.lua +++ b/triggers.lua @@ -118,63 +118,6 @@ awards.register_trigger("eat", function(def) end end) -awards.register_trigger("death", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.death, tmp) - def.getProgress = function(self, data) - local itemcount = data.deaths or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 deaths", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Die.", "Die @1 times.", n, n) - end -end) - -awards.register_trigger("chat", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.chat, tmp) - def.getProgress = function(self, data) - local itemcount = data.chats or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 chat messages", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Write something in chat.", "Write @1 chat messages.", n, n) - end -end) - -awards.register_trigger("join", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.join, tmp) - def.getProgress = function(self, data) - local itemcount = data.joins or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 game joins", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Join the game.", "Join the game @1 times.", n, n) - end -end) - awards.register_trigger("craft", function(def) local tmp = { award = def.name, @@ -326,69 +269,69 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv end) end) -minetest.register_on_dieplayer(function(player) - -- Run checks - local name = player:get_player_name() - if not player or not name or name=="" then - return +awards.register_trigger_counted("death", function(def) + local tmp = { + award = def.name, + target = def.trigger.target, + } + table.insert(awards.on.death, tmp) + def.getProgress = function(self, data) + local itemcount = data.deaths or 0 + return { + perc = itemcount / tmp.target, + label = S("@1/@2 deaths", itemcount, tmp.target), + } + end + def.getDefaultDescription = function(self) + local n = self.trigger.target + return NS("Die.", "Die @1 times.", n, n) end - - -- Get player - awards.assertPlayer(name) - local data = awards.players[name] - - -- Increment counter - data.deaths = data.deaths + 1 - - awards.run_trigger_callbacks(player, data, "death", function(entry) - if entry.target and entry.award and data.deaths and - data.deaths >= entry.target then - return entry.award - end - end) end) +minetest.register_on_dieplayer(awards.notify_death) -minetest.register_on_joinplayer(function(player) - -- Run checks - local name = player:get_player_name() - if not player or not name or name=="" then - return +awards.register_trigger_counted("chat", function(def) + local tmp = { + award = def.name, + target = def.trigger.target, + } + table.insert(awards.on.chat, tmp) + def.getProgress = function(self, data) + local itemcount = data.chats or 0 + return { + perc = itemcount / tmp.target, + label = S("@1/@2 chat messages", itemcount, tmp.target), + } + end + def.getDefaultDescription = function(self) + local n = self.trigger.target + return NS("Write something in chat.", "Write @1 chat messages.", n, n) end - - -- Get player - awards.assertPlayer(name) - local data = awards.players[name] - - -- Increment counter - data.joins = data.joins + 1 - - awards.run_trigger_callbacks(player, data, "join", function(entry) - if entry.target and entry.award and data.joins and - data.joins >= entry.target then - return entry.award - end - end) end) - minetest.register_on_chat_message(function(name, message) - -- Run checks - local idx = string.find(message,"/") - if not name or (idx ~= nil and idx <= 1) then + local player = minetest.get_player_by_name(name) + if not player or string.find(message, "/") then return end - -- Get player - awards.assertPlayer(name) - local data = awards.players[name] - local player = minetest.get_player_by_name(name) - - -- Increment counter - data.chats = data.chats + 1 + awards.notify_chat(player) +end) - awards.run_trigger_callbacks(player, data, "chat", function(entry) - if entry.target and entry.award and data.chats and - data.chats >= entry.target then - return entry.award - end - end) +awards.register_trigger_counted("join", function(def) + local tmp = { + award = def.name, + target = def.trigger.target, + } + table.insert(awards.on.join, tmp) + def.getProgress = function(self, data) + local itemcount = data.joins or 0 + return { + perc = itemcount / tmp.target, + label = S("@1/@2 game joins", itemcount, tmp.target), + } + end + def.getDefaultDescription = function(self) + local n = self.trigger.target + return NS("Join the game.", "Join the game @1 times.", n, n) + end end) +minetest.register_on_joinplayer(awards.notify_join) -- cgit v1.2.3 From ef19940edc20791d2342b7b68b7bb098a0abc2db Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 17:01:55 +0100 Subject: Add trigger definitions rather than register function --- triggers.lua | 449 +++++++++++++++++++---------------------------------------- 1 file changed, 144 insertions(+), 305 deletions(-) (limited to 'triggers.lua') diff --git a/triggers.lua b/triggers.lua index 7d028ee..476ca7f 100644 --- a/triggers.lua +++ b/triggers.lua @@ -14,143 +14,75 @@ -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -- -local S, NS = awards.gettext, awards.ngettext +awards.register_trigger("death", { + type = "counted", + progress = "@1/@2 deaths", + auto_description = { "Die once", "Die @1 times" }, +}) +minetest.register_on_dieplayer(awards.notify_death) -awards.register_trigger("dig", function(def) - local tmp = { - award = def.name, - node = def.trigger.node, - target = def.trigger.target, - } - table.insert(awards.on.dig, tmp) - def.getProgress = function(self, data) - local itemcount - if tmp.node then - itemcount = awards.get_item_count(data, "count", tmp.node) or 0 - else - itemcount = awards.get_total_item_count(data, "count") - end - return { - perc = itemcount / tmp.target, - label = S("@1/@2 dug", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - if self.trigger.node then - local nname = minetest.registered_nodes[self.trigger.node].description - if nname == nil then - nname = self.trigger.node - end - -- Translators: @1 is count, @2 is description. - return NS("Mine: @2", "Mine: @1×@2", n, n, nname) - else - return NS("Mine @1 block.", "Mine @1 blocks.", n, n) - end +awards.register_trigger("chat", { + type = "counted", + progress = "@1/@2 chat messages", + auto_description = { "Send a chat message", "Chat @1 times" }, +}) +minetest.register_on_chat_message(function(name, message) + local player = minetest.get_player_by_name(name) + if not player or string.find(message, "/") then + return end -end) -awards.register_trigger("place", function(def) - local tmp = { - award = def.name, - node = def.trigger.node, - target = def.trigger.target, - } - table.insert(awards.on.place, tmp) - def.getProgress = function(self, data) - local itemcount - if tmp.node then - itemcount = awards.get_item_count(data, "place", tmp.node) or 0 - else - itemcount = awards.get_total_item_count(data, "place") - end - return { - perc = itemcount / tmp.target, - label = S("@1/@2 placed", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - if self.trigger.node then - local nname = minetest.registered_nodes[self.trigger.node].description - if nname == nil then - nname = self.trigger.node - end - -- Translators: @1 is count, @2 is description. - return NS("Place: @2", "Place: @1×@2", n, n, nname) - else - return NS("Place a block.", "Place @1 blocks.", n, n) - end - end + awards.notify_chat(player) end) -awards.register_trigger("eat", function(def) - local tmp = { - award = def.name, - item = def.trigger.item, - target = def.trigger.target, - } - table.insert(awards.on.eat, tmp) - def.getProgress = function(self, data) - local itemcount - if tmp.item then - itemcount = awards.get_item_count(data, "eat", tmp.item) or 0 - else - itemcount = awards.get_total_item_count(data, "eat") - end - return { - perc = itemcount / tmp.target, - label = S("@1/@2 eaten", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - if self.trigger.item then - local iname = minetest.registered_items[self.trigger.item].description - if iname == nil then - iname = self.trigger.iode - end - -- Translators: @1 is count, @2 is description. - return NS("Eat: @2", "Eat: @1×@2", n, n, iname) - else - return NS("Eat an item.", "Eat @1 items.", n, n) - end - end -end) +awards.register_trigger("join", { + type = "counted", + progress = "@1/@2 joins", + auto_description = { "Join once", "Join @1 times" }, +}) +minetest.register_on_joinplayer(awards.notify_join) + +-- +-- awards.register_trigger("dig", { +-- type = "counted_key", +-- progress = "@1/@2 dug", +-- auto_description = { "Mine: @2", "Mine: @1×@2" }, +-- auto_description_total = { "Mine @1 block.", "Mine @1 blocks." }, +-- get_key = function(self, def) +-- return minetest.registered_aliases[def.trigger.node] or def.trigger.node +-- end +-- }) +-- +-- minetest.register_on_dignode(function(pos, oldnode, player) +-- if not player or not pos or not oldnode then +-- return +-- end +-- +-- local node_name = oldnode.name +-- node_name = minetest.registered_aliases[node_name] or node_name +-- awards.notify_dig(player, node_name) +-- end) +-- +-- awards.register_trigger("place", { +-- type = "counted_key", +-- progress = "@1/@2 placed", +-- auto_description = { "Place: @2", "Place: @1×@2" }, +-- auto_description_total = { "Place @1 block.", "Place @1 blocks." }, +-- get_key = function(self, def) +-- return minetest.registered_aliases[def.trigger.node] or def.trigger.node +-- end +-- }) +-- +-- awards.register_trigger("craft", { +-- type = "counted_key", +-- progress = "@1/@2 crafted", +-- auto_description = { "Craft: @2", "Craft: @1×@2" }, +-- auto_description_total = { "Craft @1 item", "Craft @1 items." }, +-- get_key = function(self, def) +-- return minetest.registered_aliases[def.trigger.item] or def.trigger.item +-- end +-- }) -awards.register_trigger("craft", function(def) - local tmp = { - award = def.name, - item = def.trigger.item, - target = def.trigger.target, - } - table.insert(awards.on.craft, tmp) - def.getProgress = function(self, data) - local itemcount - if tmp.item then - itemcount = awards.get_item_count(data, "craft", tmp.item) or 0 - else - itemcount = awards.get_total_item_count(data, "craft") - end - return { - perc = itemcount / tmp.target, - label = S("@1/@2 crafted", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - if self.trigger.item then - local iname = minetest.registered_items[self.trigger.item].description - if iname == nil then - iname = self.trigger.item - end - -- Translators: @1 is count, @2 is description. - return NS("Craft: @2", "Craft: @1×@2", n, n, iname) - else - return NS("Craft an item.", "Craft @1 items.", n) - end - end -end) -- Backwards compatibility awards.register_onDig = awards.register_on_dig @@ -161,177 +93,84 @@ awards.register_onJoin = awards.register_on_join awards.register_onCraft = awards.register_on_craft -- Trigger Handles -minetest.register_on_dignode(function(pos, oldnode, digger) - if not digger or not pos or not oldnode then - return - end - - local data = awards.players[digger:get_player_name()] - if not awards.increment_item_counter(data, "count", oldnode.name) then - return - end - awards.run_trigger_callbacks(digger, data, "dig", function(entry) - if entry.target then - if entry.node then - local tnodedug = string.split(entry.node, ":") - local tmod = tnodedug[1] - local titem = tnodedug[2] - if not (not tmod or not titem or not data.count[tmod] or - not data.count[tmod][titem]) and - data.count[tmod][titem] > entry.target-1 then - return entry.award - end - elseif awards.get_total_item_count(data, "count") > entry.target-1 then - return entry.award - end - end - end) -end) - -minetest.register_on_placenode(function(pos, node, digger) - if not digger or not pos or not node or not digger:get_player_name() or digger:get_player_name()=="" then - return - end - local data = awards.players[digger:get_player_name()] - if not awards.increment_item_counter(data, "place", node.name) then - return - end - - awards.run_trigger_callbacks(digger, data, "place", function(entry) - if entry.target then - if entry.node then - local tnodedug = string.split(entry.node, ":") - local tmod = tnodedug[1] - local titem = tnodedug[2] - if not (not tmod or not titem or not data.place[tmod] or - not data.place[tmod][titem]) and - data.place[tmod][titem] > entry.target-1 then - return entry.award - end - elseif awards.get_total_item_count(data, "place") > entry.target-1 then - return entry.award - end - end - end) -end) - -minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, user, pointed_thing) - if not user or not itemstack or not user:get_player_name() or user:get_player_name()=="" then - return - end - local data = awards.players[user:get_player_name()] - if not awards.increment_item_counter(data, "eat", itemstack:get_name()) then - return - end - awards.run_trigger_callbacks(user, data, "eat", function(entry) - if entry.target then - if entry.item then - local titemstring = string.split(entry.item, ":") - local tmod = titemstring[1] - local titem = titemstring[2] - if not (not tmod or not titem or not data.eat[tmod] or - not data.eat[tmod][titem]) and - data.eat[tmod][titem] > entry.target-1 then - return entry.award - end - elseif awards.get_total_item_count(data, "eat") > entry.target-1 then - return entry.award - end - end - end) -end) - -minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) - if not player or not itemstack then - return - end - - local data = awards.players[player:get_player_name()] - if not awards.increment_item_counter(data, "craft", itemstack:get_name(), itemstack:get_count()) then - return - end - - awards.run_trigger_callbacks(player, data, "craft", function(entry) - if entry.target then - if entry.item then - local titemcrafted = string.split(entry.item, ":") - local tmod = titemcrafted[1] - local titem = titemcrafted[2] - if not (not tmod or not titem or not data.craft[tmod] or - not data.craft[tmod][titem]) and - data.craft[tmod][titem] > entry.target-1 then - return entry.award - end - elseif awards.get_total_item_count(data, "craft") > entry.target-1 then - return entry.award - end - end - end) -end) - -awards.register_trigger_counted("death", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.death, tmp) - def.getProgress = function(self, data) - local itemcount = data.deaths or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 deaths", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Die.", "Die @1 times.", n, n) - end -end) -minetest.register_on_dieplayer(awards.notify_death) - -awards.register_trigger_counted("chat", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.chat, tmp) - def.getProgress = function(self, data) - local itemcount = data.chats or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 chat messages", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Write something in chat.", "Write @1 chat messages.", n, n) - end -end) -minetest.register_on_chat_message(function(name, message) - local player = minetest.get_player_by_name(name) - if not player or string.find(message, "/") then - return - end - - awards.notify_chat(player) -end) - -awards.register_trigger_counted("join", function(def) - local tmp = { - award = def.name, - target = def.trigger.target, - } - table.insert(awards.on.join, tmp) - def.getProgress = function(self, data) - local itemcount = data.joins or 0 - return { - perc = itemcount / tmp.target, - label = S("@1/@2 game joins", itemcount, tmp.target), - } - end - def.getDefaultDescription = function(self) - local n = self.trigger.target - return NS("Join the game.", "Join the game @1 times.", n, n) - end -end) -minetest.register_on_joinplayer(awards.notify_join) +-- +-- minetest.register_on_placenode(function(pos, node, digger) +-- if not digger or not pos or not node or not digger:get_player_name() or digger:get_player_name()=="" then +-- return +-- end +-- local data = awards.players[digger:get_player_name()] +-- if not awards.increment_item_counter(data, "place", node.name) then +-- return +-- end +-- +-- awards.run_trigger_callbacks(digger, data, "place", function(entry) +-- if entry.target then +-- if entry.node then +-- local tnodedug = string.split(entry.node, ":") +-- local tmod = tnodedug[1] +-- local titem = tnodedug[2] +-- if not (not tmod or not titem or not data.place[tmod] or +-- not data.place[tmod][titem]) and +-- data.place[tmod][titem] > entry.target-1 then +-- return entry.award +-- end +-- elseif awards.get_total_item_count(data, "place") > entry.target-1 then +-- return entry.award +-- end +-- end +-- end) +-- end) +-- +-- minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, user, pointed_thing) +-- if not user or not itemstack or not user:get_player_name() or user:get_player_name()=="" then +-- return +-- end +-- local data = awards.players[user:get_player_name()] +-- if not awards.increment_item_counter(data, "eat", itemstack:get_name()) then +-- return +-- end +-- awards.run_trigger_callbacks(user, data, "eat", function(entry) +-- if entry.target then +-- if entry.item then +-- local titemstring = string.split(entry.item, ":") +-- local tmod = titemstring[1] +-- local titem = titemstring[2] +-- if not (not tmod or not titem or not data.eat[tmod] or +-- not data.eat[tmod][titem]) and +-- data.eat[tmod][titem] > entry.target-1 then +-- return entry.award +-- end +-- elseif awards.get_total_item_count(data, "eat") > entry.target-1 then +-- return entry.award +-- end +-- end +-- end) +-- end) +-- +-- minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) +-- if not player or not itemstack then +-- return +-- end +-- +-- local data = awards.players[player:get_player_name()] +-- if not awards.increment_item_counter(data, "craft", itemstack:get_name(), itemstack:get_count()) then +-- return +-- end +-- +-- awards.run_trigger_callbacks(player, data, "craft", function(entry) +-- if entry.target then +-- if entry.item then +-- local titemcrafted = string.split(entry.item, ":") +-- local tmod = titemcrafted[1] +-- local titem = titemcrafted[2] +-- if not (not tmod or not titem or not data.craft[tmod] or +-- not data.craft[tmod][titem]) and +-- data.craft[tmod][titem] > entry.target-1 then +-- return entry.award +-- end +-- elseif awards.get_total_item_count(data, "craft") > entry.target-1 then +-- return entry.award +-- end +-- end +-- end) +-- end) -- cgit v1.2.3 From 7c5fd79b57fad2b9485d801eea07c1c9fedaabe9 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 17:37:43 +0100 Subject: Clean up formspec related code --- triggers.lua | 9 --------- 1 file changed, 9 deletions(-) (limited to 'triggers.lua') diff --git a/triggers.lua b/triggers.lua index 476ca7f..f3fe708 100644 --- a/triggers.lua +++ b/triggers.lua @@ -83,15 +83,6 @@ minetest.register_on_joinplayer(awards.notify_join) -- end -- }) - --- Backwards compatibility -awards.register_onDig = awards.register_on_dig -awards.register_onPlace = awards.register_on_place -awards.register_onDeath = awards.register_on_death -awards.register_onChat = awards.register_on_chat -awards.register_onJoin = awards.register_on_join -awards.register_onCraft = awards.register_on_craft - -- Trigger Handles -- -- minetest.register_on_placenode(function(pos, node, digger) -- cgit v1.2.3 From 28c59340eae2fa64a42e0c063343b9966b13a737 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 18:30:47 +0100 Subject: Implement conted_key trigger type --- triggers.lua | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'triggers.lua') diff --git a/triggers.lua b/triggers.lua index f3fe708..e564bec 100644 --- a/triggers.lua +++ b/triggers.lua @@ -42,26 +42,26 @@ awards.register_trigger("join", { }) minetest.register_on_joinplayer(awards.notify_join) --- --- awards.register_trigger("dig", { --- type = "counted_key", --- progress = "@1/@2 dug", --- auto_description = { "Mine: @2", "Mine: @1×@2" }, --- auto_description_total = { "Mine @1 block.", "Mine @1 blocks." }, --- get_key = function(self, def) --- return minetest.registered_aliases[def.trigger.node] or def.trigger.node --- end --- }) --- --- minetest.register_on_dignode(function(pos, oldnode, player) --- if not player or not pos or not oldnode then --- return --- end --- --- local node_name = oldnode.name --- node_name = minetest.registered_aliases[node_name] or node_name --- awards.notify_dig(player, node_name) --- end) + +awards.register_trigger("dig", { + type = "counted_key", + progress = "@1/@2 dug", + auto_description = { "Mine: @2", "Mine: @1×@2" }, + auto_description_total = { "Mine @1 block.", "Mine @1 blocks." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.node] or def.trigger.node + end +}) + +minetest.register_on_dignode(function(pos, oldnode, player) + if not player or not pos or not oldnode then + return + end + + local node_name = oldnode.name + node_name = minetest.registered_aliases[node_name] or node_name + awards.notify_dig(player, node_name) +end) -- -- awards.register_trigger("place", { -- type = "counted_key", -- cgit v1.2.3 From eaab883e716d5bbe3d38f7868e23f08a0964dc5a Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 18:35:28 +0100 Subject: Readd triggers --- triggers.lua | 172 ++++++++++++++++++++++------------------------------------- 1 file changed, 65 insertions(+), 107 deletions(-) (limited to 'triggers.lua') diff --git a/triggers.lua b/triggers.lua index e564bec..9721b38 100644 --- a/triggers.lua +++ b/triggers.lua @@ -21,6 +21,7 @@ awards.register_trigger("death", { }) minetest.register_on_dieplayer(awards.notify_death) + awards.register_trigger("chat", { type = "counted", progress = "@1/@2 chat messages", @@ -35,6 +36,7 @@ minetest.register_on_chat_message(function(name, message) awards.notify_chat(player) end) + awards.register_trigger("join", { type = "counted", progress = "@1/@2 joins", @@ -52,116 +54,72 @@ awards.register_trigger("dig", { return minetest.registered_aliases[def.trigger.node] or def.trigger.node end }) - -minetest.register_on_dignode(function(pos, oldnode, player) - if not player or not pos or not oldnode then +minetest.register_on_dignode(function(pos, node, player) + if not player or not pos or not node then return end - local node_name = oldnode.name + local node_name = node.name node_name = minetest.registered_aliases[node_name] or node_name awards.notify_dig(player, node_name) end) --- --- awards.register_trigger("place", { --- type = "counted_key", --- progress = "@1/@2 placed", --- auto_description = { "Place: @2", "Place: @1×@2" }, --- auto_description_total = { "Place @1 block.", "Place @1 blocks." }, --- get_key = function(self, def) --- return minetest.registered_aliases[def.trigger.node] or def.trigger.node --- end --- }) --- --- awards.register_trigger("craft", { --- type = "counted_key", --- progress = "@1/@2 crafted", --- auto_description = { "Craft: @2", "Craft: @1×@2" }, --- auto_description_total = { "Craft @1 item", "Craft @1 items." }, --- get_key = function(self, def) --- return minetest.registered_aliases[def.trigger.item] or def.trigger.item --- end --- }) - --- Trigger Handles --- --- minetest.register_on_placenode(function(pos, node, digger) --- if not digger or not pos or not node or not digger:get_player_name() or digger:get_player_name()=="" then --- return --- end --- local data = awards.players[digger:get_player_name()] --- if not awards.increment_item_counter(data, "place", node.name) then --- return --- end --- --- awards.run_trigger_callbacks(digger, data, "place", function(entry) --- if entry.target then --- if entry.node then --- local tnodedug = string.split(entry.node, ":") --- local tmod = tnodedug[1] --- local titem = tnodedug[2] --- if not (not tmod or not titem or not data.place[tmod] or --- not data.place[tmod][titem]) and --- data.place[tmod][titem] > entry.target-1 then --- return entry.award --- end --- elseif awards.get_total_item_count(data, "place") > entry.target-1 then --- return entry.award --- end --- end --- end) --- end) --- --- minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, user, pointed_thing) --- if not user or not itemstack or not user:get_player_name() or user:get_player_name()=="" then --- return --- end --- local data = awards.players[user:get_player_name()] --- if not awards.increment_item_counter(data, "eat", itemstack:get_name()) then --- return --- end --- awards.run_trigger_callbacks(user, data, "eat", function(entry) --- if entry.target then --- if entry.item then --- local titemstring = string.split(entry.item, ":") --- local tmod = titemstring[1] --- local titem = titemstring[2] --- if not (not tmod or not titem or not data.eat[tmod] or --- not data.eat[tmod][titem]) and --- data.eat[tmod][titem] > entry.target-1 then --- return entry.award --- end --- elseif awards.get_total_item_count(data, "eat") > entry.target-1 then --- return entry.award --- end --- end --- end) --- end) --- --- minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) --- if not player or not itemstack then --- return --- end --- --- local data = awards.players[player:get_player_name()] --- if not awards.increment_item_counter(data, "craft", itemstack:get_name(), itemstack:get_count()) then --- return --- end --- --- awards.run_trigger_callbacks(player, data, "craft", function(entry) --- if entry.target then --- if entry.item then --- local titemcrafted = string.split(entry.item, ":") --- local tmod = titemcrafted[1] --- local titem = titemcrafted[2] --- if not (not tmod or not titem or not data.craft[tmod] or --- not data.craft[tmod][titem]) and --- data.craft[tmod][titem] > entry.target-1 then --- return entry.award --- end --- elseif awards.get_total_item_count(data, "craft") > entry.target-1 then --- return entry.award --- end --- end --- end) --- end) + + +awards.register_trigger("place", { + type = "counted_key", + progress = "@1/@2 placed", + auto_description = { "Place: @2", "Place: @1×@2" }, + auto_description_total = { "Place @1 block.", "Place @1 blocks." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.node] or def.trigger.node + end +}) +minetest.register_on_placenode(function(pos, node, player) + if not player or not pos or not node then + return + end + + local node_name = node.name + node_name = minetest.registered_aliases[node_name] or node_name + awards.notify_place(player, node_name) +end) + + +awards.register_trigger("craft", { + type = "counted_key", + progress = "@1/@2 crafted", + auto_description = { "Craft: @2", "Craft: @1×@2" }, + auto_description_total = { "Craft @1 item", "Craft @1 items." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.item] or def.trigger.item + end +}) +minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) + if not player or itemstack:empty() then + return + end + + local itemname = itemstack:get_name() + itemname = minetest.registered_aliases[itemname] or itemname + awards.notify_craft(player, itemname, itemstack:get_count()) +end) + + +awards.register_trigger("eat", { + type = "counted_key", + progress = "@1/@2 eaten", + auto_description = { "Eat @2", "Eat @1×@2" }, + auto_description_total = { "Eat @1 item", "Eat @1 items." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.item] or def.trigger.item + end +}) +minetest.register_on_item_eat(function(_, _, itemstack, player, _) + if not player or itemstack:empty() then + return + end + + local itemname = itemstack:get_name() + itemname = minetest.registered_aliases[itemname] or itemname + awards.notify_craft(player, itemname, itemstack:get_count()) +end) -- cgit v1.2.3 From f571f9d6eb7b11dc003aaf6c0a19b9db40ad356e Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 4 Apr 2018 20:55:33 +0100 Subject: Add support for death reasons --- triggers.lua | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'triggers.lua') diff --git a/triggers.lua b/triggers.lua index 9721b38..6f39cf3 100644 --- a/triggers.lua +++ b/triggers.lua @@ -14,13 +14,6 @@ -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -- -awards.register_trigger("death", { - type = "counted", - progress = "@1/@2 deaths", - auto_description = { "Die once", "Die @1 times" }, -}) -minetest.register_on_dieplayer(awards.notify_death) - awards.register_trigger("chat", { type = "counted", @@ -45,6 +38,25 @@ awards.register_trigger("join", { minetest.register_on_joinplayer(awards.notify_join) +awards.register_trigger("death", { + type = "counted_key", + progress = "@1/@2 deaths", + auto_description = { "Die once of @2", "Die @1 times of @2" }, + auto_description_total = { "Die @1 times.", "Mine @1 times" }, + get_key = function(self, def) + return def.trigger.reason + end, +}) +minetest.register_on_dieplayer(function(player, reason) + if reason then + reason = reason.type + else + reason = "unknown" + end + awards.notify_death(player, reason) +end) + + awards.register_trigger("dig", { type = "counted_key", progress = "@1/@2 dug", -- cgit v1.2.3