mirror of
https://gitlab.com/rubenwardy/awards.git
synced 2025-06-28 14:16:12 +02:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
48ae012210 | |||
1fe166e2c5 | |||
96746aaf1e | |||
f430b4eb68 | |||
dee1880fd3 |
58
api.lua
58
api.lua
@ -61,6 +61,43 @@ function awards.register_trigger(name, func)
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
@ -144,14 +181,12 @@ function awards.unlock(name, award)
|
||||
if awdef and awdef.prizes then
|
||||
for i = 1, #awdef.prizes do
|
||||
local itemstack = ItemStack(awdef.prizes[i])
|
||||
if itemstack:is_empty() or not itemstack:is_known() then
|
||||
return
|
||||
if not itemstack:is_empty() then
|
||||
local receiverref = minetest.get_player_by_name(name)
|
||||
if receiverref then
|
||||
receiverref:get_inventory():add_item("main", itemstack)
|
||||
end
|
||||
end
|
||||
local receiverref = core.get_player_by_name(name)
|
||||
if not receiverref then
|
||||
return
|
||||
end
|
||||
receiverref:get_inventory():add_item("main", itemstack)
|
||||
end
|
||||
end
|
||||
|
||||
@ -367,3 +402,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
end)
|
||||
|
||||
awards.init()
|
||||
|
||||
minetest.register_on_newplayer(function(player)
|
||||
local playern = player:get_player_name()
|
||||
awards.assertPlayer(playern)
|
||||
end)
|
||||
|
||||
minetest.register_on_shutdown(function()
|
||||
awards.save()
|
||||
end)
|
||||
|
@ -26,6 +26,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)
|
||||
|
6
init.lua
6
init.lua
@ -36,7 +36,7 @@ awards.register_achievement("award_lightitup",{
|
||||
trigger = {
|
||||
type = "place",
|
||||
node = "default:torch",
|
||||
target = 500
|
||||
target = 100
|
||||
}
|
||||
})
|
||||
|
||||
@ -100,7 +100,7 @@ awards.register_achievement("award_obsessed_with_obsidian",{
|
||||
trigger = {
|
||||
type = "dig",
|
||||
node = "default:obsidian",
|
||||
target = 1
|
||||
target = 50
|
||||
}
|
||||
})
|
||||
|
||||
@ -124,7 +124,7 @@ awards.register_achievement("award_lumberjack", {
|
||||
trigger = {
|
||||
type = "dig",
|
||||
node = "default:tree",
|
||||
target = 1
|
||||
target = 100
|
||||
}
|
||||
})
|
||||
|
||||
|
2
mod.conf
2
mod.conf
@ -4,4 +4,4 @@ author = rubenwardy
|
||||
description = Adds achievements to Minetest, and an API to register new ones.
|
||||
license = LGPL 2.1 or later
|
||||
forum = https://forum.minetest.net/viewtopic.php?t=4870
|
||||
version = 2.0
|
||||
version = 2.1
|
||||
|
14
readme.md
14
readme.md
@ -28,6 +28,20 @@ old fork in Carbone, under same license.
|
||||
* return true to cancel register_on_unlock callbacks and HUD
|
||||
* awards.register_trigger(name, func(awardname, def))
|
||||
* 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.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.
|
||||
|
241
triggers.lua
241
triggers.lua
@ -78,163 +78,69 @@ 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
|
||||
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 player = digger
|
||||
local data = awards.players[playern]
|
||||
for i=1, #awards.on.dig do
|
||||
local res = nil
|
||||
if type(awards.on.dig[i]) == "function" then
|
||||
-- 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
|
||||
if not awards.increment_item_counter(data, "count", oldnode.name) then
|
||||
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!
|
||||
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 titem=tnodedug[2]
|
||||
if tmod==nil or titem==nil or not data.count[tmod] or not data.count[tmod][titem] then
|
||||
-- table running failed!
|
||||
elseif data.count[tmod][titem] > awards.on.dig[i].target-1 then
|
||||
res=awards.on.dig[i].award
|
||||
end
|
||||
elseif data.count[tmod][titem] > entry.target-1 then
|
||||
return entry.award
|
||||
end
|
||||
end
|
||||
|
||||
if res then
|
||||
awards.unlock(playern,res)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
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
|
||||
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 player = digger
|
||||
local data = awards.players[playern]
|
||||
for i=1,# awards.on.place do
|
||||
local res = nil
|
||||
if type(awards.on.place[i]) == "function" then
|
||||
-- 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 titem = tnodedug[2]
|
||||
if tmod==nil or titem==nil or not data.place[tmod] or not data.place[tmod][titem] then
|
||||
-- table running failed!
|
||||
elseif data.place[tmod][titem] > awards.on.place[i].target-1 then
|
||||
res = awards.on.place[i].award
|
||||
end
|
||||
if not awards.increment_item_counter(data, "place", node.name) then
|
||||
return
|
||||
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
|
||||
return entry.award
|
||||
end
|
||||
end
|
||||
|
||||
if res then
|
||||
awards.unlock(playern,res)
|
||||
end
|
||||
end
|
||||
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.on.craft do
|
||||
local res = nil
|
||||
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
|
||||
if not awards.increment_item_counter(data, "craft", itemstack:get_name()) then
|
||||
return
|
||||
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!
|
||||
print("[ERROR] awards - onCraft trigger "..i.." is invalid!")
|
||||
else
|
||||
-- run the table
|
||||
local titemcrafted = string.split(entry.item, ":")
|
||||
local tmod=titemcrafted[1]
|
||||
local titem=titemcrafted[2]
|
||||
if not tmod==nil 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
|
||||
res=awards.on.craft[i].award
|
||||
end
|
||||
elseif data.craft[tmod][titem] > entry.target-1 then
|
||||
return entry.award
|
||||
end
|
||||
end
|
||||
|
||||
if res then
|
||||
awards.give_achievement(playern,res)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
minetest.register_on_dieplayer(function(player)
|
||||
@ -251,22 +157,12 @@ minetest.register_on_dieplayer(function(player)
|
||||
-- Increment counter
|
||||
data.deaths = data.deaths + 1
|
||||
|
||||
-- Run callbacks and triggers
|
||||
for _,trigger in pairs(awards.on.death) do
|
||||
local res = nil
|
||||
if type(trigger) == "function" then
|
||||
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
|
||||
awards.run_trigger_callbacks(player, data, "death", function(entry)
|
||||
if entry.target and entry.award and data.deaths and
|
||||
data.deaths >= entry.target then
|
||||
return entry.award
|
||||
end
|
||||
if res ~= nil then
|
||||
awards.unlock(name,res)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
@ -283,22 +179,12 @@ minetest.register_on_joinplayer(function(player)
|
||||
-- Increment counter
|
||||
data.joins = data.joins + 1
|
||||
|
||||
-- Run callbacks and triggers
|
||||
for _, trigger in pairs(awards.on.join) do
|
||||
local res = nil
|
||||
if type(trigger) == "function" then
|
||||
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
|
||||
awards.run_trigger_callbacks(player, data, "join", function(entry)
|
||||
if entry.target and entry.award and data.joins and
|
||||
data.joins >= entry.target then
|
||||
return entry.award
|
||||
end
|
||||
if res ~= nil then
|
||||
awards.unlock(name,res)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
minetest.register_on_chat_message(function(name, message)
|
||||
@ -316,29 +202,10 @@ minetest.register_on_chat_message(function(name, message)
|
||||
-- Increment counter
|
||||
data.chats = data.chats + 1
|
||||
|
||||
-- Run callbacks and triggers
|
||||
for _,trigger in pairs(awards.on.chat) do
|
||||
local res = nil
|
||||
if type(trigger) == "function" then
|
||||
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
|
||||
awards.run_trigger_callbacks(player, data, "chat", function(entry)
|
||||
if entry.target and entry.award and data.chats and
|
||||
data.chats >= entry.target then
|
||||
return entry.award
|
||||
end
|
||||
if res ~= nil then
|
||||
awards.unlock(name,res)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
minetest.register_on_newplayer(function(player)
|
||||
local playern = player:get_player_name()
|
||||
awards.assertPlayer(playern)
|
||||
end)
|
||||
|
||||
minetest.register_on_shutdown(function()
|
||||
awards.save()
|
||||
end)
|
||||
end)
|
||||
|
Reference in New Issue
Block a user