1
0
mirror of https://gitlab.com/rubenwardy/awards.git synced 2024-11-15 23:20:26 +01:00
awards/triggers.lua

338 lines
9.8 KiB
Lua
Raw Normal View History

2014-09-26 21:36:40 +02:00
-- AWARDS
2015-06-10 19:48:20 +02:00
--
-- 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.
--
2013-02-22 18:53:42 +01:00
2017-02-21 08:45:45 +01:00
local S, NS = awards.gettext, awards.ngettext
2015-06-10 20:15:16 +02:00
awards.register_trigger("dig", function(def)
2015-06-10 19:39:22 +02:00
local tmp = {
2015-06-10 20:15:16 +02:00
award = def.name,
2015-06-10 19:39:22 +02:00
node = def.trigger.node,
target = def.trigger.target,
2015-06-10 19:39:22 +02:00
}
table.insert(awards.on.dig, tmp)
2016-08-01 21:27:12 +02:00
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
2016-08-01 21:27:12 +02:00
return {
perc = itemcount / tmp.target,
2017-02-21 08:45:45 +01:00
label = S("@1/@2 dug", itemcount, tmp.target),
2016-08-01 21:27:12 +02:00
}
end
def.getDefaultDescription = function(self)
2017-02-21 08:45:45 +01:00
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
2017-02-21 08:45:45 +01:00
-- Translators: @1 is count, @2 is description.
return NS("Mine: @2", "Mine: @1×@2", n, n, nname)
else
2017-02-21 08:45:45 +01:00
return NS("Mine @1 block.", "Mine @1 blocks.", n, n)
end
end
2015-06-10 19:39:22 +02:00
end)
2015-06-10 20:15:16 +02:00
awards.register_trigger("place", function(def)
2015-06-10 19:39:22 +02:00
local tmp = {
2015-06-10 20:15:16 +02:00
award = def.name,
2015-06-10 19:39:22 +02:00
node = def.trigger.node,
target = def.trigger.target,
2015-06-10 19:39:22 +02:00
}
table.insert(awards.on.place, tmp)
2016-08-01 21:27:12 +02:00
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
2016-08-01 21:27:12 +02:00
return {
perc = itemcount / tmp.target,
2017-02-21 08:45:45 +01:00
label = S("@1/@2 placed", itemcount, tmp.target),
2016-08-01 21:27:12 +02:00
}
end
def.getDefaultDescription = function(self)
2017-02-21 08:45:45 +01:00
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
2017-02-21 08:45:45 +01:00
-- Translators: @1 is count, @2 is description.
return NS("Place: @2", "Place: @1×@2", n, n, nname)
else
2017-02-21 08:45:45 +01:00
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,
2017-02-21 08:45:45 +01:00
label = S("@1/@2 eaten", itemcount, tmp.target),
}
end
def.getDefaultDescription = function(self)
2017-02-21 08:45:45 +01:00
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
2017-02-21 08:45:45 +01:00
-- Translators: @1 is count, @2 is description.
return NS("Eat: @2", "Eat: @1×@2", n, n, iname)
else
2017-02-21 08:45:45 +01:00
return NS("Eat an item.", "Eat @1 items.", n, n)
end
end
2015-06-10 19:39:22 +02:00
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)
2016-08-01 21:27:12 +02:00
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
2016-08-01 21:27:12 +02:00
return {
perc = itemcount / tmp.target,
2017-02-21 08:45:45 +01:00
label = S("@1/@2 crafted", itemcount, tmp.target),
2016-08-01 21:27:12 +02:00
}
end
def.getDefaultDescription = function(self)
2017-02-21 08:45:45 +01:00
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
2017-02-21 08:45:45 +01:00
-- Translators: @1 is count, @2 is description.
return NS("Craft: @2", "Craft: @1×@2", n, n, iname)
else
2017-02-21 08:45:45 +01:00
return NS("Craft an item.", "Craft @1 items.", n)
end
2016-08-01 21:27:12 +02:00
end
end)
2015-06-10 19:39:22 +02:00
-- 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
2013-02-22 18:53:42 +01:00
-- Trigger Handles
minetest.register_on_dignode(function(pos, oldnode, digger)
2013-11-06 19:33:01 +01:00
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
2013-11-06 19:33:01 +01:00
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]
2018-04-04 16:35:56 +02:00
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
2013-02-23 13:02:02 +01:00
end
2013-11-06 19:33:01 +01:00
end
end)
2013-02-22 18:53:42 +01:00
end)
2015-06-10 19:39:22 +02:00
minetest.register_on_placenode(function(pos, node, digger)
2013-11-06 19:33:01 +01:00
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
2013-11-06 19:33:01 +01:00
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]
2018-04-04 16:35:56 +02:00
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]
2018-04-04 16:35:56 +02:00
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
2013-02-23 13:02:02 +01:00
end
2013-11-06 19:33:01 +01:00
end
end)
2013-02-22 21:38:01 +01:00
end)
2016-06-01 19:41:24 +02:00
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
2016-06-01 19:41:24 +02:00
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]
2018-04-04 16:35:56 +02:00
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
2016-06-01 19:41:24 +02:00
end
end
end)
2016-06-01 19:41:24 +02:00
end)
awards.register_trigger_counted("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)
2013-11-06 19:33:01 +01:00
end
end)
minetest.register_on_dieplayer(awards.notify_death)
awards.register_trigger_counted("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
2014-05-02 20:01:41 +02:00
end)
minetest.register_on_chat_message(function(name, message)
local player = minetest.get_player_by_name(name)
if not player or string.find(message, "/") then
2014-05-02 20:01:41 +02:00
return
end
awards.notify_chat(player)
end)
2015-06-10 19:39:22 +02:00
awards.register_trigger_counted("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
2013-02-23 16:13:21 +01:00
end)
minetest.register_on_joinplayer(awards.notify_join)