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