]> git.lizzy.rs Git - mcl_wither_spawning.git/blob - init.lua
Merge pull request #1 from EliasFleckenstein03/add-license-1
[mcl_wither_spawning.git] / init.lua
1 local dim = {"x", "z"}
2
3 local modpath = minetest.get_modpath(minetest.get_current_modname())
4
5 local function load_schem(filename)
6         local file = io.open(modpath .. "/schems/" .. filename, "r")
7         local data = minetest.deserialize(file:read())
8         file:close()
9         return data
10 end
11
12 local wither_spawn_schems = {}
13
14 for _, d in pairs(dim) do
15         wither_spawn_schems[d] = load_schem("wither_spawn_" .. d .. ".we")
16 end
17
18 local function check_schem(pos, schem)
19         for _, n in pairs(schem) do
20                 if minetest.get_node(vector.add(pos, n)).name ~= n.name then
21                         return false
22                 end
23         end
24         return true
25 end
26
27 local function remove_schem(pos, schem)
28         for _, n in pairs(schem) do
29                 minetest.remove_node(vector.add(pos, n))
30         end
31 end
32
33 local function wither_spawn(pos)
34         for _, d in pairs(dim) do
35                 for i = 0, 2 do
36                         local p = vector.add(pos, {x = 0, y = -2, z = 0, [d] = -i})
37                         local schem = wither_spawn_schems[d]
38                         if check_schem(p, schem) then
39                                 remove_schem(p, schem)
40                                 minetest.add_entity(vector.add(p, {x = 0, y = 1, z = 0, [d] = 1}), "mobs_mc:wither")
41                         end
42                 end
43         end
44 end
45
46 minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack, pointed_thing)
47         if newnode.name == "mcl_heads:wither_skeleton" then
48                  minetest.after(0, wither_spawn, pos)
49         end
50 end)