From 4e402ec39fb1852b148e62637df0b72ae70ecd7d Mon Sep 17 00:00:00 2001
From: Gundul <gundul@mailbox.org>
Date: Sun, 18 Aug 2024 19:40:14 +0200
Subject: [PATCH] Fix crash if bones punched by non-player (#3146)

---
 mods/bones/init.lua | 37 ++++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 11 deletions(-)

diff --git a/mods/bones/init.lua b/mods/bones/init.lua
index 725f6622..85cb480f 100644
--- a/mods/bones/init.lua
+++ b/mods/bones/init.lua
@@ -16,6 +16,27 @@ local function is_owner(pos, name)
 	return false
 end
 
+local function drop(pos, itemstack)
+	local obj = minetest.add_item(pos, itemstack:take_item(itemstack:get_count()))
+	if obj then
+		obj:set_velocity({
+			x = math.random(-10, 10) / 9,
+			y = 5,
+			z = math.random(-10, 10) / 9,
+		})
+	end
+end
+
+local function drop_contents(pos)
+	local inv = minetest.get_meta(pos):get_inventory()
+
+	for i = 1, inv:get_size("main") do
+		local stk = inv:get_stack("main", i)
+		drop(pos, stk)
+	end
+	minetest.remove_node(pos)
+end
+
 local bones_formspec =
 	"size[8,9]" ..
 	"list[current_name;main;0,0.3;8,4;]" ..
@@ -87,6 +108,11 @@ local bones_def = {
 			return
 		end
 
+		if not player:is_player() then
+			drop_contents(pos)
+			return
+		end
+
 		if minetest.get_meta(pos):get_string("infotext") == "" then
 			return
 		end
@@ -171,17 +197,6 @@ local function may_replace(pos, player)
 	return node_definition.buildable_to
 end
 
-local drop = function(pos, itemstack)
-	local obj = minetest.add_item(pos, itemstack:take_item(itemstack:get_count()))
-	if obj then
-		obj:set_velocity({
-			x = math.random(-10, 10) / 9,
-			y = 5,
-			z = math.random(-10, 10) / 9,
-		})
-	end
-end
-
 local player_inventory_lists = { "main", "craft" }
 bones.player_inventory_lists = player_inventory_lists