External triggers, on join trigger, intlib

This commit is contained in:
rubenwardy 2014-05-02 19:52:25 +01:00
parent 8bd3658d9e
commit 442346623a
3 changed files with 116 additions and 25 deletions

40
api.lua
View File

@ -64,6 +64,7 @@ function awards.assertPlayer(playern)
awards.tbv(awards.players[playern], "place") awards.tbv(awards.players[playern], "place")
awards.tbv(awards.players[playern], "count") awards.tbv(awards.players[playern], "count")
awards.tbv(awards.players[playern], "deaths", 0) awards.tbv(awards.players[playern], "deaths", 0)
awards.tbv(awards.players[playern], "joins", 0)
awards.tbv(awards.players[playern], "chats", 0) awards.tbv(awards.players[playern], "chats", 0)
end end
@ -72,6 +73,32 @@ dofile(minetest.get_modpath("awards").."/triggers.lua")
dofile(minetest.get_modpath("awards").."/config.txt") dofile(minetest.get_modpath("awards").."/config.txt")
-- API Functions -- API Functions
function awards._additional_triggers(name, data_table)
-- To add triggers in another mod, you should override this function
-- If the code can't handle the trigger passed, then call the last value of _additional_triggers
--[[
local add_trig = awards._additional_triggers
awards._additional_triggers = function(name, data_table)
if data_table.trigger.type == "trigger" then
local tmp = {
award = name,
node = data_table.trigger.node,
target = data_table.trigger.target,
}
table.insert(awards.onTrigger,tmp)
elseif data_table.trigger.type == "trigger2" then
local tmp = {
award = name,
node = data_table.trigger.node,
target = data_table.trigger.target,
}
table.insert(awards.onTrigger2,tmp)
else
add_trig(name, data_table)
end
end
]]--
end
function awards.register_achievement(name,data_table) function awards.register_achievement(name,data_table)
-- see if a trigger is defined in the achievement definition -- see if a trigger is defined in the achievement definition
if data_table.trigger and data_table.trigger.type then if data_table.trigger and data_table.trigger.type then
@ -101,6 +128,14 @@ function awards.register_achievement(name,data_table)
target = data_table.trigger.target, target = data_table.trigger.target,
} }
table.insert(awards.onChat,tmp) table.insert(awards.onChat,tmp)
elseif data_table.trigger.type == "join" then
local tmp = {
award = name,
target = data_table.trigger.target,
}
table.insert(awards.onJoin,tmp)
else
awards._additional_triggers(name, data_table)
end end
end end
@ -140,6 +175,11 @@ function awards.register_onChat(func)
table.insert(awards.onChat,func) table.insert(awards.onChat,func)
end end
-- run a function when a player joins
function awards.register_onJoin(func)
table.insert(awards.onJoin,func)
end
-- This function is called whenever a target condition is met. -- 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 checks if a player already has that achievement, and if they do not,
-- it gives it to them -- it gives it to them

View File

