Use player names for unlocked statuses for forwards compatibility
parent
1c95dd9b66
commit
f316ab6230
|
@ -47,6 +47,14 @@ be used twice: `default:wood, group:wood`.
|
|||
* crafting.get_recipe(id)
|
||||
* Get recipe by ID
|
||||
|
||||
* crafting.get_unlocked(name)
|
||||
* `name` is the player's name
|
||||
* Returns a dictionary of recipe output to boolean.
|
||||
|
||||
* crafting.unlock(name, v)
|
||||
* `name` is the player's name
|
||||
* `v` is a single output or list of outputs
|
||||
|
||||
* crafting.get_all_for_player(player, type, level)
|
||||
* Returns a list of results, each a table
|
||||
* `items` - a key-value table, key being item name and value being a table:
|
||||
|
|
25
api.lua
25
api.lua
|
@ -46,12 +46,13 @@ function crafting.register_recipe(def)
|
|||
end
|
||||
|
||||
local unlocked_cache = {}
|
||||
function crafting.get_unlocked(player)
|
||||
function crafting.get_unlocked(name)
|
||||
local player = minetest.get_player_by_name(name)
|
||||
if not player then
|
||||
minetest.log("warning", "Crafting doesn't support getting unlocks for offline players")
|
||||
return {}
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local retval = unlocked_cache[name]
|
||||
if not retval then
|
||||
retval = minetest.parse_json(player:get_attribute("crafting:unlocked")
|
||||
|
@ -68,20 +69,26 @@ if minetest then
|
|||
end)
|
||||
end
|
||||
|
||||
function crafting.unlock(player, output)
|
||||
local unlocked = crafting.get_unlocked(player)
|
||||
function crafting.unlock(name, output)
|
||||
local player = minetest.get_player_by_name(name)
|
||||
if not player then
|
||||
minetest.log("warning", "Crafting doesn't support setting unlocks for offline players")
|
||||
return {}
|
||||
end
|
||||
|
||||
local unlocked = crafting.get_unlocked(name)
|
||||
|
||||
if type(output) == "table" then
|
||||
for i=1, #output do
|
||||
unlocked[output[i]] = true
|
||||
minetest.chat_send_player(player:get_player_name(), "You've unlocked " .. output[i])
|
||||
minetest.chat_send_player(name, "You've unlocked " .. output[i])
|
||||
end
|
||||
else
|
||||
unlocked[output] = true
|
||||
minetest.chat_send_player(player:get_player_name(), "You've unlocked " .. output)
|
||||
minetest.chat_send_player(name, "You've unlocked " .. output)
|
||||
end
|
||||
|
||||
unlocked_cache[player:get_player_name()] = unlocked
|
||||
unlocked_cache[name] = unlocked
|
||||
player:set_attribute("crafting:unlocked", minetest.write_json(unlocked))
|
||||
end
|
||||
|
||||
|
@ -145,7 +152,7 @@ function crafting.set_item_hashes_from_list(inv, listname, item_hash)
|
|||
end
|
||||
|
||||
function crafting.get_all_for_player(player, type, level)
|
||||
local unlocked = crafting.get_unlocked(player)
|
||||
local unlocked = crafting.get_unlocked(player:get_player_name())
|
||||
|
||||
-- Get items hashed
|
||||
local item_hash = {}
|
||||
|
@ -155,7 +162,7 @@ function crafting.get_all_for_player(player, type, level)
|
|||
end
|
||||
|
||||
function crafting.can_craft(name, type, level, recipe)
|
||||
local unlocked = crafting.get_unlocked(minetest.get_player_by_name(name))
|
||||
local unlocked = crafting.get_unlocked(name)
|
||||
|
||||
return recipe.type == type and recipe.level <= level and
|
||||
(recipe.always_known or unlocked[recipe.output])
|
||||
|
|
|
@ -56,7 +56,6 @@ end
|
|||
function default_def.on_timer(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local player_name = meta:get_string("user")
|
||||
local player = minetest.get_player_by_name(player_name)
|
||||
local inv = meta:get_inventory()
|
||||
local def = minetest.registered_items[minetest.get_node(pos).name]
|
||||
if player_name == "" or not def then
|
||||
|
@ -69,7 +68,7 @@ function default_def.on_timer(pos)
|
|||
local item_hash = {}
|
||||
crafting.set_item_hashes_from_list(inv, "input", item_hash)
|
||||
|
||||
local unlocked = crafting.get_unlocked(player)
|
||||
local unlocked = crafting.get_unlocked(player_name)
|
||||
local recipes = crafting.get_all(def.craft_type, def.craft_level, item_hash, unlocked)
|
||||
-- TODO: unlocked crafts
|
||||
|
||||
|
|
3
init.lua
3
init.lua
|
@ -28,8 +28,7 @@ end
|
|||
if minetest.global_exists("awards") then
|
||||
awards.register_on_unlock(function(name, award)
|
||||
if award.unlocks_crafts then
|
||||
local player = minetest.get_player_by_name(name)
|
||||
crafting.unlock(player, award.unlocks_crafts)
|
||||
crafting.unlock(name, award.unlocks_crafts)
|
||||
end
|
||||
end)
|
||||
|
||||
|
|
Loading…
Reference in New Issue