Christopher Head
2019-01-26 4f78a69ffc714886c9d6e812f78d543bb33fe674
commit | author | age
82cba9 1
354ee6 2 local S = technic.getter
S 3
a353a8 4 local fs_helpers = pipeworks.fs_helpers
VE 5
8479a8 6 local tube_entry = "^pipeworks_tube_connection_metallic.png"
VE 7
f3bba0 8 local function inject_items (pos)
4f78a6 9         local meta=minetest.get_meta(pos)
f3bba0 10         local inv = meta:get_inventory()
VE 11         local mode=meta:get_string("mode")
12         if mode=="single items" then
13             local i=0
14             for _,stack in ipairs(inv:get_list("main")) do
15             i=i+1
16                 if stack then
17                 local item0=stack:to_table()
4f78a6 18                 if item0 then
baf7f6 19                     item0["count"] = "1"
049129 20                     technic.tube_inject_item(pos, pos, vector.new(0, -1, 0), item0)
baf7f6 21                     stack:take_item(1)
f3bba0 22                     inv:set_stack("main", i, stack)
VE 23                     return
24                     end
25                 end
26             end
27         end
28         if mode=="whole stacks" then
29             local i=0
30             for _,stack in ipairs(inv:get_list("main")) do
31             i=i+1
32                 if stack then
33                 local item0=stack:to_table()
4f78a6 34                 if item0 then
049129 35                     technic.tube_inject_item(pos, pos, vector.new(0, -1, 0), item0)
f3bba0 36                     stack:clear()
VE 37                     inv:set_stack("main", i, stack)
38                     return
39                     end
40                 end
41             end
42         end
43         
44 end
45
91cdd1 46 minetest.register_craft({
R 47     output = 'technic:injector 1',
48     recipe = {
49         {'', 'technic:control_logic_unit',''},
50         {'', 'default:chest',''},
749df3 51         {'', 'pipeworks:tube_1',''},
91cdd1 52     }
R 53 })
54
827509 55 local function set_injector_formspec(meta)
Z 56     local is_stack = meta:get_string("mode") == "whole stacks"
57     meta:set_string("formspec",
fb9338 58         "size[8,9;]"..
a353a8 59         "item_image[0,0;1,1;technic:injector]"..
VE 60         "label[1,0;"..S("Self-Contained Injector").."]"..
61         (is_stack and
62             "button[0,1;2,1;mode_item;"..S("Stackwise").."]" or
63             "button[0,1;2,1;mode_stack;"..S("Itemwise").."]")..
64         "list[current_name;main;0,2;8,2;]"..
65         "list[current_player;main;0,5;8,4;]"..
66         "listring[]"..
67         fs_helpers.cycling_button(
68             meta,
fab2c4 69             pipeworks.button_base,
a353a8 70             "splitstacks",
VE 71             {
fab2c4 72                 pipeworks.button_off,
VE 73                 pipeworks.button_on
a353a8 74             }
fab2c4 75         )..pipeworks.button_label
a353a8 76     )
827509 77 end
Z 78
82cba9 79 minetest.register_node("technic:injector", {
827509 80     description = S("Self-Contained Injector"),
8479a8 81     tiles = {
VE 82         "technic_injector_top.png"..tube_entry,
83         "technic_injector_bottom.png",
84         "technic_injector_side.png"..tube_entry,
85         "technic_injector_side.png"..tube_entry,
86         "technic_injector_side.png"..tube_entry,
87         "technic_injector_side.png"
88     },
89     paramtype2 = "facedir",
ce40d1 90     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, tubedevice=1, tubedevice_receiver=1},
7ed1aa 91     tube = {
T 92         can_insert = function(pos, node, stack, direction)
a353a8 93             local meta = minetest.get_meta(pos)
VE 94             local inv = meta:get_inventory()
95             if meta:get_int("splitstacks") == 1 then
96                 stack = stack:peek_item(1)
97             end
98             return meta:get_inventory():room_for_item("main", stack)
7ed1aa 99         end,
T 100         insert_object = function(pos, node, stack, direction)
af3922 101             return minetest.get_meta(pos):get_inventory():add_item("main", stack)
7ed1aa 102         end,
8479a8 103         connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
7ed1aa 104     },
82cba9 105     sounds = default.node_sound_wood_defaults(),
R 106     on_construct = function(pos)
a41390 107         local meta = minetest.get_meta(pos)
827509 108         meta:set_string("infotext", S("Self-Contained Injector"))
82cba9 109         local inv = meta:get_inventory()
db0f8e 110         inv:set_size("main", 8*2)
fd26a8 111         meta:set_string("mode","single items")
827509 112         set_injector_formspec(meta)
82cba9 113     end,
R 114     can_dig = function(pos,player)
a41390 115         local meta = minetest.get_meta(pos);
82cba9 116         local inv = meta:get_inventory()
R 117         return inv:is_empty("main")
fd26a8 118     end,
R 119     on_receive_fields = function(pos, formanme, fields, sender)
a41390 120         local meta = minetest.get_meta(pos)
827509 121         if fields.mode_item then meta:set_string("mode", "single items") end
Z 122         if fields.mode_stack then meta:set_string("mode", "whole stacks") end
a353a8 123
VE 124         if fields["fs_helpers_cycling:0:splitstacks"]
125           or fields["fs_helpers_cycling:1:splitstacks"] then
126             if not pipeworks.may_configure(pos, sender) then return end
127             fs_helpers.on_receive_fields(pos, fields)
128         end
827509 129         set_injector_formspec(meta)
82cba9 130     end,
0809dd 131     allow_metadata_inventory_put = technic.machine_inventory_put,
S 132     allow_metadata_inventory_take = technic.machine_inventory_take,
133     allow_metadata_inventory_move = technic.machine_inventory_move,
bccefd 134     after_place_node = pipeworks.after_place,
VE 135     after_dig_node = pipeworks.after_dig
82cba9 136 })
R 137
74cf47 138 minetest.register_abm({
78f16c 139     label = "Machines: run injector",
74cf47 140     nodenames = {"technic:injector"},
R 141     interval = 1,
142     chance = 1,
143     action = function(pos, node, active_object_count, active_object_count_wider)
35b10a 144         local pos1 = vector.add(pos, vector.new(0, -1, 0))
4f78a6 145         local node1 = minetest.get_node(pos1)
35b10a 146         if minetest.get_item_group(node1.name, "tubedevice") > 0 then
N 147             inject_items(pos)
148         end
82cba9 149     end,
R 150 })
151