forked from minetest-mods/item_drop
		
	Compare commits
	
		
			113 Commits
		
	
	
		
			nalc-1.0
			...
			29983259ee
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 29983259ee | |||
|  | de38b20fa6 | ||
|  | f015cc1631 | ||
|  | 72f4225058 | ||
|  | 7cb8086a25 | ||
| c6a80bd4fa | |||
|  | 8ef6ba3c0f | ||
|  | fadfbf7e05 | ||
|  | 682a345495 | ||
|  | 5611b4d639 | ||
|  | 871be4048e | ||
|  | 26d0532cbd | ||
|  | 95e89b1c94 | ||
|  | becdfd113a | ||
|  | faf6d9236d | ||
|  | 934ebff45a | ||
|  | c943cf2042 | ||
|  | 5069afa834 | ||
|  | 666ff41136 | ||
|  | 70b3386322 | ||
|  | 1157ec15df | ||
|  | 1e9e73e978 | ||
|  | 765bb0a405 | ||
|  | f7d62113ff | ||
|  | aef629f5b3 | ||
|  | 3a9a506011 | ||
|  | 6637bbb813 | ||
|  | 427117d786 | ||
| c1637b1975 | |||
| e10486d2aa | |||
|  | f8b60b0b05 | ||
|  | 532a3cb0d0 | ||
|  | 0aa32c1fed | ||
|  | 56d2eeda5c | ||
|  | 4c0a23855a | ||
|  | c52ff0ea55 | ||
|  | 0cee07b9d5 | ||
|  | 8e89c148be | ||
|  | 97158f7fac | ||
|  | 726b5f4872 | ||
|  | dd01ddf5ff | ||
|  | 8e1878d101 | ||
|  | d3687f027a | ||
|  | f6b8bcd7a7 | ||
|  | 62dbd6bc0d | ||
|  | 6b7ed8e7ca | ||
|  | 02683056e5 | ||
|  | 1d2e42eaab | ||
|  | 0112f1f9ec | ||
|  | 5f3e6e6b1b | ||
|  | 13be42cd39 | ||
|  | e2d370a17a | ||
|  | 50b976748d | ||
|  | 1901524074 | ||
|  | 608b5dfe6e | ||
|  | 1e5718cbac | ||
|  | c57d3c1514 | ||
|  | cce81f6fe9 | ||
|  | 88897d9049 | ||
|  | b6b1a8e95b | ||
|  | 08ba364510 | ||
|  | b576e882a7 | ||
|  | 15968a0240 | ||
|  | f1fcc478a1 | ||
|  | 3d88b7b5c6 | ||
|  | 70068fd307 | ||
|  | 2577b99556 | ||
|  | d5b28e01b0 | ||
|  | 41e93d3bf6 | ||
|  | 124e3588e5 | ||
|  | 464fce9a87 | ||
|  | cbd717b549 | ||
|  | 609d441ef4 | ||
|  | 3a9a985cfc | ||
|  | b5c7fbaa38 | ||
|  | 8543bad2aa | ||
|  | f811009c80 | ||
|  | ac2da1689e | ||
|  | 35786b597c | ||
|  | 1e2287a672 | ||
|  | 06c1212d5b | ||
|  | 253cc75c51 | ||
|  | 88df92b125 | ||
|  | 6de1402ebc | ||
|  | b06129c3a3 | ||
|  | c86bc605bd | ||
|  | c21158ffab | ||
|  | 9de8d39af6 | ||
|  | f2e3338265 | ||
|  | 31229760e8 | ||
|  | fced0590c6 | ||
|  | f91c73d2e3 | ||
|  | 8c3a470690 | ||
|  | ce059ad5ad | ||
|  | ac3386a402 | ||
|  | 0eace77417 | ||
|  | bfcc14bb4b | ||
|  | 04c441768f | ||
|  | 31e8c9b0cc | ||
|  | a88fa5f1b7 | ||
|  | ad4e83d243 | ||
|  | 07eb65bb31 | ||
|  | c63a4ee2a3 | ||
|  | 5aed10c3e7 | ||
|  | dbd9538a78 | ||
|  | 1091537584 | ||
|  | a7cdd68cc9 | ||
|  | dbb43c2ed1 | ||
|  | 67025939e7 | ||
|  | 7f38ae5261 | ||
|  | a523b5a574 | ||
|  | 04f75caeb3 | ||
|  | 2d42301d85 | 
							
								
								
									
										11
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | on: [push, pull_request] | ||||||
|  | name: build | ||||||
|  | jobs: | ||||||
|  |   lint: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |     steps: | ||||||
|  |       - uses: actions/checkout@master | ||||||
|  |       - name: lint | ||||||
|  |         uses: Roang-zero1/factorio-mod-luacheck@master | ||||||
|  |         with: | ||||||
|  |           luacheckrc_url: https://raw.githubusercontent.com/minetest-mods/item_drop/master/.luacheckrc | ||||||
							
								
								
									
										19
									
								
								.luacheckrc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								.luacheckrc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | unused_args = false | ||||||
