diff options
| author | rubenwardy <rubenwardy@gmail.com> | 2016-07-29 17:25:02 +0100 | 
|---|---|---|
| committer | rubenwardy <rubenwardy@gmail.com> | 2016-07-29 17:25:02 +0100 | 
| commit | 96746aaf1e2c2c07dccafd9a6cebdbb15914ee98 (patch) | |
| tree | 4e65b11a539d8ce28ba1f22571d47c5400c8bdab | |
| parent | f430b4eb6852afed312357a26a1d7ff96da7c65e (diff) | |
Add awards.increment_item_counter() and use it
| -rw-r--r-- | api.lua | 21 | ||||
| -rw-r--r-- | readme.md | 6 | ||||
| -rw-r--r-- | triggers.lua | 59 | 
3 files changed, 33 insertions, 53 deletions
| @@ -77,6 +77,27 @@ function awards.run_trigger_callbacks(player, data, trigger, table_func)  	end  end +function awards.increment_item_counter(data, field, itemname) +	local name_split = string.split(itemname, ":") +	if #name_split ~= 2 then +		return false +	end +	local mod = name_split[1] +	local item = name_split[2] + +	if data and field and mod and item then +		awards.assertPlayer(data) +		awards.tbv(data, field) +		awards.tbv(data[field], mod) +		awards.tbv(data[field][mod], item, 0) + +		data[field][mod][item] = data[field][mod][item] + 1 +		return true +	else +		return false +	end +end +  function awards.register_on_unlock(func)  	table.insert(awards.on_unlock, func)  end @@ -36,6 +36,12 @@ old fork in Carbone, under same license.  	* table_func is called if the trigger is a table - simply return an  	  award name to unlock it  	* See triggers.lua for examples +* awards.increment_item_counter(data, field, itemname) +	* add to an item's statistic count +	* for example, (data, "place", "default:stone") will add 1 to the number of +	  times default:stone has been placed. +	* data is the player's award data, ie: awards.players[player_name] +	* returns true on success, false on failure (eg: cannot get modname and item from itemname)  * awards.register_on_unlock(func(name, def))  	* name is the player name  	* def is the award def. diff --git a/triggers.lua b/triggers.lua index 241f497..809388c 100644 --- a/triggers.lua +++ b/triggers.lua @@ -78,26 +78,10 @@ minetest.register_on_dignode(function(pos, oldnode, digger)  	if not digger or not pos or not oldnode then  		return  	end -	local nodedug = string.split(oldnode.name, ":") -	if #nodedug ~= 2 then -		return -	end -	local mod = nodedug[1] -	local item = nodedug[2] -	local playern = digger:get_player_name() - -	if (not playern or not nodedug or not mod or not item) then +	local data = awards.players[playern] +	if not awards.increment_item_counter(data, "count", oldnode.name) then  		return  	end -	awards.assertPlayer(playern) -	awards.tbv(awards.players[playern].count, mod) -	awards.tbv(awards.players[playern].count[mod], item, 0) - -	-- Increment counter -	awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item] + 1 - -	-- Run callbacks and triggers -	local data = awards.players[playern]  	awards.run_trigger_callbacks(digger, data, "dig", function(entry)  		if entry.node and entry.target then  			local tnodedug = string.split(entry.node, ":") @@ -116,27 +100,11 @@ 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 nodedug = string.split(node.name, ":") -	if #nodedug ~= 2 then -		return -	end -	local mod=nodedug[1] -	local item=nodedug[2] -	local playern = digger:get_player_name() - -	-- Run checks -	if (not playern or not nodedug or not mod or not item) then +	local data = awards.players[playern] +	if not awards.increment_item_counter(data, "place", node.name) then  		return  	end -	awards.assertPlayer(playern) -	awards.tbv(awards.players[playern].place, mod) -	awards.tbv(awards.players[playern].place[mod], item, 0) - -	-- Increment counter -	awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item] + 1 -	-- Run callbacks and triggers -	local data = awards.players[playern]  	awards.run_trigger_callbacks(digger, data, "place", function(entry)  		if entry.node and entry.target then  			local tnodedug = string.split(entry.node, ":") @@ -155,27 +123,12 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv  	if not player or not itemstack then  		return  	end -	local itemcrafted = string.split(itemstack:get_name(), ":") -	if #itemcrafted ~= 2 then -		--minetest.log("error","Awards mod: "..itemstack:get_name().." is in wrong format!") -		return -	end -	local mod = itemcrafted[1] -	local item = itemcrafted[2] -	local playern = player:get_player_name() -	if (not playern or not itemcrafted or not mod or not item) then +	local data = awards.players[playern] +	if not awards.increment_item_counter(data, "craft", itemstack:get_name()) then  		return  	end -	awards.assertPlayer(playern) -	awards.tbv(awards.players[playern].craft, mod) -	awards.tbv(awards.players[playern].craft[mod], item, 0) -	-- Increment counter -	awards.players[playern].craft[mod][item] = awards.players[playern].craft[mod][item] + 1 - -	-- Run callbacks and triggers -	local data = awards.players[playern]  	awards.run_trigger_callbacks(player, data, "craft", function(entry)  		if entry.item and entry.target then  			local titemcrafted = string.split(entry.item, ":") | 
