This commit is contained in:
Rubenwardy 2013-11-12 19:50:25 +00:00
parent 6387bd1d9e
commit 4fe55f3a14
2 changed files with 26 additions and 36 deletions

18
api.lua
View File

@ -35,11 +35,10 @@ end
-- A table of award definitions -- A table of award definitions
awards.def = {} awards.def = {}
function awards.tbv(tb,value) function awards.tbv(tb,value,default)
awards.tbv_default(tb,value,{}) if not default then
end default = {}
end
function awards.tbv_default(tb,value,default)
if not tb or type(tb) ~= "table" then if not tb or type(tb) ~= "table" then
if not value then if not value then
value = "[NULL]" value = "[NULL]"
@ -58,6 +57,15 @@ function awards.tbv_default(tb,value,default)
end end
end end
function awards.assertPlayer(playern)
awards.tbv(awards.players, playern)
awards.tbv(awards.players[playern], "name", playern)
awards.tbv(awards.players[playern], "unlocked")
awards.tbv(awards.players[playern], "place")
awards.tbv(awards.players[playern], "count")
awards.tbv(awards.players[playern], "deaths", 0)
end
-- Load files -- Load files
dofile(minetest.get_modpath("awards").."/triggers.lua") dofile(minetest.get_modpath("awards").."/triggers.lua")
dofile(minetest.get_modpath("awards").."/config.txt") dofile(minetest.get_modpath("awards").."/config.txt")

View File

@ -27,13 +27,9 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
if (not playern or not nodedug or not mod or not item) then if (not playern or not nodedug or not mod or not item) then
return return
end end
awards.assertPlayer(playern)
-- Run checks awards.tbv(awards.players[playern].count, mod)
awards.tbv(awards.players, playern ) awards.tbv(awards.players[playern].count[mod], item, 0 )
awards.tbv(awards.players[playern], "count" )
awards.tbv(awards.players[playern], "name", playern )
awards.tbv(awards.players[playern].count, mod)
awards.tbv(awards.players[playern].count[mod], item, 0 )
-- Increment counder -- Increment counder
awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item]+1 awards.players[playern].count[mod][item]=awards.players[playern].count[mod][item]+1
@ -47,14 +43,12 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
local res = nil local res = nil
if type(awards.onDig[i]) == "function" then if type(awards.onDig[i]) == "function" then
-- Run trigger callback -- Run trigger callback
print(i.." is a function")
res = awards.onDig[i](player,data) res = awards.onDig[i](player,data)
elseif type(awards.onDig[i]) == "table" then elseif type(awards.onDig[i]) == "table" then
-- Handle table trigger -- 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 if not awards.onDig[i].node or not awards.onDig[i].target or not awards.onDig[i].award then
-- table running failed! -- table running failed!
print("onDig trigger "..i.." is invalid!") print("[ERROR] awards - onDig trigger "..i.." is invalid!")
else else
-- run the table -- run the table
local tnodedug = string.split(awards.onDig[i].node, ":") local tnodedug = string.split(awards.onDig[i].node, ":")
@ -91,15 +85,13 @@ minetest.register_on_placenode(function(pos,node,digger)
if (not playern or not nodedug or not mod or not item) then if (not playern or not nodedug or not mod or not item) then
return return
end end
awards.tbv(awards.players, playern ) awards.assertPlayer(playern)
awards.tbv(awards.players[playern], "place" ) awards.tbv(awards.players[playern].place, mod)
awards.tbv(awards.players[playern], "name", playern ) awards.tbv(awards.players[playern].place[mod], item, 0 )
awards.tbv(awards.players[playern].place, mod)
awards.tbv(awards.players[playern].place[mod], item, 0 )
-- Increment counder -- Increment counder
awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item]+1 awards.players[playern].place[mod][item] = awards.players[playern].place[mod][item]+1
print(" - "..mod..":"..item.." 's count is now "..(awards.players[playern].place[mod][item])) print(" - "..mod..":"..item.." 's place count is now "..(awards.players[playern].place[mod][item]))
-- Run callbacks and triggers -- Run callbacks and triggers
local player = digger local player = digger
@ -108,14 +100,12 @@ minetest.register_on_placenode(function(pos,node,digger)
local res = nil local res = nil
if type(awards.onPlace[i]) == "function" then if type(awards.onPlace[i]) == "function" then
-- Run trigger callback -- Run trigger callback
print(i.." is a function")
res = awards.onPlace[i](player,data) res = awards.onPlace[i](player,data)
elseif type(awards.onPlace[i]) == "table" then elseif type(awards.onPlace[i]) == "table" then
-- Handle table trigger -- 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 if not awards.onPlace[i].node or not awards.onPlace[i].target or not awards.onPlace[i].award then
-- table running failed! -- table running failed!
print("onPlace trigger "..i.." is invalid!") print("[ERROR] awards - onPlace trigger "..i.." is invalid!")
else else
-- run the table -- run the table
local tnodedug = string.split(awards.onPlace[i].node, ":") local tnodedug = string.split(awards.onPlace[i].node, ":")
@ -141,9 +131,8 @@ minetest.register_on_dieplayer(function(player)
return return
end end
local playern = player:get_player_name() local playern = player:get_player_name()
awards.tbv(awards.players, playern ) awards.assertPlayer(playern)
awards.tbv(awards.players[playern], "name", playern )
awards.tbv(awards.players[playern], "deaths", 0 )
-- Increment counter -- Increment counter
awards.players[player:get_player_name()].deaths = awards.players[player:get_player_name()].deaths + 1 awards.players[player:get_player_name()].deaths = awards.players[player:get_player_name()].deaths + 1
@ -154,14 +143,12 @@ minetest.register_on_dieplayer(function(player)
local res=nil local res=nil
if type(awards.onDeath[i]) == "function" then if type(awards.onDeath[i]) == "function" then
-- Run trigger callback -- Run trigger callback
print(i.." is a function")
res=awards.onDeath[i](player,data) res=awards.onDeath[i](player,data)
elseif type(awards.onDeath[i]) == "table" then elseif type(awards.onDeath[i]) == "table" then
-- handle table here -- 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! -- table running failed!
print("onDeath trigger "..i.." is invalid!") print("[ERROR] awards - onDeath trigger "..i.." is invalid!")
else else
-- run the table -- run the table
if not data.deaths then if not data.deaths then
@ -180,12 +167,7 @@ end)
minetest.register_on_newplayer(function(player) minetest.register_on_newplayer(function(player)
local playern = player:get_player_name() local playern = player:get_player_name()
awards.tbv(awards.players, playern) awards.assertPlayer(playern)
awards.tbv(awards.players[playern], "name", playern)
awards.tbv(awards.players[playern], "unlocked")
awards.tbv(awards.players[playern], "place")
awards.tbv(awards.players[playern], "count")
awards.tbv(awards.players[playern], "deaths", 0)
end) end)
minetest.register_on_shutdown(function() minetest.register_on_shutdown(function()