1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
-- AWARDS
--
-- Copyright (C) 2013-2015 rubenwardy
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
-- You should have received a copy of the GNU Lesser General Public License along
-- with this program; if not, write to the Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
--
local S, NS = awards.gettext, awards.ngettext
awards.register_trigger("dig", function(def)
local tmp = {
award = def.name,
node = def.trigger.node,
target = def.trigger.target,
}
table.insert(awards.on.dig, tmp)
def.getProgress = function(self, data)
local itemcount
if tmp.node then
itemcount = awards.get_item_count(data, "count", tmp.node) or 0
else
itemcount = awards.get_total_item_count(data, "count")
end
return {
perc = itemcount / tmp.target,
label = S("@1/@2 dug", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
if self.trigger.node then
local nname = minetest.registered_nodes[self.trigger.node].description
if nname == nil then
nname = self.trigger.node
end
-- Translators: @1 is count, @2 is description.
return NS("Mine: @2", "Mine: @1×@2", n, n, nname)
else
return NS("Mine @1 block.", "Mine @1 blocks.", n, n)
end
end
end)
awards.register_trigger("place", function(def)
local tmp = {
award = def.name,
node = def.trigger.node,
target = def.trigger.target,
}
table.insert(awards.on.place, tmp)
def.getProgress = function(self, data)
local itemcount
if tmp.node then
itemcount = awards.get_item_count(data, "place", tmp.node) or 0
else
itemcount = awards.get_total_item_count(data, "place")
end
return {
perc = itemcount / tmp.target,
label = S("@1/@2 placed", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
if self.trigger.node then
local nname = minetest.registered_nodes[self.trigger.node].description
if nname == nil then
nname = self.trigger.node
end
-- Translators: @1 is count, @2 is description.
return NS("Place: @2", "Place: @1×@2", n, n, nname)
else
return NS("Place a block.", "Place @1 blocks.", n, n)
end
end
end)
awards.register_trigger("eat", function(def)
local tmp = {
award = def.name,
item = def.trigger.item,
target = def.trigger.target,
}
table.insert(awards.on.eat, tmp)
def.getProgress = function(self, data)
local itemcount
if tmp.item then
itemcount = awards.get_item_count(data, "eat", tmp.item) or 0
else
itemcount = awards.get_total_item_count(data, "eat")
end
return {
perc = itemcount / tmp.target,
label = S("@1/@2 eaten", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
if self.trigger.item then
local iname = minetest.registered_items[self.trigger.item].description
if iname == nil then
iname = self.trigger.iode
end
-- Translators: @1 is count, @2 is description.
return NS("Eat: @2", "Eat: @1×@2", n, n, iname)
else
return NS("Eat an item.", "Eat @1 items.", n, n)
end
end
end)
awards.register_trigger("death", function(def)
local tmp = {
award = def.name,
target = def.trigger.target,
}
table.insert(awards.on.death, tmp)
def.getProgress = function(self, data)
local itemcount = data.deaths or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 deaths", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
return NS("Die.", "Die @1 times.", n, n)
end
end)
awards.register_trigger("chat", function(def)
local tmp = {
award = def.name,
target = def.trigger.target,
}
table.insert(awards.on.chat, tmp)
def.getProgress = function(self, data)
local itemcount = data.chats or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 chat messages", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
return NS("Write something in chat.", "Write @1 chat messages.", n, n)
end
end)
awards.register_trigger("join", function(def)
local tmp = {
award = def.name,
target = def.trigger.target,
}
table.insert(awards.on.join, tmp)
def.getProgress = function(self, data)
local itemcount = data.joins or 0
return {
perc = itemcount / tmp.target,
label = S("@1/@2 game joins", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
return NS("Join the game.", "Join the game @1 times.", n, n)
end
end)
awards.register_trigger("craft", function(def)
local tmp = {
award = def.name,
item = def.trigger.item,
target = def.trigger.target,
}
table.insert(awards.on.craft, tmp)
def.getProgress = function(self, data)
local itemcount
if tmp.item then
itemcount = awards.get_item_count(data, "craft", tmp.item) or 0
else
itemcount = awards.get_total_item_count(data, "craft")
end
return {
perc = itemcount / tmp.target,
label = S("@1/@2 crafted", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
local n = self.trigger.target
if self.trigger.item then
local iname = minetest.registered_items[self.trigger.item].description
if iname == nil then
iname = self.trigger.item
end
-- Translators: @1 is count, @2 is description.
return NS("Craft: @2", "Craft: @1×@2", n, n, iname)
else
return NS("Craft an item.", "Craft @1 items.", n)
end
end
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
awards.register_onCraft = awards.register_on_craft
-- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger)
if not digger or not pos or not oldnode then
return
end
local data = awards.players[digger:get_player_name()]
if not awards.increment_item_counter(data, "count", oldnode.name) then
return
end
awards.run_trigger_callbacks(digger, data, "dig", function(entry)
if entry.target then
if entry.node then
local tnodedug = string.split(entry.node, ":")
local tmod = tnodedug[1]
local titem = tnodedug[2]
if not (not tmod or not titem or not data.count[tmod] or
not data.count[tmod][titem]) and
data.count[tmod][titem] > entry.target-1 then
return entry.award
end
elseif awards.get_total_item_count(data, "count") > entry.target-1 then
return entry.award
end
end
end)
end)
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 data = awards.players[digger:get_player_name()]
if not awards.increment_item_counter(data, "place", node.name) then
return
end
awards.run_trigger_callbacks(digger, data, "place", function(entry)
if entry.target then
if entry.node then
local tnodedug = string.split(entry.node, ":")
local tmod = tnodedug[1]
local titem = tnodedug[2]
if not (not tmod or not titem or not data.place[tmod] or
not data.place[tmod][titem]) and
data.place[tmod][titem] > entry.target-1 then
return entry.award
end
elseif awards.get_total_item_count(data, "place") > entry.target-1 then
return entry.award
end
end
end)
end)
minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, user, pointed_thing)
if not user or not itemstack or not user:get_player_name() or user:get_player_name()=="" then
return
end
local data = awards.players[user:get_player_name()]
if not awards.increment_item_counter(data, "eat", itemstack:get_name()) then
return
end
awards.run_trigger_callbacks(user, data, "eat", function(entry)
if entry.target then
if entry.item then
local titemstring = string.split(entry.item, ":")
local tmod = titemstring[1]
local titem = titemstring[2]
if not (not tmod or not titem or not data.eat[tmod] or
not data.eat[tmod][titem]) and
data.eat[tmod][titem] > entry.target-1 then
return entry.award
end
elseif awards.get_total_item_count(data, "eat") > entry.target-1 then
return entry.award
end
end
end)
end)
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if not player or not itemstack then
return
end
local data = awards.players[player:get_player_name()]
if not awards.increment_item_counter(data, "craft", itemstack:get_name(), itemstack:get_count()) then
return
end
awards.run_trigger_callbacks(player, data, "craft", function(entry)
if entry.target then
if entry.item then
local titemcrafted = string.split(entry.item, ":")
local tmod = titemcrafted[1]
local titem = titemcrafted[2]
if not (not tmod or not titem or not data.craft[tmod] or
not data.craft[tmod][titem]) and
data.craft[tmod][titem] > entry.target-1 then
return entry.award
end
elseif awards.get_total_item_count(data, "craft") > entry.target-1 then
return entry.award
end
end
end)
end)
minetest.register_on_dieplayer(function(player)
-- Run checks
local name = player:get_player_name()
if not player or not name or name=="" then
return
end
-- Get player
awards.assertPlayer(name)
local data = awards.players[name]
-- Increment counter
data.deaths = data.deaths + 1
awards.run_trigger_callbacks(player, data, "death", function(entry)
if entry.target and entry.award and data.deaths and
data.deaths >= entry.target then
return entry.award
end
end)
end)
minetest.register_on_joinplayer(function(player)
-- Run checks
local name = player:get_player_name()
if not player or not name or name=="" then
return
end
-- Get player
awards.assertPlayer(name)
local data = awards.players[name]
-- Increment counter
data.joins = data.joins + 1
awards.run_trigger_callbacks(player, data, "join", function(entry)
if entry.target and entry.award and data.joins and
data.joins >= entry.target then
return entry.award
end
end)
end)
minetest.register_on_chat_message(function(name, message)
-- Run checks
local idx = string.find(message,"/")
if not name or (idx ~= nil and idx <= 1) then
return
end
-- Get player
awards.assertPlayer(name)
local data = awards.players[name]
local player = minetest.get_player_by_name(name)
-- Increment counter
data.chats = data.chats + 1
awards.run_trigger_callbacks(player, data, "chat", function(entry)
if entry.target and entry.award and data.chats and
data.chats >= entry.target then
return entry.award
end
end)
end)
|