This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
UnderVoid's profile picture

Turtle to take specific item out of chest

Started by UnderVoid, 03 June 2015 - 05:16 PM
UnderVoid #1
Posted 03 June 2015 - 07:16 PM
I've been coding several different turtle programs but I'm having trouble making my narcissistic turtle remove only wheat and put it in a specific shape to be crafted.

What code do I put in to scan a chest for items, only take specific ones and then craft, in my case, bread?

My current system, doesn't have the crafting implemented but maybe it could help if you know what my code looks like. 2cnwkCQv


Thanks!
SquidDev #2
Posted 03 June 2015 - 09:12 PM
If you have a narcissistic turtle I presume you have OpenPeripherals installed. This provides a pushItemIntoSlot method:


chest.pushItemIntoSlot("north", 5) -- push to the north side from slot 5.

You can use getAllStacks and getStackInSlot to get the contents of the chest.

Also, don't override the table methods. Instead do:


local function goToChest()
  ...
end

...

while true do
  goToChest()
  sleep(2)
  comeBack()
  sleep(90)
  startBack()
end
Edited on 03 June 2015 - 07:12 PM
UnderVoid #3
Posted 03 June 2015 - 10:14 PM
If you have a narcissistic turtle I presume you have OpenPeripherals installed. This provides a pushItemIntoSlot method:


chest.pushItemIntoSlot("north", 5) -- push to the north side from slot 5.

You can use getAllStacks and getStackInSlot to get the contents of the chest.

Also, don't override the table methods. Instead do:


local function goToChest()
  ...
end

...

while true do
  goToChest()
  sleep(2)
  comeBack()
  sleep(90)
  startBack()
end

Sorry I still don't understand, I've been self teaching myself most of this and I don't know what you mean by that line of code. How would I insert it to only take wheat for instance? I tried a few commands but none worked.

Thanks for replying though.
SquidDev #4
Posted 04 June 2015 - 08:11 AM
Sorry, I should have been slightly clearer. I guess it might be worth skimming through this tutorial on peripherals - but all you really need is to know peripheral.wrap.

So first we want to access the chest. I'm going to presume your chest is on the north side of the turtle and the turtle is facing towards it. We do this with peipheral.wrap.

local the_luggage = peripheral.wrap("front") -- This find the chest at the front and allows you do do things to it. 

Then we want to find where the wheat is and move it into the turtle

-- getAllStacks returns a table of slot id to stacks
for slot, item in pairs(the_luggage.getAllStacks()) do
    if item.id == "minecraft:wheat" then
        -- If the chest is on the north side of the turtle, the turtle is on the south side of the peripheral
        the_luggage.pushItem("south", slot)
   end
end

An alternative method is to ensure the turtle always has one wheat in it and use turtle.suck:


turtle.select(1) -- If wheat is in the 1st slot
turtle.suck() -- Suck wheat from the chest
theoriginalbit #5
Posted 04 June 2015 - 09:51 AM
An alternative method is to ensure the turtle always has one wheat in it and use turtle.suck:

turtle.select(1) -- If wheat is in the 1st slot
turtle.suck() -- Suck wheat from the chest
This isn't a guarantee, turtle.suck will get the next item in the inventory regardless of whether it matches the currently selected slot. The only guaranteed way with turtle.suck is to ensure every slot in the turtle is full with at least one wheat in it, otherwise anything other than wheat can be sucked from the chest into a slot.
SquidDev #6
Posted 04 June 2015 - 10:46 AM
This isn't a guarantee, turtle.suck will get the next item in the inventory regardless of whether it matches the currently selected slot. The only guaranteed way with turtle.suck is to ensure every slot in the turtle is full with at least one wheat in it, otherwise anything other than wheat can be sucked from the chest into a slot.
Ah OK, shows you how little I use turtles. :P/> Learn something new every day I guess.
flaghacker #7
Posted 04 June 2015 - 03:40 PM
An alternative method is to ensure the turtle always has one wheat in it and use turtle.suck:

turtle.select(1) -- If wheat is in the 1st slot
turtle.suck() -- Suck wheat from the chest
This isn't a guarantee, turtle.suck will get the next item in the inventory regardless of whether it matches the currently selected slot. The only guaranteed way with turtle.suck is to ensure every slot in the turtle is full with at least one wheat in it, otherwise anything other than wheat can be sucked from the chest into a slot.

You could also put 1 wheat in the first slot and do

turtle.select(1)
turtle.suck()
turtle.select(2)
turtle.push()
theoriginalbit #8
Posted 04 June 2015 - 04:14 PM
You could also put 1 wheat in the first slot and do

turtle.select(1)
turtle.suck()
turtle.select(2)
turtle.push()

Also doesn't guarantee that what was pulled was wheat, it just guarantees that wheat overflow or something that wasn't wheat was returned, which given the way that push works it would mean that if it wasn't wheat it will just be picked up again the next time.

Again, the two ways to guarantee that wheat is obtained within vanilla ComputerCraft are with inventories setup

like this
or this
Edited on 04 June 2015 - 02:15 PM