summaryrefslogtreecommitdiff
path: root/triggers.lua
diff options
context:
space:
mode:
authorrubenwardy <rubenwardy@gmail.com>2015-06-10 18:39:22 +0100
committerrubenwardy <rubenwardy@gmail.com>2016-07-29 16:00:58 +0100
commit87c04be9ea8cfd02c73ed9050e246884e5e026a7 (patch)
treecab789fe3cfb84a070e8b083080bff1139b2b796 /triggers.lua
parent85efc6704932898bf6c98ff5c043a989ce83f293 (diff)
awards.register_trigger
Diffstat (limited to 'triggers.lua')
-rw-r--r--triggers.lua123
1 files changed, 81 insertions, 42 deletions
diff --git a/triggers.lua b/triggers.lua
index f4fb994..fc058f9 100644
--- a/triggers.lua
+++ b/triggers.lua
@@ -4,13 +4,54 @@
-- this is the trigger handler file for the awards mod
-------------------------------------------------------
--- Function and table holders for Triggers
-awards.onDig = {}
-awards.onPlace = {}
-awards.onCraft = {}
-awards.onChat = {}
-awards.onDeath = {}
-awards.onJoin = {}
+awards.register_trigger("dig", function(name, def)
+ local tmp = {
+ award = name,
+ node = def.trigger.node,
+ target = def.trigger.target
+ }
+ table.insert(awards.on.dig, tmp)
+end)
+
+awards.register_trigger("place", function(name, def)
+ local tmp = {
+ award = name,
+ node = def.trigger.node,
+ target = def.trigger.target
+ }
+ table.insert(awards.on.place, tmp)
+end)
+
+awards.register_trigger("death", function(name, def)
+ local tmp = {
+ award = name,
+ target = def.trigger.target
+ }
+ table.insert(awards.on.death, tmp)
+end)
+
+awards.register_trigger("chat", function(name, def)
+ local tmp = {
+ award = name,
+ target = def.trigger.target
+ }
+ table.insert(awards.on.chat, tmp)
+end)
+
+awards.register_trigger("join", function(name, def)
+ local tmp = {
+ award = name,
+ target = def.trigger.target
+ }
+ table.insert(awards.on.join, tmp)
+end)
+
+-- Backwards compatibility
+awards.register_onDig = awards.register_on_dig
+awards.register_onPlace = awards.register_on_place
+awards.register_onDeath = awards.register_on_death
+awards.register_onChat = awards.register_on_chat
+awards.register_onJoin = awards.register_on_join
-- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger)
@@ -19,7 +60,6 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
end
local nodedug = string.split(oldnode.name, ":")
if #nodedug ~= 2 then
- --minetest.log("error","Awards mod: "..oldnode.name.." is in wrong format!")
return
end
local mod = nodedug[1]
@@ -37,27 +77,27 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
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.onDig do
+ local player = digger
+ local data = awards.players[playern]
+ for i=1, #awards.on.dig do
local res = nil
- if type(awards.onDig[i]) == "function" then
+ if type(awards.on.dig[i]) == "function" then
-- Run trigger callback
- res = awards.onDig[i](player,data)
- elseif type(awards.onDig[i]) == "table" then
+ res = awards.on.dig[i](player,data)
+ elseif type(awards.on.dig[i]) == "table" then
-- Handle table trigger
- if not awards.onDig[i].node or not awards.onDig[i].target or not awards.onDig[i].award then
+ 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 - onDig trigger "..i.." is invalid!")
+ print("[ERROR] awards - on.dig trigger "..i.." is invalid!")
else
-- run the table
- local tnodedug = string.split(awards.onDig[i].node, ":")
+ 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.onDig[i].target-1 then
- res=awards.onDig[i].award
+ elseif data.count[tmod][titem] > awards.on.dig[i].target-1 then
+ res=awards.on.dig[i].award
end
end
end
@@ -68,13 +108,12 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
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
return
end
local nodedug = string.split(node.name, ":")
if #nodedug ~= 2 then
- --minetest.log("error","Awards mod: "..node.name.." is in wrong format!")
return
end
local mod=nodedug[1]
@@ -95,24 +134,24 @@ minetest.register_on_placenode(function(pos,node,digger)
-- Run callbacks and triggers
local player = digger
local data = awards.players[playern]
- for i=1,# awards.onPlace do
+ for i=1,# awards.on.place do
local res = nil
- if type(awards.onPlace[i]) == "function" then
+ if type(awards.on.place[i]) == "function" then
-- Run trigger callback
- res = awards.onPlace[i](player,data)
- elseif type(awards.onPlace[i]) == "table" then
+ res = awards.on.place[i](player,data)
+ elseif type(awards.on.place[i]) == "table" then
-- Handle table trigger
- if not awards.onPlace[i].node or not awards.onPlace[i].target or not awards.onPlace[i].award then
- print("[ERROR] awards - onPlace trigger "..i.." is invalid!")
+ 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.onPlace[i].node, ":")
+ 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.onPlace[i].target-1 then
- res = awards.onPlace[i].award
+ elseif data.place[tmod][titem] > awards.on.place[i].target-1 then
+ res = awards.on.place[i].award
end
end
end
@@ -183,16 +222,16 @@ minetest.register_on_dieplayer(function(player)
if not player or not name or name=="" then
return
end
-
- -- Get player
+
+ -- Get player
awards.assertPlayer(name)
local data = awards.players[name]
-- Increment counter
data.deaths = data.deaths + 1
-
+
-- Run callbacks and triggers
- for _,trigger in pairs(awards.onDeath) do
+ for _,trigger in pairs(awards.on.death) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)
@@ -215,16 +254,16 @@ minetest.register_on_joinplayer(function(player)
if not player or not name or name=="" then
return
end
-
- -- Get player
+
+ -- 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
+ for _, trigger in pairs(awards.on.join) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)
@@ -252,12 +291,12 @@ minetest.register_on_chat_message(function(name, message)
awards.assertPlayer(name)
local data = awards.players[name]
local player = minetest.get_player_by_name(name)
-
+
-- Increment counter
data.chats = data.chats + 1
-
- -- Run callbacks and triggers
- for _,trigger in pairs(awards.onChat) do
+
+ -- Run callbacks and triggers
+ for _,trigger in pairs(awards.on.chat) do
local res = nil
if type(trigger) == "function" then
res = trigger(player,data)