mirror of
https://gitlab.com/rubenwardy/awards.git
synced 2024-11-26 17:23:49 +01:00
Add awards.run_trigger_callbacks() and use it
This commit is contained in:
parent
e92be32ec1
commit
dee1880fd3
6
init.lua
6
init.lua
@ -36,7 +36,7 @@ awards.register_achievement("award_lightitup",{
|
|||||||
trigger = {
|
trigger = {
|
||||||
type = "place",
|
type = "place",
|
||||||
node = "default:torch",
|
node = "default:torch",
|
||||||
target = 500
|
target = 100
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -100,7 +100,7 @@ awards.register_achievement("award_obsessed_with_obsidian",{
|
|||||||
trigger = {
|
trigger = {
|
||||||
type = "dig",
|
type = "dig",
|
||||||
node = "default:obsidian",
|
node = "default:obsidian",
|
||||||
target = 1
|
target = 50
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ awards.register_achievement("award_lumberjack", {
|
|||||||
trigger = {
|
trigger = {
|
||||||
type = "dig",
|
type = "dig",
|
||||||
node = "default:tree",
|
node = "default:tree",
|
||||||
target = 1
|
target = 100
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -28,6 +28,14 @@ old fork in Carbone, under same license.
|
|||||||
* return true to cancel register_on_unlock callbacks and HUD
|
* return true to cancel register_on_unlock callbacks and HUD
|
||||||
* awards.register_trigger(name, func(awardname, def))
|
* awards.register_trigger(name, func(awardname, def))
|
||||||
* Note: awards.on[name] is automatically created for triggers
|
* Note: awards.on[name] is automatically created for triggers
|
||||||
|
* awards.run_trigger_callbacks(player, data, trigger, table_func(entry))
|
||||||
|
* Goes through and checks all triggers registered to a trigger type,
|
||||||
|
unlocking the award if conditions are met.
|
||||||
|
* data is the player's award data, ie: awards.players[player_name]
|
||||||
|
* trigger is the name of the trigger type. Ie: awards.on[trigger]
|
||||||
|
* table_func is called if the trigger is a table - simply return an
|
||||||
|
award name to unlock it
|
||||||
|
* See triggers.lua for examples
|
||||||
* awards.register_on_unlock(func(name, def))
|
* awards.register_on_unlock(func(name, def))
|
||||||
* name is the player name
|
* name is the player name
|
||||||
* def is the award def.
|
* def is the award def.
|
||||||
|
162
triggers.lua
162
triggers.lua
@ -73,6 +73,22 @@ awards.register_onChat = awards.register_on_chat
|
|||||||
awards.register_onJoin = awards.register_on_join
|
awards.register_onJoin = awards.register_on_join
|
||||||
awards.register_onCraft = awards.register_on_craft
|
awards.register_onCraft = awards.register_on_craft
|
||||||
|
|
||||||
|
function awards.run_trigger_callbacks(player, data, trigger, table_func)
|
||||||
|
for i = 1, #awards.on[trigger] do
|
||||||
|
local res = nil
|
||||||
|
local entry = awards.on[trigger][i]
|
||||||
|
if type(entry) == "function" then
|
||||||
|
res = entry(player, data)
|
||||||
|
elseif type(entry) == "table" and entry.award then
|
||||||
|
res = table_func(entry)
|
||||||
|
end
|
||||||
|
|
||||||
|
if res then
|
||||||
|
awards.unlock(player:get_player_name(), res)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Trigger Handles
|
-- Trigger Handles
|
||||||
minetest.register_on_dignode(function(pos, oldnode, digger)
|
minetest.register_on_dignode(function(pos, oldnode, digger)
|
||||||
if not digger or not pos or not oldnode then
|
if not digger or not pos or not oldnode then
|
||||||
@ -97,36 +113,20 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
|
|||||||
awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item] + 1
|
awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item] + 1
|
||||||
|
|
||||||
-- Run callbacks and triggers
|
-- Run callbacks and triggers
|
||||||
local player = digger
|
|
||||||
local data = awards.players[playern]
|
local data = awards.players[playern]
|
||||||
for i=1, #awards.on.dig do
|
awards.run_trigger_callbacks(digger, data, "dig", function(entry)
|
||||||
local res = nil
|
if entry.node and entry.target then
|
||||||
if type(awards.on.dig[i]) == "function" then
|
local tnodedug = string.split(entry.node, ":")
|
||||||
-- Run trigger callback
|
|
||||||
res = awards.on.dig[i](player,data)
|
|
||||||
elseif type(awards.on.dig[i]) == "table" then
|
|
||||||
-- Handle table trigger
|
|
||||||
if not awards.on.dig[i].node or not awards.on.dig[i].target or not awards.on.dig[i].award then
|
|
||||||
-- table running failed!
|
|
||||||
print("[ERROR] awards - on.dig trigger "..i.." is invalid!")
|
|
||||||
else
|
|
||||||
-- run the table
|
|
||||||
local tnodedug = string.split(awards.on.dig[i].node, ":")
|
|
||||||
local tmod = tnodedug[1]
|
local tmod = tnodedug[1]
|
||||||
local titem = tnodedug[2]
|
local titem = tnodedug[2]
|
||||||
if tmod==nil or titem==nil or not data.count[tmod] or not data.count[tmod][titem] then
|
if not tmod or not titem or not data.count[tmod] or not data.count[tmod][titem] then
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
elseif data.count[tmod][titem] > awards.on.dig[i].target-1 then
|
elseif data.count[tmod][titem] > entry.target-1 then
|
||||||
res=awards.on.dig[i].award
|
return entry.award
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if res then
|
|
||||||
awards.unlock(playern,res)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_on_placenode(function(pos, node, digger)
|
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
|
if not digger or not pos or not node or not digger:get_player_name() or digger:get_player_name()=="" then
|
||||||
@ -152,35 +152,20 @@ minetest.register_on_placenode(function(pos, node, digger)
|
|||||||
awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item] + 1
|
awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item] + 1
|
||||||
|
|
||||||
-- Run callbacks and triggers
|
-- Run callbacks and triggers
|
||||||
local player = digger
|
|
||||||
local data = awards.players[playern]
|
local data = awards.players[playern]
|
||||||
for i=1,# awards.on.place do
|
awards.run_trigger_callbacks(digger, data, "place", function(entry)
|
||||||
local res = nil
|
if entry.node and entry.target then
|
||||||
if type(awards.on.place[i]) == "function" then
|
local tnodedug = string.split(entry.node, ":")
|
||||||
-- Run trigger callback
|
|
||||||
res = awards.on.place[i](player,data)
|
|
||||||
elseif type(awards.on.place[i]) == "table" then
|
|
||||||
-- Handle table trigger
|
|
||||||
if not awards.on.place[i].node or not awards.on.place[i].target or not awards.on.place[i].award then
|
|
||||||
print("[ERROR] awards - on.place trigger "..i.." is invalid!")
|
|
||||||
else
|
|
||||||
-- run the table
|
|
||||||
local tnodedug = string.split(awards.on.place[i].node, ":")
|
|
||||||
local tmod = tnodedug[1]
|
local tmod = tnodedug[1]
|
||||||
local titem = tnodedug[2]
|
local titem = tnodedug[2]
|
||||||
if tmod==nil or titem==nil or not data.place[tmod] or not data.place[tmod][titem] then
|
if not tmod or not titem or not data.place[tmod] or not data.place[tmod][titem] then
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
elseif data.place[tmod][titem] > awards.on.place[i].target-1 then
|
elseif data.place[tmod][titem] > entry.target-1 then
|
||||||
res = awards.on.place[i].award
|
return entry.award
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if res then
|
|
||||||
awards.unlock(playern,res)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
|
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
|
||||||
if not player or not itemstack then
|
if not player or not itemstack then
|
||||||
@ -199,6 +184,7 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
awards.assertPlayer(playern)
|
awards.assertPlayer(playern)
|
||||||
|
awards.tbv(awards.players[playern], "craft")
|
||||||
awards.tbv(awards.players[playern].craft, mod)
|
awards.tbv(awards.players[playern].craft, mod)
|
||||||
awards.tbv(awards.players[playern].craft[mod], item, 0)
|
awards.tbv(awards.players[playern].craft[mod], item, 0)
|
||||||
|
|
||||||
@ -207,35 +193,19 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
|
|||||||
|
|
||||||
-- Run callbacks and triggers
|
-- Run callbacks and triggers
|
||||||
local data = awards.players[playern]
|
local data = awards.players[playern]
|
||||||
for i=1, #awards.on.craft do
|
awards.run_trigger_callbacks(player, data, "craft", function(entry)
|
||||||
local res = nil
|
if entry.item and entry.target then
|
||||||
local entry = awards.on.craft[i]
|
|
||||||
if type(entry) == "function" then
|
|
||||||
-- Run trigger callback
|
|
||||||
res = entry(player,data)
|
|
||||||
elseif type(entry) == "table" then
|
|
||||||
-- Handle table trigger
|
|
||||||
if not entry.item or not entry.target or not entry.award then
|
|
||||||
-- table running failed!
|
|
||||||
print("[ERROR] awards - onCraft trigger "..i.." is invalid!")
|
|
||||||
else
|
|
||||||
-- run the table
|
|
||||||
local titemcrafted = string.split(entry.item, ":")
|
local titemcrafted = string.split(entry.item, ":")
|
||||||
local tmod = titemcrafted[1]
|
local tmod = titemcrafted[1]
|
||||||
local titem = titemcrafted[2]
|
local titem = titemcrafted[2]
|
||||||
if not tmod==nil or not titem or not data.craft[tmod] or not data.craft[tmod][titem] then
|
if not tmod or not titem or not data.craft[tmod] or not data.craft[tmod][titem] then
|
||||||
-- table running failed!
|
-- table running failed!
|
||||||
elseif data.craft[tmod][titem] > entry.target-1 then
|
elseif data.craft[tmod][titem] > entry.target-1 then
|
||||||
res=awards.on.craft[i].award
|
return entry.award
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if res then
|
|
||||||
awards.give_achievement(playern,res)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_on_dieplayer(function(player)
|
minetest.register_on_dieplayer(function(player)
|
||||||
-- Run checks
|
-- Run checks
|
||||||
@ -251,23 +221,13 @@ minetest.register_on_dieplayer(function(player)
|
|||||||
-- Increment counter
|
-- Increment counter
|
||||||
data.deaths = data.deaths + 1
|
data.deaths = data.deaths + 1
|
||||||
|
|
||||||
-- Run callbacks and triggers
|
awards.run_trigger_callbacks(player, data, "death", function(entry)
|
||||||
for _,trigger in pairs(awards.on.death) do
|
if entry.target and entry.award and data.deaths and
|
||||||
local res = nil
|
data.deaths >= entry.target then
|
||||||
if type(trigger) == "function" then
|
return entry.award
|
||||||
res = trigger(player,data)
|
|
||||||
elseif type(trigger) == "table" then
|
|
||||||
if trigger.target and trigger.award then
|
|
||||||
if data.deaths and data.deaths >= trigger.target then
|
|
||||||
res = trigger.award
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if res ~= nil then
|
|
||||||
awards.unlock(name,res)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
-- Run checks
|
-- Run checks
|
||||||
@ -283,23 +243,13 @@ minetest.register_on_joinplayer(function(player)
|
|||||||
-- Increment counter
|
-- Increment counter
|
||||||
data.joins = data.joins + 1
|
data.joins = data.joins + 1
|
||||||
|
|
||||||
-- Run callbacks and triggers
|
awards.run_trigger_callbacks(player, data, "join", function(entry)
|
||||||
for _, trigger in pairs(awards.on.join) do
|
if entry.target and entry.award and data.joins and
|
||||||
local res = nil
|
data.joins >= entry.target then
|
||||||
if type(trigger) == "function" then
|
return entry.award
|
||||||
res = trigger(player,data)
|
|
||||||
elseif type(trigger) == "table" then
|
|
||||||
if trigger.target and trigger.award then
|
|
||||||
if data.joins and data.joins >= trigger.target then
|
|
||||||
res = trigger.award
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if res ~= nil then
|
|
||||||
awards.unlock(name,res)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_on_chat_message(function(name, message)
|
minetest.register_on_chat_message(function(name, message)
|
||||||
-- Run checks
|
-- Run checks
|
||||||
@ -316,23 +266,13 @@ minetest.register_on_chat_message(function(name, message)
|
|||||||
-- Increment counter
|
-- Increment counter
|
||||||
data.chats = data.chats + 1
|
data.chats = data.chats + 1
|
||||||
|
|
||||||
-- Run callbacks and triggers
|
awards.run_trigger_callbacks(player, data, "chat", function(entry)
|
||||||
for _,trigger in pairs(awards.on.chat) do
|
if entry.target and entry.award and data.chats and
|
||||||
local res = nil
|
data.chats >= entry.target then
|
||||||
if type(trigger) == "function" then
|
return entry.award
|
||||||
res = trigger(player,data)
|
|
||||||
elseif type(trigger) == "table" then
|
|
||||||
if trigger.target and trigger.award then
|
|
||||||
if data.chats and data.chats >= trigger.target then
|
|
||||||
res = trigger.award
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if res ~= nil then
|
|
||||||
awards.unlock(name,res)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
minetest.register_on_newplayer(function(player)
|
minetest.register_on_newplayer(function(player)
|
||||||
local playern = player:get_player_name()
|
local playern = player:get_player_name()
|
||||||
|
Loading…
Reference in New Issue
Block a user