diff options
author | Rubenwardy <anjayward@gmail.com> | 2013-11-06 18:33:01 +0000 |
---|---|---|
committer | Rubenwardy <anjayward@gmail.com> | 2013-11-06 18:33:01 +0000 |
commit | 773f7993203ee0d953bf019fd5e0f818e7008241 (patch) | |
tree | 5238947cc5ce4a3482bbb4bd59d4b49b4c3a2490 /triggers.lua | |
parent | 076f91853cbbd8d33a26927d5c0caa7c9aa89289 (diff) |
Restructure for bulletproof-ness
Diffstat (limited to 'triggers.lua')
-rw-r--r-- | triggers.lua | 258 |
1 files changed, 121 insertions, 137 deletions
diff --git a/triggers.lua b/triggers.lua index c285464..4b99786 100644 --- a/triggers.lua +++ b/triggers.lua @@ -10,175 +10,164 @@ awards.onPlace={} awards.onTick={} awards.onDeath={} --- Player functions - -- Trigger Handles 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 + print(oldnode.name.." is in wrong format!") + return + end local mod=nodedug[1] local item=nodedug[2] - local playern = digger:get_player_name() - if (playern~=nil and nodedug~=nil and mod~=nil and item~=nil) then - --check the player's directory - if not player_data[playern] then - player_data[playern]={} - player_data[playern]['name']=playern - end - if not player_data[playern]['count'] then - player_data[playern]['count']={} - end + if (not playern or not nodedug or not mod or not item) then + return + end + + -- Run checks + awards.tbv(player_data, playern ) + awards.tbv(player_data[playern], "count" ) + awards.tbv(player_data[playern], "name", playern ) + awards.tbv(player_data[playern].count, mod) + awards.tbv(player_data[playern].count[mod], item, 0 ) + + -- Increment counder + player_data[playern].count[mod][item]=player_data[playern].count[mod][item]+1 + print(" - "..mod..":"..item.." 's count is now "..(player_data[playern].count[mod][item])) + + -- Run callbacks and triggers + local player=digger + local data=player_data[playern] - --check player.count.mod - if not player_data[playern].count[mod] then - player_data[playern]['count'][mod]={} - end - - --check player.count.mod.item - if not player_data[playern]['count'][mod][item] then - player_data[playern]['count'][mod][item]=0 - end - - player_data[playern]['count'][mod][item]=player_data[playern]['count'][mod][item]+1 - - print(" - "..mod..":"..item.." 's count is now "..(player_data[playern]['count'][mod][item])) - - -- Roll through the onDig functions - local player=digger - local data=player_data[playern] - - for i=1,# awards.onDig do - local res=nil - - if type(awards.onDig[i]) == "function" then - -- run the function - print(i.." is a function") - res=awards.onDig[i](player,data) - elseif type(awards.onDig[i]) == "table" then - -- handle table here - print(i.." is a table") - if not awards.onDig[i]['node'] or not awards.onDig[i]['target'] or not awards.onDig[i]['award'] then + for i=1,# awards.onDig do + local res = nil + if type(awards.onDig[i]) == "function" then + -- Run trigger callback + print(i.." is a function") + res = awards.onDig[i](player,data) + elseif type(awards.onDig[i]) == "table" then + -- Handle table trigger + print(i.." is a table") + if not awards.onDig[i].node or not awards.onDig[i].target or not awards.onDig[i].award then + -- table running failed! + print("onDig trigger "..i.." is invalid!") + else + -- run the table + local tnodedug = string.split(awards.onDig[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! - else - -- run the table - local tnodedug = string.split(awards.onDig[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'] - end + elseif data.count[tmod][titem] > awards.onDig[i].target-1 then + res=awards.onDig[i].award end end + end - if res~=nil then - awards.give_achievement(playern,res) - end + if res then + awards.give_achievement(playern,res) end end end) -minetest.register_on_placenode(function(pos, newnode, placer) - local nodedug = string.split(newnode.name, ":") - +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 + print(oldnode.name.." is in wrong format!") + return + end local mod=nodedug[1] local item=nodedug[2] + local playern = digger:get_player_name() - local playern = placer:get_player_name() - - if (playern~=nil and nodedug~=nil and mod~=nil and item~=nil) then - --check the player's directory - if not player_data[playern] then - player_data[playern]={} - player_data[playern].place=playern - end - if not player_data[playern].place then - player_data[playern]['place']={} - end - - --check player.count.mod - if not player_data[playern].place[mod] then - player_data[playern].place[mod]={} - end - - --check player.count.mod.item - if not player_data[playern].place[mod][item] then - player_data[playern].place[mod][item]=0 - end - - player_data[playern].place[mod][item]=player_data[playern].place[mod][item]+1 - - print(" - "..mod..":"..item.." 's place is now "..(player_data[playern].place[mod][item])) - - -- Roll through the onDig functions - local player=placer - local data=player_data[playern] - - for i=1,# awards.onPlace do - local res=nil - - if type(awards.onPlace[i]) == "function" then - -- run the function - print(i.." is a function") - res=awards.onPlace[i](player,data) - elseif type(awards.onPlace[i]) == "table" then - -- handle table here - print(i.." is a table") - if not awards.onPlace[i]['node'] or not awards.onPlace[i].target or not awards.onPlace[i].award then + -- Run checks + if (not playern or not nodedug or not mod or not item) then + return + end + awards.tbv(player_data, playern ) + awards.tbv(player_data[playern], "place" ) + awards.tbv(player_data[playern], "name", playern ) + awards.tbv(player_data[playern].place, mod) + awards.tbv(player_data[playern].place[mod], item, 0 ) + + -- Increment counder + player_data[playern].place[mod][item] = player_data[playern].place[mod][item]+1 + print(" - "..mod..":"..item.." 's count is now "..(player_data[playern].place[mod][item])) + + -- Run callbacks and triggers + local player = digger + local data = player_data[playern] + for i=1,# awards.onPlace do + local res = nil + if type(awards.onPlace[i]) == "function" then + -- Run trigger callback + print(i.." is a function") + res = awards.onPlace[i](player,data) + elseif type(awards.onPlace[i]) == "table" then + -- Handle table trigger + print(i.." is a table") + if not awards.onPlace[i].node or not awards.onPlace[i].target or not awards.onPlace[i].award then + -- table running failed! + print("onPlace trigger "..i.." is invalid!") + else + -- run the table + local tnodedug = string.split(awards.onPlace[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! - else - -- run the table - local tnodedug = string.split(awards.onPlace[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'] - end + elseif data.place[tmod][titem] > awards.onPlace[i].target-1 then + res = awards.onPlace[i].award end end + end - if res~=nil then - awards.give_achievement(playern,res) - end + if res then + awards.give_achievement(playern,res) end end end) minetest.register_on_dieplayer(function(player) - player_data[player:get_player_name()]['deaths']=player_data[player:get_player_name()]['deaths']+1 + -- Run checks + if not player or not player:get_player_name() or player:get_player_name()=="" then + return + end + awards.tbv(player_data, playern ) + awards.tbv(player_data[playern], "name", playern ) + awards.tbv(player_data[playern], "deaths", 0 ) + + -- Increment counter + player_data[player:get_player_name()].deaths = player_data[player:get_player_name()].deaths + 1 - -- Set up the variables + -- Run callbacks and triggers local playern=player:get_player_name() local data=player_data[playern] - - -- Roll through the onDeath functions for i=1,# awards.onDeath do local res=nil if type(awards.onDeath[i]) == "function" then - -- run the function + -- Run trigger callback print(i.." is a function") res=awards.onDeath[i](player,data) elseif type(awards.onDeath[i]) == "table" then -- handle table here print(i.." is a table") - if not awards.onDeath[i]['target'] or not awards.onDeath[i]['award'] then + if not awards.onDeath[i].target or not awards.onDeath[i].award then -- table running failed! + print("onDeath trigger "..i.." is invalid!") else -- run the table - - if not data['deaths'] then + if not data.deaths then -- table running failed! - elseif data['deaths'] > awards.onDeath[i]['target']-1 then - res=awards.onDeath[i]['award'] + elseif data.deaths > awards.onDeath[i].target-1 then + res=awards.onDeath[i].award end end end @@ -190,17 +179,12 @@ minetest.register_on_dieplayer(function(player) end) minetest.register_on_newplayer(function(player) - --Player data root - player_data[player:get_player_name()]={} - player_data[player:get_player_name()]['name']=player:get_player_name() - player_data[player:get_player_name()]['deaths']=0 - - --The player counter - player_data[player:get_player_name()]['count']={} - player_data[player:get_player_name()]['place']={} - - --Table to contain achievement records - player_data[player:get_player_name()]['unlocked']={} + awards.tbv(player_data, player:get_player_name()) + awards.tbv(player_data[playern], "name", player:get_player_name()) + awards.tbv(player_data[playern], "unlocked") + awards.tbv(player_data[playern], "place") + awards.tbv(player_data[playern], "count") + awards.tbv(player_data[playern], "deaths", 0) end) minetest.register_on_shutdown(function() |