ShadowNinja
2013-12-17 5cf765b2f19ef9bf443178e26787fe16233b3f4c
commit | author | age
82cba9 1
354ee6 2 local S = technic.getter
S 3
91cdd1 4 minetest.register_craft({
R 5     output = 'technic:injector 1',
6     recipe = {
7         {'', 'technic:control_logic_unit',''},
8         {'', 'default:chest',''},
9         {'', 'pipeworks:tube_000000',''},
10     }
11 })
12
82cba9 13 minetest.register_node("technic:injector", {
354ee6 14     description = S("Injector"),
74cf47 15     tiles = {"technic_injector_top.png", "technic_injector_bottom.png", "technic_injector_side.png",
R 16         "technic_injector_side.png", "technic_injector_side.png", "technic_injector_side.png"},
17     groups = chest_groups1,
18     tube = tubes_properties,
82cba9 19     sounds = default.node_sound_wood_defaults(),
R 20     on_construct = function(pos)
21         local meta = minetest.env:get_meta(pos)
22         meta:set_string("formspec",
74cf47 23                 "invsize[8,9;]"..
354ee6 24                 "label[0,0;"..S("Injector").."]"..
fd26a8 25                 "button[0,1;.8,.8;mode;]"..
R 26                 "label[.8,1;Mode: single items]"..
82cba9 27                 "list[current_name;main;0,2;8,2;]"..
R 28                 "list[current_player;main;0,5;8,4;]")
354ee6 29         meta:set_string("infotext", S("Injector"))
82cba9 30         local inv = meta:get_inventory()
R 31         inv:set_size("main", 8*4)
fd26a8 32         meta:set_string("mode","single items")
82cba9 33     end,
R 34     can_dig = function(pos,player)
35         local meta = minetest.env:get_meta(pos);
36         local inv = meta:get_inventory()
37         return inv:is_empty("main")
fd26a8 38     end,
R 39     on_receive_fields = function(pos, formanme, fields, sender)
0809dd 40         local meta = minetest.env:get_meta(pos)
S 41         local mode=meta:get_string("mode")
42         if fields.mode then 
43             if mode == "single items" then
44                 mode = "whole stacks" 
45             else
46                 mode = "single items"
47             end
48             meta:set_string("mode", mode)
fd26a8 49         end
0809dd 50         meta:set_string("formspec",
fd26a8 51                 "invsize[8,9;]"..
354ee6 52                 "label[0,0;"..S("Injector").."]"..
fd26a8 53                 "button[0,1;.8,.8;mode;]"..
R 54                 "label[.8,1;Mode: "..mode.."]"..
55                 "list[current_name;main;0,2;8,2;]"..
56                 "list[current_player;main;0,5;8,4;]")
82cba9 57     end,
0809dd 58     allow_metadata_inventory_put = technic.machine_inventory_put,
S 59     allow_metadata_inventory_take = technic.machine_inventory_take,
60     allow_metadata_inventory_move = technic.machine_inventory_move,
82cba9 61 })
R 62
74cf47 63 minetest.register_abm({
R 64     nodenames = {"technic:injector"},
65     interval = 1,
66     chance = 1,
67     action = function(pos, node, active_object_count, active_object_count_wider)
68     local pos1={}
69     pos1.x = pos.x
70     pos1.y = pos.y-1
71     pos1.z = pos.z
72     local meta=minetest.env:get_meta(pos1) 
73         if meta:get_int("tubelike")==1 then inject_items (pos) end
82cba9 74     end,
R 75 })
76
5cf765 77 local function inject_items (pos)
74cf47 78         local meta=minetest.env:get_meta(pos) 
R 79         local inv = meta:get_inventory()
fd26a8 80         local mode=meta:get_string("mode")
R 81         if mode=="single items" then
82             local i=0
83             for _,stack in ipairs(inv:get_list("main")) do
84             i=i+1
85                 if stack then
86                 local item0=stack:to_table()
87                 if item0 then 
88                     item0["count"]="1"
8ef3f2 89                     local item1=pipeworks.tube_item({x=pos.x,y=pos.y,z=pos.z},item0)
fd26a8 90                     item1:get_luaentity().start_pos = {x=pos.x,y=pos.y,z=pos.z}
R 91                     item1:setvelocity({x=0, y=-1, z=0})
92                     item1:setacceleration({x=0, y=0, z=0})
93                     stack:take_item(1);
94                     inv:set_stack("main", i, stack)
95                     return
96                     end
74cf47 97                 end
R 98             end
99         end
fd26a8 100         if mode=="whole stacks" then
R 101             local i=0
102             for _,stack in ipairs(inv:get_list("main")) do
103             i=i+1
104                 if stack then
105                 local item0=stack:to_table()
106                 if item0 then 
8ef3f2 107                     local item1=pipeworks.tube_item({x=pos.x,y=pos.y,z=pos.z},item0)
fd26a8 108                     item1:get_luaentity().start_pos = {x=pos.x,y=pos.y,z=pos.z}
R 109                     item1:setvelocity({x=0, y=-1, z=0})
110                     item1:setacceleration({x=0, y=0, z=0})
111                     stack:clear()
112                     inv:set_stack("main", i, stack)
113                     return
114                     end
115                 end
116             end
117         end
118         
82cba9 119 end