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

13
api.lua
View File

@ -65,6 +65,7 @@ function awards.assertPlayer(playern)
awards.tbv(awards.players[playern], "unlocked")
awards.tbv(awards.players[playern], "place")
awards.tbv(awards.players[playern], "count")
awards.tbv(awards.players[playern], "craft")
awards.tbv(awards.players[playern], "deaths", 0)
awards.tbv(awards.players[playern], "joins", 0)
awards.tbv(awards.players[playern], "chats", 0)
@ -117,6 +118,13 @@ function awards.register_achievement(name,data_table)
target = data_table.trigger.target,
}
table.insert(awards.onPlace,tmp)
elseif data_table.trigger.type == "craft" then
local tmp = {
award = name,
item = data_table.trigger.item,
target = data_table.trigger.target,
}
table.insert(awards.onCraft,tmp)
elseif data_table.trigger.type == "death" then
local tmp = {
award = name,
@ -181,6 +189,11 @@ function awards.register_onJoin(func)
table.insert(awards.onJoin,func)
end
-- run a function when an item is crafted
function awards.register_onCraft(func)
table.insert(awards.onCraft,func)
end
-- This function is called whenever a target condition is met.
-- It checks if a player already has that achievement, and if they do not,
-- it gives it to them

View File

@ -20,8 +20,9 @@ The API
* sound [optional]
* image [optional]
* trigger [optional] [table]
* type - "dig", "place", "death", "chat" or "join"
* type - "dig", "place", "craft", "death", "chat" or "join"
* (for dig/place type) node - the nodes name
* (for craft type) item - the items name
* (for all types) target - how many to dig / place
* secret [optional] - if true, then player needs to unlock to find out what it is.
* awards.give_achievement(name,award)
@ -36,6 +37,8 @@ The API
* -- return award name or null
* awards.register_onJoin(func(player,data))
* -- return award name or null
* awards.register_onCraft(func(player,data))
* -- return award name or null
Player Data
@ -50,6 +53,9 @@ A list of data referenced/hashed by the player's name.
* place [table] - place counter
* modname [table]
* itemname [int]
* craft [table] - craft counter
* modname [table]
* itemname [int]
* deaths
* chats
* joins

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()