Add settings for pickups and drops

This commit is contained in:
tacotexmex 2017-06-26 15:24:56 +02:00
parent d9581ec795
commit a666252f1c

181
init.lua

@ -1,67 +1,72 @@
minetest.register_globalstep(function(dtime) local enable_item_pickup = minetest.settings:get_bool("enable_item_pickup") or true
for _,player in ipairs(minetest.get_connected_players()) do local enable_item_drops = minetest.settings:get_bool("enable_item_drops") or true
if player:get_hp() > 0 or not minetest.settings:get_bool("enable_damage") then
local pos = player:getpos()
pos.y = pos.y+0.5
local inv = player:get_inventory()
for _,object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do if enable_item_pickup then
if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then minetest.register_globalstep(function(dtime)
if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then for _,player in ipairs(minetest.get_connected_players()) do
inv:add_item("main", ItemStack(object:get_luaentity().itemstring)) if player:get_hp() > 0 or not minetest.settings:get_bool("enable_damage") then
if object:get_luaentity().itemstring ~= "" then local pos = player:getpos()
minetest.sound_play("item_drop_pickup", { pos.y = pos.y+0.5
to_player = player:get_player_name(), local inv = player:get_inventory()
gain = 0.4,
}) 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 inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
inv:add_item("main", ItemStack(object:get_luaentity().itemstring))
if object:get_luaentity().itemstring ~= "" then
minetest.sound_play("item_drop_pickup", {
to_player = player:get_player_name(),
gain = 0.4,
})
end
object:get_luaentity().itemstring = ""
object:remove()
end end
object:get_luaentity().itemstring = ""
object:remove()
end end
end end
end
for _,object in ipairs(minetest.get_objects_inside_radius(pos, 2)) do for _,object in ipairs(minetest.get_objects_inside_radius(pos, 2)) do
if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then 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().collect then
if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
local pos1 = pos local pos1 = pos
pos1.y = pos1.y+0.2 pos1.y = pos1.y+0.2
local pos2 = object:getpos() local pos2 = object:getpos()
local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z} local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z}
vec.x = vec.x*3 vec.x = vec.x*3
vec.y = vec.y*3 vec.y = vec.y*3
vec.z = vec.z*3 vec.z = vec.z*3
object:setvelocity(vec) object:setvelocity(vec)
object:get_luaentity().physical_state = false object:get_luaentity().physical_state = false
object:get_luaentity().object:set_properties({ object:get_luaentity().object:set_properties({
physical = false physical = false
}) })
minetest.after(1, function(args) minetest.after(1, function(args)
local lua = object:get_luaentity() local lua = object:get_luaentity()
if object == nil or lua == nil or lua.itemstring == nil then if object == nil or lua == nil or lua.itemstring == nil then
return return
end end
if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
inv:add_item("main", ItemStack(object:get_luaentity().itemstring)) inv:add_item("main", ItemStack(object:get_luaentity().itemstring))
if object:get_luaentity().itemstring ~= "" then if object:get_luaentity().itemstring ~= "" then
minetest.sound_play("item_drop_pickup", { minetest.sound_play("item_drop_pickup", {
to_player = player:get_player_name(), to_player = player:get_player_name(),
gain = 0.4, gain = 0.4,
})
end
object:get_luaentity().itemstring = ""
object:remove()
else
object:setvelocity({x=0,y=0,z=0})
object:get_luaentity().physical_state = true
object:get_luaentity().object:set_properties({
physical = true
}) })
end end
object:get_luaentity().itemstring = "" end, {player, object})
object:remove()
else
object:setvelocity({x=0,y=0,z=0})
object:get_luaentity().physical_state = true
object:get_luaentity().object:set_properties({
physical = true
})
end
end, {player, object})
end
end end
end end
end end
@ -70,40 +75,42 @@ minetest.register_globalstep(function(dtime)
end end
end) end)
function minetest.handle_node_drops(pos, drops, digger) if enable_item_drops then
local inv function minetest.handle_node_drops(pos, drops, digger)
if minetest.settings:get_bool("creative_mode") and digger and digger:is_player() then local inv
inv = digger:get_inventory() if minetest.settings:get_bool("creative_mode") and digger and digger:is_player() then
end inv = digger:get_inventory()
for _,item in ipairs(drops) do
local count, name
if type(item) == "string" then
count = 1
name = item
else
count = item:get_count()
name = item:get_name()
end end
if not inv or not inv:contains_item("main", ItemStack(name)) then for _,item in ipairs(drops) do
for i=1,count do local count, name
local obj = minetest.add_item(pos, name) if type(item) == "string" then
if obj ~= nil then count = 1
obj:get_luaentity().collect = true name = item
local x = math.random(1, 5) else
if math.random(1,2) == 1 then count = item:get_count()
x = -x name = item:get_name()
end end
local z = math.random(1, 5) if not inv or not inv:contains_item("main", ItemStack(name)) then
if math.random(1,2) == 1 then for i=1,count do
z = -z local obj = minetest.add_item(pos, name)
end if obj ~= nil then
obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z}) 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:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z})
-- FIXME this doesnt work for deactiveted objects -- FIXME this doesnt work for deactiveted objects
if minetest.settings:get("remove_items") and tonumber(minetest.settings:get("remove_items")) then if minetest.settings:get("remove_items") and tonumber(minetest.settings:get("remove_items")) then
minetest.after(tonumber(minetest.settings:get("remove_items")), function(obj) minetest.after(tonumber(minetest.settings:get("remove_items")), function(obj)
obj:remove() obj:remove()
end, obj) end, obj)
end
end end
end end
end end