You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.3 KiB
75 lines
2.3 KiB
local maxStackSize = tonumber(minetest.settings:get("default_stack_max"))
|
|
local maxIncrement = function(stack1)
|
|
return (maxStackSize - stack1:get_count()) * -1
|
|
end
|
|
|
|
|
|
local combineStack = function(stack1, stack2)
|
|
if stack1:get_name() == stack2:get_name() then
|
|
local count1 = stack1:get_count()
|
|
local count2 = stack2:get_count()
|
|
local newcount1 = count1 + count2
|
|
local newcount2 = 0
|
|
if newcount1 > maxStackSize then
|
|
newcount2 = newcount1 - maxStackSize
|
|
newcount1 = maxStackSize
|
|
end
|
|
minetest.debug(newcount1, newcount2)
|
|
stack1:set_count(newcount1)
|
|
stack2:set_count(newcount2)
|
|
return stack1, stack2
|
|
elseif stack1:get_name() == "" then
|
|
return stack2, stack1
|
|
else
|
|
return stack1, stack2
|
|
end
|
|
end
|
|
|
|
|
|
minetest.register_on_punchnode(function(position, node, puncher, pointed)
|
|
local nodemeta = minetest.get_meta(position)
|
|
local noderef = minetest.registered_nodes[node.name]
|
|
if not noderef then return false end
|
|
local wieldItem = puncher:get_wielded_item()
|
|
local inventory = nodemeta:get_inventory()
|
|
if not inventory then return false end
|
|
|
|
local putfunc = noderef.allow_metadata_inventory_put
|
|
for listName in pairs(inventory:get_lists()) do
|
|
local transstack
|
|
if putfunc then
|
|
transstack = putfunc(position, listName, nil, wieldItem, puncher)
|
|
else
|
|
transstack = wieldItem:get_count()
|
|
end
|
|
if transstack ~= 0 then
|
|
local leftover = inventory:add_item(listName, wieldItem)
|
|
puncher:set_wielded_item(leftover)
|
|
noderef.on_metadata_inventory_put(position, listName, nil, wieldItem, puncher)
|
|
return
|
|
end
|
|
end
|
|
|
|
local takefunc = noderef.allow_metadata_inventory_take
|
|
for listName in pairs(inventory:get_lists()) do
|
|
for index = 1, inventory:get_size(listName) do
|
|
wieldItem = puncher:get_wielded_item()
|
|
local item = inventory:get_stack(listName, index)
|
|
if (wieldItem:get_name() == "" and item:get_name() ~= "") or wieldItem:get_name() == item:get_name() then
|
|
local maxAmmount
|
|
if not takefunc then
|
|
maxAmmount = inventory:get_stack(listname, index):get_count()
|
|
else
|
|
maxAmmount = takefunc(position, listName, index, item, puncher)
|
|
end
|
|
local ammount = math.min(maxAmmount, maxIncrement(wieldItem))
|
|
if ammount ~= 0 then
|
|
local stack1, stack2 = combineStack(wieldItem, item)
|
|
puncher:set_wielded_item(stack1)
|
|
inventory:set_stack(listName, index, stack2)
|
|
return
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
|