1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-12-24 01:30:38 +01:00

optimize item_drop

Set settings var on load mod (very useless define any 0.10s)
Replace minetest.add_item() -> minetest.spawn_item()
Remove "remove_items" var setting, (minetest.after(time, obj:remove()) ), useless -> "__builtin:item"
Replace "collect" var -> "always_collect"
This commit is contained in:
crabman77 2015-04-11 23:11:06 +02:00
parent 5863e01b49
commit 6383d5eca5

37
mods/item_drop/init.lua Executable file → Normal file
View File

@ -1,6 +1,10 @@
local enable_damage = minetest.setting_getbool("enable_damage")
local creative_mode = minetest.setting_getbool("creative_mode")
minetest.register_globalstep(function(dtime)
for _,player in ipairs(minetest.get_connected_players()) do
if player:get_hp() > 0 or not minetest.setting_getbool("enable_damage") then
if player:get_hp() > 0 or not enable_damage then
local pos = player:getpos()
local inv = player:get_inventory()
@ -19,7 +23,7 @@ minetest.register_globalstep(function(dtime)
for _,object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then
if object:get_luaentity().collect then
if object:get_luaentity().always_collect then
if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
local pos1 = pos
pos1.y = pos1.y+0.2
@ -65,7 +69,7 @@ end)
function minetest.handle_node_drops(pos, drops, digger)
local inv
if minetest.setting_getbool("creative_mode") and digger and digger:is_player() then
if creative_mode and digger and digger:is_player() then
inv = digger:get_inventory()
end
for _,item in ipairs(drops) do
@ -79,31 +83,24 @@ function minetest.handle_node_drops(pos, drops, digger)
end
if not inv or not inv:contains_item("main", ItemStack(name)) then
for i=1,count do
local obj = minetest.add_item(pos, name)
local obj
local x = math.random(1, 5)
if math.random(1,2) == 1 then x = -x end
local z = math.random(1, 5)
if math.random(1,2) == 1 then z = -z end
obj = minetest.spawn_item(pos, name)
if obj ~= nil then
obj:get_luaentity().collect = true
local x = math.random(1, 5)
if math.random(1,2) == 1 then
x = -x
end
local z = math.random(1, 5)
if math.random(1,2) == 1 then
z = -z
end
obj:get_luaentity().always_collect = true
obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z})
-- FIXME this doesnt work for deactiveted objects
if minetest.setting_get("remove_items") and tonumber(minetest.setting_get("remove_items")) then
minetest.after(tonumber(minetest.setting_get("remove_items")), function(obj)
obj:remove()
end, obj)
end
end
end
end
end
end
if minetest.setting_getbool("log_mods") then
minetest.log("action", "[item_drop] loaded.")
end