Compare commits

...

10 Commits

Author SHA1 Message Date
HybridDog
8bebf6324e
Fix error when picking up a node with deprecated tile definition (#40)
Some checks failed
build / lint (push) Has been cancelled
If `image` instead of `tile` was used, the mod passed nil to minetest.inventorycube.
The color field of a tile is now also supported (the code is used only for the particle image calculation).
2022-10-06 18:36:50 +02:00
sfan5
d19d00d690
Mark pickup sound as ephemeral 2021-09-02 23:42:56 +02:00
HybridDog
1545f82cb7
Fix error for 'nil' player in minetest.handle_node_drops (#36) 2020-09-27 19:15:57 +02:00
David Leal
de38b20fa6
Add GitHub workflow and LuaCheck (#34)
* Add GitHub workflow and LuaCheck

* Fix LuaCheck warnings

* Improve README.md
2020-05-25 20:42:09 +02:00
HybridDog
f015cc1631 Ignore players which are outside the map 2020-05-17 21:12:57 +02:00
HybridDog
72f4225058 Support the moveresult parameter of on_step
The parameter is available in Minetest 5.3.0-dev.
thanks to sofar and VanessaE for the fix and report
2020-05-08 11:54:33 +02:00
Loïc Blot
7cb8086a25
Get player inventory before looping (#30)
This prevents the inventory check on each object in the loop and thus shortens the code.
Additionally, the inventory existence check is removed because get_inventory never returns nil for player objects.
2020-04-15 20:21:48 +02:00
HybridDog
8ef6ba3c0f Do not drop items when a Node Breaker digs a node 2019-09-05 12:00:13 +02:00
HybridDog
682a345495 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
2019-07-01 15:46:06 +02:00
texmex
871be4048e Replace another deprecated function 2019-06-15 13:26:24 +02:00
4 changed files with 141 additions and 57 deletions

11
.github/workflows/build.yml vendored Normal file
View 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
View 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",
}

View File

@ -1,15 +1,14 @@
# item_drop
# Item Drop [![](https://github.com/minetest-mods/item_drop/workflows/build/badge.svg)](https://github.com/minetest-mods/item_drop/actions) [![License](https://img.shields.io/badge/license-LGPLv2.1%2B-blue.svg)](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),
[texmex](https://github.com/tacotexmex/), [hybriddog](https://github.com/hybriddog/).
## Description
A highly configurable mod providing item magnet and in-world node drops
## Licensing
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
`item_drop` can be played with Minetest 0.4.16 or above. It was originally
developed by [PilzAdam](https://github.com/PilzAdam/item_drop).
## List of features

159
init.lua
View File

@ -93,34 +93,46 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
flowingliquid = true,
}
-- Get an image string from a tile definition
local function tile_to_image(tile, fallback_image)
if not tile then
return fallback_image
end
local tile_type = type(tile)
if tile_type == "string" then
return tile
end
assert(tile_type == "table", "Tile definition is not a string or table")
local image = tile.name or tile.image
assert(image, "Tile definition has no image file specified")
if tile.color then
local colorstr = minetest.colorspec_to_colorstring(tile.color)
if colorstr then
return image .. "^[multiply:" .. colorstr
end
end
return image
end
-- 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,
})
}, true)
if pickup_particle then
local item = minetest.registered_nodes[
ent.itemstring:gsub("(.*)%s.*$", "%1")]
local image = ""
local image
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.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
-- color in the tile definition is handled by tile_to_image.
-- color in the node definition is not yet supported here.
local top = tile_to_image(tiles[1])
local left = tile_to_image(tiles[3], top)
local right = tile_to_image(tiles[5], left)
image = minetest.inventorycube(top, left, right)
else
image = item.inventory_image or item.tiles[1]
@ -201,9 +213,9 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
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)
function itemdef.on_step(self, ...)
if not self.is_magnet_item then
return old_on_step(self, dtime)
return old_on_step(self, ...)
end
ObjectRef = ObjectRef or getmetatable(self.object)
local old_funcs = {}
@ -212,7 +224,7 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
old_funcs[method] = ObjectRef[method]
ObjectRef[method] = do_nothing
end
old_on_step(self, dtime)
old_on_step(self, ...)
for i = 1, #blocked_methods do
local method = blocked_methods[i]
ObjectRef[method] = old_funcs[method]
@ -252,6 +264,13 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
return keys_pressed ~= key_invert
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
local function pickupfunc(player)
if not has_keys_pressed(player)
@ -260,9 +279,13 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
return
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
local inv
local inv = player:get_inventory()
local objectlist = minetest.get_objects_inside_radius(pos,
magnet_mode and magnet_radius or pickup_radius)
@ -271,14 +294,6 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
local ent = opt_get_ent(object)
if ent
and item_drop.can_pickup(ent, player) then
if not inv 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)
if inv:room_for_item("main", item) then
local flying_item
@ -330,17 +345,65 @@ 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)
-- 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
local old_handle_node_drops = minetest.handle_node_drops
function minetest.handle_node_drops(pos, drops, player)
if not player or 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]
local count, name
if type(item) == "string" then
count = 1
name = item
else
count = item:get_count()
name = item:get_name()
-- 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 == ""
@ -348,24 +411,16 @@ and not minetest.settings:get_bool("creative_mode") then
count = 0
end
for _ = 1,count do
local obj = minetest.add_item(pos, name)
if not obj then
error("Couldn't spawn item " .. name .. ", drops: " .. dump(drops))
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
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)
spawn_items(pos, items_to_spawn)
end
end
end