Alloy furnace: Accept only unique ItemStacks from tubes (#523)

Co-authored-by: Luke aka SwissalpS <Luke@SwissalpS.ws>
Co-authored-by: SmallJoker <SmallJoker@users.noreply.github.com>
This commit is contained in:
Serhiy Zahoriya 2020-01-11 22:46:33 +02:00 committed by SmallJoker
parent e90b28895c
commit 1a45ad19d4
3 changed files with 73 additions and 16 deletions

View File

@ -64,6 +64,10 @@ Registration functions
### Specific machines
* `technic.register_solar_array(data)`
* data is a table
* `technic.can_insert_unique_stack(pos, node, stack, direction)`
* `technic.insert_object_unique_stack(pos, node, stack, direction)`
* Functions for the parameters `can_insert` and `insert_object` to avoid
filling multiple inventory slots with same type of item.
Used itemdef fields
-------------------

View File

@ -1,10 +1,50 @@
local S = technic.getter
function technic.insert_object_unique_stack(pos, node, incoming_stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local incoming_name = incoming_stack:get_name()
local stack_index = nil
for inv_index, inv_stack in pairs(inv:get_list("src")) do
if inv_stack:get_name() == incoming_name then
stack_index = inv_index
break
end
end
if stack_index == nil then
return inv:add_item("src", incoming_stack)
end
local present_stack = inv:get_stack("src", stack_index)
local leftover = present_stack:add_item(incoming_stack)
inv:set_stack("src", stack_index, present_stack)
return leftover
end
function technic.can_insert_unique_stack(pos, node, incoming_stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local incoming_name = incoming_stack:get_name()
if meta:get_int("splitstacks") == 0 then
-- avoid looping second time with inv:contains_item("src", incoming_stack)
for _, inv_stack in pairs(inv:get_list("src")) do
if inv_stack:get_name() == incoming_name then
return inv_stack:item_fits(incoming_stack)
end
end
end
return technic.default_can_insert(pos, node, incoming_stack, direction)
end
function technic.register_alloy_furnace(data)
data.typename = "alloy"
data.machine_name = "alloy_furnace"
data.machine_desc = S("%s Alloy Furnace")
data.insert_object = technic.insert_object_unique_stack
data.can_insert = technic.can_insert_unique_stack
technic.register_base_machine(data)
end

View File

@ -4,22 +4,26 @@ local S = technic.getter
local fs_helpers = pipeworks.fs_helpers
local tube_entry = "^pipeworks_tube_connection_metallic.png"
local tube = {
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("src", stack)
end,
can_insert = function(pos, node, stack, direction)
function technic.default_can_insert(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if meta:get_int("splitstacks") == 1 then
stack = stack:peek_item(1)
end
return inv:room_for_item("src", stack)
end
function technic.new_default_tube()
return {
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("src", stack)
end,
can_insert = technic.default_can_insert,
connect_sides = {left = 1, right = 1, back = 1, top = 1, bottom = 1},
}
}
end
local connect_default = {"bottom", "back", "left", "right"}
@ -64,6 +68,14 @@ function technic.register_base_machine(data)
"listring[current_player;main]"
end
local tube = technic.new_default_tube()
if data.can_insert then
tube.can_insert = data.can_insert
end
if data.insert_object then
tube.insert_object = data.insert_object
end
local run = function(pos, node)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
@ -144,6 +156,7 @@ function technic.register_base_machine(data)
if ltier == "lv" then
tentry = ""
end
minetest.register_node("technic:"..ltier.."_"..machine_name, {
description = machine_desc:format(tier),
tiles = {