@ -4,12 +4,20 @@
-- this is the init file for the award mod -- this is the init file for the award mod
------------------------------------------------------- -------------------------------------------------------
local S
if (intllib) then
dofile(minetest.get_modpath("intllib").."/intllib.lua")
S = intllib.Getter(minetest.get_current_modname())
else
S = function ( s ) return s end
end
dofile(minetest.get_modpath("awards").."/api.lua") dofile(minetest.get_modpath("awards").."/api.lua")
-- Light it up -- Light it up
awards.register_achievement("award_lightitup",{ awards.register_achievement("award_lightitup",{
title = "Light It Up", title = S("Light It Up"),
description = "You have placed 100 torches", description = S("Place 100 torches"),
icon = "novicebuilder.png", icon = "novicebuilder.png",
trigger = { trigger = {
type = "place", type = "place",
@ -20,8 +28,8 @@ awards.register_achievement("award_lightitup",{
-- Lumber Jack -- Lumber Jack
awards.register_achievement("award_lumberjack",{ awards.register_achievement("award_lumberjack",{
title = "Lumber Jack", title = S("Lumber Jack"),
description = "You have dug 100 tree blocks", description = S("Dig 100 tree blocks"),
trigger = { trigger = {
type = "dig", type = "dig",
node = "default:tree", node = "default:tree",
@ -31,21 +39,21 @@ awards.register_achievement("award_lumberjack",{
-- Found some Mese! -- Found some Mese!
awards.register_achievement("award_mesefind",{ awards.register_achievement("award_mesefind",{
title = "First Mese Find", title = S("First Mese Find"),
description = "Found some Mese!", description = S("Found some Mese!"),
icon = "mese.png", icon = "mese.png",
background = "bg_mining.png", background = "bg_mining.png",
trigger = { trigger = {
type = "dig", type = "dig",
node = "default:stone_with_mese", node = "default:stone_with_mese",
target = 100, target = 1,
} }
}) })
-- Found a Nyan cat! -- Found a Nyan cat!
awards.register_achievement("award_nyanfind",{ awards.register_achievement("award_nyanfind",{
title = "OMG, Nyan Cat!", title = S("OMG, Nyan Cat!"),
description = "Find a nyan cat", description = S("Find a nyan cat"),
trigger = { trigger = {
type = "dig", type = "dig",
node = "default:nyancat", node = "default:nyancat",
@ -55,8 +63,8 @@ awards.register_achievement("award_nyanfind",{
-- Just entered the mine -- Just entered the mine
awards.register_achievement("award_mine1",{ awards.register_achievement("award_mine1",{
title = "Entering the mine", title = S("Entering the mine"),
description = "You have dug 10 stone blocks", description = S("You have dug 10 stone blocks"),
icon = "miniminer.png", icon = "miniminer.png",
background = "bg_mining.png", background = "bg_mining.png",
trigger = { trigger = {
@ -68,8 +76,8 @@ awards.register_achievement("award_mine1",{
-- Mini Miner -- Mini Miner
awards.register_achievement("award_mine2",{ awards.register_achievement("award_mine2",{
title = "Mini Miner", title = S("Mini Miner"),
description = "You have dug 100 stone blocks", description = S("You have dug 100 stone blocks"),
icon = "miniminer.png", icon = "miniminer.png",
background = "bg_mining.png", background = "bg_mining.png",
trigger = { trigger = {
@ -81,8 +89,8 @@ awards.register_achievement("award_mine2",{
-- Hardened Miner -- Hardened Miner
awards.register_achievement("award_mine3",{ awards.register_achievement("award_mine3",{
title = "Hardened Miner", title = S("Hardened Miner"),
description = "You have dug 1000 stone blocks", description = S("You have dug 1000 stone blocks"),
icon = "miniminer.png", icon = "miniminer.png",
background = "bg_mining.png", background = "bg_mining.png",
trigger = { trigger = {
@ -94,8 +102,8 @@ awards.register_achievement("award_mine3",{
-- Master Miner -- Master Miner
awards.register_achievement("award_mine4",{ awards.register_achievement("award_mine4",{
title = "Master Miner", title = S("Master Miner"),
description = "You have dug 10000 stone blocks", description = S("You have dug 10000 stone blocks"),
icon = "miniminer.png", icon = "miniminer.png",
background = "bg_mining.png", background = "bg_mining.png",
trigger = { trigger = {
@ -107,8 +115,8 @@ awards.register_achievement("award_mine4",{
-- First Death -- First Death
awards.register_achievement("award_death1",{ awards.register_achievement("award_death1",{
title = "First Death", title = S("First Death"),
description = "Oh well, it does not matter\nyou have more lives than a cat", description = S("You have more lives than a cat!"),
trigger = { trigger = {
type = "death", type = "death",
target = 1, target = 1,
@ -117,18 +125,29 @@ awards.register_achievement("award_death1",{
-- Burned to death -- Burned to death
awards.register_achievement("award_burn",{ awards.register_achievement("award_burn",{
title = "You're a witch!", title = S("You're a witch!"),
description = "Burn to death in a fire", description = S("Burn to death in a fire"),
}) })
-- 1 sentence -- 1 sentence
awards.register_achievement("award_chat1",{ awards.register_achievement("award_chat1",{
title = "First Word", title = S("First Word"),
description = "Use the chat to talk to players", description = S("Use the chat to talk to players"),
trigger = { trigger = {
type = "chat", type = "chat",
target = 1, target = 1,
}, }
})
-- Join
awards.register_achievement("award_join1",{
title = S("Welcome!"),
description = S("Connect to the server"),
trigger = {
type = "join",
target = 1,
}
}) })
awards.register_onDeath(function(player,data) awards.register_onDeath(function(player,data)

View File

@ -7,9 +7,9 @@
-- Function and table holders for Triggers -- Function and table holders for Triggers
awards.onDig = {} awards.onDig = {}
awards.onPlace = {} awards.onPlace = {}
awards.onTick = {}
awards.onChat = {} awards.onChat = {}
awards.onDeath = {} awards.onDeath = {}
awards.onJoin = {}
-- Trigger Handles -- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger) minetest.register_on_dignode(function(pos, oldnode, digger)
@ -154,6 +154,38 @@ minetest.register_on_dieplayer(function(player)
end end
end) end)
minetest.register_on_joinplayer(function(player)
-- Run checks
local name = player:get_player_name()
if not player or not name or name=="" then
return
end
-- Get player
awards.assertPlayer(name)
local data = awards.players[name]
-- Increment counter
data.joins = data.joins + 1
-- Run callbacks and triggers
for _,trigger in pairs(awards.onJoin) 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
end
if res ~= nil then
awards.give_achievement(name,res)
end
end
end)
minetest.register_on_chat_message(function(name, message) minetest.register_on_chat_message(function(name, message)
-- Run checks -- Run checks
local idx = string.find(message,"/") local idx = string.find(message,"/")