From 2ce5c5415a81b06756f8dbe6d1f326d5d3d976ec Mon Sep 17 00:00:00 2001
From: kay27 <kay27@bk.ru>
Date: Sat, 27 Feb 2021 03:33:51 +0400
Subject: [PATCH] Fix village initialization:
 https://git.minetest.land/MineClone2/MineClone2/issues/1206
 https://git.minetest.land/MineClone2/MineClone2/issues/1207
 https://git.minetest.land/MineClone2/MineClone2/issues/1209

---
 mods/MAPGEN/mcl_villages/buildings.lua | 36 ++++++++++++++++++++++++-
 mods/MAPGEN/mcl_villages/init.lua      |  3 ---
 mods/MAPGEN/mcl_villages/utils.lua     | 37 --------------------------
 3 files changed, 35 insertions(+), 41 deletions(-)

diff --git a/mods/MAPGEN/mcl_villages/buildings.lua b/mods/MAPGEN/mcl_villages/buildings.lua
index 260fac432..18d6c1e0b 100644
--- a/mods/MAPGEN/mcl_villages/buildings.lua
+++ b/mods/MAPGEN/mcl_villages/buildings.lua
@@ -175,6 +175,36 @@ end
 -------------------------------------------------------------------------------
 -- evaluate settlement_info and place schematics
 -------------------------------------------------------------------------------
+-- Initialize node
+local function construct_node(p1, p2, name)
+	local r = minetest.registered_nodes[name]
+	if r then
+		if r.on_construct then
+			local nodes = minetest.find_nodes_in_area(p1, p2, name)
+			for p=1, #nodes do
+				local pos = nodes[p]
+				r.on_construct(pos)
+			end
+			return nodes
+		end
+		minetest.log("warning","[mcl_villages] No on_construct defined for node name " .. name)
+		return
+	end
+	minetest.log("warning","[mcl_villages] Attempt to 'construct' inexistant nodes: " .. name)
+end
+local function init_nodes(p1, p2, size, rotation, pr)
+	construct_node(p1, p2, "mcl_itemframes:item_frame")
+	construct_node(p1, p2, "mcl_furnaces:furnace")
+	construct_node(p1, p2, "mcl_anvils:anvil")
+
+	local nodes = construct_node(p1, p2, "mcl_chests:chest")
+	if nodes and #nodes > 0 then
+		for p=1, #nodes do
+			local pos = nodes[p]
+			settlements.fill_chest(pos, pr)
+		end
+	end
+end
 function settlements.place_schematics(settlement_info, pr)
 	local building_all_info
 	for i, built_house in ipairs(settlement_info) do
@@ -243,6 +273,10 @@ function settlements.place_schematics(settlement_info, pr)
 			schematic, 
 			rotation, 
 			nil, 
-			true)
+			true,
+			nil,
+			init_nodes,
+			pr
+		)
 	end
 end
diff --git a/mods/MAPGEN/mcl_villages/init.lua b/mods/MAPGEN/mcl_villages/init.lua
index 584155d96..d9c484f10 100644
--- a/mods/MAPGEN/mcl_villages/init.lua
+++ b/mods/MAPGEN/mcl_villages/init.lua
@@ -67,9 +67,6 @@ local function build_a_settlement(minp, maxp, blockseed)
 
 	-- evaluate settlement_info and place schematics
 	settlements.place_schematics(settlement_info, pr)
-
-	-- evaluate settlement_info and initialize furnaces and chests
-	settlements.initialize_nodes(settlement_info, pr)
 end
 
 local function ecb_village(blockpos, action, calls_remaining, param)
diff --git a/mods/MAPGEN/mcl_villages/utils.lua b/mods/MAPGEN/mcl_villages/utils.lua
index 2f411b301..2d96ba26f 100644
--- a/mods/MAPGEN/mcl_villages/utils.lua
+++ b/mods/MAPGEN/mcl_villages/utils.lua
@@ -218,43 +218,6 @@ function settlements.initialize_anvil(pos)
   end
 end
 -------------------------------------------------------------------------------
--- initialize furnace, chests, anvil
--------------------------------------------------------------------------------
-local building_all_info
-function settlements.initialize_nodes(settlement_info, pr)
-	for i, built_house in ipairs(settlement_info) do
-		for j, schem in ipairs(settlements.schematic_table) do
-			if settlement_info[i]["name"] == schem["name"] then
-				building_all_info = schem
-				break
-			end
-		end
-
-		local width = building_all_info["hwidth"]
-		local depth = building_all_info["hdepth"]
-		local height = building_all_info["hheight"]
-
-		local p = settlement_info[i]["pos"]
-		for yi = 1,height do
-			for xi = 0,width do
-				for zi = 0,depth do
-					local ptemp = {x=p.x+xi, y=p.y+yi, z=p.z+zi}
-					local node = mcl_mapgen_core.get_node(ptemp)
-					if node.name == "mcl_furnaces:furnace" or
-						node.name == "mcl_chests:chest" or
-						node.name == "mcl_anvils:anvil" then
-							minetest.registered_nodes[node.name].on_construct(ptemp)
-					end
-					-- when chest is found -> fill with stuff
-					if node.name == "mcl_chests:chest" then
-						minetest.after(3, settlements.fill_chest, ptemp, pr)
-					end
-				end
-			end
-		end
-	end
-end
--------------------------------------------------------------------------------
 -- randomize table
 -------------------------------------------------------------------------------
 function shuffle(tbl, pr)