-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
97 lines (84 loc) · 2.78 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
-- split_plant_name splits a node name, like "farming:wheat_8"
-- to "farming:wheat" and "8".
local function split_plantname(nodename)
return string.match(nodename, "(.+)_(%d+)")
end
-- get_plantname_from_seedname returns a full plantname (modname + plantname)
-- from a seed node name.
local function get_plantname_from_seedname(nodename)
local modname, plantname = string.match(nodename, "(.+):seed_(.+)")
if (not modname) or (not plantname) then
return nil
end
return modname .. ":" .. plantname
end
-- plants_maxsize represents a table that contains base plant names
-- and its maximum size.
local plants_maxsize = {}
-- register plants_maxsize table.
minetest.after(0, function()
for nodename, _ in pairs(minetest.registered_nodes) do
if minetest.get_item_group(nodename, "plant") > 0 then
local plantname, size = split_plantname(nodename)
if (not plantname) or (not size) then
break
end
if not plants_maxsize[plantname] or plants_maxsize[plantname] < size then
plants_maxsize[plantname] = size
end
end
end
end)
-- grow_up grows up a plant that exists at position pos.
local function grow_up(pos, plantname)
local maxsize = plants_maxsize[plantname]
local new_nodename = plantname .. "_" .. tostring(maxsize)
minetest.set_node(pos, {name = new_nodename})
end
-- can_grow_tree represents whether trees can be grown by this mod.
local can_grow_tree =
default.can_grow and default.grow_sapling
-- on_use is a callback that called when speedgrow is used.
local function on_use(itemstack, user, pointed_thing)
if pointed_thing.under == nil then
return nil
end
local position = pointed_thing.under
local node = minetest.get_node(position)
if minetest.get_item_group(node.name, "plant") > 0 then
local plantname, size = split_plantname(node.name)
if (not plantname) or (not size) then
return nil
end
grow_up(position, plantname)
elseif minetest.get_item_group(node.name, "seed") > 0 then
local plantname = get_plantname_from_seedname(node.name)
if not plantname then
return nil
end
grow_up(position, plantname)
elseif can_grow_tree and minetest.get_item_group(node.name, "sapling") > 0 then
if default.can_grow(position) then
default.grow_sapling(position)
end
end
return nil
end
-- register a definition of speedgrow.
minetest.register_craftitem("speedgrow:speedgrow", {
description = "Speedgrow",
inventory_image = "speedgrow.png",
wield_image = "speedgrow.png",
stack_max = 1,
groups = {speedgrow = 1},
on_use = on_use,
})
-- register a craft of speedgrow.
minetest.register_craft{
output = "speedgrow:speedgrow",
recipe = {
{"default:leaves", "default:leaves", "default:leaves"},
{"default:leaves", "default:tree", "default:leaves"},
{ "", "default:tree", ""},
},
}