3 -- Copyright (C) 2013-2015 rubenwardy
4 -- This program is free software; you can redistribute it and/or modify
5 -- it under the terms of the GNU Lesser General Public License as published by
6 -- the Free Software Foundation; either version 2.1 of the License, or
7 -- (at your option) any later version.
8 -- This program is distributed in the hope that it will be useful,
9 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 -- GNU Lesser General Public License for more details.
12 -- You should have received a copy of the GNU Lesser General Public License along
13 -- with this program; if not, write to the Free Software Foundation, Inc.,
14 -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 if minetest
.get_modpath("intllib") then
21 S
= function ( s
) return s
end
24 awards
.register_trigger("dig", function(def
)
27 node
= def
.trigger
.node
,
28 target
= def
.trigger
.target
,
30 table.insert(awards
.on
.dig
, tmp
)
31 def
.getProgress
= function(self
, data
)
34 itemcount
= awards
.get_item_count(data
, "count", tmp
.node
) or 0
36 itemcount
= awards
.get_total_item_count(data
, "count")
39 perc
= itemcount
/ tmp
.target
,
40 label
= string.format(S("%d/%d dug"), itemcount
, tmp
.target
)
43 def
.getDefaultDescription
= function(self
)
44 if self
.trigger
.node
then
45 local nname
= minetest
.registered_nodes
[self
.trigger
.node
].description
47 nname
= self
.trigger
.node
49 if self
.trigger
.target
~= 1 then
50 return string.format(S("Mine blocks: %d×%s"), self
.trigger
.target
, nname
)
52 return string.format(S("Mine a block: %s"), nname
)
55 return string.format(S("Mine %d block(s)."), self
.trigger
.target
)
60 awards
.register_trigger("place", function(def
)
63 node
= def
.trigger
.node
,
64 target
= def
.trigger
.target
,
66 table.insert(awards
.on
.place
, tmp
)
67 def
.getProgress
= function(self
, data
)
70 itemcount
= awards
.get_item_count(data
, "place", tmp
.node
) or 0
72 itemcount
= awards
.get_total_item_count(data
, "place")
75 perc
= itemcount
/ tmp
.target
,
76 label
= string.format(S("%d/%d placed"), itemcount
, tmp
.target
)
79 def
.getDefaultDescription
= function(self
)
80 if self
.trigger
.node
then
81 local nname
= minetest
.registered_nodes
[self
.trigger
.node
].description
83 nname
= self
.trigger
.node
85 if self
.trigger
.target
~= 1 then
86 return string.format(S("Place blocks: %d×%s"), self
.trigger
.target
, nname
)
88 return string.format(S("Place a block: %s"), nname
)
91 return string.format(S("Place %d block(s)."), self
.trigger
.target
)
96 awards
.register_trigger("eat", function(def
)
99 item
= def
.trigger
.item
,
100 target
= def
.trigger
.target
,
102 table.insert(awards
.on
.eat
, tmp
)
103 def
.getProgress
= function(self
, data
)
106 itemcount
= awards
.get_item_count(data
, "eat", tmp
.item
) or 0
108 itemcount
= awards
.get_total_item_count(data
, "eat")
111 perc
= itemcount
/ tmp
.target
,
112 label
= string.format(S("%d/%d eaten"), itemcount
, tmp
.target
)
115 def
.getDefaultDescription
= function(self
)
116 if self
.trigger
.item
then
117 local iname
= minetest
.registered_items
[self
.trigger
.item
].description
119 iname
= self
.trigger
.iode
121 if self
.trigger
.target
~= 1 then
122 return string.format(S("Eat: %d×%s"), self
.trigger
.target
, iname
)
124 return string.format(S("Eat: %s"), iname
)
127 return string.format(S("Eat %d item(s)."), self
.trigger
.target
)
132 awards
.register_trigger("death", function(def
)
135 target
= def
.trigger
.target
,
137 table.insert(awards
.on
.death
, tmp
)
138 def
.getProgress
= function(self
, data
)
139 local itemcount
= data
.deaths
or 0
141 perc
= itemcount
/ tmp
.target
,
142 label
= string.format(S("%d/%d deaths"), itemcount
, tmp
.target
)
145 def
.getDefaultDescription
= function(self
)
146 if self
.trigger
.target
~= 1 then
147 return string.format(S("Die %d times."), self
.trigger
.target
)
154 awards
.register_trigger("chat", function(def
)
157 target
= def
.trigger
.target
,
159 table.insert(awards
.on
.chat
, tmp
)
160 def
.getProgress
= function(self
, data
)
161 local itemcount
= data
.chats
or 0
163 perc
= itemcount
/ tmp
.target
,
164 label
= string.format(S("%d/%d chat messages"), itemcount
, tmp
.target
)
167 def
.getDefaultDescription
= function(self
)
168 if self
.trigger
.target
~= 1 then
169 return string.format(S("Write %d chat messages."), self
.trigger
.target
)
171 return S("Write something in chat.")
176 awards
.register_trigger("join", function(def
)
179 target
= def
.trigger
.target
,
181 table.insert(awards
.on
.join
, tmp
)
182 def
.getProgress
= function(self
, data
)
183 local itemcount
= data
.joins
or 0
185 perc
= itemcount
/ tmp
.target
,
186 label
= string.format(S("%d/%d game joins"), itemcount
, tmp
.target
)
189 def
.getDefaultDescription
= function(self
)
190 if self
.trigger
.target
~= 1 then
191 return string.format(S("Join the game %d times."), self
.trigger
.target
)
193 return S("Join the game.")
198 awards
.register_trigger("craft", function(def
)
201 item
= def
.trigger
.item
,
202 target
= def
.trigger
.target
,
204 table.insert(awards
.on
.craft
, tmp
)
205 def
.getProgress
= function(self
, data
)
208 itemcount
= awards
.get_item_count(data
, "craft", tmp
.item
) or 0
210 itemcount
= awards
.get_total_item_count(data
, "craft")
213 perc
= itemcount
/ tmp
.target
,
214 label
= string.format(S("%d/%d crafted"), itemcount
, tmp
.target
)
217 def
.getDefaultDescription
= function(self
)
218 if self
.trigger
.item
then
219 local iname
= minetest
.registered_items
[self
.trigger
.item
].description
221 iname
= self
.trigger
.item
223 if self
.trigger
.target
~= 1 then
224 return string.format(S("Craft: %d×%s"), self
.trigger
.target
, iname
)
226 return string.format(S("Craft: %s"), iname
)
229 return string.format(S("Craft %d item(s)."), self
.trigger
.target
)
234 -- Backwards compatibility
235 awards
.register_onDig
= awards
.register_on_dig
236 awards
.register_onPlace
= awards
.register_on_place
237 awards
.register_onDeath
= awards
.register_on_death
238 awards
.register_onChat
= awards
.register_on_chat
239 awards
.register_onJoin
= awards
.register_on_join
240 awards
.register_onCraft
= awards
.register_on_craft
243 minetest
.register_on_dignode(function(pos
, oldnode
, digger
)
244 if not digger
or not pos
or not oldnode
then
248 local data
= awards
.players
[digger
:get_player_name()]
249 if not awards
.increment_item_counter(data
, "count", oldnode
.name
) then
252 awards
.run_trigger_callbacks(digger
, data
, "dig", function(entry
)
255 local tnodedug
= string.split(entry
.node
, ":")
256 local tmod
= tnodedug
[1]
257 local titem
= tnodedug
[2]
258 if not tmod
or not titem
or not data
.count
[tmod
] or not data
.count
[tmod
][titem
] then
259 -- table running failed!
260 elseif data
.count
[tmod
][titem
] > entry
.target
-1 then
263 elseif awards
.get_total_item_count(data
, "count") > entry
.target
-1 then
270 minetest
.register_on_placenode(function(pos
, node
, digger
)
271 if not digger
or not pos
or not node
or not digger
:get_player_name() or digger
:get_player_name()=="" then
274 local data
= awards
.players
[digger
:get_player_name()]
275 if not awards
.increment_item_counter(data
, "place", node
.name
) then
279 awards
.run_trigger_callbacks(digger
, data
, "place", function(entry
)
282 local tnodedug
= string.split(entry
.node
, ":")
283 local tmod
= tnodedug
[1]
284 local titem
= tnodedug
[2]
285 if not tmod
or not titem
or not data
.place
[tmod
] or not data
.place
[tmod
][titem
] then
286 -- table running failed!
287 elseif data
.place
[tmod
][titem
] > entry
.target
-1 then
290 elseif awards
.get_total_item_count(data
, "place") > entry
.target
-1 then
297 minetest
.register_on_item_eat(function(hp_change
, replace_with_item
, itemstack
, user
, pointed_thing
)
298 if not user
or not itemstack
or not user
:get_player_name() or user
:get_player_name()=="" then
301 local data
= awards
.players
[user
:get_player_name()]
302 if not awards
.increment_item_counter(data
, "eat", itemstack
:get_name()) then
305 awards
.run_trigger_callbacks(user
, data
, "eat", function(entry
)
308 local titemstring
= string.split(entry
.item
, ":")
309 local tmod
= titemstring
[1]
310 local titem
= titemstring
[2]
311 if not tmod
or not titem
or not data
.eat
[tmod
] or not data
.eat
[tmod
][titem
] then
312 -- table running failed!
313 elseif data
.eat
[tmod
][titem
] > entry
.target
-1 then
316 elseif awards
.get_total_item_count(data
, "eat") > entry
.target
-1 then
323 minetest
.register_on_craft(function(itemstack
, player
, old_craft_grid
, craft_inv
)
324 if not player
or not itemstack
then
328 local data
= awards
.players
[player
:get_player_name()]
329 if not awards
.increment_item_counter(data
, "craft", itemstack
:get_name(), itemstack
:get_count()) then
333 awards
.run_trigger_callbacks(player
, data
, "craft", function(entry
)
336 local titemcrafted
= string.split(entry
.item
, ":")
337 local tmod
= titemcrafted
[1]
338 local titem
= titemcrafted
[2]
339 if not tmod
or not titem
or not data
.craft
[tmod
] or not data
.craft
[tmod
][titem
] then
340 -- table running failed!
341 elseif data
.craft
[tmod
][titem
] > entry
.target
-1 then
344 elseif awards
.get_total_item_count(data
, "craft") > entry
.target
-1 then
351 minetest
.register_on_dieplayer(function(player
)
353 local name
= player
:get_player_name()
354 if not player
or not name
or name
=="" then
359 awards
.assertPlayer(name
)
360 local data
= awards
.players
[name
]
363 data
.deaths
= data
.deaths
+ 1
365 awards
.run_trigger_callbacks(player
, data
, "death", function(entry
)
366 if entry
.target
and entry
.award
and data
.deaths
and
367 data
.deaths
>= entry
.target
then
373 minetest
.register_on_joinplayer(function(player
)
375 local name
= player
:get_player_name()
376 if not player
or not name
or name
=="" then
381 awards
.assertPlayer(name
)
382 local data
= awards
.players
[name
]
385 data
.joins
= data
.joins
+ 1
387 awards
.run_trigger_callbacks(player
, data
, "join", function(entry
)
388 if entry
.target
and entry
.award
and data
.joins
and
389 data
.joins
>= entry
.target
then
395 minetest
.register_on_chat_message(function(name
, message
)
397 local idx
= string.find(message
,"/")
398 if not name
or (idx
~= nil and idx
<= 1) then
403 awards
.assertPlayer(name
)
404 local data
= awards
.players
[name
]
405 local player
= minetest
.get_player_by_name(name
)
408 data
.chats
= data
.chats
+ 1
410 awards
.run_trigger_callbacks(player
, data
, "chat", function(entry
)
411 if entry
.target
and entry
.award
and data
.chats
and
412 data
.chats
>= entry
.target
then