Specify item damage

master
D4VID 12 months ago
parent a074e052c9
commit 2db5040c52

@ -9,39 +9,64 @@ RESULT_SLOT = 4
local iron_pickaxe_recipe = { local iron_pickaxe_recipe = {
result = { result = {
name = 'minecraft:iron_pickaxe', item = { 'minecraft:iron_pickaxe', 0 },
count = 1, count = 1,
}, },
ingredients = { ingredients = {
'minecraft:iron_ingot', 'minecraft:iron_ingot', 'minecraft:iron_ingot', -- pair of name (string id) and damage separated by ';'
nil, 'minecraft:stick', nil, 'minecraft:iron_ingot;0', 'minecraft:iron_ingot;0', 'minecraft:iron_ingot;0',
nil, 'minecraft:stick', nil, nil, 'minecraft:stick;0', nil,
nil, 'minecraft:stick;0', nil,
} }
} }
-- robots inventory is 4 wide and crafting uses the top left portion -- robots inventory is 4 wide and crafting uses the top left portion
local slot_map = { local slot_map = {
1,2,3, 1, 2, 3,
5,6,7, 5, 6, 7,
9,10,11 9, 10, 11
} }
-- local function read_chest_contents() ---"item" being pair string id and damage separated by ';' eg. "minecraft:wool;3"
-- local size = inventory.getInventorySize(sides.forward) ---returns "minecraft:wool", 3
-- for slot = 1, size do ---@param str string
-- local stack = inventory.getStackInSlot(sides.forward, slot) ---@param delimiter string
-- if stack then ---@return string|nil
-- if stack.name == item then ---@return number|nil
-- inventory.suckFromSlot(sides.forward, slot, 1) local function parse_item(str, delimiter)
-- return true local pos = string.find(str, delimiter)
-- end if not pos then
-- end return nil,nil
-- end end
-- end return string.sub(str, 1, pos-1), tonumber(string.sub(str, pos+1))
end
-- store the item in the selected slot into the facing inventory ---read the facing inventory and return a table of item names and their count or nil if not facing an inventory
---@return table|nil
local function read_chest_contents()
local contents = {}
local size = inventory.getInventorySize(sides.forward)
if not size then
return nil
end
for slot = 1, size do
local stack = inventory.getStackInSlot(sides.forward, slot)
if stack then
local name = stack.name .. ";" .. stack.damage
if contents[name] then
contents[name] = contents[name] + stack.size
else
contents[name] = stack.size
end
end
end
return contents
end
---store the item in the selected slot into the facing inventory
---@return boolean
local function store_item() local function store_item()
-- check if that item is present in the inventory -- check if that item is present in the inventory
local item = inventory.getStackInInternalSlot() local item = inventory.getStackInInternalSlot()
local size = inventory.getInventorySize(sides.forward) local size = inventory.getInventorySize(sides.forward)
@ -82,9 +107,14 @@ local function store_item()
end end
end end
-- Search the facing chest for "item" and take "count" items and place them in "slot" ---Search the facing chest for "item" and take "count" items and place them in "slot"
-- Returns number of items fetched ---"item" being pair string id and damage separated by ';' eg. "minecraft:wool;3"
---Returns number of items fetched
---@param item string
---@param output_slot number
---@return boolean
local function fetch_item(item, output_slot) local function fetch_item(item, output_slot)
local name, damage = parse_item(item, ";")
robot.select(output_slot) robot.select(output_slot)
local size = inventory.getInventorySize(sides.forward) local size = inventory.getInventorySize(sides.forward)
if not size then if not size then
@ -95,7 +125,7 @@ local function fetch_item(item, output_slot)
for slot = 1, size do for slot = 1, size do
local stack = inventory.getStackInSlot(sides.forward, slot) local stack = inventory.getStackInSlot(sides.forward, slot)
if stack then if stack then
if stack.name == item then if stack.name == name and stack.damage == damage then
inventory.suckFromSlot(sides.forward, slot, 1) inventory.suckFromSlot(sides.forward, slot, 1)
return true return true
end end
@ -106,8 +136,14 @@ local function fetch_item(item, output_slot)
return false return false
end end
-- accepts a recipe table containing result and ingredients ---accepts a recipe table containing result and ingredients
---@param recipe table
---@return boolean
local function craft_recipe(recipe) local function craft_recipe(recipe)
if inventory.getStackInInternalSlot(RESULT_SLOT) then
print('Cannot craft - output slot occupied')
return false
end
for i, ingredient in pairs(recipe.ingredients) do for i, ingredient in pairs(recipe.ingredients) do
print('Fetching ' .. ingredient) print('Fetching ' .. ingredient)
fetch_item(ingredient, slot_map[i]) fetch_item(ingredient, slot_map[i])
@ -118,10 +154,12 @@ local function craft_recipe(recipe)
-- craft the resulting item -- craft the resulting item
local crafted = crafting.craft(1) local crafted = crafting.craft(1)
if not crafted then if not crafted then
print('Crafting failed') print('Crafting failed')
return false
end end
return store_item()
end end
-- local target = ... -- local target = ...
@ -131,5 +169,15 @@ end
-- return -- return
-- end -- end
-- craft_recipe(iron_pickaxe_recipe) craft_recipe(iron_pickaxe_recipe)
store_item()
-- store_item()
-- local contents = read_chest_contents()
-- if contents then
-- for k,v in pairs(contents) do
-- print(k,v)
-- end
-- else
-- print('Not facing a chest')
-- end

Loading…
Cancel
Save