summaryrefslogtreecommitdiff
path: root/compat-chests.lua
diff options
context:
space:
mode:
authorVanessa Ezekowitz <vanessaezekowitz@gmail.com>2017-04-10 23:13:44 -0400
committerVanessa Ezekowitz <vanessaezekowitz@gmail.com>2017-04-11 01:33:06 -0400
commit8f3d8cc5865b0d1ef8aafdd8908221bdebb79a25 (patch)
treee49dd978fbd2b866f97965e04c2872b4aa0c746b /compat-chests.lua
parentcf97f02434af2d6342b3e11156d089f40a7a2460 (diff)
downloadpipeworks-8f3d8cc5865b0d1ef8aafdd8908221bdebb79a25.tar
pipeworks-8f3d8cc5865b0d1ef8aafdd8908221bdebb79a25.tar.gz
pipeworks-8f3d8cc5865b0d1ef8aafdd8908221bdebb79a25.tar.bz2
pipeworks-8f3d8cc5865b0d1ef8aafdd8908221bdebb79a25.tar.xz
pipeworks-8f3d8cc5865b0d1ef8aafdd8908221bdebb79a25.zip
allow stack per-chest/per-furnace stack splitting
defaults to disabled, whether the node has a new formspec or not note that furnace fuel stacks can't be split.
Diffstat (limited to 'compat-chests.lua')
-rw-r--r--compat-chests.lua185
1 files changed, 185 insertions, 0 deletions
diff --git a/compat-chests.lua b/compat-chests.lua
new file mode 100644
index 0000000..ef771ab
--- /dev/null
+++ b/compat-chests.lua
@@ -0,0 +1,185 @@
+-- this bit of code modifies the default chests and furnaces to be compatible
+-- with pipeworks.
+--
+-- the formspecs found here are basically copies of the ones from minetest_game
+-- plus bits from pipeworks' sorting tubes
+
+local fs_helpers = pipeworks.fs_helpers
+
+local base_chest_formspec = "size[8,9]" ..
+ default.gui_bg ..
+ default.gui_bg_img ..
+ default.gui_slots ..
+ "list[current_player;main;0,4.85;8,1;]" ..
+ "list[current_player;main;0,6.08;8,3;8]" ..
+ "listring[current_player;main]" ..
+ default.get_hotbar_bg(0,4.85)
+
+local function update_chest_formspec(pos)
+ local meta = minetest.get_meta(pos)
+ local formspec = base_chest_formspec ..
+ "list[current_name;main;0,0.3;8,4;]" ..
+ "listring[current_name;main]" ..
+ fs_helpers.cycling_button(
+ meta,
+ "image_button[0,4.3;1,0.6",
+ "splitstacks",
+ {
+ {text="", texture="pipeworks_button_off.png", addopts="false;false;pipeworks_button_interm.png"},
+ {text="", texture="pipeworks_button_on.png", addopts="false;false;pipeworks_button_interm.png"}
+ }
+ ).."label[0.9,4.31;Allow splitting incoming stacks from tubes]"
+ meta:set_string("formspec", formspec)
+end
+
+minetest.override_item("default:chest", {
+ tiles = {
+ "default_chest_top.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_top.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_side.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_side.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_side.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_front.png"
+ },
+ groups = {choppy = 2, oddly_breakable_by_hand = 2, tubedevice = 1, tubedevice_receiver = 1},
+ tube = {
+ insert_object = function(pos, node, stack, direction)
+ local meta = minetest.get_meta(pos)
+ local inv = meta:get_inventory()
+ return inv:add_item("main", stack)
+ end,
+ can_insert = function(pos, node, stack, direction)
+ local meta = minetest.get_meta(pos)
+ local inv = meta:get_inventory()
+ if meta:get_int("splitstacks") == 1 then
+ stack = stack:peek_item(1)
+ end
+ return inv:room_for_item("main", stack)
+ end,
+ input_inventory = "main",
+ connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
+ },
+ after_place_node = pipeworks.after_place,
+ after_dig_node = pipeworks.after_dig,
+ on_construct = function(pos)
+ local meta = minetest.get_meta(pos)
+ local inv = meta:get_inventory()
+ inv:set_size("main", 8*4)
+ update_chest_formspec(pos)
+ end,
+ on_receive_fields = function(pos, formname, fields, sender)
+ if not pipeworks.may_configure(pos, sender) then return end
+ fs_helpers.on_receive_fields(pos, fields)
+ update_chest_formspec(pos)
+ end,
+})
+
+-- =====================
+
+local function get_locked_chest_formspec(pos)
+ local spos = pos.x .. "," .. pos.y .. "," .. pos.z
+ local formspec = base_chest_formspec ..
+ "list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
+ "listring[nodemeta:" .. spos .. ";main]"
+ return formspec
+end
+
+local function setup_locked_formspec(pos, meta)
+ meta:set_string("formspec",
+ get_locked_chest_formspec(pos) ..
+ fs_helpers.cycling_button(
+ meta,
+ "image_button[0,4.3;1,0.6",
+ "splitstacks",
+ {
+ {text="", texture="pipeworks_button_off.png", addopts="false;false;pipeworks_button_interm.png"},
+ {text="", texture="pipeworks_button_on.png", addopts="false;false;pipeworks_button_interm.png"}
+ }
+ ).."label[0.9,4.31;Allow splitting incoming stacks from tubes]"
+ )
+end
+
+minetest.override_item("default:chest_locked", {
+ tiles = {
+ "default_chest_top.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_top.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_side.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_side.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_side.png^pipeworks_tube_connection_wooden.png",
+ "default_chest_lock.png"
+ },
+ groups = {choppy = 2, oddly_breakable_by_hand = 2, tubedevice = 1, tubedevice_receiver = 1},
+ tube = {
+ insert_object = function(pos, node, stack, direction)
+ local meta = minetest.get_meta(pos)
+ local inv = meta:get_inventory()
+ return inv:add_item("main", stack)
+ end,
+ can_insert = function(pos, node, stack, direction)
+ local meta = minetest.get_meta(pos)
+ local inv = meta:get_inventory()
+ if meta:get_int("splitstacks") == 1 then
+ stack = stack:peek_item(1)
+ end
+ return inv:room_for_item("main", stack)
+ end,
+ connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
+ },
+ after_place_node = function (pos, placer)
+ local meta = minetest.get_meta(pos)
+ meta:set_string("owner", placer:get_player_name() or "")
+ meta:set_string("infotext", "Locked Chest (owned by "..
+ meta:get_string("owner")..")")
+ pipeworks.after_place(pos)
+ end,
+ on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
+ if default.can_interact_with_node(clicker, pos) then
+ local meta = minetest.get_meta(pos)
+ local formspec = meta:get_string("formspec")
+ print("on_rightclick")
+ print(dump(formspec))
+ setup_locked_formspec(pos, meta, clicker)
+ minetest.show_formspec(
+ clicker:get_player_name(),
+ "default:chest_locked",
+ get_locked_chest_formspec(pos)
+ )
+ end
+ return itemstack
+ end,
+ on_key_use = function(pos, player)
+ local secret = minetest.get_meta(pos):get_string("key_lock_secret")
+ local itemstack = player:get_wielded_item()
+ local key_meta = itemstack:get_meta()
+
+ if key_meta:get_string("secret") == "" then
+ key_meta:set_string("secret", minetest.parse_json(itemstack:get_metadata()).secret)
+ itemstack:set_metadata("")
+ end
+
+ if secret ~= key_meta:get_string("secret") then
+ return
+ end
+ setup_locked_formspec(pos, minetest.get_meta(pos))
+ end,
+ on_construct = function(pos)
+ local meta = minetest.get_meta(pos)
+ local inv = meta:get_inventory()
+ inv:set_size("main", 8*4)
+ setup_locked_formspec(pos, meta)
+ end,
+ on_receive_fields = function(pos, formname, fields, sender)
+ if not pipeworks.may_configure(pos, sender) then return end
+ fs_helpers.on_receive_fields(pos, fields)
+ local formspec = get_locked_chest_formspec(pos)
+ print("on_receive_fields")
+ print(dump(formspec))
+
+ if formspec == "" then
+ meta:set_string("formspec", formspec)
+ else
+ setup_locked_formspec(pos, minetest.get_meta(pos))
+ end
+ end,
+ after_dig_node = pipeworks.after_dig
+})