Add a /craft command to show recipe of pointed node

This commit is contained in:
Jean-Patrick Guerrero 2018-12-13 17:39:47 +01:00
parent 0b4f202ffb
commit b2a7f5430a
2 changed files with 85 additions and 34 deletions

@ -6,4 +6,5 @@ read_globals = {
"default", "default",
"sfinv", "sfinv",
"sfinv_buttons", "sfinv_buttons",
"vector",
} }

118
init.lua

@ -16,6 +16,7 @@ craftguide.intllib = S
-- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage: -- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
-- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1 -- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1
local remove, maxn, sort = table.remove, table.maxn, table.sort local remove, maxn, sort = table.remove, table.maxn, table.sort
local vector_add, vector_mul = vector.add, vector.multiply
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
local DEFAULT_SIZE = 10 local DEFAULT_SIZE = 10
@ -380,6 +381,10 @@ function craftguide:get_init_items()
datas.init_items = items_list datas.init_items = items_list
end end
minetest.register_on_mods_loaded(function()
craftguide:get_init_items()
end)
function craftguide:get_filter_items(data, player) function craftguide:get_filter_items(data, player)
local filter = data.filter local filter = data.filter
if datas.searches[filter] then if datas.searches[filter] then
@ -454,6 +459,16 @@ function craftguide:get_item_usages(item)
return usages return usages
end end
local show_fs = function(player, player_name, is_fuel)
if sfinv_only then
local context = sfinv.get_or_create_context(player)
context.fuel = is_fuel
sfinv.set_player_inventory_formspec(player, context)
else
craftguide:get_formspec(player_name, is_fuel)
end
end
local function get_fields(player, ...) local function get_fields(player, ...)
local args, formname, fields = {...} local args, formname, fields = {...}
if sfinv_only then if sfinv_only then
@ -466,16 +481,6 @@ local function get_fields(player, ...)
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = datas[player_name] local data = datas[player_name]
local show_fs = function(is_fuel)
if sfinv_only then
local context = sfinv.get_or_create_context(player)
context.fuel = is_fuel
sfinv.set_player_inventory_formspec(player, context)
else
craftguide:get_formspec(player_name, is_fuel)
end
end
if fields.clear then if fields.clear then
data.show_usage = nil data.show_usage = nil
data.filter = "" data.filter = ""
@ -484,7 +489,7 @@ local function get_fields(player, ...)
data.rnum = 1 data.rnum = 1
data.items = progressive_mode and data.init_filter_items or datas.init_items data.items = progressive_mode and data.init_filter_items or datas.init_items
show_fs() show_fs(player, player_name)
elseif fields.alternate then elseif fields.alternate then
local num local num
@ -495,14 +500,16 @@ local function get_fields(player, ...)
end end
data.rnum = num and data.rnum + 1 or 1 data.rnum = num and data.rnum + 1 or 1
show_fs()
show_fs(player, player_name)
elseif (fields.key_enter_field == "filter" or fields.search) and elseif (fields.key_enter_field == "filter" or fields.search) and
fields.filter ~= "" then fields.filter ~= "" then
data.filter = fields.filter:lower() data.filter = fields.filter:lower()
data.pagenum = 1 data.pagenum = 1
craftguide:get_filter_items(data, player) craftguide:get_filter_items(data, player)
show_fs()
show_fs(player, player_name)
elseif fields.prev or fields.next then elseif fields.prev or fields.next then
data.pagenum = data.pagenum - (fields.prev and 1 or -1) data.pagenum = data.pagenum - (fields.prev and 1 or -1)
@ -513,13 +520,14 @@ local function get_fields(player, ...)
data.pagenum = data.pagemax data.pagenum = data.pagemax
end end
show_fs() show_fs(player, player_name)
elseif (fields.size_inc and data.iX < MAX_LIMIT) or elseif (fields.size_inc and data.iX < MAX_LIMIT) or
(fields.size_dec and data.iX > MIN_LIMIT) then (fields.size_dec and data.iX > MIN_LIMIT) then
data.pagenum = 1 data.pagenum = 1
data.iX = data.iX - (fields.size_dec and 1 or -1) data.iX = data.iX - (fields.size_dec and 1 or -1)
show_fs()
show_fs(player, player_name)
else for item in pairs(fields) do else for item in pairs(fields) do
if item:find(":") then if item:find(":") then
@ -540,7 +548,7 @@ local function get_fields(player, ...)
data.rnum = 1 data.rnum = 1
end end
show_fs() show_fs(player, player_name)
else else
if progressive_mode then if progressive_mode then
local inv = player:get_inventory() local inv = player:get_inventory()
@ -555,13 +563,20 @@ local function get_fields(player, ...)
data.rnum = 1 data.rnum = 1
data.show_usage = nil data.show_usage = nil
show_fs(is_fuel) show_fs(player, player_name, is_fuel)
end end
end end
end end
end end
end end
local function init_datas(user, name)
datas[name] = {filter = "", pagenum = 1, iX = sfinv_only and 8 or DEFAULT_SIZE}
if progressive_mode then
craftguide:get_filter_items(datas[name], user)
end
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",
@ -574,18 +589,11 @@ if sfinv_only then
) )
end, end,
on_enter = function(self, player, context) on_enter = function(self, player, context)
if not datas.init_items then
craftguide:get_init_items()
end
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = datas[player_name] local data = datas[player_name]
if progressive_mode or not data then if progressive_mode or not data then
datas[player_name] = {filter = "", pagenum = 1, iX = 8} init_datas(player, player_name)
if progressive_mode then
craftguide:get_filter_items(datas[player_name], player)
end
end end
end, end,
on_player_receive_fields = function(self, player, context, fields) on_player_receive_fields = function(self, player, context, fields)
@ -596,19 +604,11 @@ else
mt.register_on_player_receive_fields(get_fields) mt.register_on_player_receive_fields(get_fields)
function craftguide:on_use(itemstack, user) function craftguide:on_use(itemstack, user)
if not datas.init_items then
self:get_init_items()
end
local player_name = user:get_player_name() local player_name = user:get_player_name()
local data = datas[player_name] local data = datas[player_name]
if progressive_mode or not data then if progressive_mode or not data then
datas[player_name] = {filter = "", pagenum = 1, iX = DEFAULT_SIZE} init_datas(user, player_name)
if progressive_mode then
self:get_filter_items(datas[player_name], user)
end
self:get_formspec(player_name) self:get_formspec(player_name)
else else
show_formspec(player_name, "craftguide", data.formspec) show_formspec(player_name, "craftguide", data.formspec)
@ -687,6 +687,56 @@ if rawget(_G, "sfinv_buttons") then
}) })
end end
if not progressive_mode then
minetest.register_chatcommand("craft", {
description = S("Show recipe(s) of the pointed node"),
func = function(name)
local player = minetest.get_player_by_name(name)
local ppos = player:get_pos()
local dir = player:get_look_dir()
local eye_h = {x = ppos.x, y = ppos.y + 1.625, z = ppos.z}
local node_name
for i = 1, 10 do
local look_at = vector_add(eye_h, vector_mul(dir, i))
local node = minetest.get_node(look_at)
if node.name ~= "air" then
node_name = node.name
break
end
end
if not node_name then
return false, colorize("[craftguide] ") .. S("No node pointed")
elseif not datas[name] then
init_datas(player, name)
end
local data = datas[name]
local is_fuel = get_fueltime(node_name) > 0
local recipes = get_recipes(node_name)
if not recipes and not is_fuel then
return false, colorize("[craftguide] ") ..
S("No recipe for this node:") .. " " ..
colorize(node_name)
end
data.show_usage = nil
data.filter = ""
data.item = node_name
data.pagenum = 1
data.rnum = 1
data.recipes_item = recipes
data.items = progressive_mode and data.init_filter_items or
datas.init_items
return true, show_fs(player, name, is_fuel)
end,
})
end
--[[ Custom recipes (>3x3) test code --[[ Custom recipes (>3x3) test code
mt.register_craftitem("craftguide:custom_recipe_test", { mt.register_craftitem("craftguide:custom_recipe_test", {