auouymous
2021-02-06 0f7810e53895f5742ab577559584539e6533a0dc
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
befe3e 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
a8daa4 43
f3bba0 44 end
VE 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").."]")..
0f7810 64         (meta:get_int("public") == 1 and
A 65             "button[2,1;2,1;mode_private;"..S("Public").."]" or
66             "button[2,1;2,1;mode_public;"..S("Private").."]")..
a353a8 67         "list[current_name;main;0,2;8,2;]"..
VE 68         "list[current_player;main;0,5;8,4;]"..
69         "listring[]"..
70         fs_helpers.cycling_button(
71             meta,
fab2c4 72             pipeworks.button_base,
a353a8 73             "splitstacks",
VE 74             {
fab2c4 75                 pipeworks.button_off,
VE 76                 pipeworks.button_on
a353a8 77             }
fab2c4 78         )..pipeworks.button_label
a353a8 79     )
827509 80 end
Z 81
82cba9 82 minetest.register_node("technic:injector", {
827509 83     description = S("Self-Contained Injector"),
8479a8 84     tiles = {
VE 85         "technic_injector_top.png"..tube_entry,
86         "technic_injector_bottom.png",
87         "technic_injector_side.png"..tube_entry,
88         "technic_injector_side.png"..tube_entry,
89         "technic_injector_side.png"..tube_entry,
90         "technic_injector_side.png"
91     },
92     paramtype2 = "facedir",
ce40d1 93     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, tubedevice=1, tubedevice_receiver=1},
7ed1aa 94     tube = {
T 95         can_insert = function(pos, node, stack, direction)
a353a8 96             local meta = minetest.get_meta(pos)
VE 97             local inv = meta:get_inventory()
98             if meta:get_int("splitstacks") == 1 then
99                 stack = stack:peek_item(1)
100             end
76a39e 101             return inv:room_for_item("main", stack)
7ed1aa 102         end,
T 103         insert_object = function(pos, node, stack, direction)
af3922 104             return minetest.get_meta(pos):get_inventory():add_item("main", stack)
7ed1aa 105         end,
8479a8 106         connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
7ed1aa 107     },
82cba9 108     sounds = default.node_sound_wood_defaults(),
R 109     on_construct = function(pos)
a41390 110         local meta = minetest.get_meta(pos)
827509 111         meta:set_string("infotext", S("Self-Contained Injector"))
82cba9 112         local inv = meta:get_inventory()
db0f8e 113         inv:set_size("main", 8*2)
fd26a8 114         meta:set_string("mode","single items")
827509 115         set_injector_formspec(meta)
82cba9 116     end,
R 117     can_dig = function(pos,player)
a41390 118         local meta = minetest.get_meta(pos);
82cba9 119         local inv = meta:get_inventory()
R 120         return inv:is_empty("main")
fd26a8 121     end,
R 122     on_receive_fields = function(pos, formanme, fields, sender)
03bb4d 123         if minetest.is_protected(pos, sender:get_player_name()) then return end
A 124
a41390 125         local meta = minetest.get_meta(pos)
827509 126         if fields.mode_item then meta:set_string("mode", "single items") end
Z 127         if fields.mode_stack then meta:set_string("mode", "whole stacks") end
a353a8 128
0f7810 129         if fields.mode_private then meta:set_int("public", 0) end
A 130         if fields.mode_public then meta:set_int("public", 1) end
131
a353a8 132         if fields["fs_helpers_cycling:0:splitstacks"]
VE 133           or fields["fs_helpers_cycling:1:splitstacks"] then
134             if not pipeworks.may_configure(pos, sender) then return end
135             fs_helpers.on_receive_fields(pos, fields)
136         end
827509 137         set_injector_formspec(meta)
82cba9 138     end,
0809dd 139     allow_metadata_inventory_put = technic.machine_inventory_put,
S 140     allow_metadata_inventory_take = technic.machine_inventory_take,
141     allow_metadata_inventory_move = technic.machine_inventory_move,
bccefd 142     after_place_node = pipeworks.after_place,
VE 143     after_dig_node = pipeworks.after_dig
82cba9 144 })
R 145
74cf47 146 minetest.register_abm({
78f16c 147     label = "Machines: run injector",
74cf47 148     nodenames = {"technic:injector"},
R 149     interval = 1,
150     chance = 1,
151     action = function(pos, node, active_object_count, active_object_count_wider)
35b10a 152         local pos1 = vector.add(pos, vector.new(0, -1, 0))
4f78a6 153         local node1 = minetest.get_node(pos1)
35b10a 154         if minetest.get_item_group(node1.name, "tubedevice") > 0 then
N 155             inject_items(pos)
156         end
82cba9 157     end,
R 158 })
159