add onDeath

This commit is contained in:
Rubenwardy 2013-02-23 15:13:21 +00:00
parent 6b031a601d
commit 4cf05cbc47
4 changed files with 60 additions and 3 deletions

10
api.lua
View File

@ -46,6 +46,12 @@ function awards.register_achievement(name,data_table)
target=data_table['trigger']['target'],
}
table.insert(awards.onPlace,tmp)
elseif data_table['trigger']['type']=="death" then
local tmp={
award=name,
target=data_table['trigger']['target'],
}
table.insert(awards.onDeath,tmp)
end
end
@ -60,6 +66,10 @@ function awards.register_onPlace(func)
table.insert(awards.onPlace,func);
end
function awards.register_onDeath(func)
table.insert(awards.onDeath,func);
end
function awards.give_achievement(name,award)
local data=player_data[name]

View File

@ -81,4 +81,14 @@ awards.register_achievement("award_mine4",{
node="default:stone",
target=10000,
},
})
-- First Death
awards.register_achievement("award_death1",{
title = "First Death",
description = "Oh well, it does not matter you have more lives than a cat",
trigger={
type="death",
target=1,
},
})

View File

@ -19,13 +19,15 @@ The API
* trigger [optional] [table]
* type - "dig" or "place"
* (for dig/place type) node - the nodes name
* (for dig/place type) target - how many to dig / place
* (for all types) target - how many to dig / place
* awards.give_achievement(name,award)
* -- gives an award to a player
* awards.register_onDig(func(player,data))
* -- return award name or null
* awards.register_onPlace(func(player,data))
* -- return award name or null
* awards.register_onDeath(func(player,data))
* -- return award name or null
Player Data

View File

@ -149,12 +149,47 @@ minetest.register_on_placenode(function(pos, newnode, placer)
end
end)
minetest.register_on_newplayer(function(player)
minetest.chat_send_player(player:get_player_name(),"[Awards] Registering you now...")
minetest.register_on_dieplayer(function(player)
player_data[player:get_player_name()]['deaths']=player_data[player:get_player_name()]['deaths']+1
-- Set up the variables
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
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
-- table running failed!
else
-- run the table
if not data['deaths'] then
-- table running failed!
elseif data['deaths'] > awards.onDeath[i]['target']-1 then
res=awards.onDeath[i]['award']
end
end
end
if res~=nil then
awards.give_achievement(playern,res)
end
end
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']={}