[mcl_dispensers] fix warnings

This commit is contained in:
AFCMS 2021-05-23 11:50:16 +02:00
parent b814fe47f9
commit d43b2149e6

View File

@ -96,125 +96,127 @@ local dispenserdef = {
end, end,
_mcl_blast_resistance = 3.5, _mcl_blast_resistance = 3.5,
_mcl_hardness = 3.5, _mcl_hardness = 3.5,
mesecons = {effector = { mesecons = {
-- Dispense random item when triggered effector = {
action_on = function (pos, node) -- Dispense random item when triggered
local meta = minetest.get_meta(pos) action_on = function (pos, node)
local inv = meta:get_inventory() local meta = minetest.get_meta(pos)
local droppos, dropdir local inv = meta:get_inventory()
if node.name == "mcl_dispensers:dispenser" then local droppos, dropdir
dropdir = vector.multiply(minetest.facedir_to_dir(node.param2), -1) if node.name == "mcl_dispensers:dispenser" then
droppos = vector.add(pos, dropdir) dropdir = vector.multiply(minetest.facedir_to_dir(node.param2), -1)
elseif node.name == "mcl_dispensers:dispenser_up" then droppos = vector.add(pos, dropdir)
dropdir = {x=0, y=1, z=0} elseif node.name == "mcl_dispensers:dispenser_up" then
droppos = {x=pos.x, y=pos.y+1, z=pos.z} dropdir = {x=0, y=1, z=0}
elseif node.name == "mcl_dispensers:dispenser_down" then droppos = {x=pos.x, y=pos.y+1, z=pos.z}
dropdir = {x=0, y=-1, z=0} elseif node.name == "mcl_dispensers:dispenser_down" then
droppos = {x=pos.x, y=pos.y-1, z=pos.z} dropdir = {x=0, y=-1, z=0}
end droppos = {x=pos.x, y=pos.y-1, z=pos.z}
local dropnode = minetest.get_node(droppos)
local dropnodedef = minetest.registered_nodes[dropnode.name]
local stacks = {}
for i=1,inv:get_size("main") do
local stack = inv:get_stack("main", i)
if not stack:is_empty() then
table.insert(stacks, {stack = stack, stackpos = i})
end end
end local dropnode = minetest.get_node(droppos)
if #stacks >= 1 then local dropnodedef = minetest.registered_nodes[dropnode.name]
local r = math.random(1, #stacks) local stacks = {}
local stack = stacks[r].stack for i=1,inv:get_size("main") do
local dropitem = ItemStack(stack) local stack = inv:get_stack("main", i)
dropitem:set_count(1) if not stack:is_empty() then
local stack_id = stacks[r].stackpos table.insert(stacks, {stack = stack, stackpos = i})
local stackdef = stack:get_definition() end
local iname = stack:get_name() end
local igroups = minetest.registered_items[iname].groups if #stacks >= 1 then
local r = math.random(1, #stacks)
local stack = stacks[r].stack
local dropitem = ItemStack(stack)
dropitem:set_count(1)
local stack_id = stacks[r].stackpos
local stackdef = stack:get_definition()
local iname = stack:get_name()
local igroups = minetest.registered_items[iname].groups
--[===[ Dispense item ]===] --[===[ Dispense item ]===]
-- Hardcoded dispensions -- -- Hardcoded dispensions --
-- Armor, mob heads and pumpkins -- Armor, mob heads and pumpkins
if igroups.armor then if igroups.armor then
local droppos_below = {x = droppos.x, y = droppos.y - 1, z = droppos.z} local droppos_below = {x = droppos.x, y = droppos.y - 1, z = droppos.z}
for _, objs in ipairs({minetest.get_objects_inside_radius(droppos, 1), minetest.get_objects_inside_radius(droppos_below, 1)}) do for _, objs in ipairs({minetest.get_objects_inside_radius(droppos, 1), minetest.get_objects_inside_radius(droppos_below, 1)}) do
for _, obj in ipairs(objs) do for _, obj in ipairs(objs) do
stack = mcl_armor.equip(stack, obj) stack = mcl_armor.equip(stack, obj)
if stack:is_empty() then
break
end
end
if stack:is_empty() then if stack:is_empty() then
break break
end end
end end
if stack:is_empty() then
break
end
end
-- Place head or pumpkin as node, if equipping it as armor has failed -- Place head or pumpkin as node, if equipping it as armor has failed
if not stack:is_empty() then if not stack:is_empty() then
if igroups.head or iname == "mcl_farming:pumpkin_face" then if igroups.head or iname == "mcl_farming:pumpkin_face" then
if dropnodedef.buildable_to then if dropnodedef.buildable_to then
minetest.set_node(droppos, {name = iname, param2 = node.param2}) minetest.set_node(droppos, {name = iname, param2 = node.param2})
stack:take_item() stack:take_item()
end
end end
end end
end
inv:set_stack("main", stack_id, stack)
-- Spawn Egg
elseif igroups.spawn_egg then
-- Spawn mob
if not dropnodedef.walkable then
pointed_thing = { above = droppos, under = { x=droppos.x, y=droppos.y-1, z=droppos.z } }
minetest.add_entity(droppos, stack:get_name())
stack:take_item()
inv:set_stack("main", stack_id, stack) inv:set_stack("main", stack_id, stack)
end -- Spawn Egg
elseif igroups.spawn_egg then
-- Spawn mob
if not dropnodedef.walkable then
--pointed_thing = { above = droppos, under = { x=droppos.x, y=droppos.y-1, z=droppos.z } }
minetest.add_entity(droppos, stack:get_name())
-- Generalized dispension
elseif (not dropnodedef.walkable or stackdef._dispense_into_walkable) then
--[[ _on_dispense(stack, pos, droppos, dropnode, dropdir)
* stack: Itemstack which is dispense
* pos: Position of dispenser
* droppos: Position to which to dispense item
* dropnode: Node of droppos
* dropdir: Drop direction
_dispense_into_walkable: If true, can dispense into walkable nodes
]]
if stackdef._on_dispense then
-- Item-specific dispension (if defined)
local od_ret = stackdef._on_dispense(dropitem, pos, droppos, dropnode, dropdir)
if od_ret then
local newcount = stack:get_count() - 1
stack:set_count(newcount)
inv:set_stack("main", stack_id, stack)
if newcount == 0 then
inv:set_stack("main", stack_id, od_ret)
elseif inv:room_for_item("main", od_ret) then
inv:add_item("main", od_ret)
else
minetest.add_item(droppos, dropitem)
end
else
stack:take_item() stack:take_item()
inv:set_stack("main", stack_id, stack) inv:set_stack("main", stack_id, stack)
end end
else
-- Drop item otherwise -- Generalized dispension
minetest.add_item(droppos, dropitem) elseif (not dropnodedef.walkable or stackdef._dispense_into_walkable) then
stack:take_item() --[[ _on_dispense(stack, pos, droppos, dropnode, dropdir)
inv:set_stack("main", stack_id, stack) * stack: Itemstack which is dispense
* pos: Position of dispenser
* droppos: Position to which to dispense item
* dropnode: Node of droppos
* dropdir: Drop direction
_dispense_into_walkable: If true, can dispense into walkable nodes
]]
if stackdef._on_dispense then
-- Item-specific dispension (if defined)
local od_ret = stackdef._on_dispense(dropitem, pos, droppos, dropnode, dropdir)
if od_ret then
local newcount = stack:get_count() - 1
stack:set_count(newcount)
inv:set_stack("main", stack_id, stack)
if newcount == 0 then
inv:set_stack("main", stack_id, od_ret)
elseif inv:room_for_item("main", od_ret) then
inv:add_item("main", od_ret)
else
minetest.add_item(droppos, dropitem)
end
else
stack:take_item()
inv:set_stack("main", stack_id, stack)
end
else
-- Drop item otherwise
minetest.add_item(droppos, dropitem)
stack:take_item()
inv:set_stack("main", stack_id, stack)
end
end end
end end
end,
rules = mesecon.rules.alldirs,
end },
end, },
rules = mesecon.rules.alldirs,
}},
on_rotate = on_rotate, on_rotate = on_rotate,
} }