improve autocrafter infotext handling

use the item description and provide more information in paused states
This commit is contained in:
Tim 2015-01-29 17:00:05 +01:00
parent 502395aa13
commit 73b3466a7b

View File

@ -13,6 +13,13 @@ local function count_index(invlist)
return index return index
end end
local function get_item_info(stack)
local name = stack:get_name()
local def = minetest.registered_items[name]
local description = def and def.description or "Unknown item"
return description, name
end
local function get_craft(pos, inventory, hash) local function get_craft(pos, inventory, hash)
local hash = hash or minetest.hash_node_position(pos) local hash = hash or minetest.hash_node_position(pos)
local craft = autocrafterCache[hash] local craft = autocrafterCache[hash]
@ -58,10 +65,10 @@ local function run_autocrafter(pos, elapsed)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inventory = meta:get_inventory() local inventory = meta:get_inventory()
local craft = get_craft(pos, inventory) local craft = get_craft(pos, inventory)
local output_item = craft.output.item
-- only use crafts that have an actual result -- only use crafts that have an actual result
if craft.output.item:is_empty() then if output_item:is_empty() then
meta:set_string("infotext", text or "Autocrafter: unknown recipe") meta:set_string("infotext", "unconfigured Autocrafter: unknown recipe")
return false return false
end end
@ -117,7 +124,8 @@ local function after_recipe_change(pos, inventory)
craft = craft or get_craft(pos, inventory, hash) craft = craft or get_craft(pos, inventory, hash)
local output_item = craft.output.item local output_item = craft.output.item
meta:set_string("infotext", "Autocrafter: " .. output_item:get_name()) local description, name = get_item_info(output_item)
meta:set_string("infotext", string.format("'%s' Autocrafter (%s)", description, name))
inventory:set_stack("output", 1, output_item) inventory:set_stack("output", 1, output_item)
after_inventory_change(pos) after_inventory_change(pos)
@ -149,8 +157,10 @@ local function on_output_change(pos, inventory, stack)
after_recipe_change(pos, inventory) after_recipe_change(pos, inventory)
end end
local function set_formspec(meta, enabled) -- returns false if we shouldn't bother attempting to start the timer again after this
local function update_meta(meta, enabled)
local state = enabled and "on" or "off" local state = enabled and "on" or "off"
meta:set_int("enabled", enabled and 1 or 0)
meta:set_string("formspec", meta:set_string("formspec",
"size[8,11]".. "size[8,11]"..
"list[context;recipe;0,0;3,3;]".. "list[context;recipe;0,0;3,3;]"..
@ -164,21 +174,36 @@ local function set_formspec(meta, enabled)
default.gui_slots.. default.gui_slots..
default.get_hotbar_bg(0,7) .. default.get_hotbar_bg(0,7) ..
"list[current_player;main;0,7;8,4;]") "list[current_player;main;0,7;8,4;]")
-- toggling the button doesn't quite call for running a recipe change check
-- so instead we run a minimal version for infotext setting only
-- this might be more written code, but actually executes less
local output = meta:get_inventory():get_stack("output", 1)
if output:is_empty() then -- doesn't matter if paused or not
meta:set_string("infotext", "unconfigured Autocrafter")
return false
end
local description, name = get_item_info(output)
local infotext = enabled and string.format("'%s' Autocrafter (%s)", description, name)
or string.format("paused '%s' Autocrafter", description)
meta:set_string("infotext", infotext)
return enabled
end end
-- 1st version of the autocrafter had actual items in the crafting grid -- 1st version of the autocrafter had actual items in the crafting grid
-- the 2nd replaced these with virtual items, dropped the content on update and set "virtual_items" to string "1" -- the 2nd replaced these with virtual items, dropped the content on update and set "virtual_items" to string "1"
-- the third added an output inventory, changed the formspec and added a button for enabling/disabling -- the third added an output inventory, changed the formspec and added a button for enabling/disabling
-- so we work out way backwards on this history and update each single case to the newest version -- so we work out way backwards on this history and update each single case to the newest version
local function update_autocrafter(pos, meta) local function upgrade_autocrafter(pos, meta)
local meta = meta or minetest.get_meta(pos) local meta = meta or minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:get_size("output") == 0 then -- we are version 2 or 1 if inv:get_size("output") == 0 then -- we are version 2 or 1
inv:set_size("output", 1) inv:set_size("output", 1)
-- migrate the old autocrafters into an "enabled" state -- migrate the old autocrafters into an "enabled" state
meta:set_int("enabled", 1) update_meta(meta, true)
set_formspec(meta, true)
if meta:get_string("virtual_items") == "1" then -- we are version 2 if meta:get_string("virtual_items") == "1" then -- we are version 2
-- we allready dropped stuff, so lets remove the metadatasetting (we are not being called again for this node) -- we allready dropped stuff, so lets remove the metadatasetting (we are not being called again for this node)
@ -221,29 +246,26 @@ minetest.register_node("pipeworks:autocrafter", {
connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}}, connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
set_formspec(meta, false)
meta:set_string("infotext", "unconfigured Autocrafter")
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size("src", 3*8) inv:set_size("src", 3*8)
inv:set_size("recipe", 3*3) inv:set_size("recipe", 3*3)
inv:set_size("dst", 4*3) inv:set_size("dst", 4*3)
inv:set_size("output", 1) inv:set_size("output", 1)
update_meta(meta, false)
end, end,
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if fields.on then if fields.on then
meta:set_int("enabled", 0) update_meta(meta, false)
set_formspec(meta, false)
minetest.get_node_timer(pos):stop() minetest.get_node_timer(pos):stop()
meta:set_string("infotext", text or "paused Autocrafter")
elseif fields.off then elseif fields.off then
meta:set_int("enabled", 1) if update_meta(meta, true) then
set_formspec(meta, true)
start_crafter(pos) start_crafter(pos)
end end
end
end, end,
can_dig = function(pos, player) can_dig = function(pos, player)
update_autocrafter(pos) upgrade_autocrafter(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
return (inv:is_empty("src") and inv:is_empty("dst")) return (inv:is_empty("src") and inv:is_empty("dst"))
@ -256,7 +278,7 @@ minetest.register_node("pipeworks:autocrafter", {
autocrafterCache[minetest.hash_node_position(pos)] = nil autocrafterCache[minetest.hash_node_position(pos)] = nil
end, end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player) allow_metadata_inventory_put = function(pos, listname, index, stack, player)
update_autocrafter(pos) upgrade_autocrafter(pos)
local inv = minetest.get_meta(pos):get_inventory() local inv = minetest.get_meta(pos):get_inventory()
if listname == "recipe" then if listname == "recipe" then
stack:set_count(1) stack:set_count(1)
@ -271,7 +293,7 @@ minetest.register_node("pipeworks:autocrafter", {
return stack:get_count() return stack:get_count()
end, end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player) allow_metadata_inventory_take = function(pos, listname, index, stack, player)
update_autocrafter(pos) upgrade_autocrafter(pos)
local inv = minetest.get_meta(pos):get_inventory() local inv = minetest.get_meta(pos):get_inventory()
if listname == "recipe" then if listname == "recipe" then
inv:set_stack(listname, index, ItemStack("")) inv:set_stack(listname, index, ItemStack(""))
@ -285,7 +307,7 @@ minetest.register_node("pipeworks:autocrafter", {
return stack:get_count() return stack:get_count()
end, end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
update_autocrafter(pos) upgrade_autocrafter(pos)
local inv = minetest.get_meta(pos):get_inventory() local inv = minetest.get_meta(pos):get_inventory()
local stack = inv:get_stack(from_list, from_index) local stack = inv:get_stack(from_list, from_index)