summaryrefslogtreecommitdiff
path: root/triggers.lua
diff options
context:
space:
mode:
authorWuzzy <wuzzy2@mail.ru>2016-11-15 01:57:01 +0100
committerGitHub <noreply@github.com>2016-11-15 01:57:01 +0100
commit38c504e938e169e0337591b7ffe13863ff21250d (patch)
treee35f4fdc8eda2f3eb67f2ee7bf806bbe7304efff /triggers.lua
parentab7206e15050b050257c7e45b804dc9b1352d41d (diff)
Add eat trigger, extend existing triggers, add 9 awards (#35)
* Add eat trigger and 2 new awards * Add mushroom eating awards * Add support for counting any eat/dig/craft/place * Update readme * Add 4 builder awards * Update German locale * Update dependencies * Fix mistake in German translation * Revert removal of background
Diffstat (limited to 'triggers.lua')
-rw-r--r--triggers.lua190
1 files changed, 145 insertions, 45 deletions
diff --git a/triggers.lua b/triggers.lua
index 108a5c5..88a952f 100644
--- a/triggers.lua
+++ b/triggers.lua
@@ -29,22 +29,30 @@ awards.register_trigger("dig", function(def)
}
table.insert(awards.on.dig, tmp)
def.getProgress = function(self, data)
- local itemcount = awards.get_item_count(data, "count", tmp.node) or 0
+ 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 = string.format(S("%d/%d dug"), itemcount, tmp.target)
}
end
def.getDefaultDescription = function(self)
- local nname = minetest.registered_nodes[self.trigger.node].description
- if nname == nil then
- nname = self.trigger.node
- end
- local ddesc
- if self.trigger.target ~= 1 then
- return string.format(S("Mine blocks: %d×%s"), self.trigger.target, nname)
+ if self.trigger.node then
+ local nname = minetest.registered_nodes[self.trigger.node].description
+ if nname == nil then
+ nname = self.trigger.node
+ end
+ if self.trigger.target ~= 1 then
+ return string.format(S("Mine blocks: %d×%s"), self.trigger.target, nname)
+ else
+ return string.format(S("Mine a block: %s"), nname)
+ end
else
- return string.format(S("Mine a block: %s"), nname)
+ return string.format(S("Mine %d block(s)."), self.trigger.target)
end
end
end)
@@ -57,21 +65,66 @@ awards.register_trigger("place", function(def)
}
table.insert(awards.on.place, tmp)
def.getProgress = function(self, data)
- local itemcount = awards.get_item_count(data, "place", tmp.node) or 0
+ 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 = string.format(S("%d/%d placed"), itemcount, tmp.target)
}
end
def.getDefaultDescription = function(self)
- local nname = minetest.registered_nodes[self.trigger.node].description
- if nname == nil then
- nname = self.trigger.node
+ if self.trigger.node then
+ local nname = minetest.registered_nodes[self.trigger.node].description
+ if nname == nil then
+ nname = self.trigger.node
+ end
+ if self.trigger.target ~= 1 then
+ return string.format(S("Place blocks: %d×%s"), self.trigger.target, nname)
+ else
+ return string.format(S("Place a block: %s"), nname)
+ end
+ else
+ return string.format(S("Place %d block(s)."), self.trigger.target)
end
- if self.trigger.target ~= 1 then
- return string.format(S("Place blocks: %d×%s"), self.trigger.target, nname)
+ end
+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 = string.format(S("%d/%d eaten"), itemcount, tmp.target)
+ }
+ end
+ def.getDefaultDescription = function(self)
+ if self.trigger.item then
+ local iname = minetest.registered_items[self.trigger.item].description
+ if iname == nil then
+ iname = self.trigger.iode
+ end
+ if self.trigger.target ~= 1 then
+ return string.format(S("Eat: %d×%s"), self.trigger.target, iname)
+ else
+ return string.format(S("Eat: %s"), iname)
+ end
else
- return string.format(S("Place a block: %s"), nname)
+ return string.format(S("Eat %d item(s)."), self.trigger.target)
end
end
end)
@@ -150,21 +203,30 @@ awards.register_trigger("craft", function(def)
}
table.insert(awards.on.craft, tmp)
def.getProgress = function(self, data)
- local itemcount = awards.get_item_count(data, "craft", tmp.item) or 0
+ 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 = string.format(S("%d/%d crafted"), itemcount, tmp.target)
}
end
def.getDefaultDescription = function(self)
- local iname = minetest.registered_items[self.trigger.item].description
- if iname == nil then
- iname = self.trigger.item
- end
- if self.trigger.target ~= 1 then
- return string.format(S("Craft: %d×%s"), self.trigger.target, iname)
+ if self.trigger.item then
+ local iname = minetest.registered_items[self.trigger.item].description
+ if iname == nil then
+ iname = self.trigger.item
+ end
+ if self.trigger.target ~= 1 then
+ return string.format(S("Craft: %d×%s"), self.trigger.target, iname)
+ else
+ return string.format(S("Craft: %s"), iname)
+ end
else
- return string.format(S("Craft: %s"), iname)
+ return string.format(S("Craft %d item(s)."), self.trigger.target)
end
end
end)
@@ -188,13 +250,17 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
return
end
awards.run_trigger_callbacks(digger, data, "dig", function(entry)
- if entry.node and entry.target then
- local tnodedug = string.split(entry.node, ":")
- local tmod = tnodedug[1]
- local titem = tnodedug[2]
- if not tmod or not titem or not data.count[tmod] or not data.count[tmod][titem] then
- -- table running failed!
- elseif data.count[tmod][titem] > entry.target-1 then
+ if entry.target then
+ if entry.node then
+ local tnodedug = string.split(entry.node, ":")
+ local tmod = tnodedug[1]
+ local titem = tnodedug[2]
+ if not tmod or not titem or not data.count[tmod] or not data.count[tmod][titem] then
+ -- table running failed!
+ elseif 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
@@ -211,13 +277,43 @@ minetest.register_on_placenode(function(pos, node, digger)
end
awards.run_trigger_callbacks(digger, data, "place", function(entry)
- if entry.node and entry.target then
- local tnodedug = string.split(entry.node, ":")
- local tmod = tnodedug[1]
- local titem = tnodedug[2]
- if not tmod or not titem or not data.place[tmod] or not data.place[tmod][titem] then
- -- table running failed!
- elseif data.place[tmod][titem] > entry.target-1 then
+ if entry.target then
+ if entry.node then
+ local tnodedug = string.split(entry.node, ":")
+ local tmod = tnodedug[1]
+ local titem = tnodedug[2]
+ if not tmod or not titem or not data.place[tmod] or not data.place[tmod][titem] then
+ -- table running failed!
+ elseif 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 tmod or not titem or not data.eat[tmod] or not data.eat[tmod][titem] then
+ -- table running failed!
+ elseif 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
@@ -235,13 +331,17 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
end
awards.run_trigger_callbacks(player, data, "craft", function(entry)
- if entry.item and entry.target then
- local titemcrafted = string.split(entry.item, ":")
- local tmod = titemcrafted[1]
- local titem = titemcrafted[2]
- if not tmod or not titem or not data.craft[tmod] or not data.craft[tmod][titem] then
- -- table running failed!
- elseif data.craft[tmod][titem] > entry.target-1 then
+ if entry.target then
+ if entry.item then
+ local titemcrafted = string.split(entry.item, ":")
+ local tmod = titemcrafted[1]
+ local titem = titemcrafted[2]
+ if not tmod or not titem or not data.craft[tmod] or not data.craft[tmod][titem] then
+ -- table running failed!
+ elseif 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