mirror of
https://github.com/minetest-mods/item_drop.git
synced 2025-07-22 07:20:21 +02:00
Compare commits
24 Commits
pickerpart
...
c1637b1975
Author | SHA1 | Date | |
---|---|---|---|
c1637b1975 | |||
e10486d2aa | |||
532a3cb0d0 | |||
56d2eeda5c | |||
c52ff0ea55 | |||
8e89c148be | |||
726b5f4872 | |||
8e1878d101 | |||
f6b8bcd7a7 | |||
6b7ed8e7ca | |||
2730deca16 | |||
fc89b2d2b4 | |||
f5187108f1 | |||
902daba7cf | |||
3c9c0e34e3 | |||
eaa44e2a36 | |||
a69bdd2275 | |||
1b0bc8461c | |||
b42a64cfa4 | |||
d51b468428 | |||
293fd77614 | |||
3e6a731cab | |||
760acbc593 | |||
d282d1ab0c |
11
README.md
11
README.md
@ -3,10 +3,10 @@ By [PilzAdam](https://github.com/PilzAdam),
|
||||
[texmex](https://github.com/tacotexmex/), [hybriddog](https://github.com/hybriddog/).
|
||||
|
||||
## Description
|
||||
This mod adds Minecraft like drop/pick up of items to Minetest.
|
||||
A highly configurable mod providing item magnet and in-world node drops
|
||||
|
||||
## Licensing
|
||||
LGPLv2.1/CC BY-SA 3.0.
|
||||
LGPLv2.1/CC BY-SA 3.0. Particle code from WCILA mod by Aurailus, originally licensed MIT.
|
||||
|
||||
## Notes
|
||||
item_drop can be played with Minetest 0.4.16 or above. It was originally
|
||||
@ -19,6 +19,8 @@ developed by [PilzAdam](https://github.com/PilzAdam/item_drop).
|
||||
`true` (true by default) It does nothing in creative mode.
|
||||
* Puts dropped items to the player's inventory if `item_drop.enable_item_pickup`
|
||||
is `true` (true by default)
|
||||
* Multiple items are picked in a quick succession instead of all at once which
|
||||
is indicated by the pickup sound.
|
||||
* It uses a node radius set in `item_drop.pickup_radius` (default 0.75),
|
||||
if items are within this radius around the player's belt, they're picked.
|
||||
* If `item_drop.pickup_age` is something positive, items dropped by players
|
||||
@ -29,6 +31,8 @@ developed by [PilzAdam](https://github.com/PilzAdam/item_drop).
|
||||
items between these radii are flying to the player for
|
||||
`item_drop.magnet_time` seconds, after this time, they're picked or stop
|
||||
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
|
||||
`item_drop.pickup_sound_gain` (default 0.2)
|
||||
* Requires a key to be pressed in order to pick items if
|
||||
@ -41,6 +45,9 @@ developed by [PilzAdam](https://github.com/PilzAdam/item_drop).
|
||||
* Sneak key and right mouse button combined (`SneakAndRMB`)
|
||||
* 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.
|
||||
* Displays a particle of the picked item above the player if
|
||||
`item_drop.pickup_particle` is `true` (true by default)
|
||||
|
||||
|
||||
## Known issues
|
||||
|
||||
|
169
init.lua
169
init.lua
@ -1,7 +1,29 @@
|
||||
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 or
|
||||
-- Functions which can be overridden by mods
|
||||
item_drop = {
|
||||
-- This function is executed before picking up an item or making it fly to
|
||||
-- the player. If it does not return true, the item is ignored.
|
||||
-- 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,
|
||||
}
|
||||
|
||||
if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false and
|
||||
minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
local pickup_gain = tonumber(
|
||||
minetest.settings:get("item_drop.pickup_sound_gain")) or
|
||||
@ -9,7 +31,8 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
local pickup_particle =
|
||||
minetest.settings:get_bool("item_drop.pickup_particle") ~= false
|
||||
local pickup_radius = tonumber(
|
||||
minetest.settings:get("item_drop.pickup_radius")) or 0.75
|
||||
minetest.settings:get("item_drop.pickup_radius")) or
|
||||
tonumber(minetest.settings:get("item_pickup_radius")) or 0.75
|
||||
local magnet_radius = tonumber(
|
||||
minetest.settings:get("item_drop.magnet_radius")) or -1
|
||||
local magnet_time = tonumber(
|
||||
@ -30,6 +53,11 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
pickup_age = math.min(pickup_age, 0)
|
||||
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 zero_velocity_mode = pickup_age == -1
|
||||
@ -38,43 +66,50 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
error"zero velocity mode can't be used together with magnet mode"
|
||||
end
|
||||
|
||||
-- tells whether an inventorycube should be shown as pickup_particle or not
|
||||
-- for known drawtypes
|
||||
local inventorycube_drawtypes = {
|
||||
normal = true,
|
||||
allfaces = true,
|
||||
allfaces_optional = true,
|
||||
glasslike = true,
|
||||
glasslike_framed = true,
|
||||
glasslike_framed_optional = true,
|
||||
liquid = true,
|
||||
flowingliquid = true,
|
||||
}
|
||||
|
||||
-- adds the item to the inventory and removes the object
|
||||
local function collect_item(ent, pos, player)
|
||||
item_drop.before_collect(ent, pos, player)
|
||||
minetest.sound_play("item_drop_pickup", {
|
||||
pos = pos,
|
||||
gain = pickup_gain,
|
||||
})
|
||||
if pickup_particle then
|
||||
local item = minetest.registered_nodes[ent.itemstring:gsub("(.*)%s.*$","%1")]
|
||||
local item = minetest.registered_nodes[
|
||||
ent.itemstring:gsub("(.*)%s.*$", "%1")]
|
||||
local image = ""
|
||||
if minetest.registered_items[item.name] and minetest.registered_items[item.name].tiles then
|
||||
if minetest.registered_items[item.name].tiles[1] then
|
||||
local dt = minetest.registered_items[item.name].drawtype
|
||||
if dt == "normal" or dt == "allfaces" or dt == "allfaces_optional"
|
||||
or dt == "glasslike" or dt =="glasslike_framed" or dt == "glasslike_framed_optional"
|
||||
or dt == "liquid" or dt == "flowingliquid" then
|
||||
local tiles = minetest.registered_items[item.name].tiles
|
||||
if item and item.tiles and item.tiles[1] then
|
||||
if inventorycube_drawtypes[item.drawtype] then
|
||||
local tiles = item.tiles
|
||||
|
||||
local top = tiles[1]
|
||||
if (type(top) == "table") then top = top.item end
|
||||
local left = tiles[3]
|
||||
if not left then left = top end
|
||||
if (type(left) == "table") then left = left.item end
|
||||
local right = tiles[5]
|
||||
if not right then right = left end
|
||||
if (type(right) == "table") then right = right.item end
|
||||
local top = tiles[1]
|
||||
if type(top) == "table" then
|
||||
top = top.name
|
||||
end
|
||||
local left = tiles[3] or top
|
||||
if type(left) == "table" then
|
||||
left = left.name
|
||||
end
|
||||
local right = tiles[5] or left
|
||||
if type(right) == "table" then
|
||||
right = right.name
|
||||
end
|
||||
|
||||
image = "[inventorycube{"..top.."{"..left.."{"..right
|
||||
else
|
||||
image = minetest.registered_items[item.name].inventory_image
|
||||
if not image then image = minetest.registered_items[item.name].tiles[1] end
|
||||
end
|
||||
end
|
||||
end
|
||||
if item then
|
||||
local texture = item.tiles[1] or item.inventory_image or ""
|
||||
if item.drawtype == "normal" then
|
||||
texture = item.tiles
|
||||
image = minetest.inventorycube(top, left, right)
|
||||
else
|
||||
image = item.inventory_image or item.tiles[1]
|
||||
end
|
||||
minetest.add_particle({
|
||||
pos = {x = pos.x, y = pos.y + 1.5, z = pos.z},
|
||||
@ -88,6 +123,7 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
end
|
||||
end
|
||||
ent:on_punch(player)
|
||||
item_drop.after_collect(ent, pos, player)
|
||||
end
|
||||
|
||||
-- opt_get_ent gets the object's luaentity if it can be collected
|
||||
@ -133,16 +169,42 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
end
|
||||
local item = ItemStack(ent.itemstring)
|
||||
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)
|
||||
else
|
||||
object:setvelocity({x=0,y=0,z=0})
|
||||
ent.physical_state = true
|
||||
ent.object:set_properties({
|
||||
physical = true
|
||||
})
|
||||
-- the acceleration will be reset by the object's on_step
|
||||
object:set_velocity({x=0,y=0,z=0})
|
||||
ent.is_magnet_item = false
|
||||
end
|
||||
end
|
||||
|
||||
-- disable velocity and acceleration changes of items flying to players
|
||||
minetest.after(0, function()
|
||||
local ObjectRef
|
||||
local blocked_methods = {"set_acceleration", "set_velocity",
|
||||
"setacceleration", "setvelocity"}
|
||||
local itemdef = minetest.registered_entities["__builtin:item"]
|
||||
local old_on_step = itemdef.on_step
|
||||
local function do_nothing() end
|
||||
function itemdef.on_step(self, dtime)
|
||||
if not self.is_magnet_item then
|
||||
return old_on_step(self, dtime)
|
||||
end
|
||||
ObjectRef = ObjectRef or getmetatable(self.object)
|
||||
local old_funcs = {}
|
||||
for i = 1, #blocked_methods do
|
||||
local method = blocked_methods[i]
|
||||
old_funcs[method] = ObjectRef[method]
|
||||
ObjectRef[method] = do_nothing
|
||||
end
|
||||
old_on_step(self, dtime)
|
||||
for i = 1, #blocked_methods do
|
||||
local method = blocked_methods[i]
|
||||
ObjectRef[method] = old_funcs[method]
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- set keytype to the key name if possible
|
||||
@ -193,7 +255,8 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
for i = 1,#objectlist do
|
||||
local object = objectlist[i]
|
||||
local ent = opt_get_ent(object)
|
||||
if ent then
|
||||
if ent
|
||||
and item_drop.can_pickup(ent, player) then
|
||||
if not inv then
|
||||
inv = player:get_inventory()
|
||||
if not inv then
|
||||
@ -211,19 +274,19 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
flying_item = vector.distance(pos, pos2) > pickup_radius
|
||||
end
|
||||
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 one item at a time to avoid the loud pop
|
||||
return true
|
||||
end
|
||||
local vel = vector.multiply(
|
||||
vector.subtract(pos, pos2), 3)
|
||||
-- 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)
|
||||
vel.y = vel.y + 0.6
|
||||
object:setvelocity(vel)
|
||||
if ent.physical_state then
|
||||
ent.physical_state = false
|
||||
ent.object:set_properties({
|
||||
physical = false
|
||||
})
|
||||
object:set_velocity(vel)
|
||||
if not ent.is_magnet_item then
|
||||
ent.object:set_acceleration({x=0, y=0, z=0})
|
||||
ent.is_magnet_item = true
|
||||
|
||||
minetest.after(magnet_time, afterflight,
|
||||
object, inv, player)
|
||||
@ -251,7 +314,7 @@ minetest.settings:get_bool("enable_item_pickup") ~= false then
|
||||
minetest.after(3.0, pickup_step)
|
||||
end
|
||||
|
||||
if minetest.settings:get_bool("item_drop.enable_item_drop") ~= false or
|
||||
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
|
||||
function minetest.handle_node_drops(pos, drops)
|
||||
@ -266,10 +329,16 @@ and not minetest.settings:get_bool("creative_mode") then
|
||||
name = item:get_name()
|
||||
end
|
||||
|
||||
-- Sometimes nothing should be dropped
|
||||
if name == ""
|
||||
or not minetest.registered_nodes[name] then
|
||||
count = 0
|
||||
end
|
||||
|
||||
for _ = 1,count do
|
||||
local obj = minetest.add_item(pos, name)
|
||||
if not obj then
|
||||
error("Couldn't spawn item")
|
||||
error("Couldn't spawn item " .. name .. ", drops: " .. dump(drops))
|
||||
end
|
||||
|
||||
local vel = obj:getvelocity()
|
||||
@ -283,7 +352,7 @@ and not minetest.settings:get_bool("creative_mode") then
|
||||
z = z+1
|
||||
end
|
||||
vel.z = 1 / z
|
||||
obj:setvelocity(vel)
|
||||
obj:set_velocity(vel)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -295,5 +364,5 @@ local msg = "[item_drop] loaded after ca. " .. time .. " seconds."
|
||||
if time > 0.01 then
|
||||
print(msg)
|
||||
else
|
||||
minetest.log("info", msg)
|
||||
minetest.log("action", msg)
|
||||
end
|
||||
|
BIN
screenshot.png
Normal file
BIN
screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 216 KiB |
@ -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
|
||||
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