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.
136 lines
3.6 KiB
136 lines
3.6 KiB
local component = require("component")
|
|
local robot = require("robot")
|
|
local sides = require("sides")
|
|
|
|
local crafting = component.crafting
|
|
local inventory = component.inventory_controller
|
|
|
|
RESULT_SLOT = 4
|
|
|
|
local iron_pickaxe_recipe = {
|
|
result = {
|
|
name = 'minecraft:iron_pickaxe',
|
|
count = 1,
|
|
},
|
|
ingredients = {
|
|
'minecraft:iron_ingot', 'minecraft:iron_ingot', 'minecraft:iron_ingot',
|
|
nil, 'minecraft:stick', nil,
|
|
nil, 'minecraft:stick', nil,
|
|
}
|
|
}
|
|
|
|
-- robots inventory is 4 wide and crafting uses the top left portion
|
|
local slot_map = {
|
|
1,2,3,
|
|
5,6,7,
|
|
9,10,11
|
|
}
|
|
|
|
-- local function read_chest_contents()
|
|
-- local size = inventory.getInventorySize(sides.forward)
|
|
-- for slot = 1, size do
|
|
-- local stack = inventory.getStackInSlot(sides.forward, slot)
|
|
-- if stack then
|
|
-- if stack.name == item then
|
|
-- inventory.suckFromSlot(sides.forward, slot, 1)
|
|
-- return true
|
|
-- end
|
|
-- end
|
|
-- end
|
|
-- end
|
|
|
|
-- store the item in the selected slot into the facing inventory
|
|
local function store_item()
|
|
-- check if that item is present in the inventory
|
|
local item = inventory.getStackInInternalSlot()
|
|
|
|
local size = inventory.getInventorySize(sides.forward)
|
|
local empty_slot = -1
|
|
for slot = 1, size do
|
|
local stack = inventory.getStackInSlot(sides.forward, slot)
|
|
if stack then
|
|
if stack.name == item.name then
|
|
inventory.dropIntoSlot(sides.forward, slot)
|
|
-- check if all items have been stored
|
|
local remaining = inventory.getStackInInternalSlot()
|
|
if not remaining then
|
|
print('Stored successfully')
|
|
return true
|
|
end
|
|
end
|
|
else
|
|
if empty_slot == -1 then
|
|
empty_slot = slot
|
|
end
|
|
end
|
|
end
|
|
|
|
-- item not present in inventory
|
|
if empty_slot ~= -1 then
|
|
-- store in first empty slot
|
|
local result, error = inventory.dropIntoSlot(sides.forward, empty_slot)
|
|
if not result then
|
|
print('Cannot store item: ' .. error .. "\n")
|
|
return false
|
|
else
|
|
print('Stored successfully')
|
|
return true
|
|
end
|
|
else
|
|
print('Cannot store item - Inventory full')
|
|
return false
|
|
end
|
|
end
|
|
|
|
-- Search the facing chest for "item" and take "count" items and place them in "slot"
|
|
-- Returns number of items fetched
|
|
local function fetch_item(item, output_slot)
|
|
robot.select(output_slot)
|
|
local size = inventory.getInventorySize(sides.forward)
|
|
if not size then
|
|
print('Not facing a chest')
|
|
return false
|
|
end
|
|
|
|
for slot = 1, size do
|
|
local stack = inventory.getStackInSlot(sides.forward, slot)
|
|
if stack then
|
|
if stack.name == item then
|
|
inventory.suckFromSlot(sides.forward, slot, 1)
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
|
|
print('Missing ' .. item)
|
|
return false
|
|
end
|
|
|
|
-- accepts a recipe table containing result and ingredients
|
|
local function craft_recipe(recipe)
|
|
for i, ingredient in pairs(recipe.ingredients) do
|
|
print('Fetching ' .. ingredient)
|
|
fetch_item(ingredient, slot_map[i])
|
|
end
|
|
|
|
-- select slot for the result
|
|
robot.select(RESULT_SLOT)
|
|
|
|
-- craft the resulting item
|
|
local crafted = crafting.craft(1)
|
|
|
|
if not crafted then
|
|
print('Crafting failed')
|
|
end
|
|
end
|
|
|
|
-- local target = ...
|
|
|
|
-- if not target then
|
|
-- print("Usage: craft <item>")
|
|
-- return
|
|
-- end
|
|
|
|
-- craft_recipe(iron_pickaxe_recipe)
|
|
store_item()
|