From 442346623a124be9d403d2c83c72dfef21c88b0d Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 2 May 2014 19:52:25 +0100 Subject: [PATCH] External triggers, on join trigger, intlib --- api.lua | 40 +++++++++++++++++++++++++++++++ init.lua | 67 +++++++++++++++++++++++++++++++++------------------- triggers.lua | 34 +++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 25 deletions(-) diff --git a/api.lua b/api.lua index 71ddd2f..650c127 100644 --- a/api.lua +++ b/api.lua @@ -64,6 +64,7 @@ function awards.assertPlayer(playern) awards.tbv(awards.players[playern], "place") awards.tbv(awards.players[playern], "count") awards.tbv(awards.players[playern], "deaths", 0) + awards.tbv(awards.players[playern], "joins", 0) awards.tbv(awards.players[playern], "chats", 0) end @@ -72,6 +73,32 @@ dofile(minetest.get_modpath("awards").."/triggers.lua") dofile(minetest.get_modpath("awards").."/config.txt") -- 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) -- see if a trigger is defined in the achievement definition 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, } 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 @@ -140,6 +175,11 @@ function awards.register_onChat(func) table.insert(awards.onChat,func) 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. -- It checks if a player already has that achievement, and if they do not, -- it gives it to them diff --git a/init.lua b/init.lua index b87c884..7ff788b 100644 --- a/init.lua +++ b/init.lua @@ -4,12 +4,20 @@ -- 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") -- Light it up awards.register_achievement("award_lightitup",{ - title = "Light It Up", - description = "You have placed 100 torches", + title = S("Light It Up"), + description = S("Place 100 torches"), icon = "novicebuilder.png", trigger = { type = "place", @@ -20,8 +28,8 @@ awards.register_achievement("award_lightitup",{ -- Lumber Jack awards.register_achievement("award_lumberjack",{ - title = "Lumber Jack", - description = "You have dug 100 tree blocks", + title = S("Lumber Jack"), + description = S("Dig 100 tree blocks"), trigger = { type = "dig", node = "default:tree", @@ -31,21 +39,21 @@ awards.register_achievement("award_lumberjack",{ -- Found some Mese! awards.register_achievement("award_mesefind",{ - title = "First Mese Find", - description = "Found some Mese!", + title = S("First Mese Find"), + description = S("Found some Mese!"), icon = "mese.png", background = "bg_mining.png", trigger = { type = "dig", node = "default:stone_with_mese", - target = 100, + target = 1, } }) -- Found a Nyan cat! awards.register_achievement("award_nyanfind",{ - title = "OMG, Nyan Cat!", - description = "Find a nyan cat", + title = S("OMG, Nyan Cat!"), + description = S("Find a nyan cat"), trigger = { type = "dig", node = "default:nyancat", @@ -55,8 +63,8 @@ awards.register_achievement("award_nyanfind",{ -- Just entered the mine awards.register_achievement("award_mine1",{ - title = "Entering the mine", - description = "You have dug 10 stone blocks", + title = S("Entering the mine"), + description = S("You have dug 10 stone blocks"), icon = "miniminer.png", background = "bg_mining.png", trigger = { @@ -68,8 +76,8 @@ awards.register_achievement("award_mine1",{ -- Mini Miner awards.register_achievement("award_mine2",{ - title = "Mini Miner", - description = "You have dug 100 stone blocks", + title = S("Mini Miner"), + description = S("You have dug 100 stone blocks"), icon = "miniminer.png", background = "bg_mining.png", trigger = { @@ -81,8 +89,8 @@ awards.register_achievement("award_mine2",{ -- Hardened Miner awards.register_achievement("award_mine3",{ - title = "Hardened Miner", - description = "You have dug 1000 stone blocks", + title = S("Hardened Miner"), + description = S("You have dug 1000 stone blocks"), icon = "miniminer.png", background = "bg_mining.png", trigger = { @@ -94,8 +102,8 @@ awards.register_achievement("award_mine3",{ -- Master Miner awards.register_achievement("award_mine4",{ - title = "Master Miner", - description = "You have dug 10000 stone blocks", + title = S("Master Miner"), + description = S("You have dug 10000 stone blocks"), icon = "miniminer.png", background = "bg_mining.png", trigger = { @@ -107,8 +115,8 @@ awards.register_achievement("award_mine4",{ -- First Death awards.register_achievement("award_death1",{ - title = "First Death", - description = "Oh well, it does not matter\nyou have more lives than a cat", + title = S("First Death"), + description = S("You have more lives than a cat!"), trigger = { type = "death", target = 1, @@ -117,18 +125,29 @@ awards.register_achievement("award_death1",{ -- Burned to death awards.register_achievement("award_burn",{ - title = "You're a witch!", - description = "Burn to death in a fire", + title = S("You're a witch!"), + description = S("Burn to death in a fire"), }) -- 1 sentence awards.register_achievement("award_chat1",{ - title = "First Word", - description = "Use the chat to talk to players", + title = S("First Word"), + description = S("Use the chat to talk to players"), trigger = { type = "chat", 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) diff --git a/triggers.lua b/triggers.lua index b6005be..8bb909e 100644 --- a/triggers.lua +++ b/triggers.lua @@ -7,9 +7,9 @@ -- Function and table holders for Triggers awards.onDig = {} awards.onPlace = {} -awards.onTick = {} awards.onChat = {} awards.onDeath = {} +awards.onJoin = {} -- Trigger Handles minetest.register_on_dignode(function(pos, oldnode, digger) @@ -154,6 +154,38 @@ minetest.register_on_dieplayer(function(player) 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) -- Run checks local idx = string.find(message,"/")