Merge pull request #57 from pauloue/fixes

Misc. fixes and refactors
This commit is contained in:
Jean-Patrick Guerrero 2019-02-07 14:19:58 +01:00 committed by GitHub
commit 7757eb72fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,13 +56,13 @@ local function table_merge(t, t2)
return t return t
end end
local function table_clean(t) local function clean_items(items)
local hash, ct = {}, {} local hash, ct = {}, {}
for i = 1, #t do for i = 1, #items do
local v = t[i] local item = items[i]
if not hash[v] then if not hash[item] and reg_items[item] then
ct[#ct + 1] = v ct[#ct + 1] = item
hash[v] = true hash[item] = true
end end
end end
@ -410,9 +410,6 @@ local function make_formspec(name)
fs[#fs + 1] = [[ fs[#fs + 1] = [[
no_prepend[] no_prepend[]
background[1,1;1,1;craftguide_bg.png;true] background[1,1;1,1;craftguide_bg.png;true]
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
field_close_on_enter[filter;false]
]] ]]
fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]" fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]"
@ -424,6 +421,12 @@ local function make_formspec(name)
",0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;]" ",0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;]"
end end
fs[#fs + 1] = [[
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
field_close_on_enter[filter;false]
]]
fs[#fs + 1] = "tooltip[search;" .. S("Search") .. "]" fs[#fs + 1] = "tooltip[search;" .. S("Search") .. "]"
fs[#fs + 1] = "tooltip[clear;" .. S("Reset") .. "]" fs[#fs + 1] = "tooltip[clear;" .. S("Reset") .. "]"
fs[#fs + 1] = "tooltip[prev;" .. S("Previous page") .. "]" fs[#fs + 1] = "tooltip[prev;" .. S("Previous page") .. "]"
@ -587,7 +590,7 @@ local function item_in_inv(item, inv_items)
end end
end end
local function init_data(player, name) local function init_data(name)
player_data[name] = { player_data[name] = {
filter = "", filter = "",
pagenum = 1, pagenum = 1,
@ -725,6 +728,18 @@ local function on_receive_fields(player, fields)
end end
end end
mt.register_on_mods_loaded(get_init_items)
mt.register_on_joinplayer(function(player)
local name = player:get_player_name()
init_data(name)
end)
mt.register_on_leaveplayer(function(player)
local name = player:get_player_name()
player_data[name] = nil
end)
if sfinv_only then if sfinv_only then
sfinv.register_page("craftguide:craftguide", { sfinv.register_page("craftguide:craftguide", {
title = "Craft Guide", title = "Craft Guide",
@ -737,10 +752,9 @@ if sfinv_only then
end, end,
on_enter = function(self, player, context) on_enter = function(self, player, context)
if next(recipe_filters) then
local name = player:get_player_name() local name = player:get_player_name()
local data = player_data[name] local data = player_data[name]
if next(recipe_filters) then
data.items_raw = get_filtered_items(player) data.items_raw = get_filtered_items(player)
search(data) search(data)
end end
@ -759,9 +773,9 @@ else
local function on_use(user) local function on_use(user)
local name = user:get_player_name() local name = user:get_player_name()
local data = player_data[name]
if next(recipe_filters) then if next(recipe_filters) then
local data = player_data[name]
data.items_raw = get_filtered_items(user) data.items_raw = get_filtered_items(user)
search(data) search(data)
end end
@ -809,8 +823,9 @@ else
mt.register_craft({ mt.register_craft({
output = "craftguide:book", output = "craftguide:book",
type = "shapeless", recipe = {
recipe = {"default:book"} {"default:book"}
}
}) })
mt.register_craft({ mt.register_craft({
@ -821,8 +836,9 @@ else
mt.register_craft({ mt.register_craft({
output = "craftguide:sign", output = "craftguide:sign",
type = "shapeless", recipe = {
recipe = {"default:sign_wall_wood"} {"default:sign_wall_wood"}
}
}) })
mt.register_craft({ mt.register_craft({
@ -843,18 +859,6 @@ else
end end
end end
mt.register_on_mods_loaded(get_init_items)
mt.register_on_joinplayer(function(player)
local name = player:get_player_name()
init_data(player, name)
end)
mt.register_on_leaveplayer(function(player)
local name = player:get_player_name()
player_data[name] = nil
end)
if progressive_mode then if progressive_mode then
local function recipe_in_inv(recipe, inv_items) local function recipe_in_inv(recipe, inv_items)
for _, item in pairs(recipe.items) do for _, item in pairs(recipe.items) do
@ -870,7 +874,7 @@ if progressive_mode then
local name = player:get_player_name() local name = player:get_player_name()
local discovered = player_data[name].inv_items local discovered = player_data[name].inv_items
local inv_items = get_inv_items(player) local inv_items = get_inv_items(player)
discovered = table_clean(table_merge(discovered, inv_items)) discovered = clean_items(table_merge(discovered, inv_items))
if #discovered == 0 then if #discovered == 0 then
return {} return {}
@ -910,8 +914,9 @@ if progressive_mode then
save_meta(players[i]) save_meta(players[i])
end end
end) end)
else end
mt.register_chatcommand("craft", {
mt.register_chatcommand("craft", {
description = S("Show recipe(s) of the pointed node"), description = S("Show recipe(s) of the pointed node"),
func = function(name) func = function(name)
local player = mt.get_player_by_name(name) local player = mt.get_player_by_name(name)
@ -941,7 +946,11 @@ else
local recipes = recipes_cache[node_name] local recipes = recipes_cache[node_name]
local is_fuel = fuel_cache[node_name] local is_fuel = fuel_cache[node_name]
if not recipes then if recipes then
recipes = apply_recipe_filters(recipes, player)
end
if not recipes or #recipes == 0 then
if is_fuel then if is_fuel then
recipes = get_item_usages(node_name) recipes = get_item_usages(node_name)
if #recipes > 0 then if #recipes > 0 then
@ -959,8 +968,7 @@ else
return true, show_fs(player, name) return true, show_fs(player, name)
end, end,
}) })
end
--[[ Custom recipes (>3x3) test code --[[ Custom recipes (>3x3) test code