added count parameter to room_for_item() and return available room from room_for_*()

This commit is contained in:
auouymous 2018-08-13 19:48:26 -06:00
parent 86d4dbb426
commit 48758722f9
3 changed files with 47 additions and 12 deletions

16
API
View File

@ -36,9 +36,14 @@ node definition implements all or some of these functions:
-- liquid should be the name of a source liquid (in bucket.liquids of bucket mod)
-- 1 bucket or source block is 1000 millibuckets
node_io_room_for_item(pos, node, side, itemstack) -> bool
node_io_room_for_liquid(pos, node, side, liquid, millibuckets) -> bool
-- use millibuckets=1 to check if not full, then call put_liquid() with actual amount to transfer, leftover
node_io_room_for_item(pos, node, side, itemstack, count) -> int
-- ignore count in itemstack, only use the count parameter
-- returns count if inventory can hold entire stack, else returns amount the inventory can hold
-- use count=1 to check if not full, then call put_item() with actual amount to transfer
-- the non-zero return count is needed by transfer nodes that take from one inventory node and put in another inventory node
node_io_room_for_liquid(pos, node, side, liquid, millibuckets) -> int
-- returns millibuckets if inventory can hold entire amount, else returns amount the inventory can hold
-- use millibuckets=1 to check if not full, then call put_liquid() with actual amount to transfer
-- use millibuckets=1000 with room_for_liquid() and put_liquid() to only insert full buckets
@ -83,6 +88,11 @@ API has utility functions for standard inventory nodes to easily implement the A
-- returns a "fake" itemstack based on stack but only containing count items
-- this can be used with node_io.room_for_item(), and then use itemstack=stack:take_item(count) to get a real itemstack
node_io.compare_itemstack(itemstack1, itemstack2) -> bool
-- returns true if both itemstacks are identical, excluding count
node_io.room_for_item_in_inventory(inv, inv_name, itemstack, count) -> int
-- call from node_io_room_for_item()
node_io.get_inventory_size(pos, inv_name)
-- call from node_io_get_item_size()
node_io.get_inventory_name(pos, inv_name, index)

View File

@ -61,14 +61,14 @@ node_io.can_take_liquid = function(pos, node, side)
return ndef.node_io_can_take_liquid(pos, node, side)
end
node_io.room_for_item = function(pos, node, side, itemstack)
node_io.room_for_item = function(pos, node, side, itemstack, count) -- returns non-negative number
local ndef = minetest.registered_nodes[node.name]
if not ndef.node_io_room_for_item then return false end
return ndef.node_io_room_for_item(pos, node, side, itemstack)
if not ndef.node_io_room_for_item then return 0 end
return ndef.node_io_room_for_item(pos, node, side, itemstack, count)
end
node_io.room_for_liquid = function(pos, node, side, liquid, millibuckets)
node_io.room_for_liquid = function(pos, node, side, liquid, millibuckets) -- returns non-negative number
local ndef = minetest.registered_nodes[node.name]
if not ndef.node_io_room_for_liquid then return false end
if not ndef.node_io_room_for_liquid then return 0 end
return ndef.node_io_room_for_liquid(pos, node, side, liquid, millibuckets)
end
@ -130,6 +130,31 @@ node_io.make_itemstack = function(stack, count)
return itemstack
end
node_io.compare_itemstack = function(itemstack1, itemstack2)
if itemstack1:get_name() ~= itemstack2:get_name() then return false end
if itemstack1:get_wear() ~= itemstack2:get_wear() then return false end
if itemstack1:get_metadata() ~= itemstack2:get_metadata() then return false end
return true
end
node_io.room_for_item_in_inventory = function(inv, inv_name, itemstack, count)
local max = itemstack:get_stack_max()
local put_count = max
if count < put_count then put_count = count end
local room = 0
for i = 1, inv:get_size(inv_name) do
local stack = inv:get_stack("main", i)
if stack:is_empty() then return put_count end
if node_io.compare_itemstack(stack, itemstack) then
if stack:get_count() < max then
local room = room + max - stack:get_count()
if room >= put_count then return put_count end
end
end
end
return room
end
node_io.get_inventory_size = function(pos, inv_name)
local inv = minetest.get_meta(pos):get_inventory()
if not inv then return 0 end
@ -196,10 +221,10 @@ node_io.init_main_inventory = function(node_name, allow_take)
local def = {}
def.node_io_can_put_item = function(pos, node, side) return true end
def.node_io_room_for_item = function(pos, node, side, itemstack)
def.node_io_room_for_item = function(pos, node, side, itemstack, count)
local inv = minetest.get_meta(pos):get_inventory()
if not inv then return false end
return inv:room_for_item("main", itemstack)
if not inv then return 0 end
return node_io.room_for_item_in_inventory(inv, "main", itemstack, count)
end
def.node_io_put_item = function(pos, node, side, putter, itemstack)
return node_io.put_item_in_inventory(pos, node, "main", putter, itemstack)

View File

@ -169,7 +169,7 @@ index 062c739..3fadaee 100644
+ for i = 1, inv:get_size("main") do
+ local stack = inv:get_stack("main", i)
+ local item = stack:get_name()
+ if item ~= "" and node_io.room_for_item(put_pos, put_node, put_side, node_io.make_itemstack(stack, 1)) then
+ if item ~= "" and node_io.room_for_item(put_pos, put_node, put_side, stack, 1) > 0 then
+ node_io.put_item(put_pos, put_node, put_side, nil, stack:take_item(1))
+ inv:set_stack("main", i, stack)
+ minetest.get_node_timer(put_pos):start(0.5)