mirror of
https://github.com/minetest-mods/item_drop.git
synced 2025-07-15 20:30:23 +02:00
Compare commits
1 Commits
soundpitch
...
nalc-1.0
Author | SHA1 | Date | |
---|---|---|---|
2730deca16 |
@ -3,7 +3,7 @@ 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
|
## Description
|
||||||
A highly configurable mod providing item magnet and in-world node drops
|
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.
|
||||||
@ -31,8 +31,6 @@ 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
|
||||||
@ -43,7 +41,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`, items are
|
* If `item_drop.pickup_keyinvert` is `true` (false by default), 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 @@
|
|||||||
A highly configurable mod providing item magnet and in-world node drops
|
This mod adds Minecraft like drop/pick up of items to Minetest.
|
||||||
|
177
init.lua
177
init.lua
@ -1,64 +1,26 @@
|
|||||||
local load_time_start = minetest.get_us_time()
|
local load_time_start = minetest.get_us_time()
|
||||||
math.randomseed(os.time())
|
pickup_radius = tonumber(minetest.settings:get("item_pickup_radius"))
|
||||||
|
|
||||||
-- Functions which can be overridden by mods
|
if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false and
|
||||||
item_drop = {
|
minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||||
-- This function is executed before picking up an item or making it fly to
|
local pickup_gain = tonumber(
|
||||||
-- the player. If it does not return true, the item is ignored.
|
minetest.settings:get("item_drop.pickup_sound_gain")) or
|
||||||
-- It is also executed before collecting the item after it flew to
|
tonumber(minetest.settings:get("item_pickup_gain")) or 0.2
|
||||||
-- 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", true)
|
minetest.settings:get_bool("item_drop.pickup_particle") ~= false
|
||||||
local pickup_radius = legacy_setting_getnumber("item_drop.pickup_radius",
|
local pickup_radius = tonumber(
|
||||||
"item_pickup_radius", 0.75)
|
minetest.settings:get("item_drop.pickup_radius")) or 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 = legacy_setting_getbool("item_drop.enable_pickup_key",
|
local key_triggered = minetest.settings:get_bool(
|
||||||
"enable_item_pickup_key", true)
|
"item_drop.enable_pickup_key") or
|
||||||
|
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") ~= false
|
"item_drop.pickup_keyinvert") or 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
|
||||||
@ -68,11 +30,6 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
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
|
||||||
@ -96,11 +53,9 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
|
|
||||||
-- 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,
|
||||||
pitch = (100 - (math.random(-2, 2)) * 10) / 100,
|
|
||||||
})
|
})
|
||||||
if pickup_particle then
|
if pickup_particle then
|
||||||
local item = minetest.registered_nodes[
|
local item = minetest.registered_nodes[
|
||||||
@ -139,7 +94,6 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
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
|
||||||
@ -147,7 +101,7 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
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:get_velocity(), {x=0, y=0, z=0}) then
|
or not vector.equals(object:getvelocity(), {x=0, y=0, z=0}) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local ent = object:get_luaentity()
|
local ent = object:get_luaentity()
|
||||||
@ -185,8 +139,7 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
end
|
end
|
||||||
local item = ItemStack(ent.itemstring)
|
local item = ItemStack(ent.itemstring)
|
||||||
if inv
|
if inv
|
||||||
and inv:room_for_item("main", item)
|
and inv:room_for_item("main", item) then
|
||||||
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
|
||||||
@ -262,7 +215,7 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local pos = player:get_pos()
|
local pos = player:getpos()
|
||||||
pos.y = pos.y+0.5
|
pos.y = pos.y+0.5
|
||||||
local inv
|
local inv
|
||||||
|
|
||||||
@ -271,8 +224,7 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
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
|
if ent then
|
||||||
and item_drop.can_pickup(ent, player) then
|
|
||||||
if not inv then
|
if not inv then
|
||||||
inv = player:get_inventory()
|
inv = player:get_inventory()
|
||||||
if not inv then
|
if not inv then
|
||||||
@ -286,17 +238,14 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
local flying_item
|
local flying_item
|
||||||
local pos2
|
local pos2
|
||||||
if magnet_mode then
|
if magnet_mode then
|
||||||
pos2 = object:get_pos()
|
pos2 = object:getpos()
|
||||||
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
|
||||||
-- The item is near enough to pick it
|
-- collect one item at a time to avoid the loud pop
|
||||||
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)
|
||||||
@ -330,78 +279,44 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
|
|||||||
minetest.after(3.0, pickup_step)
|
minetest.after(3.0, pickup_step)
|
||||||
end
|
end
|
||||||
|
|
||||||
if legacy_setting_getbool("item_drop.enable_item_drop", "enable_item_drop", true)
|
if minetest.settings:get_bool("item_drop.enable_item_drop") ~= false and
|
||||||
|
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
|
||||||
-- 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
|
|
||||||
|
|
||||||
-- 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
|
|
||||||
|
|
||||||
local vel = obj:get_velocity()
|
|
||||||
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
|
|
||||||
|
|
||||||
function minetest.handle_node_drops(pos, drops)
|
function minetest.handle_node_drops(pos, drops)
|
||||||
for i = 1,#drops do
|
for i = 1,#drops do
|
||||||
local item = drops[i]
|
local item = drops[i]
|
||||||
|
local count, name
|
||||||
if type(item) == "string" then
|
if type(item) == "string" then
|
||||||
-- The string is not necessarily only the item name,
|
count = 1
|
||||||
-- so always convert it to ItemStack
|
name = item
|
||||||
item = ItemStack(item)
|
else
|
||||||
|
count = item:get_count()
|
||||||
|
name = item:get_name()
|
||||||
end
|
end
|
||||||
local count = item:get_count()
|
|
||||||
local name = item:get_name()
|
|
||||||
|
|
||||||
-- Sometimes nothing should be dropped
|
if name == "" then
|
||||||
if name == ""
|
-- Sometimes nothing should be dropped
|
||||||
or not minetest.registered_items[name] then
|
|
||||||
count = 0
|
count = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
if count > 0 then
|
for _ = 1,count do
|
||||||
-- Split items if possible
|
local obj = minetest.add_item(pos, name)
|
||||||
local items_to_spawn = {item}
|
if not obj then
|
||||||
if can_split_item(item) then
|
error("Couldn't spawn item " .. name .. ", drops: " .. dump(drops))
|
||||||
for i = 1,count do
|
|
||||||
items_to_spawn[i] = name
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
spawn_items(pos, items_to_spawn)
|
local vel = obj:getvelocity()
|
||||||
|
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
|
||||||
@ -413,5 +328,5 @@ local msg = "[item_drop] loaded after ca. " .. time .. " seconds."
|
|||||||
if time > 0.01 then
|
if time > 0.01 then
|
||||||
print(msg)
|
print(msg)
|
||||||
else
|
else
|
||||||
minetest.log("info", msg)
|
minetest.log("action", msg)
|
||||||
end
|
end
|
||||||
|
1
mod.conf
1
mod.conf
@ -1,2 +1 @@
|
|||||||
name = item_drop
|
name = item_drop
|
||||||
description = A highly configurable mod providing item magnet and in-world node drops
|
|
||||||
|
BIN
screenshot.png
BIN
screenshot.png
Binary file not shown.
Before 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 true
|
item_drop.pickup_keyinvert (Invert pickup key) bool false
|
||||||
|
|
||||||
#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,6 +33,3 @@ 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
|
|
||||||
|
BIN
sounds/item_drop_pickup.2.ogg
Normal file
BIN
sounds/item_drop_pickup.2.ogg
Normal file
Binary file not shown.
BIN
sounds/item_drop_pickup.3.ogg
Normal file
BIN
sounds/item_drop_pickup.3.ogg
Normal file
Binary file not shown.
BIN
sounds/item_drop_pickup.4.ogg
Normal file
BIN
sounds/item_drop_pickup.4.ogg
Normal file
Binary file not shown.
Reference in New Issue
Block a user