|  | allow_defined_top = true | ||||||
|  | max_line_length = 999 | ||||||
|  |  | ||||||
|  | ignore = { | ||||||
|  |     "name", "drops", "i", | ||||||
|  | } | ||||||
|  |  | ||||||
|  | globals = { | ||||||
|  |     "minetest", | ||||||
|  | } | ||||||
|  |  | ||||||
|  | read_globals = { | ||||||
|  |     string = {fields = {"split", "trim"}}, | ||||||
|  |     table = {fields = {"copy", "getn"}}, | ||||||
|  |  | ||||||
|  |     "vector", "ItemStack", | ||||||
|  |     "dump", | ||||||
|  | } | ||||||
							
								
								
									
										13
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								README.md
									
									
									
									
									
								
							| @@ -1,15 +1,14 @@ | |||||||
| # item_drop | # Item Drop [](https://github.com/minetest-mods/item_drop/actions) [](https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html) | ||||||
|  |  | ||||||
|  | A highly configurable mod providing item magnet and in-world node drops\ | ||||||
| By [PilzAdam](https://github.com/PilzAdam), | By [PilzAdam](https://github.com/PilzAdam), | ||||||
| [texmex](https://github.com/tacotexmex/), [hybriddog](https://github.com/hybriddog/). | [texmex](https://github.com/tacotexmex/), [hybriddog](https://github.com/hybriddog/). | ||||||
|  |  | ||||||
| ## Description |  | ||||||
| This mod adds Minecraft like drop/pick up of items to Minetest. |  | ||||||
|  |  | ||||||
| ## Licensing | ## Licensing | ||||||
| LGPLv2.1/CC BY-SA 3.0. Particle code from WCILA mod by Aurailus, originally licensed MIT. | LGPLv2.1/CC BY-SA 3.0. Particle code from WCILA mod by Aurailus, originally licensed MIT. | ||||||
|  |  | ||||||
| ## Notes | ## Notes | ||||||
| item_drop can be played with Minetest 0.4.16 or above. It was originally | `item_drop` can be played with Minetest 0.4.16 or above. It was originally | ||||||
| developed by [PilzAdam](https://github.com/PilzAdam/item_drop). | developed by [PilzAdam](https://github.com/PilzAdam/item_drop). | ||||||
|  |  | ||||||
| ## List of features | ## List of features | ||||||
| @@ -31,6 +30,8 @@ developed by [PilzAdam](https://github.com/PilzAdam/item_drop). | |||||||
|     items between these radii are flying to the player for |     items between these radii are flying to the player for | ||||||
|     `item_drop.magnet_time` seconds, after this time, they're picked or stop |     `item_drop.magnet_time` seconds, after this time, they're picked or stop | ||||||
|     flying. |     flying. | ||||||
|  |   * Enable manual item pickups by mouse only if `item_drop.mouse_pickup` is | ||||||
|  |     `true` (true by default) | ||||||
| * Plays a sound when the items are picked up with the gain level set to | * Plays a sound when the items are picked up with the gain level set to | ||||||
|   `item_drop.pickup_sound_gain` (default 0.2) |   `item_drop.pickup_sound_gain` (default 0.2) | ||||||
| * Requires a key to be pressed in order to pick items if | * Requires a key to be pressed in order to pick items if | ||||||
| @@ -41,7 +42,7 @@ developed by [PilzAdam](https://github.com/PilzAdam/item_drop). | |||||||
|     * Left and Right keys combined (`LeftAndRight`) |     * Left and Right keys combined (`LeftAndRight`) | ||||||
|     * Right mouse button (`RMB`) |     * Right mouse button (`RMB`) | ||||||
|     * Sneak key and right mouse button combined (`SneakAndRMB`) |     * Sneak key and right mouse button combined (`SneakAndRMB`) | ||||||
|   * If `item_drop.pickup_keyinvert` is `true` (false by default), items are |   * If `item_drop.pickup_keyinvert` is `true`, items are | ||||||
|     collected when the key is not pressed instead of when it's pressed. |     collected when the key is not pressed instead of when it's pressed. | ||||||
| * Displays a particle of the picked item above the player if | * Displays a particle of the picked item above the player if | ||||||
|   `item_drop.pickup_particle` is `true` (true by default) |   `item_drop.pickup_particle` is `true` (true by default) | ||||||
|   | |||||||
| @@ -1 +1 @@ | |||||||
| This mod adds Minecraft like drop/pick up of items to Minetest. | A highly configurable mod providing item magnet and in-world node drops | ||||||
|   | |||||||
							
								
								
									
										234
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										234
									
								
								init.lua
									
									
									
									
									
								
							| @@ -1,26 +1,63 @@ | |||||||
| local load_time_start = minetest.get_us_time() | local load_time_start = minetest.get_us_time() | ||||||
| pickup_radius = tonumber(minetest.settings:get("item_pickup_radius")) |  | ||||||
|  |  | ||||||
| if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false and | -- Functions which can be overridden by mods | ||||||
| minetest.settings:get_bool("enable_item_pickup") ~= false then | item_drop = { | ||||||
| 	local pickup_gain = tonumber( | 	-- This function is executed before picking up an item or making it fly to | ||||||
| 		minetest.settings:get("item_drop.pickup_sound_gain")) or | 	-- the player. If it does not return true, the item is ignored. | ||||||
| 		tonumber(minetest.settings:get("item_pickup_gain")) or 0.2 | 	-- It is also executed before collecting the item after it flew to | ||||||
|  | 	-- the player and did not reach him/her for magnet_time seconds. | ||||||
|  | 	can_pickup = function(entity, player) | ||||||
|  | 		if entity.item_drop_picked then | ||||||
|  | 			-- Ignore items where picking has already failed | ||||||
|  | 			return false | ||||||
|  | 		end | ||||||
|  | 		return true | ||||||
|  | 	end, | ||||||
|  |  | ||||||
|  | 	-- before_collect and after_collect are executed before and after an item | ||||||
|  | 	-- is collected by a player | ||||||
|  | 	before_collect = function(entity, pos, player) | ||||||
|  | 	end, | ||||||
|  | 	after_collect = function(entity, pos, player) | ||||||
|  | 		entity.item_drop_picked = true | ||||||
|  | 	end, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | local function legacy_setting_getbool(name_new, name_old, default) | ||||||
|  | 	local v = minetest.settings:get_bool(name_new) | ||||||
|  | 	if v == nil then | ||||||
|  | 		v = minetest.settings:get_bool(name_new) | ||||||
|  | 	end | ||||||
|  | 	if default then | ||||||
|  | 		return v ~= false | ||||||
|  | 	end | ||||||
|  | 	return v | ||||||
|  | end | ||||||
|  |  | ||||||
|  | local function legacy_setting_getnumber(name_new, name_old, default) | ||||||
|  | 	return tonumber(minetest.settings:get(name_new)) | ||||||
|  | 		or tonumber(minetest.settings:get(name_old)) | ||||||
|  | 		or default | ||||||
|  | end | ||||||
|  |  | ||||||
|  | if legacy_setting_getbool("item_drop.enable_item_pickup", | ||||||
|  | 		"enable_item_pickup", true) then | ||||||
|  | 	local pickup_gain = legacy_setting_getnumber("item_drop.pickup_sound_gain", | ||||||
|  | 		"item_pickup_gain", 0.2) | ||||||
| 	local pickup_particle = | 	local pickup_particle = | ||||||
| 		minetest.settings:get_bool("item_drop.pickup_particle") ~= false | 		minetest.settings:get_bool("item_drop.pickup_particle", true) | ||||||
| 	local pickup_radius = tonumber( | 	local pickup_radius = legacy_setting_getnumber("item_drop.pickup_radius", | ||||||
| 		minetest.settings:get("item_drop.pickup_radius")) or 0.75 | 		"item_pickup_radius", 0.75) | ||||||
| 	local magnet_radius = tonumber( | 	local magnet_radius = tonumber( | ||||||
| 		minetest.settings:get("item_drop.magnet_radius")) or -1 | 		minetest.settings:get("item_drop.magnet_radius")) or -1 | ||||||
| 	local magnet_time = tonumber( | 	local magnet_time = tonumber( | ||||||
| 		minetest.settings:get("item_drop.magnet_time")) or 5.0 | 		minetest.settings:get("item_drop.magnet_time")) or 5.0 | ||||||
| 	local pickup_age = tonumber( | 	local pickup_age = tonumber( | ||||||
| 		minetest.settings:get("item_drop.pickup_age")) or 0.5 | 		minetest.settings:get("item_drop.pickup_age")) or 0.5 | ||||||
| 	local key_triggered = minetest.settings:get_bool( | 	local key_triggered = legacy_setting_getbool("item_drop.enable_pickup_key", | ||||||
| 		"item_drop.enable_pickup_key") or | 		"enable_item_pickup_key", true) | ||||||
| 		minetest.settings:get_bool("enable_item_pickup_key") ~= false |  | ||||||
| 	local key_invert = minetest.settings:get_bool( | 	local key_invert = minetest.settings:get_bool( | ||||||
| 		"item_drop.pickup_keyinvert") or false | 		"item_drop.pickup_keyinvert") ~= false | ||||||
| 	local keytype | 	local keytype | ||||||
| 	if key_triggered then | 	if key_triggered then | ||||||
| 		keytype = minetest.settings:get("item_drop.pickup_keytype") or | 		keytype = minetest.settings:get("item_drop.pickup_keytype") or | ||||||
| @@ -30,6 +67,11 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 			pickup_age = math.min(pickup_age, 0) | 			pickup_age = math.min(pickup_age, 0) | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
|  | 	local mouse_pickup = minetest.settings:get_bool( | ||||||
|  | 		"item_drop.mouse_pickup") ~= false | ||||||
|  | 	if not mouse_pickup then | ||||||
|  | 		minetest.registered_entities["__builtin:item"].pointable = false | ||||||
|  | 	end | ||||||
|  |  | ||||||
| 	local magnet_mode = magnet_radius > pickup_radius | 	local magnet_mode = magnet_radius > pickup_radius | ||||||
| 	local zero_velocity_mode = pickup_age == -1 | 	local zero_velocity_mode = pickup_age == -1 | ||||||
| @@ -53,6 +95,7 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
|  |  | ||||||
| 	-- adds the item to the inventory and removes the object | 	-- adds the item to the inventory and removes the object | ||||||
| 	local function collect_item(ent, pos, player) | 	local function collect_item(ent, pos, player) | ||||||
|  | 		item_drop.before_collect(ent, pos, player) | ||||||
| 		minetest.sound_play("item_drop_pickup", { | 		minetest.sound_play("item_drop_pickup", { | ||||||
| 			pos = pos, | 			pos = pos, | ||||||
| 			gain = pickup_gain, | 			gain = pickup_gain, | ||||||
| @@ -60,7 +103,7 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 		if pickup_particle then | 		if pickup_particle then | ||||||
| 			local item = minetest.registered_nodes[ | 			local item = minetest.registered_nodes[ | ||||||
| 				ent.itemstring:gsub("(.*)%s.*$", "%1")] | 				ent.itemstring:gsub("(.*)%s.*$", "%1")] | ||||||
| 			local image = "" | 			local image | ||||||
| 			if item and item.tiles and item.tiles[1] then | 			if item and item.tiles and item.tiles[1] then | ||||||
| 				if inventorycube_drawtypes[item.drawtype] then | 				if inventorycube_drawtypes[item.drawtype] then | ||||||
| 					local tiles = item.tiles | 					local tiles = item.tiles | ||||||
| @@ -94,6 +137,7 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 		ent:on_punch(player) | 		ent:on_punch(player) | ||||||
|  | 		item_drop.after_collect(ent, pos, player) | ||||||
| 	end | 	end | ||||||
|  |  | ||||||
| 	-- opt_get_ent gets the object's luaentity if it can be collected | 	-- opt_get_ent gets the object's luaentity if it can be collected | ||||||
| @@ -101,7 +145,7 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 	if zero_velocity_mode then | 	if zero_velocity_mode then | ||||||
| 		function opt_get_ent(object) | 		function opt_get_ent(object) | ||||||
| 			if object:is_player() | 			if object:is_player() | ||||||
| 			or not vector.equals(object:getvelocity(), {x=0, y=0, z=0}) then | 			or not vector.equals(object:get_velocity(), {x=0, y=0, z=0}) then | ||||||
| 				return | 				return | ||||||
| 			end | 			end | ||||||
| 			local ent = object:get_luaentity() | 			local ent = object:get_luaentity() | ||||||
| @@ -139,7 +183,8 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 			end | 			end | ||||||
| 			local item = ItemStack(ent.itemstring) | 			local item = ItemStack(ent.itemstring) | ||||||
| 			if inv | 			if inv | ||||||
| 			and inv:room_for_item("main", item) then | 			and inv:room_for_item("main", item) | ||||||
|  | 			and item_drop.can_pickup(ent, player) then | ||||||
| 				collect_item(ent, object:get_pos(), player) | 				collect_item(ent, object:get_pos(), player) | ||||||
| 			else | 			else | ||||||
| 				-- the acceleration will be reset by the object's on_step | 				-- the acceleration will be reset by the object's on_step | ||||||
| @@ -156,9 +201,9 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 			local itemdef = minetest.registered_entities["__builtin:item"] | 			local itemdef = minetest.registered_entities["__builtin:item"] | ||||||
| 			local old_on_step = itemdef.on_step | 			local old_on_step = itemdef.on_step | ||||||
| 			local function do_nothing() end | 			local function do_nothing() end | ||||||
| 			function itemdef.on_step(self, dtime) | 			function itemdef.on_step(self, ...) | ||||||
| 				if not self.is_magnet_item then | 				if not self.is_magnet_item then | ||||||
| 					return old_on_step(self, dtime) | 					return old_on_step(self, ...) | ||||||
| 				end | 				end | ||||||
| 				ObjectRef = ObjectRef or getmetatable(self.object) | 				ObjectRef = ObjectRef or getmetatable(self.object) | ||||||
| 				local old_funcs = {} | 				local old_funcs = {} | ||||||
| @@ -167,7 +212,7 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 					old_funcs[method] = ObjectRef[method] | 					old_funcs[method] = ObjectRef[method] | ||||||
| 					ObjectRef[method] = do_nothing | 					ObjectRef[method] = do_nothing | ||||||
| 				end | 				end | ||||||
| 				old_on_step(self, dtime) | 				old_on_step(self, ...) | ||||||
| 				for i = 1, #blocked_methods do | 				for i = 1, #blocked_methods do | ||||||
| 					local method = blocked_methods[i] | 					local method = blocked_methods[i] | ||||||
| 					ObjectRef[method] = old_funcs[method] | 					ObjectRef[method] = old_funcs[method] | ||||||
| @@ -207,6 +252,13 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 		return keys_pressed ~= key_invert | 		return keys_pressed ~= key_invert | ||||||
| 	end | 	end | ||||||
|  |  | ||||||
|  | 	local function is_inside_map(pos) | ||||||
|  | 		local bound = 31000 | ||||||
|  | 		return -bound < pos.x and pos.x < bound | ||||||
|  | 			and -bound < pos.y and pos.y < bound | ||||||
|  | 			and -bound < pos.z and pos.z < bound | ||||||
|  | 	end | ||||||
|  |  | ||||||
| 	-- called for each player to possibly collect an item, returns true if so | 	-- called for each player to possibly collect an item, returns true if so | ||||||
| 	local function pickupfunc(player) | 	local function pickupfunc(player) | ||||||
| 		if not has_keys_pressed(player) | 		if not has_keys_pressed(player) | ||||||
| @@ -215,37 +267,37 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 			return | 			return | ||||||
| 		end | 		end | ||||||
|  |  | ||||||
| 		local pos = player:getpos() | 		local pos = player:get_pos() | ||||||
|  | 		if not is_inside_map(pos) then | ||||||
|  | 			-- get_objects_inside_radius crashes for too far positions | ||||||
|  | 			return | ||||||
|  | 		end | ||||||
| 		pos.y = pos.y+0.5 | 		pos.y = pos.y+0.5 | ||||||
| 		local inv | 		local inv = player:get_inventory() | ||||||
|  |  | ||||||
| 		local objectlist = minetest.get_objects_inside_radius(pos, | 		local objectlist = minetest.get_objects_inside_radius(pos, | ||||||
| 			magnet_mode and magnet_radius or pickup_radius) | 			magnet_mode and magnet_radius or pickup_radius) | ||||||
| 		for i = 1,#objectlist do | 		for i = 1,#objectlist do | ||||||
| 			local object = objectlist[i] | 			local object = objectlist[i] | ||||||
| 			local ent = opt_get_ent(object) | 			local ent = opt_get_ent(object) | ||||||
| 			if ent then | 			if ent | ||||||
| 				if not inv then | 			and item_drop.can_pickup(ent, player) then | ||||||
| 					inv = player:get_inventory() |  | ||||||
| 					if not inv then |  | ||||||
| 						minetest.log("error", "[item_drop] Couldn't " .. |  | ||||||
| 							"get inventory") |  | ||||||
| 						return |  | ||||||
| 					end |  | ||||||
| 				end |  | ||||||
| 				local item = ItemStack(ent.itemstring) | 				local item = ItemStack(ent.itemstring) | ||||||
| 				if inv:room_for_item("main", item) then | 				if inv:room_for_item("main", item) then | ||||||
| 					local flying_item | 					local flying_item | ||||||
| 					local pos2 | 					local pos2 | ||||||
| 					if magnet_mode then | 					if magnet_mode then | ||||||
| 						pos2 = object:getpos() | 						pos2 = object:get_pos() | ||||||
| 						flying_item = vector.distance(pos, pos2) > pickup_radius | 						flying_item = vector.distance(pos, pos2) > pickup_radius | ||||||
| 					end | 					end | ||||||
| 					if not flying_item then | 					if not flying_item then | ||||||
| 						-- collect one item at a time to avoid the loud pop | 						-- The item is near enough to pick it | ||||||
| 						collect_item(ent, pos, player) | 						collect_item(ent, pos, player) | ||||||
|  | 						-- Collect one item at a time to avoid the loud pop | ||||||
| 						return true | 						return true | ||||||
| 					end | 					end | ||||||
|  | 					-- The item is not too far a way but near enough to be | ||||||
|  | 					-- magnetised, make it fly to the player | ||||||
| 					local vel = vector.multiply(vector.subtract(pos, pos2), 3) | 					local vel = vector.multiply(vector.subtract(pos, pos2), 3) | ||||||
| 					vel.y = vel.y + 0.6 | 					vel.y = vel.y + 0.6 | ||||||
| 					object:set_velocity(vel) | 					object:set_velocity(vel) | ||||||
| @@ -279,49 +331,105 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then | |||||||
| 	minetest.after(3.0, pickup_step) | 	minetest.after(3.0, pickup_step) | ||||||
| end | end | ||||||
|  |  | ||||||
| if minetest.settings:get_bool("item_drop.enable_item_drop") ~= false and | if legacy_setting_getbool("item_drop.enable_item_drop", "enable_item_drop", true) | ||||||
| minetest.settings:get_bool("enable_item_drop") ~= false |  | ||||||
| and not minetest.settings:get_bool("creative_mode") then | and not minetest.settings:get_bool("creative_mode") then | ||||||
| 	function minetest.handle_node_drops(pos, drops) | 	-- Workaround to test if an item metadata (ItemStackMetaRef) is empty | ||||||
| 		for i = 1,#drops do | 	local function itemmeta_is_empty(meta) | ||||||
| 			local item = drops[i] | 		local t = meta:to_table() | ||||||
| 			local count, name | 		for k, v in pairs(t) do | ||||||
| 			if type(item) == "string" then | 			if k ~= "fields" then | ||||||
| 				count = 1 | 				return false | ||||||
| 				name = item | 			end | ||||||
| 			else | 			assert(type(v) == "table") | ||||||
| 				count = item:get_count() | 			if next(v) ~= nil then | ||||||
| 				name = item:get_name() | 				return false | ||||||
|  | 			end | ||||||
|  | 		end | ||||||
|  | 		return true | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	-- 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 | ||||||
|  |  | ||||||
|  | 	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)) | ||||||
| 			end | 			end | ||||||
|  |  | ||||||
| 			if name == "" then | 			local vel = obj:get_velocity() | ||||||
| 				-- Sometimes nothing should be dropped | 			local x = math.random(-5, 4) | ||||||
|  | 			if x >= 0 then | ||||||
|  | 				x = x+1 | ||||||
|  | 			end | ||||||
|  | 			vel.x = 1 / x | ||||||
|  | 			local z = math.random(-5, 4) | ||||||
|  | 			if z >= 0 then | ||||||
|  | 				z = z+1 | ||||||
|  | 			end | ||||||
|  | 			vel.z = 1 / z | ||||||
|  | 			obj:set_velocity(vel) | ||||||
|  | 		end | ||||||
|  | 	end | ||||||
|  |  | ||||||
|  | 	local old_handle_node_drops = minetest.handle_node_drops | ||||||
|  | 	function minetest.handle_node_drops(pos, drops, player) | ||||||
|  | 		if player.is_fake_player then | ||||||
|  | 			-- Node Breaker or similar machines should receive items in the | ||||||
|  | 			-- inventory | ||||||
|  | 			return old_handle_node_drops(pos, drops, player) | ||||||
|  | 		end | ||||||
|  | 		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 | 				count = 0 | ||||||
| 			end | 			end | ||||||
|  |  | ||||||
| 			for _ = 1,count do | 			if count > 0 then | ||||||
| 				local obj = minetest.add_item(pos, name) | 				-- Split items if possible | ||||||
| 				if not obj then | 				local items_to_spawn = {item} | ||||||
| 					error("Couldn't spawn item " .. name .. ", drops: " .. dump(drops)) | 				if can_split_item(item) then | ||||||
|  | 					for i = 1,count do | ||||||
|  | 						items_to_spawn[i] = name | ||||||
|  | 					end | ||||||
| 				end | 				end | ||||||
|  |  | ||||||
| 				local vel = obj:getvelocity() | 				spawn_items(pos, items_to_spawn) | ||||||
| 				local x = math.random(-5, 4) |  | ||||||
| 				if x >= 0 then |  | ||||||
| 					x = x+1 |  | ||||||
| 				end |  | ||||||
| 				vel.x = 1 / x |  | ||||||
| 				local z = math.random(-5, 4) |  | ||||||
| 				if z >= 0 then |  | ||||||
| 					z = z+1 |  | ||||||
| 				end |  | ||||||
| 				vel.z = 1 / z |  | ||||||
| 				obj:set_velocity(vel) |  | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| end |  | ||||||
|  |  | ||||||
|  | 	local function pickup_step() | ||||||
|  | 		local got_item | ||||||
|  | 		local players = minetest.get_connected_players() | ||||||
|  | 		for i = 1,#players do | ||||||
|  | 			got_item = got_item or pickupfunc(players[i]) | ||||||
|  | 		end | ||||||
|  | 		-- lower step if takeable item(s) were found | ||||||
|  | 		local time | ||||||
|  | 		if got_item then | ||||||
|  | 			time = 0.02 | ||||||
|  | 		else | ||||||
|  | 			time = 0.2 | ||||||
|  | 		end | ||||||
|  | 		minetest.after(time, pickup_step) | ||||||
|  | 	end | ||||||
|  | 	minetest.after(3.0, pickup_step) | ||||||
|  | end | ||||||
|  |  | ||||||
| local time = (minetest.get_us_time() - load_time_start) / 1000000 | local time = (minetest.get_us_time() - load_time_start) / 1000000 | ||||||
| local msg = "[item_drop] loaded after ca. " .. time .. " seconds." | local msg = "[item_drop] loaded after ca. " .. time .. " seconds." | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								mod.conf
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								mod.conf
									
									
									
									
									
								
							| @@ -1 +1,2 @@ | |||||||
| name = item_drop | name = item_drop | ||||||
|  | description = A highly configurable mod providing item magnet and in-world node drops | ||||||
|   | |||||||
							
								
								
									
										
											BIN
										
									
								
								screenshot.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								screenshot.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 216 KiB | 
| @@ -8,7 +8,7 @@ item_drop.enable_item_drop (Enable item drops) bool true | |||||||
| item_drop.enable_pickup_key (Use pickup key) bool true | item_drop.enable_pickup_key (Use pickup key) bool true | ||||||
|  |  | ||||||
| #Collect items when the key is not pressed instead of when it is pressed | #Collect items when the key is not pressed instead of when it is pressed | ||||||
| item_drop.pickup_keyinvert (Invert pickup key) bool false | item_drop.pickup_keyinvert (Invert pickup key) bool true | ||||||
|  |  | ||||||
| #What keytype to use as pickup key | #What keytype to use as pickup key | ||||||
| item_drop.pickup_keytype (Pickup keytype) enum Use Use,Sneak,LeftAndRight,RMB,SneakAndRMB | item_drop.pickup_keytype (Pickup keytype) enum Use Use,Sneak,LeftAndRight,RMB,SneakAndRMB | ||||||
| @@ -33,3 +33,6 @@ item_drop.magnet_time (Magnet time) float 5.0 | |||||||
|  |  | ||||||
| #Time delay in seconds after autopicking an item if it's dropped by a player | #Time delay in seconds after autopicking an item if it's dropped by a player | ||||||
| item_drop.pickup_age (Pickup age) float 0.5 | item_drop.pickup_age (Pickup age) float 0.5 | ||||||
|  |  | ||||||
|  | #Enable manual item pickups by mouse | ||||||
|  | item_drop.mouse_pickup (Mouse pickup) bool true | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user