summaryrefslogtreecommitdiff
path: root/autocrafter.lua
diff options
context:
space:
mode:
authorTim <t4im@users.noreply.github.com>2015-01-27 21:55:19 +0100
committerTim <t4im@users.noreply.github.com>2015-01-28 00:22:35 +0100
commit6d1bc7b3fc892e709d5d53345358bf24a9f36db8 (patch)
treed76964d43ea4b22d7b1646ff6d484dec6688fd8f /autocrafter.lua
parent5ac0053f8c14090e86906bd4346e4a95687c78eb (diff)
downloadpipeworks-6d1bc7b3fc892e709d5d53345358bf24a9f36db8.tar
pipeworks-6d1bc7b3fc892e709d5d53345358bf24a9f36db8.tar.gz
pipeworks-6d1bc7b3fc892e709d5d53345358bf24a9f36db8.tar.bz2
pipeworks-6d1bc7b3fc892e709d5d53345358bf24a9f36db8.tar.xz
pipeworks-6d1bc7b3fc892e709d5d53345358bf24a9f36db8.zip
add an enable/disable button to the autocrafter
the reaction time of the button might be a bit confusing though until minetest/minetest#2220 is being resolved
Diffstat (limited to 'autocrafter.lua')
-rw-r--r--autocrafter.lua53
1 files changed, 39 insertions, 14 deletions
diff --git a/autocrafter.lua b/autocrafter.lua
index 9033cff..3d56539 100644
--- a/autocrafter.lua
+++ b/autocrafter.lua
@@ -40,6 +40,16 @@ local function get_craft(pos, inventory, hash)
end
end
+local function start_crafter(pos)
+ local meta = minetest.get_meta(pos)
+ if meta:get_int("enabled") == 1 then
+ local timer = minetest.get_node_timer(pos)
+ if not timer:is_started() then
+ timer:start(craft_time)
+ end
+ end
+end
+
-- note, that this function assumes allready being updated to virtual items
-- and doesn't handle recipes with stacksizes > 1
local function on_recipe_change(pos, inventory)
@@ -68,17 +78,11 @@ local function on_recipe_change(pos, inventory)
end
end
- local timer = minetest.get_node_timer(pos)
- if not timer:is_started() then
- timer:start(craft_time)
- end
+ start_crafter(pos)
end
local function on_inventory_change(pos, inventory)
- local timer = minetest.get_node_timer(pos)
- if not timer:is_started() then
- timer:start(craft_time)
- end
+ start_crafter(pos)
end
local function autocraft(inventory, craft)
@@ -137,6 +141,17 @@ local function update_autocrafter(pos)
end
end
+local function set_formspec(meta, enabled)
+ local state = enabled and "on" or "off"
+ meta:set_string("formspec",
+ "size[8,11]"..
+ "list[context;recipe;0,0;3,3;]"..
+ "image_button[3,2;1,1;pipeworks_button_" .. state .. ".png;" .. state .. ";;;false;pipeworks_button_interm.png]" ..
+ "list[context;src;0,3.5;8,3;]"..
+ "list[context;dst;4,0;4,3;]"..
+ "list[current_player;main;0,7;8,4;]")
+end
+
minetest.register_node("pipeworks:autocrafter", {
description = "Autocrafter",
drawtype = "normal",
@@ -158,12 +173,7 @@ minetest.register_node("pipeworks:autocrafter", {
connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
- meta:set_string("formspec",
- "size[8,11]"..
- "list[current_name;recipe;0,0;3,3;]"..
- "list[current_name;src;0,3.5;8,3;]"..
- "list[current_name;dst;4,0;4,3;]"..
- "list[current_player;main;0,7;8,4;]")
+ set_formspec(meta)
meta:set_string("infotext", "unconfigured Autocrafter")
meta:set_string("virtual_items", "1")
local inv = meta:get_inventory()
@@ -171,6 +181,21 @@ minetest.register_node("pipeworks:autocrafter", {
inv:set_size("recipe", 3*3)
inv:set_size("dst", 4*3)
end,
+ on_receive_fields = function(pos, formname, fields, sender)
+ local meta = minetest.get_meta(pos)
+ if fields.on then
+ meta:set_int("enabled", 0)
+ set_formspec(meta, false)
+ minetest.get_node_timer(pos):stop()
+ meta:set_string("infotext", text or "paused Autocrafter")
+ elseif fields.off then
+ meta:set_int("enabled", 1)
+ set_formspec(meta, true)
+ start_crafter(pos)
+ else -- update formspec on esc for now
+ set_formspec(meta, meta:get_int("enabled") == 1)
+ end
+ end,
on_punch = update_autocrafter,
can_dig = function(pos, player)
update_autocrafter(pos)