mirror of
				https://github.com/minetest-mods/item_drop.git
				synced 2025-11-04 09:35:33 +01:00 
			
		
		
		
	Handle item drops more carefully
Do not ignore itemstrings which contain more than just the name; fixes missing clay drops Support dropped items with toolwear and/or metadata
This commit is contained in:
		
							
								
								
									
										68
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								init.lua
									
									
									
									
									
								
							@@ -330,28 +330,32 @@ end
 | 
			
		||||
 | 
			
		||||
if legacy_setting_getbool("item_drop.enable_item_drop", "enable_item_drop", true)
 | 
			
		||||
and not minetest.settings:get_bool("creative_mode") then
 | 
			
		||||
	function minetest.handle_node_drops(pos, drops)
 | 
			
		||||
		for i = 1,#drops do
 | 
			
		||||
			local item = drops[i]
 | 
			
		||||
			local count, name
 | 
			
		||||
			if type(item) == "string" then
 | 
			
		||||
				count = 1
 | 
			
		||||
				name = item
 | 
			
		||||
			else
 | 
			
		||||
				count = item:get_count()
 | 
			
		||||
				name = item:get_name()
 | 
			
		||||
	-- Workaround to test if an item metadata (ItemStackMetaRef) is empty
 | 
			
		||||
	local function itemmeta_is_empty(meta)
 | 
			
		||||
		local t = meta:to_table()
 | 
			
		||||
		for k, v in pairs(t) do
 | 
			
		||||
			if k ~= "fields" then
 | 
			
		||||
				return false
 | 
			
		||||
			end
 | 
			
		||||
			assert(type(v) == "table")
 | 
			
		||||
			if next(v) ~= nil then
 | 
			
		||||
				return false
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
		return true
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
			-- Sometimes nothing should be dropped
 | 
			
		||||
			if name == ""
 | 
			
		||||
			or not minetest.registered_items[name] then
 | 
			
		||||
				count = 0
 | 
			
		||||
	-- Tests if the item has special information such as metadata
 | 
			
		||||
	local function can_split_item(item)
 | 
			
		||||
		return item:get_wear() == 0 and itemmeta_is_empty(item:get_meta())
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
			for _ = 1,count do
 | 
			
		||||
				local obj = minetest.add_item(pos, name)
 | 
			
		||||
	local function spawn_items(pos, items_to_spawn)
 | 
			
		||||
		for i = 1,#items_to_spawn do
 | 
			
		||||
			local obj = minetest.add_item(pos, items_to_spawn[i])
 | 
			
		||||
			if not obj then
 | 
			
		||||
					error("Couldn't spawn item " .. name .. ", drops: " .. dump(drops))
 | 
			
		||||
				error("Couldn't spawn item " .. name .. ", drops: "
 | 
			
		||||
					.. dump(drops))
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			local vel = obj:get_velocity()
 | 
			
		||||
@@ -368,6 +372,36 @@ and not minetest.settings:get_bool("creative_mode") then
 | 
			
		||||
			obj:set_velocity(vel)
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	function minetest.handle_node_drops(pos, drops)
 | 
			
		||||
		for i = 1,#drops do
 | 
			
		||||
			local item = drops[i]
 | 
			
		||||
			if type(item) == "string" then
 | 
			
		||||
				-- The string is not necessarily only the item name,
 | 
			
		||||
				-- so always convert it to ItemStack
 | 
			
		||||
				item = ItemStack(item)
 | 
			
		||||
			end
 | 
			
		||||
			local count = item:get_count()
 | 
			
		||||
			local name = item:get_name()
 | 
			
		||||
 | 
			
		||||
			-- Sometimes nothing should be dropped
 | 
			
		||||
			if name == ""
 | 
			
		||||
			or not minetest.registered_items[name] then
 | 
			
		||||
				count = 0
 | 
			
		||||
			end
 | 
			
		||||
 | 
			
		||||
			if count > 0 then
 | 
			
		||||
				-- Split items if possible
 | 
			
		||||
				local items_to_spawn = {item}
 | 
			
		||||
				if can_split_item(item) then
 | 
			
		||||
					for i = 1,count do
 | 
			
		||||
						items_to_spawn[i] = name
 | 
			
		||||
					end
 | 
			
		||||
				end
 | 
			
		||||
 | 
			
		||||
				spawn_items(pos, items_to_spawn)
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user