Get items from a inventory with a turtle #1552
-
I tried to make a turtle get a item in a specific slot on a inventory, but there is no function like local modem = peripheral.wrap("front")
local chest = peripheral.find("chest")
chest.pushItems(modem.getNameLocal(), slot, count) But this solution is't what i am looking for because this need a wired modem to use and my turtle that will walk freely will not have it every time. What work the best, for me, is to place a hopper facing the turtle like on this: turtle.suckSlot = function (slot)
turtle.placeUp()
sleep(1) -- tick of placing the hopper
local hopper = peripheral.wrap("top")
local chest = peripheral.find("minecraft:chest")
chest.pushItems( "top", slot)
while hopper.list()[1] do
sleep(1)
end
turtle.digUp()
end |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You can wrap the chest as a peripheral even if its not on a modem. From there, you can pushitems to itself to move items around. If you move the item you want to slot 1, then The caveat is that at least one slot must be available in the chest, otherwise you cannot move items around. Example code: --- Quick script to move a desired item in the chest to slot 1 (moving other items out of the way if needed)
--- Find an item in a chest, given the chest's position and item name.
---@param item_list itemList The list of items in the chest.
---@param item_name string The name of the item to find.
---@return integer? The slot number of the item, or nil if not found.
local function find_item(item_list, item_name)
for slot, item in pairs(item_list) do
if item.name == item_name then
return slot
end
end
return nil
end
--- Find the first empty slot in a chest.
---@param item_list itemList The list of items in the chest.
---@param size integer The size of the chest.
---@return integer? slot The slot number of the first empty slot, or nil if none are empty.
local function find_empty_slot(item_list, size)
for slot = 1, size do
if not item_list[slot] then
return slot
end
end
return nil
end
--- Move an item from one slot to another in a given inventory.
---@param inventory_name string The name of the inventory to move items in.
---@param from_slot integer The slot to move from.
---@param to_slot integer The slot to move to.
local function move_item_stack(inventory_name, from_slot, to_slot)
return peripheral.call(inventory_name, "pushItems", inventory_name, from_slot, nil, to_slot)
end
--- Move a specific item to slot one, moving other items out of the way if needed.
---@param chest_name string The name of the chest to search.
---@param item_name string The name of the item to find.
---@return boolean success Whether or not the item was successfully moved to slot one (or already existed there)
local function move_item_to_slot_one(chest_name, item_name)
local list = peripheral.call(chest_name, "list")
local size = peripheral.call(chest_name, "size")
local slot = find_item(list, item_name)
-- If the item didn't exist, or is already in the first slot, we're done.
if not slot then
printError("Item not found")
return false
end
if slot == 1 then
print("Item already in slot 1")
return true
end
-- If an item is blocking the first slot (we already know it's not the one we want), we need to move it.
if list[1] then
print("Slot 1 is occupied, moving to first empty slot")
local empty_slot = find_empty_slot(list, size)
-- If there are no empty slots, we can't move the item.
if not empty_slot then
printError("No empty slots")
return false
end
-- Move the item to the first empty slot.
if not move_item_stack(chest_name, 1, empty_slot) then
printError("Failed to move item to slot " .. empty_slot)
return false
end
print("Moved item to slot " .. empty_slot)
end
-- Move the item to slot 1.
if move_item_stack(chest_name, slot, 1) == 0 then
printError("Failed to move item to slot 1")
return false
end
print("Moved item to slot 1")
return true
end
-- Example usage
move_item_to_slot_one("front", "minecraft:stone") Well, I went into writing the above to just make a small example. Wound up with something that is a bit over-engineered, but it should be robust and efficient. It also has types for Lua Language Server, if you have that VSCode extension installed :) Edits
|
Beta Was this translation helpful? Give feedback.
-
I might have found a solution that goes by the same method but much simplerI was trying to refuel a turtle using a chest but I ran into a problem where I couldn't select the item in the chest. Here's howI provide my entire code if there's something missingTHE CODE IS VERY INCOMPLETE -- File: _setup.lua_
-- Require file: _info/items_
require "info.items"
-- Get Inventory (In Front)
local inv = peripheral.wrap("front")
typeInv = "Small"
if inv.size() > 27 then
typeInv = "Large"
end
print(typeInv,inv.size())
_items = inv.list()
print(#_items)
-- Prints the items on the chest
print("Items in the chest...")
term.setTextColor(colors.gray)
print(textutils.serialise(_items))
term.setTextColor(colors.white)
sleep(0.5)
print("\nFuel: ")
print("| ".. turtle.getFuelLevel() .. "/" .. turtle.getFuelLimit())
chest = peripheral.wrap("front")
items = chest.list()
for i = 1, 27 do
if items[i] ~= nil then
sleep(0.5)
term.setTextColor(colors.gray)
print("->",items[i].name, items[i].count)
term.setTextColor(colors.white)
if items[i].name == charcoal then
if turtle.getFuelLevel() < 400 then
print("Obtaining Charcoal...")
-- THE ACTUAL CODE
-- Transfer the item from the slot 'i' to the first slot in groups of 4
n = inv.pullItems("front",i,4)
-- List the items in the chest again
items = inv.list()
-- Suck all the items in the first slot available (it should be 1)
turtle.suck(items[1].count)
end
end
end
end In the code I just put the item I want in the first slot and then suck all those items. Assuming the chest is in front of the turtleRequire Info File-- Info File
charcoal = "minecraft:charcoal"
coal = "minecraft:coal"
wheat_s = "minecraft:wheat_seeds" |
Beta Was this translation helpful? Give feedback.
You can wrap the chest as a peripheral even if its not on a modem. From there, you can pushitems to itself to move items around. If you move the item you want to slot 1, then
turtle.suck()
from the chest, you will get what you want in the turtle.The caveat is that at least one slot must be available in the chest, otherwise you cannot move items around.
Example code: