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