diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/api_awards.lua | 179 | ||||
-rw-r--r-- | src/api_triggers.lua | 215 | ||||
-rw-r--r-- | src/awards.lua | 960 | ||||
-rw-r--r-- | src/chat_commands.lua | 61 | ||||
-rw-r--r-- | src/data.lua | 111 | ||||
-rw-r--r-- | src/gui.lua | 269 | ||||
-rw-r--r-- | src/intllib.lua | 44 | ||||
-rw-r--r-- | src/triggers.lua | 141 |
8 files changed, 1980 insertions, 0 deletions
diff --git a/src/api_awards.lua b/src/api_awards.lua new file mode 100644 index 0000000..4147d43 --- /dev/null +++ b/src/api_awards.lua @@ -0,0 +1,179 @@ +-- Copyright (c) 2013-18 rubenwardy. MIT. + +local S = awards.gettext + +function awards.register_award(name, def) + def.name = name + + -- Add Triggers + if def.trigger and def.trigger.type then + local tdef = awards.registered_triggers[def.trigger.type] + assert(tdef, "Trigger not found: " .. def.trigger.type) + tdef:on_register(def) + end + + function def:can_unlock(data) + if not self.requires then + return true + end + + for i=1, #self.requires do + if not data.unlocked[self.requires[i]] then + return false + end + end + return true + end + + -- Add Award + awards.registered_awards[name] = def + + local tdef = awards.registered_awards[name] + if def.description == nil and tdef.getDefaultDescription then + def.description = tdef:getDefaultDescription() + end +end + + +-- This function is called whenever a target condition is met. +-- It checks if a player already has that award, and if they do not, +-- it gives it to them +---------------------------------------------- +--awards.unlock(name, award) +-- name - the name of the player +-- award - the name of the award to give +function awards.unlock(name, award) + -- Access Player Data + local data = awards.player(name) + local awdef = awards.registered_awards[award] + assert(awdef, "Unable to unlock an award which doesn't exist!") + + if data.disabled or + (data.unlocked[award] and data.unlocked[award] == award) then + return + end + + if not awdef:can_unlock(data) then + minetest.log("warning", "can_unlock returned false in unlock of " .. + award .. " for " .. name) + return + end + + -- Unlock Award + minetest.log("action", name.." has unlocked award "..name) + data.unlocked[award] = award + awards.save() + + -- Give Prizes + if awdef and awdef.prizes then + for i = 1, #awdef.prizes do + local itemstack = ItemStack(awdef.prizes[i]) + if not itemstack:is_empty() then + local receiverref = minetest.get_player_by_name(name) + if receiverref then + receiverref:get_inventory():add_item("main", itemstack) + end + end + end + end + + -- Run callbacks + if awdef.on_unlock and awdef.on_unlock(name, awdef) then + return + end + for _, callback in pairs(awards.on_unlock) do + if callback(name, awdef) then + return + end + end + + -- Get Notification Settings + local title = awdef.title or award + local desc = awdef.description or "" + local background = awdef.background or "awards_bg_default.png" + local icon = awdef.icon or "awards_unknown.png" + local sound = awdef.sound + if sound == nil then + -- Explicit check for nil because sound could be `false` to disable it + sound = {name="awards_got_generic", gain=0.25} + end + + -- Do Notification + if sound then + -- Enforce sound delay to prevent sound spamming + local lastsound = data.lastsound + if lastsound == nil or os.difftime(os.time(), lastsound) >= 1 then + minetest.sound_play(sound, {to_player=name}) + data.lastsound = os.time() + end + end + + if awards.show_mode == "chat" then + local chat_announce + if awdef.secret then + chat_announce = S("Secret Award Unlocked: %s") + else + chat_announce = S("Award Unlocked: %s") + end + -- use the chat console to send it + minetest.chat_send_player(name, string.format(chat_announce, title)) + if desc~="" then + minetest.chat_send_player(name, desc) + end + else + local player = minetest.get_player_by_name(name) + local one = player:hud_add({ + hud_elem_type = "image", + name = "award_bg", + scale = {x = 2, y = 1}, + text = background, + position = {x = 0.5, y = 0.05}, + offset = {x = 0, y = 138}, + alignment = {x = 0, y = -1} + }) + local hud_announce + if awdef.secret then + hud_announce = S("Secret Award Unlocked!") + else + hud_announce = S("Award Unlocked!") + end + local two = player:hud_add({ + hud_elem_type = "text", + name = "award_au", + number = 0xFFFFFF, + scale = {x = 100, y = 20}, + text = hud_announce, + position = {x = 0.5, y = 0.05}, + offset = {x = 0, y = 45}, + alignment = {x = 0, y = -1} + }) + local three = player:hud_add({ + hud_elem_type = "text", + name = "award_title", + number = 0xFFFFFF, + scale = {x = 100, y = 20}, + text = title, + position = {x = 0.5, y = 0.05}, + offset = {x = 0, y = 100}, + alignment = {x = 0, y = -1} + }) + local four = player:hud_add({ + hud_elem_type = "image", + name = "award_icon", + scale = {x = 4, y = 4}, + text = icon, + position = {x = 0.5, y = 0.05}, + offset = {x = -200.5, y = 126}, + alignment = {x = 0, y = -1} + }) + minetest.after(4, function() + local player2 = minetest.get_player_by_name(name) + if player2 then + player2:hud_remove(one) + player2:hud_remove(two) + player2:hud_remove(three) + player2:hud_remove(four) + end + end) + end +end diff --git a/src/api_triggers.lua b/src/api_triggers.lua new file mode 100644 index 0000000..9311bcf --- /dev/null +++ b/src/api_triggers.lua @@ -0,0 +1,215 @@ +-- Copyright (c) 2013-18 rubenwardy. MIT. + +local S, NS = awards.gettext, awards.ngettext + +awards.registered_awards = {} +awards.on = {} +awards.on_unlock = {} + +local default_def = {} + +function default_def:run_callbacks(player, data, table_func) + for i = 1, #self.on do + local res = nil + local entry = self.on[i] + if type(entry) == "function" then + res = entry(player, data) + elseif type(entry) == "table" and entry.award then + res = table_func(entry) + end + + if res then + awards.unlock(player:get_player_name(), res) + end + end +end + +function awards.register_trigger(tname, tdef) + assert(type(tdef) == "table", + "Passing a callback to register_trigger is not supported in 3.0") + + tdef.name = tname + for key, value in pairs(default_def) do + tdef[key] = value + end + + if tdef.type == "counted" then + local old_reg = tdef.on_register + + function tdef:on_register(def) + local tmp = { + award = def.name, + target = def.trigger.target, + } + tdef.register(tmp) + + function def.getProgress(_, data) + local done = data[tname] or 0 + return { + perc = done / tmp.target, + label = S(tdef.progress, done, tmp.target), + } + end + + function def.getDefaultDescription(_) + local n = def.trigger.target + return NS(tdef.auto_description[1], tdef.auto_description[2], n, n) + end + + if old_reg then + return old_reg(tdef, def) + end + end + + function tdef.notify(player) + assert(player and player.is_player and player:is_player()) + local name = player:get_player_name() + local data = awards.player(name) + + -- Increment counter + local currentVal = (data[tname] or 0) + 1 + data[tname] = currentVal + + tdef:run_callbacks(player, data, function(entry) + if entry.target and entry.award and currentVal and + currentVal >= entry.target then + return entry.award + end + end) + end + + awards["notify_" .. tname] = tdef.notify + + elseif tdef.type == "counted_key" then + if tdef.key_is_item then + tdef.watched_groups = {} + end + + -- On award register + local old_reg = tdef.on_register + function tdef:on_register(def) + -- Register trigger + local tmp = { + award = def.name, + key = tdef:get_key(def), + target = def.trigger.target, + } + tdef.register(tmp) + + -- If group, add it to watch list + if tdef.key_is_item and tmp.key and tmp.key:sub(1, 6) == "group:" then + tdef.watched_groups[tmp.key:sub(7, #tmp.key)] = true + end + + -- Called to get progress values and labels + function def.getProgress(_, data) + local done + data[tname] = data[tname] or {} + if tmp.key then + done = data[tname][tmp.key] or 0 + else + done = data[tname].__total or 0 + end + return { + perc = done / tmp.target, + label = S(tdef.progress, done, tmp.target), + } + end + + -- Build description if none is specificed by the award + function def.getDefaultDescription(_) + local n = def.trigger.target + if tmp.key then + local nname = tmp.key + return NS(tdef.auto_description[1], + tdef.auto_description[2], n, n, nname) + else + return NS(tdef.auto_description_total[1], + tdef.auto_description_total[2], n, n) + end + end + + -- Call on_register in trigger type definition + if old_reg then + return old_reg(tdef, def) + end + end + + function tdef.notify(player, key, n) + n = n or 1 + + if tdef.key_is_item and key:sub(1, 6) ~= "group:" then + local itemdef = minetest.registered_items[key] + if itemdef then + for groupname, _ in pairs(itemdef.groups or {}) do + if tdef.watched_groups[groupname] then + tdef.notify(player, "group:" .. groupname, n) + end + end + end + end + + assert(player and player.is_player and player:is_player() and key) + local name = player:get_player_name() + local data = awards.player(name) + + -- Increment counter + data[tname] = data[tname] or {} + local currentVal = (data[tname][key] or 0) + n + data[tname][key] = currentVal + if key:sub(1, 6) ~= "group:" then + data[tname].__total = (data[tname].__total or 0) + n + end + + tdef:run_callbacks(player, data, function(entry) + local current + if entry.key == key then + current = currentVal + elseif entry.key == nil then + current = data[tname].__total + else + return + end + + if current >= entry.target then + return entry.award + end + end) + end + + awards["notify_" .. tname] = tdef.notify + + elseif tdef.type and tdef.type ~= "custom" then + error("Unrecognised trigger type " .. tdef.type) + end + + awards.registered_triggers[tname] = tdef + + tdef.on = {} + tdef.register = function(func) + table.insert(tdef.on, func) + end + + -- Backwards compat + awards.on[tname] = tdef.on + awards['register_on_' .. tname] = tdef.register + return tdef +end + +function awards.increment_item_counter(data, field, itemname, count) + itemname = minetest.registered_aliases[itemname] or itemname + data[field][itemname] = (data[field][itemname] or 0) + 1 +end + +function awards.get_item_count(data, field, itemname) + itemname = minetest.registered_aliases[itemname] or itemname + return data[field][itemname] or 0 +end + +function awards.get_total_keyed_count(data, field) + return data[field].__total or 0 +end + +function awards.register_on_unlock(func) + table.insert(awards.on_unlock, func) +end diff --git a/src/awards.lua b/src/awards.lua new file mode 100644 index 0000000..4dc6681 --- /dev/null +++ b/src/awards.lua @@ -0,0 +1,960 @@ +-- Copyright (c) 2013-18 rubenwardy and Wuzzy. MIT. + +local S = awards.gettext + +-- Saint-Maclou +if minetest.get_modpath("moreblocks") then + awards.register_award("award_saint_maclou",{ + title = S("Saint-Maclou"), + description = S("Place 20 coal checkers."), + icon = "awards_novicebuilder.png", + trigger = { + type = "place", + node = "moreblocks:coal_checker", + target = 20 + } + }) + + -- Castorama + awards.register_award("award_castorama",{ + title = S("Castorama"), + description = S("Place 20 iron checkers."), + icon = "awards_novicebuilder.png", + trigger = { + type = "place", + node = "moreblocks:iron_checker", + target = 20 + } + }) + + -- Sam the Trapper + awards.register_award("award_sam_the_trapper",{ + title = S("Sam the Trapper"), + description = S("Place 2 trap stones."), + icon = "awards_novicebuilder.png", + trigger = { + type = "place", + node = "moreblocks:trap_stone", + target = 2 + } + }) +end + +-- This award can't be part of Unified Inventory, it would make a circular dependency +if minetest.get_modpath("unified_inventory") then + if minetest.get_all_craft_recipes("unified_inventory:bag_large") ~= nil then + awards.register_award("awards_ui_bags", { + title = S("Backpacker"), + description = S("Craft 4 large bags."), + icon = "awards_ui_bags.png", + trigger = { + type = "craft", + item = "unified_inventory:bag_large", + target = 4 + } + }) + end +end + +if minetest.get_modpath("fire") then + awards.register_award("awards_pyro", { + title = S("Pyromaniac"), + description = S("Craft 8 times flint and steel."), + icon = "fire_flint_steel.png", + trigger = { + type = "craft", + item = "fire:flint_and_steel", + target = 8 + } + }) + if minetest.settings:get_bool("disable_fire") ~= true then + awards.register_award("awards_firefighter", { + title = S("Firefighter"), + description = S("Put out 1000 fires."), + icon = "awards_firefighter.png", + trigger = { + type = "dig", + node = "fire:basic_flame", + target = 1000 + } + }) + end +end + +if minetest.get_modpath("default") then + -- Light it up + awards.register_award("award_lightitup",{ + title = S("Light It Up"), + description = S("Place 100 torches."), + icon = "awards_novicebuilder.png^awards_level1.png", + difficulty = 0.01, + trigger = { + type = "place", + node = "default:torch", + target = 100 + } + }) + + -- Light ALL the things! + awards.register_award("award_well_lit",{ + title = S("Well Lit"), + icon = "awards_novicebuilder.png^awards_level2.png", + description = S("Place 1,000 torches."), + difficulty = 0.01, + trigger = { + type = "place", + node = "default:torch", + target = 1000 + } + }) + + awards.register_award("award_meselamp",{ + title = S("Really Well Lit"), + description = S("Craft 10 mese lamps."), + icon = "default_meselamp.png", + difficulty = 0.2, + trigger = { + type = "craft", + item = "default:meselamp", + target = 10 + } + }) + + awards.register_award("awards_stonebrick", { + title = S("Outpost"), + description = S("Craft 200 stone bricks."), + icon = "default_stone_brick.png^awards_level1.png", + difficulty = 0.08, + trigger = { + type = "craft", + item = "default:stonebrick", + target = 200 + } + }) + + awards.register_award("awards_stonebrick2", { + title = S("Watchtower"), + description = S("Craft 800 stone bricks."), + icon = "default_stone_brick.png^awards_level2.png", + difficulty = 0.08, + trigger = { + type = "craft", + item = "default:stonebrick", + target = 800 + } + }) + + awards.register_award("awards_stonebrick3", { + title = S("Fortress"), + description = S("Craft 3,200 stone bricks."), + icon = "default_stone_brick.png^awards_level3.png", + difficulty = 0.08, + trigger = { + type = "craft", + item = "default:stonebrick", + target = 3200 + } + }) + + awards.register_award("awards_desert_stonebrick", { + title = S("Desert Dweller"), + description = S("Craft 400 desert stone bricks."), + icon = "default_desert_stone_brick.png", + difficulty = 0.09, + trigger = { + type = "craft", + item = "default:desert_stonebrick", + target = 400 + } + }) + + awards.register_award("awards_desertstonebrick", { + title = S("Pharaoh"), + description = S("Craft 100 sandstone bricks."), + icon = "default_sandstone_brick.png", + difficulty = 0.09, + trigger = { + type = "craft", + item = "default:sandstonebrick", + target = 100 + } + }) + + awards.register_award("awards_bookshelf", { + title = S("Little Library"), + description = S("Craft 7 bookshelves."), + icon = "default_bookshelf.png", + difficulty = 0.2, + trigger = { + type = "craft", + item = "default:bookshelf", + target = 7 + } + }) + + awards.register_award("awards_obsidian", { + title = S("Lava and Water"), + description = S("Mine your first obsidian."), + icon = "default_obsidian.png^awards_level1.png", + background = "awards_bg_mining.png", + difficulty = 1.5, + trigger = { + type = "dig", + node = "default:obsidian", + target = 1 + } + }) + + -- Obsessed with Obsidian + awards.register_award("award_obsessed_with_obsidian",{ + title = S("Obsessed with Obsidian"), + description = S("Mine 50 obsidian."), + icon = "default_obsidian.png^awards_level2.png", + background = "awards_bg_mining.png", + difficulty = 1.5, + trigger = { + type = "dig", + node = "default:obsidian", + target = 50 + } + }) + + -- Proof that player has found lava + awards.register_award("award_lavaminer",{ + title = S("Lava Miner"), + description = S("Mine any block while being very close to lava."), + background = "awards_bg_mining.png", + icon = "default_lava.png", + difficulty = 1, + }) + awards.register_on_dig(function(player,data) + local pos = player:get_pos() + if pos and (minetest.find_node_near(pos, 1, "default:lava_source") or + minetest.find_node_near(pos, 1, "default:lava_flowing")) then + return "award_lavaminer" + end + return nil + end) + + -- On the way + awards.register_award("award_on_the_way", { + title = S("On The Way"), + description = S("Place 100 rails."), + icon = "carts_rail_straight.png", + difficulty = 0.1, + trigger = { + type = "place", + node = "default:rail", + target = 100 + } + }) + + awards.register_award("award_lumberjack_firstday", { + title = S("First Day in the Woods"), + description = S("Dig 6 tree blocks."), + icon = "default_tree.png^awards_level1.png", + difficulty = 0.03, + trigger = { + type = "dig", + node = "default:tree", + target = 6 + } + }) + + -- Lumberjack + awards.register_award("award_lumberjack", { + title = S("Lumberjack"), + description = S("Dig 36 tree blocks."), + icon = "default_tree.png^awards_level2.png", + difficulty = 0.03, + trigger = { + type = "dig", + node = "default:tree", + target = 36 + } + }) + + -- Semi-pro Lumberjack + awards.register_award("award_lumberjack_semipro", { + title = S("Semi-pro Lumberjack"), + description = S("Dig 216 tree blocks."), + icon = "default_tree.png^awards_level3.png", + difficulty = 0.03, + trigger = { + type = "dig", + node = "default:tree", + target = 216 + } + }) + + -- Professional Lumberjack + awards.register_award("award_lumberjack_professional", { + title = S("Professional Lumberjack"), + description = S("Dig 1,296 tree blocks."), + icon = "default_tree.png^awards_level4.png", + difficulty = 0.03, + trigger = { + type = "dig", + node = "default:tree", + target = 1296 + } + }) + + -- Junglebaby + awards.register_award("award_junglebaby", { + title = S("Junglebaby"), + description = S("Dig 100 jungle tree blocks."), + icon = "default_jungletree.png^awards_level1.png", + difficulty = 0.05, + trigger = { + type = "dig", + node = "default:jungletree", + target = 100 + } + }) + + -- Jungleman + awards.register_award("award_jungleman", { + title = S("Jungleman"), + description = S("Dig 1,000 jungle tree blocks."), + icon = "default_jungletree.png^awards_level2.png", + difficulty = 0.05, + trigger = { + type = "dig", + node = "default:jungletree", + target = 1000 + } + }) + + -- Found some Mese! + awards.register_award("award_mesefind", { + title = S("First Mese Find"), + description = S("Mine your first mese ore."), + icon = "default_stone.png^default_mineral_mese.png", + background = "awards_bg_mining.png", + difficulty = 1, + trigger = { + type = "dig", + node = "default:stone_with_mese", + target = 1 + } + }) + + -- Mese Block + awards.register_award("award_meseblock", { + secret = true, + title = S("Mese Mastery"), + description = S("Mine a mese block."), + icon = "default_mese_block.png", + background = "awards_bg_mining.png", + difficulty = 1.1, + trigger = { + type = "dig", + node = "default:mese", + target = 1 + } + }) + + -- You're a copper + awards.register_award("award_youre_a_copper", { + title = S("You’re a copper"), + description = S("Dig 1,000 copper ores."), + icon = "default_stone.png^default_mineral_copper.png", + background = "awards_bg_mining.png", + difficulty = 0.2, + trigger = { + type = "dig", + node = "default:stone_with_copper", + target = 1000 + } + }) + + -- Mini Miner + awards.register_award("award_mine2", { + title = S("Mini Miner"), + description = S("Dig 100 stone blocks."), + icon = "awards_miniminer.png^awards_level1.png", + background = "awards_bg_mining.png", + difficulty = 0.02, + trigger = { + type = "dig", + node = "default:stone", + target = 100 + } + }) + + -- Hardened Miner + awards.register_award("award_mine3", { + title = S("Hardened Miner"), + description = S("Dig 1,000 stone blocks."), + icon = "awards_miniminer.png^awards_level2.png", + background = "awards_bg_mining.png", + difficulty = 0.02, + trigger = { + type = "dig", + node = "default:stone", + target = 1000 + } + }) + + -- Master Miner + awards.register_award("award_mine4", { + title = S("Master Miner"), + description = S("Dig 10,000 stone blocks."), + icon = "awards_miniminer.png^awards_level3.png", + background = "awards_bg_mining.png", + difficulty = 0.02, + trigger = { + type = "dig", + node = "default:stone", + target = 10000 + } + }) + + -- Marchand de sable + awards.register_award("award_marchand_de_sable", { + title = S("Marchand De Sable"), + description = S("Dig 1,000 sand."), + icon = "default_sand.png", + background = "awards_bg_mining.png", + difficulty = 0.05, + trigger = { + type = "dig", + node = "default:sand", + target = 1000 + } + }) + + awards.register_award("awards_crafter_of_sticks", { + title = S("Crafter of Sticks"), + description = S("Craft 100 sticks."), + icon = "default_stick.png", + difficulty = 0.01, + trigger = { + type = "craft", + item = "default:stick", + target = 100 + } + }) + + awards.register_award("awards_junglegrass", { + title = S("Jungle Discoverer"), + description = S("Mine your first jungle grass."), + icon = "default_junglegrass.png", + difficulty = 0.009, + trigger = { + type = "dig", + node = "default:junglegrass", + target = 1 + } + }) + + awards.register_award("awards_grass", { + title = S("Grasslands Discoverer"), + description = S("Mine some grass."), + icon = "default_grass_3.png", + difficulty = 0.009, + trigger = { + type = "dig", + node = "default:grass_1", + target = 1 + } + }) + + awards.register_award("awards_dry_grass", { + title = S("Savannah Discoverer"), + description = S("Mine some dry grass."), + icon = "default_dry_grass_3.png", + difficulty = 0.009, + trigger = { + type = "dig", + node = "default:dry_grass_3", + target = 1 + } + }) + + awards.register_award("awards_cactus", { + title = S("Desert Discoverer"), + description = S("Mine your first cactus."), + icon = "default_cactus_side.png", + difficulty = 0.03, + trigger = { + type = "dig", + node = "default:cactus", + target = 1 + } + }) + + awards.register_award("awards_dry_shrub", { + title = S("Far Lands"), + description = S("Mine your first dry shrub."), + icon = "default_dry_shrub.png", + difficulty = 0.009, + trigger = { + type = "dig", + node = "default:dry_shrub", + target = 1 + } + }) + + awards.register_award("awards_ice", { + title = S("Glacier Discoverer"), + description = S("Mine your first ice."), + icon = "default_ice.png", + difficulty = 0.02, + trigger = { + type = "dig", + node = "default:ice", + target = 1 + } + }) + + -- Proof that player visited snowy lands + awards.register_award("awards_snowblock", { + title = S("Very Simple Snow Man"), + description = S("Place two snow blocks."), + icon = "default_snow.png", + difficulty = 0.02, + trigger = { + type = "place", + node = "default:snowblock", + target = 2 + } + }) + + awards.register_award("awards_gold_ore", { + title = S("First Gold Find"), + description = S("Mine your first gold ore."), + icon = "default_stone.png^default_mineral_gold.png^awards_level1.png", + background = "awards_bg_mining.png", + difficulty = 0.9, + trigger = { + type = "dig", + node = "default:stone_with_gold", + target = 1 + } + }) + + awards.register_award("awards_gold_rush", { + title = S("Gold Rush"), + description = S("Mine 45 gold ores."), + icon = "default_stone.png^default_mineral_gold.png^awards_level2.png", + background = "awards_bg_mining.png", + difficulty = 0.9, + trigger = { + type = "dig", + node = "default:stone_with_gold", + target = 45 + } + }) + + awards.register_award("awards_diamond_ore", { + title = S("Wow, I am Diamonds!"), + description = S("Mine your first diamond ore."), + icon = "default_stone.png^default_mineral_diamond.png^awards_level1.png", + difficulty = 1, + trigger = { + type = "dig", + node = "default:stone_with_diamond", + target = 1 + } + }) + + awards.register_award("awards_diamond_rush", { + title = S("Girl's Best Friend"), + description = S("Mine 18 diamond ores."), + icon = "default_stone.png^default_mineral_diamond.png^awards_level2.png", + background = "awards_bg_mining.png", + difficulty = 1, + trigger = { + type = "dig", + node = "default:stone_with_diamond", + target = 18 + } + }) + + awards.register_award("awards_diamondblock", { + title = S("Hardest Block on Earth"), + description = S("Craft a diamond block."), + icon = "default_diamond_block.png", + difficulty = 1.1, + trigger = { + type = "craft", + item = "default:diamondblock", + target = 1 + } + }) + + awards.register_award("awards_mossycobble", { + title = S("In the Dungeon"), + description = S("Mine a mossy cobblestone."), + icon = "default_mossycobble.png", + difficulty = 0.9, + trigger = { + type = "dig", + node = "default:mossycobble", + target = 1 + } + }) + + awards.register_award("award_furnace", { + title = S("Smelter"), + description = S("Craft 10 furnaces."), + icon = "default_furnace_front.png", + difficulty = 0.08, + trigger = { + type = "craft", + item= "default:furnace", + target = 10 + } + }) + + awards.register_award("award_chest", { + title = S("Treasurer"), + description = S("Craft 15 chests."), + icon = "default_chest_front.png", + difficulty = 0.08, + trigger = { + type = "craft", + item= "default:chest", + target = 15 + } + }) + + awards.register_award("award_chest2", { + title = S("Bankier"), + description = S("Craft 30 locked chests."), + icon = "default_chest_lock.png", + difficulty = 0.08, + trigger = { + type = "craft", + item= "default:chest_locked", + target = 30 + } + }) + + awards.register_award("award_brick", { + title = S("Bricker"), + description = S("Craft 200 brick blocks."), + icon = "default_brick.png", + difficulty = 0.03, + trigger = { + type = "craft", + item= "default:brick", + target = 200 + } + }) + + awards.register_award("award_obsidianbrick", { + title = S("House of Obsidian"), + description = S("Craft 100 obsidian bricks."), + icon = "default_obsidian_brick.png", + difficulty = 0.4, + trigger = { + type = "craft", + item= "default:obsidianbrick", + target = 100 + } + }) + + awards.register_award("award_placestone", { + title = S("Build a Cave"), + description = S("Place 100 stone."), + icon = "default_stone.png", + difficulty = 0.1, + trigger = { + type = "place", + node = "default:stone", + target = 100 + } + }) + + awards.register_award("award_woodladder", { + title = S("Long Ladder"), + description = S("Place 400 wooden ladders."), + icon = "default_ladder_wood.png", + difficulty = 0.1, + trigger = { + type = "place", + node = "default:ladder_wood", + target = 400 + } + }) + + awards.register_award("award_steelladder", { + title = S("Industrial Age"), + description = S("Place 40 steel ladders."), + icon = "default_ladder_steel.png", + difficulty = 1, + trigger = { + type = "place", + node = "default:ladder_steel", + target = 40 + } + }) + + awards.register_award("award_apples", { + title = S("Yummy!"), + description = S("Eat 80 apples."), + icon = "default_apple.png", + difficulty = 0.1, + trigger = { + type = "eat", + item = "default:apple", + target = 80 + } + }) +end + +if minetest.get_modpath("vessels") then + awards.register_award("award_vessels_shelf", { + title = S("Glasser"), + icon = "vessels_shelf.png", + description = S("Craft 14 vessels shelves."), + trigger = { + type = "craft", + item= "vessels:shelf", + target = 14 + } + }) +end + +if minetest.get_modpath("farming") then + awards.register_award("awards_farmer", { + title = S("Farming Skills Acquired"), + description = S("Harvest a fully grown wheat plant."), + icon = "farming_wheat_8.png^awards_level1.png", + trigger = { + type = "dig", + node = "farming:wheat_8", + target = 1 + } + }) + awards.register_award("awards_farmer2", { + title = S("Field Worker"), + description = S("Harvest 25 fully grown wheat plants."), + icon = "farming_wheat_8.png^awards_level2.png", + trigger = { + type = "dig", + node = "farming:wheat_8", + target = 25 + } + }) + + awards.register_award("awards_farmer3", { + title = S("Aspiring Farmer"), + description = S("Harvest 125 fully grown wheat plants."), + icon = "farming_wheat_8.png^awards_level3.png", + trigger = { + type = "dig", + node = "farming:wheat_8", + target = 125 + } + }) + + awards.register_award("awards_farmer4", { + title = S("Wheat Magnate"), + description = S("Harvest 625 fully grown wheat plants."), + icon = "farming_wheat_8.png^awards_level4.png", + trigger = { + type = "dig", + node = "farming:wheat_8", + target = 625 + } + }) + + awards.register_award("award_bread", { + title = S("Baker"), + description = S("Eat 10 loaves of bread."), + icon = "farming_bread.png", + trigger = { + type = "eat", + item = "farming:bread", + target = 10 + } + }) + +end + +if minetest.get_modpath("wool") and minetest.get_modpath("farming") then + awards.register_award("awards_wool", { + title = S("Wool Over Your Eyes"), + description = S("Craft 250 white wool."), + icon = "wool_white.png", + trigger = { + type = "craft", + item = "wool:white", + target = 250 + } + }) +end + +if minetest.get_modpath("beds") then + awards.register_award("award_bed", { + title = S("Hotelier"), + description = S("Craft 15 fancy beds."), + icon = "beds_bed_fancy.png", + trigger = { + type = "craft", + item= "beds:fancy_bed_bottom", + target = 15 + } + }) +end + +if minetest.get_modpath("stairs") then + awards.register_award("award_stairs_goldblock", { + title = S("Filthy Rich"), + description = S("Craft 24 gold block stairs."), + icon = "default_gold_block.png", + trigger = { + type = "craft", + item= "stairs:stair_goldblock", + target = 24 + } + }) +end + +if minetest.get_modpath("dye") then + awards.register_award("awards_dye_red", { + title = S("Roses Are Red"), + description = S("Craft 400 red dyes."), + icon = "dye_red.png", + trigger = { + type = "craft", + item = "dye:red", + target = 400 + } + }) + + awards.register_award("awards_dye_yellow", { + title = S("Dandelions are Yellow"), + description = S("Craft 400 yellow dyes."), + icon = "dye_yellow.png", + trigger = { + type = "craft", + item = "dye:yellow", + target = 400 + } + }) + + awards.register_award("awards_dye_blue", { + title = S("Geraniums are Blue"), + description = S("Craft 400 blue dyes."), + icon = "dye_blue.png", + trigger = { + type = "craft", + item= "dye:blue", + target = 400 + } + }) + + awards.register_award("awards_dye_white", { + title = S("White Color Stock"), + description = S("Craft 100 white dyes."), + icon = "dye_white.png", + trigger = { + type = "craft", + item= "dye:white", + target = 100 + } + }) +end + +if minetest.get_modpath("flowers") then + awards.register_award("awards_brown_mushroom1", { + title = S("Tasty Mushrooms"), + description = S("Eat 3 brown mushrooms."), + icon = "flowers_mushroom_brown.png^awards_level1.png", + trigger = { + type = "eat", + item= "flowers:mushroom_brown", + target = 3, + } + }) + awards.register_award("awards_brown_mushroom2", { + title = S("Mushroom Lover"), + description = S("Eat 33 brown mushrooms."), + icon = "flowers_mushroom_brown.png^awards_level2.png", + trigger = { + type = "eat", + item= "flowers:mushroom_brown", + target = 33, + } + }) + awards.register_award("awards_brown_mushroom3", { + title = S("Underground Mushroom Farmer"), + description = S("Eat 333 brown mushrooms."), + icon = "flowers_mushroom_brown.png^awards_level3.png", + trigger = { + type = "eat", + item= "flowers:mushroom_brown", + target = 333, + } + }) +end + +-- This ensures the following code is executed after all items have been registered +minetest.after(0, function() + -- Check whether there is at least one node which can be built by the player + local building_is_possible = false + for _, def in pairs(minetest.registered_nodes) do + if (def.description and def.pointable ~= false and not def.groups.not_in_creative_inventory) then + building_is_possible = true + break + end + end + + -- The following awards require at least one node which can be built + if not building_is_possible then + return + end + + awards.register_award("awards_builder1", { + title = S("Builder"), + icon = "awards_house.png^awards_level1.png", + trigger = { + type = "place", + target = 1000, + }, + }) + awards.register_award("awards_builder2", { + title = S("Constructor"), + icon = "awards_house.png^awards_level2.png", + trigger = { + type = "place", + target = 5000, + }, + }) + awards.register_award("awards_builder3", { + title = S("Architect"), + icon = "awards_house.png^awards_level3.png", + trigger = { + type = "place", + target = 10000, + }, + }) + awards.register_award("awards_builder4", { + title = S("Master Architect"), + icon = "awards_house.png^awards_level4.png", + trigger = { + type = "place", + target = 25000, + }, + }) +end) + +if minetest.get_modpath("nyancat") then + -- Found a Nyan cat! + awards.register_award("award_nyanfind", { + secret = true, + title = S("A Cat in a Pop-Tart?!"), + description = S("Mine a nyan cat."), + icon = "nyancat_front.png", + trigger = { + type = "dig", + node = "nyancat:nyancat", + target = 1 + } + }) +end diff --git a/src/chat_commands.lua b/src/chat_commands.lua new file mode 100644 index 0000000..9c1580e --- /dev/null +++ b/src/chat_commands.lua @@ -0,0 +1,61 @@ +-- Copyright (c) 2013-18 rubenwardy. MIT. + +local S = awards.gettext + +minetest.register_chatcommand("awards", { + params = S("[c|clear|disable|enable]"), + description = S("Show, clear, disable or enable your awards"), + func = function(name, param) + if param == "clear" then + awards.clear_player(name) + minetest.chat_send_player(name, + S("All your awards and statistics have been cleared. You can now start again.")) + elseif param == "disable" then + awards.disable(name) + minetest.chat_send_player(name, S("You have disabled awards.")) + elseif param == "enable" then + awards.enable(name) + minetest.chat_send_player(name, S("You have enabled awards.")) + elseif param == "c" then + awards.show_to(name, name, nil, true) + else + awards.show_to(name, name, nil, false) + end + + if (param == "disable" or param == "enable") and minetest.global_exists("sfinv") then + local player = minetest.get_player_by_name(name) + if player then + sfinv.set_player_inventory_formspec(player) + end + end + end +}) + +minetest.register_chatcommand("awd", { + params = S("<award ID>"), + description = S("Show details of an award"), + func = function(name, param) + local def = awards.registered_awards[param] + if def then + minetest.chat_send_player(name, string.format(S("%s: %s"), def.title, def.description)) + else + minetest.chat_send_player(name, S("Award not found.")) + end + end +}) + +minetest.register_chatcommand("awpl", { + privs = { + server = true + }, + params = S("<name>"), + description = S("Get the awards statistics for the given player or yourself"), + func = function(name, param) + if not param or param == "" then + param = name + end + minetest.chat_send_player(name, param) + local player = awards.player(param) + minetest.chat_send_player(name, dump(player)) + end +}) diff --git a/src/data.lua b/src/data.lua new file mode 100644 index 0000000..27ae5e3 --- /dev/null +++ b/src/data.lua @@ -0,0 +1,111 @@ + +local storage = minetest.get_mod_storage() +local __player_data + +-- Table Save Load Functions +function awards.save() + storage:set_string("player_data", minetest.write_json(__player_data)) +end + +local function convert_data() + minetest.log("warning", "Importing awards data from previous version") + + local old_players = __player_data + __player_data = {} + for name, data in pairs(old_players) do + while name.name do + name = name.name + end + data.name = name + print("Converting data for " .. name) + + -- Just rename counted + local counted = { + chats = "chat", + deaths = "death", + joins = "join", + } + for from, to in pairs(counted) do + data[to] = data[from] + data[from] = nil + end + + data.death = { + unknown = data.death, + __total = data.death, + } + + -- Convert item db to new format + local counted_items = { + count = "dig", + place = "place", + craft = "craft", + } + for from, to in pairs(counted_items) do + local ret = {} + + local count = 0 + if data[from] then + for modname, items in pairs(data[from]) do + for itemname, value in pairs(items) do + itemname = modname .. ":" .. itemname + local key = minetest.registered_aliases[itemname] or itemname + ret[key] = value + count = count + value + end + end + end + + ret.__total = count + data[from] = nil + data[to] = ret + end + + __player_data[name] = data + end +end + +function awards.load() + local old_save_path = minetest.get_worldpath().."/awards.txt" + local file = io.open(old_save_path, "r") + if file then + local table = minetest.deserialize(file:read("*all")) + if type(table) == "table" then + __player_data = table + convert_data() + else + __player_data = {} + end + file:close() + os.rename(old_save_path, minetest.get_worldpath().."/awards.bk.txt") + awards.save() + else + __player_data = minetest.parse_json(storage:get_string("player_data")) or {} + end +end + +function awards.player(name) + assert(type(name) == "string") + local data = __player_data[name] or {} + __player_data[name] = data + + data.name = data.name or name + data.unlocked = data.unlocked or {} + return data +end + +function awards.player_or_nil(name) + return __player_data[name] +end + +function awards.enable(name) + awards.player(name).disabled = nil +end + +function awards.disable(name) + awards.player(name).disabled = true +end + +function awards.clear_player(name) + __player_data[name] = {} +end diff --git a/src/gui.lua b/src/gui.lua new file mode 100644 index 0000000..cfc82af --- /dev/null +++ b/src/gui.lua @@ -0,0 +1,269 @@ +-- Copyright (c) 2013-18 rubenwardy. MIT. + +local S = awards.gettext + +local function order_awards(name) + local hash_is_unlocked = {} + local retval = {} + + local data = awards.player(name) + if data and data.unlocked then + for awardname, _ in pairs(data.unlocked) do + local def = awards.registered_awards[awardname] + if def then + hash_is_unlocked[awardname] = true + local score = -100000 + + local difficulty = def.difficulty or 1 + if def.trigger and def.trigger.target then + difficulty = difficulty * def.trigger.target + end + score = score + difficulty + + retval[#retval + 1] = { + name = awardname, + def = def, + unlocked = true, + started = true, + score = score, + } + end + end + end + + for _, def in pairs(awards.registered_awards) do + if not hash_is_unlocked[def.name] and def:can_unlock(data) then + local started = false + local score = def.difficulty or 1 + if def.secret then + score = 1000000 + elseif def.trigger and def.trigger.target and def.getProgress then + local progress = def:getProgress(data).perc + score = score * (1 - progress) * def.trigger.target + if progress < 0.001 then + score = score + 100 + else + started = true + end + else + score = 100 + end + + retval[#retval + 1] = { + name = def.name, + def = def, + unlocked = false, + started = started, + score = score, + } + end + end + + table.sort(retval, function(a, b) + return a.score < b.score + end) + return retval +end + +function awards.get_formspec(name, to, sid) + local formspec = "" + local awards_list = order_awards(name) + local data = awards.player(name) + + if #awards_list == 0 then + formspec = formspec .. "label[3.9,1.5;"..minetest.formspec_escape(S("Error: No achivements available.")).."]" + formspec = formspec .. "button_exit[4.2,2.3;3,1;close;"..minetest.formspec_escape(S("OK")).."]" + return formspec + end + sid = awards_list[sid] and sid or 1 + + -- Sidebar + local sitem = awards_list[sid] + local sdef = sitem.def + if sdef and sdef.secret and not sitem.unlocked then + formspec = formspec .. "label[1,2.75;".. + minetest.formspec_escape(S("(Secret Award)")).."]".. + "image[1,0;3,3;awards_unknown.png]" + if sdef and sdef.description then + formspec = formspec .. "textarea[0.25,3.25;4.8,1.7;;".. + minetest.formspec_escape( + S("Unlock this award to find out what it is."))..";]" + end + else + local title = sitem.name + if sdef and sdef.title then + title = sdef.title + end + local status = "%s" + if sitem.unlocked then + status = S("%s (unlocked)") + end + + formspec = formspec .. "textarea[0.5,3.1;4.8,1.45;;" .. + string.format(status, minetest.formspec_escape(title)) .. + ";]" + + if sdef and sdef.icon then + formspec = formspec .. "image[0.6,0;3,3;" .. sdef.icon .. "]" + end + local barwidth = 3.95 + local perc = nil + local label = nil + if sdef.getProgress and data then + local res = sdef:getProgress(data) + perc = res.perc + label = res.label + end + if perc then + if perc > 1 then + perc = 1 + end + formspec = formspec .. "background[0,8.24;" .. barwidth ..",0.4;awards_progress_gray.png;false]" + formspec = formspec .. "background[0,8.24;" .. (barwidth * perc) ..",0.4;awards_progress_green.png;false]" + if label then + formspec = formspec .. "label[1.6,8.15;" .. minetest.formspec_escape(label) .. "]" + end + end + if sdef and sdef.description then + formspec = formspec .. "box[-0.05,3.75;3.9,4.2;#000]" + formspec = formspec .. "textarea[0.25,3.75;3.9,4.2;;" .. + minetest.formspec_escape(sdef.description) .. ";]" + end + end + + -- Create list box + formspec = formspec .. "textlist[4,0;3.8,8.6;awards;" + local first = true + for _, award in pairs(awards_list) do + local def = award.def + if def then + if not first then + formspec = formspec .. "," + end + first = false + + if def.secret and not award.unlocked then + formspec = formspec .. "#707070"..minetest.formspec_escape(S("(Secret Award)")) + else + local title = award.name + if def and def.title then + title = def.title + end + -- title = title .. " [" .. award.score .. "]" + if award.unlocked then + formspec = formspec .. minetest.formspec_escape(title) + elseif award.started then + formspec = formspec .. "#c0c0c0".. minetest.formspec_escape(title) + else + formspec = formspec .. "#a0a0a0".. minetest.formspec_escape(title) + end + end + end + end + return formspec .. ";"..sid.."]" +end + + +function awards.show_to(name, to, sid, text) + if name == "" or name == nil then + name = to + end + local data = awards.player(to) + if name == to and data.disabled then + minetest.chat_send_player(name, S("You've disabled awards. Type /awards enable to reenable.")) + return + end + if text then + local awards_list = order_awards(name) + if #awards_list == 0 then + minetest.chat_send_player(to, S("Error: No award available.")) + return + elseif not data or not data.unlocked then + minetest.chat_send_player(to, S("You have not unlocked any awards.")) + return + end + minetest.chat_send_player(to, string.format(S("%s’s awards:"), name)) + + for str, _ in pairs(data.unlocked) do + local def = awards.registered_awards[str] + if def then + if def.title then + if def.description then + minetest.chat_send_player(to, string.format(S("%s: %s"), def.title, def.description)) + else + minetest.chat_send_player(to, def.title) + end + else + minetest.chat_send_player(to, str) + end + end + end + else + local deco = "" + if minetest.global_exists("default") then + deco = default.gui_bg .. default.gui_bg_img + end + -- Show formspec to user + minetest.show_formspec(to,"awards:awards", + "size[8,8.6]" .. deco .. + awards.get_formspec(name, to, sid)) + end +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname ~= "awards:awards" then + return false + end + if fields.quit then + return true + end + local name = player:get_player_name() + if fields.awards then + local event = minetest.explode_textlist_event(fields.awards) + if event.type == "CHG" then + awards.show_to(name, name, event.index, false) + end + end + + return true +end) + +if minetest.get_modpath("sfinv") then + sfinv.register_page("awards:awards", { + title = S("Awards"), + on_enter = function(self, player, context) + context.awards_idx = 1 + end, + is_in_nav = function(self, player, context) + local data = awards.player(player:get_player_name()) + return not data.disabled + end, + get = function(self, player, context) + local name = player:get_player_name() + return sfinv.make_formspec(player, context, + awards.get_formspec(name, name, context.awards_idx), + false) + end, + on_player_receive_fields = function(self, player, context, fields) + if fields.awards then + local event = minetest.explode_textlist_event(fields.awards) + if event.type == "CHG" then + context.awards_idx = event.index + sfinv.set_player_inventory_formspec(player, context) + end + end + end + }) +end + +if minetest.get_modpath("unified_inventory") ~= nil then + unified_inventory.register_button("awards", { + type = "image", + image = "awards_ui_icon.png", + tooltip = S("Awards"), + action = function(player) + local name = player:get_player_name() + awards.show_to(name, name, nil, false) + end, + }) +end diff --git a/src/intllib.lua b/src/intllib.lua new file mode 100644 index 0000000..c7af2c2 --- /dev/null +++ b/src/intllib.lua @@ -0,0 +1,44 @@ +-- Fallback functions for when `intllib` is not installed. +-- Code released under Unlicense <http://unlicense.org>. + +-- Get the latest version of this file at: +-- https://raw.githubusercontent.com/minetest-mods/intllib/master/lib/intllib.lua + +local function format(str, ...) + local args = { ... } + local function repl(escape, open, num, close) + if escape == "" then + local replacement = tostring(args[tonumber(num)]) + if open == "" then + replacement = replacement..close + end + return replacement + else + return "@"..open..num..close + end + end + return (str:gsub("(@?)@(%(?)(%d+)(%)?)", repl)) +end + +local gettext, ngettext +if minetest.get_modpath("intllib") then + if intllib.make_gettext_pair then + -- New method using gettext. + gettext, ngettext = intllib.make_gettext_pair() + else + -- Old method using text files. + gettext = intllib.Getter() + end +end + +-- Fill in missing functions. + +gettext = gettext or function(msgid, ...) + return format(msgid, ...) +end + +ngettext = ngettext or function(msgid, msgid_plural, n, ...) + return format(n==1 and msgid or msgid_plural, ...) +end + +return gettext, ngettext diff --git a/src/triggers.lua b/src/triggers.lua new file mode 100644 index 0000000..32a0b85 --- /dev/null +++ b/src/triggers.lua @@ -0,0 +1,141 @@ +-- 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. +-- + + +awards.register_trigger("chat", { + type = "counted", + progress = "@1/@2 chat messages", + auto_description = { "Send a chat message", "Chat @1 times" }, +}) +minetest.register_on_chat_message(function(name, message) + local player = minetest.get_player_by_name(name) + if not player or string.find(message, "/") then + return + end + + awards.notify_chat(player) +end) + + +awards.register_trigger("join", { + type = "counted", + progress = "@1/@2 joins", + auto_description = { "Join once", "Join @1 times" }, +}) +minetest.register_on_joinplayer(awards.notify_join) + + +awards.register_trigger("death", { + type = "counted_key", + progress = "@1/@2 deaths", + auto_description = { "Die once of @2", "Die @1 times of @2" }, + auto_description_total = { "Die @1 times.", "Mine @1 times" }, + get_key = function(self, def) + return def.trigger.reason + end, +}) +minetest.register_on_dieplayer(function(player, reason) + if reason then + reason = reason.type + else + reason = "unknown" + end + awards.notify_death(player, reason) +end) + + +awards.register_trigger("dig", { + type = "counted_key", + progress = "@1/@2 dug", + auto_description = { "Mine: @2", "Mine: @1×@2" }, + auto_description_total = { "Mine @1 block.", "Mine @1 blocks." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.node] or def.trigger.node + end, + key_is_item = true, +}) +minetest.register_on_dignode(function(pos, node, player) + if not player or not pos or not node then + return + end + + local node_name = node.name + node_name = minetest.registered_aliases[node_name] or node_name + awards.notify_dig(player, node_name) +end) + + +awards.register_trigger("place", { + type = "counted_key", + progress = "@1/@2 placed", + auto_description = { "Place: @2", "Place: @1×@2" }, + auto_description_total = { "Place @1 block.", "Place @1 blocks." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.node] or def.trigger.node + end, + key_is_item = true, +}) +minetest.register_on_placenode(function(pos, node, player) + if not player or not pos or not node then + return + end + + local node_name = node.name + node_name = minetest.registered_aliases[node_name] or node_name + awards.notify_place(player, node_name) +end) + + +awards.register_trigger("craft", { + type = "counted_key", + progress = "@1/@2 crafted", + auto_description = { "Craft: @2", "Craft: @1×@2" }, + auto_description_total = { "Craft @1 item", "Craft @1 items." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.item] or def.trigger.item + end, + key_is_item = true, +}) +minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv) + if not player or itemstack:is_empty() then + return + end + + local itemname = itemstack:get_name() + itemname = minetest.registered_aliases[itemname] or itemname + awards.notify_craft(player, itemname, itemstack:get_count()) +end) + + +awards.register_trigger("eat", { + type = "counted_key", + progress = "@1/@2 eaten", + auto_description = { "Eat @2", "Eat @1×@2" }, + auto_description_total = { "Eat @1 item", "Eat @1 items." }, + get_key = function(self, def) + return minetest.registered_aliases[def.trigger.item] or def.trigger.item + end, + key_is_item = true, +}) +minetest.register_on_item_eat(function(_, _, itemstack, player, _) + if not player or itemstack:is_empty() then + return + end + + local itemname = itemstack:get_name() + itemname = minetest.registered_aliases[itemname] or itemname + awards.notify_craft(player, itemname, itemstack:get_count()) +end) |