mirror of
https://gitlab.com/rubenwardy/awards.git
synced 2025-06-28 14:16:12 +02:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
7eeebf0f40 | |||
b689dc8eaf | |||
f5cf49af2e | |||
48bb2e9656 | |||
c79f13a4a3 |
37
api.lua
37
api.lua
@ -77,7 +77,7 @@ function awards.run_trigger_callbacks(player, data, trigger, table_func)
|
||||
end
|
||||
end
|
||||
|
||||
function awards.increment_item_counter(data, field, itemname)
|
||||
function awards.increment_item_counter(data, field, itemname, count)
|
||||
local name_split = string.split(itemname, ":")
|
||||
if #name_split ~= 2 then
|
||||
return false
|
||||
@ -91,13 +91,30 @@ function awards.increment_item_counter(data, field, itemname)
|
||||
awards.tbv(data[field], mod)
|
||||
awards.tbv(data[field][mod], item, 0)
|
||||
|
||||
data[field][mod][item] = data[field][mod][item] + 1
|
||||
data[field][mod][item] = data[field][mod][item] + (count or 1)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
function awards.get_item_count(data, field, itemname)
|
||||
local name_split = string.split(itemname, ":")
|
||||
if #name_split ~= 2 then
|
||||
return false
|
||||
end
|
||||
local mod = name_split[1]
|
||||
local item = name_split[2]
|
||||
|
||||
if data and field and mod and item then
|
||||
awards.assertPlayer(data)
|
||||
awards.tbv(data, field)
|
||||
awards.tbv(data[field], mod)
|
||||
awards.tbv(data[field][mod], item, 0)
|
||||
return data[field][mod][item]
|
||||
end
|
||||
end
|
||||
|
||||
function awards.register_on_unlock(func)
|
||||
table.insert(awards.on_unlock, func)
|
||||
end
|
||||
@ -284,6 +301,7 @@ awards.give_achievement = awards.unlock
|
||||
function awards.getFormspec(name, to, sid)
|
||||
local formspec = "size[11,5]"
|
||||
local listofawards = awards._order_awards(name)
|
||||
local playerdata = awards.players[name]
|
||||
|
||||
-- Sidebar
|
||||
if sid then
|
||||
@ -308,6 +326,21 @@ function awards.getFormspec(name, to, sid)
|
||||
if def and def.icon then
|
||||
formspec = formspec .. "image[1,0;3,3;" .. def.icon .. "]"
|
||||
end
|
||||
local barwidth = 4.6
|
||||
local perc = nil
|
||||
local label = nil
|
||||
if def.getProgress and playerdata then
|
||||
local res = def:getProgress(playerdata)
|
||||
perc = res.perc
|
||||
label = res.label
|
||||
end
|
||||
if perc then
|
||||
formspec = formspec .. "background[0,4.80;" .. barwidth ..",0.25;awards_progress_gray.png]"
|
||||
formspec = formspec .. "background[0,4.80;" .. (barwidth * perc) ..",0.25;awards_progress_green.png]"
|
||||
if label then
|
||||
formspec = formspec .. "label[1.75,4.63;" .. label .. "]"
|
||||
end
|
||||
end
|
||||
if def and def.description then
|
||||
formspec = formspec .. "label[0,3.25;"..def.description.."]"
|
||||
end
|
||||
|
2
mod.conf
2
mod.conf
@ -4,4 +4,4 @@ author = rubenwardy
|
||||
description = Adds achievements to Minetest, and an API to register new ones.
|
||||
license = LGPL 2.1 or later
|
||||
forum = https://forum.minetest.net/viewtopic.php?t=4870
|
||||
version = 2.1
|
||||
version = 2.2.0
|
||||
|
BIN
textures/awards_progress_gray.png
Normal file
BIN
textures/awards_progress_gray.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 146 B |
BIN
textures/awards_progress_green.png
Normal file
BIN
textures/awards_progress_green.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 177 B |
52
triggers.lua
52
triggers.lua
@ -21,6 +21,13 @@ awards.register_trigger("dig", function(def)
|
||||
target = def.trigger.target
|
||||
}
|
||||
table.insert(awards.on.dig, tmp)
|
||||
def.getProgress = function(self, data)
|
||||
local itemcount = awards.get_item_count(data, "count", tmp.node) or 0
|
||||
return {
|
||||
perc = itemcount / tmp.target,
|
||||
label = itemcount .. " / " .. tmp.target .. " dug" -- TODO: translation
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
awards.register_trigger("place", function(def)
|
||||
@ -30,6 +37,13 @@ awards.register_trigger("place", function(def)
|
||||
target = def.trigger.target
|
||||
}
|
||||
table.insert(awards.on.place, tmp)
|
||||
def.getProgress = function(self, data)
|
||||
local itemcount = awards.get_item_count(data, "place", tmp.node) or 0
|
||||
return {
|
||||
perc = itemcount / tmp.target,
|
||||
label = itemcount .. " / " .. tmp.target .. " placed" -- TODO: translation
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
awards.register_trigger("death", function(def)
|
||||
@ -38,6 +52,13 @@ awards.register_trigger("death", function(def)
|
||||
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 = itemcount .. " deaths, need " .. tmp.target -- TODO: translation
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
awards.register_trigger("chat", function(def)
|
||||
@ -46,6 +67,13 @@ awards.register_trigger("chat", function(def)
|
||||
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 = itemcount .. " / " .. tmp.target .. " line of chat" -- TODO: translation
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
awards.register_trigger("join", function(def)
|
||||
@ -54,6 +82,14 @@ awards.register_trigger("join", function(def)
|
||||
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 = itemcount .. " game joins, need " .. tmp.target -- TODO: translation
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
awards.register_trigger("craft", function(def)
|
||||
@ -63,6 +99,13 @@ awards.register_trigger("craft", function(def)
|
||||
target = def.trigger.target
|
||||
}
|
||||
table.insert(awards.on.craft, tmp)
|
||||
def.getProgress = function(self, data)
|
||||
local itemcount = awards.get_item_count(data, "craft", tmp.item) or 0
|
||||
return {
|
||||
perc = itemcount / tmp.target,
|
||||
label = itemcount .. " / " .. tmp.target .. " crafted" -- TODO: translation
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
-- Backwards compatibility
|
||||
@ -78,7 +121,8 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
|
||||
if not digger or not pos or not oldnode then
|
||||
return
|
||||
end
|
||||
local data = awards.players[playern]
|
||||
|
||||
local data = awards.players[digger:get_player_name()]
|
||||
if not awards.increment_item_counter(data, "count", oldnode.name) then
|
||||
return
|
||||
end
|
||||
@ -100,7 +144,7 @@ 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[playern]
|
||||
local data = awards.players[digger:get_player_name()]
|
||||
if not awards.increment_item_counter(data, "place", node.name) then
|
||||
return
|
||||
end
|
||||
@ -124,8 +168,8 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
|
||||
return
|
||||
end
|
||||
|
||||
local data = awards.players[playern]
|
||||
if not awards.increment_item_counter(data, "craft", itemstack:get_name()) then
|
||||
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
|
||||
|
||||
|
Reference in New Issue
Block a user