do not spawn too many book entities

This commit is contained in:
cora 2021-12-28 03:32:51 +01:00
parent f432458ccc
commit 6ca6da2da0
1 changed files with 13 additions and 10 deletions

View File

@ -149,19 +149,22 @@ minetest.register_craftitem("mcl_enchanting:book_enchanted", {
minetest.register_alias("mcl_books:book_enchanted", "mcl_enchanting:book_enchanted")
local spawn_book_entity = function(pos, respawn)
if respawn then
-- Check if we already have a book
local objs = minetest.get_objects_inside_radius(pos, 1)
local cnt=0
-- Check if we already have books within 5 nodes distance to avoid too many entities
local objs = minetest.get_objects_inside_radius(pos, 5)
for o=1, #objs do
local obj = objs[o]
local lua = obj:get_luaentity()
if lua and lua.name == "mcl_enchanting:book" then
if lua._table_pos and vector.equals(pos, lua._table_pos) then
if respawn and lua._table_pos and vector.equals(pos, lua._table_pos) then
return
end
cnt=cnt+1
end
end
end
if cnt > 10 then return end
local obj = minetest.add_entity(vector.add(pos, mcl_enchanting.book_offset), "mcl_enchanting:book")
if obj then
local lua = obj:get_luaentity()