From 11e43ffe134f6483905b18662a544dec384b231c Mon Sep 17 00:00:00 2001
From: Cedric Haase <cedrichaase@mailbox.org>
Date: Wed, 06 Jan 2021 18:12:51 +0100
Subject: [PATCH] nuclear_reactor: Add error messages on start failure (#574)

---
 technic/machines/other/constructor.lua |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/technic/machines/other/constructor.lua b/technic/machines/other/constructor.lua
index 0a62a7c..9117224 100644
--- a/technic/machines/other/constructor.lua
+++ b/technic/machines/other/constructor.lua
@@ -88,6 +88,7 @@
 local function make_on(mark, length)
 	return function(pos, node)
 		local meta = minetest.get_meta(pos)
+		local owner = meta:get_string("owner")
 		local inv = meta:get_inventory()
 		local dir = vector.new()
 		if node.param2 == 3 then dir.x = 1 end
@@ -102,6 +103,9 @@
 			minetest.check_for_falling(pos)
 			for i = 1, length do
 				place_pos = vector.add(place_pos, dir)
+				if owner ~= "" and minetest.is_protected(place_pos, owner) then
+					return
+				end
 				local place_node = minetest.get_node(place_pos)
 				deploy_node(inv, "slot"..i, place_pos, place_node, node)
 			end
@@ -118,6 +122,12 @@
 	end
 end
 
+local function allow_inventory_put(pos, listname, index, stack, player)
+	if stack and minetest.get_item_group(stack:get_name(), "technic_constructor") == 1 then
+		return 0
+	end
+	return technic.machine_inventory_put(pos, listname, index, stack, player)
+end
 
 local function make_constructor(mark, length)
 	minetest.register_node("technic:constructor_mk"..mark.."_off", {
@@ -129,7 +139,8 @@
 			"technic_constructor_back.png",
 			"technic_constructor_front_off.png"},
 		paramtype2 = "facedir",
-		groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, mesecon = 2},
+		groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
+			mesecon = 2, technic_constructor = 1},
 		mesecons = {effector = {action_on = make_on(mark, length)}},
 		sounds = default.node_sound_stone_defaults(),
 		on_construct = function(pos)
@@ -149,6 +160,11 @@
 			for i = 1, length do
 				inv:set_size("slot"..i, 1)
 			end
+			meta:set_string("owner", "?")
+		end,
+		after_place_node = function(pos, placer)
+			local meta = minetest.get_meta(pos)
+			meta:set_string("owner", (placer and placer:get_player_name() or "?"))
 		end,
 		can_dig = function(pos, player)
 			local meta = minetest.get_meta(pos)
@@ -160,7 +176,7 @@
 			end
 			return true
 		end,
-		allow_metadata_inventory_put = technic.machine_inventory_put,
+		allow_metadata_inventory_put = allow_inventory_put,
 		allow_metadata_inventory_take = technic.machine_inventory_take,
 		allow_metadata_inventory_move = technic.machine_inventory_move,
 		on_rotate = screwdriver.rotate_simple
@@ -176,10 +192,10 @@
 		paramtype2 = "facedir",
 		drop = "technic:constructor_mk"..mark.."_off",
 		groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
-			mesecon=2, not_in_creative_inventory=1},
+			mesecon=2, not_in_creative_inventory=1, technic_constructor=1},
 		mesecons= {effector = {action_off = make_off(mark)}},
 		sounds = default.node_sound_stone_defaults(),
-		allow_metadata_inventory_put = technic.machine_inventory_put,
+		allow_metadata_inventory_put = allow_inventory_put,
 		allow_metadata_inventory_take = technic.machine_inventory_take,
 		allow_metadata_inventory_move = technic.machine_inventory_move,
 		on_rotate = false

--
Gitblit v1.8.0