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