Add craft trigger

This commit is contained in:
MrIbby
2016-06-01 10:41:24 -07:00
committed by rubenwardy
parent c711bc74b2
commit 85a40d2488
3 changed files with 75 additions and 1 deletions

View File

@ -7,6 +7,7 @@
-- Function and table holders for Triggers
awards.onDig = {}
awards.onPlace = {}
awards.onCraft = {}
awards.onChat = {}
awards.onDeath = {}
awards.onJoin = {}
@ -122,6 +123,60 @@ minetest.register_on_placenode(function(pos,node,digger)
end
end)
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
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]
for i=1,# awards.onCraft do
local res = nil
if type(awards.onCraft[i]) == "function" then
-- Run trigger callback
res = awards.onDig[i](player,data)
elseif type(awards.onCraft[i]) == "table" then
-- Handle table trigger
if not awards.onCraft[i].item or not awards.onCraft[i].target or not awards.onCraft[i].award then
-- table running failed!
print("[ERROR] awards - onCraft trigger "..i.." is invalid!")
else
-- run the table
local titemcrafted = string.split(awards.onCraft[i].item, ":")
local tmod=titemcrafted[1]
local titem=titemcrafted[2]
if tmod==nil or titem==nil or not data.craft[tmod] or not data.craft[tmod][titem] then
-- table running failed!
elseif data.craft[tmod][titem] > awards.onCraft[i].target-1 then
res=awards.onCraft[i].award
end
end
end
if res then
awards.give_achievement(playern,res)
end
end
end)
minetest.register_on_dieplayer(function(player)
-- Run checks
local name = player:get_player_name()