Add a better mossycobble texture
[oweals/minetest_game.git] / mods / bucket / init.lua
1 -- Minetest 0.4 mod: bucket
2 -- See README.txt for licensing and other information.
3
4 local LIQUID_MAX = 8  --The number of water levels when liquid_finite is enabled
5
6 minetest.register_alias("bucket", "bucket:bucket_empty")
7 minetest.register_alias("bucket_water", "bucket:bucket_water")
8 minetest.register_alias("bucket_lava", "bucket:bucket_lava")
9
10 minetest.register_craft({
11         output = 'bucket:bucket_empty 1',
12         recipe = {
13                 {'default:steel_ingot', '', 'default:steel_ingot'},
14                 {'', 'default:steel_ingot', ''},
15         }
16 })
17
18 bucket = {}
19 bucket.liquids = {}
20
21 -- Register a new liquid
22 --   source = name of the source node
23 --   flowing = name of the flowing node
24 --   itemname = name of the new bucket item (or nil if liquid is not takeable)
25 --   inventory_image = texture of the new bucket item (ignored if itemname == nil)
26 -- This function can be called from any mod (that depends on bucket).
27 function bucket.register_liquid(source, flowing, itemname, inventory_image, name)
28         bucket.liquids[source] = {
29                 source = source,
30                 flowing = flowing,
31                 itemname = itemname,
32         }
33         bucket.liquids[flowing] = bucket.liquids[source]
34
35         if itemname ~= nil then
36                 minetest.register_craftitem(itemname, {
37                         description = name,
38                         inventory_image = inventory_image,
39                         stack_max = 1,
40                         liquids_pointable = true,
41                         groups = {not_in_creative_inventory=1},
42                         on_place = function(itemstack, user, pointed_thing)
43                                 -- Must be pointing to node
44                                 if pointed_thing.type ~= "node" then
45                                         return
46                                 end
47
48                                 local place_liquid = function(pos, node, source, flowing, fullness)
49                                         if math.floor(fullness/128) == 1 or (not minetest.setting_getbool("liquid_finite")) then
50                                                 minetest.env:add_node(pos, {name=source, param2=fullness})
51                                                 return
52                                         elseif node.name == flowing then
53                                                 fullness = fullness + node.param2
54                                         elseif node.name == source then
55                                                 fullness = LIQUID_MAX
56                                         end
57
58                                         if fullness >= LIQUID_MAX then
59                                                 minetest.env:add_node(pos, {name=source, param2=LIQUID_MAX})
60                                         else
61                                                 minetest.env:add_node(pos, {name=flowing, param2=fullness})
62                                         end
63                                 end
64
65                                 -- Check if pointing to a buildable node
66                                 local node = minetest.env:get_node(pointed_thing.under)
67                                 local fullness = tonumber(itemstack:get_metadata())
68                                 if not fullness then fullness = LIQUID_MAX end
69
70                                 if minetest.registered_nodes[node.name].buildable_to then
71                                         -- buildable; replace the node
72                                         place_liquid(pointed_thing.under, node, source, flowing, fullness)
73                                 else
74                                         -- not buildable to; place the liquid above
75                                         -- check if the node above can be replaced
76                                         local node = minetest.env:get_node(pointed_thing.above)
77                                         if minetest.registered_nodes[node.name].buildable_to then
78                                                 place_liquid(pointed_thing.above, node, source, flowing, fullness)
79                                         else
80                                                 -- do not remove the bucket with the liquid
81                                                 return
82                                         end
83                                 end
84                                 return {name="bucket:bucket_empty"}
85                         end
86                 })
87         end
88 end
89
90 minetest.register_craftitem("bucket:bucket_empty", {
91         description = "Empty Bucket",
92         inventory_image = "bucket.png",
93         stack_max = 1,
94         liquids_pointable = true,
95         on_use = function(itemstack, user, pointed_thing)
96                 -- Must be pointing to node
97                 if pointed_thing.type ~= "node" then
98                         return
99                 end
100                 -- Check if pointing to a liquid source
101                 node = minetest.env:get_node(pointed_thing.under)
102                 liquiddef = bucket.liquids[node.name]
103                 if liquiddef ~= nil and liquiddef.itemname ~= nil and (node.name == liquiddef.source or
104                         (node.name == liquiddef.flowing and minetest.setting_getbool("liquid_finite"))) then
105
106                         minetest.env:add_node(pointed_thing.under, {name="air"})
107
108                         if node.name == liquiddef.source then node.param2 = LIQUID_MAX end
109                         return ItemStack({name = liquiddef.itemname, metadata = tostring(node.param2)})
110                 end
111         end,
112 })
113
114 bucket.register_liquid(
115         "default:water_source",
116         "default:water_flowing",
117         "bucket:bucket_water",
118         "bucket_water.png",
119         "Water Bucket"
120 )
121
122 bucket.register_liquid(
123         "default:lava_source",
124         "default:lava_flowing",
125         "bucket:bucket_lava",
126         "bucket_lava.png",
127         "Lava Bucket"
128 )
129
130 minetest.register_craft({
131         type = "fuel",
132         recipe = "bucket:bucket_lava",
133         burntime = 60,
134         replacements = {{"bucket:bucket_lava", "bucket:bucket_empty"}},
135 })