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

Checking for certain items with turtle.getItemDetail() not working.

Started by Haxerus, 07 May 2015 - 02:28 AM
Haxerus #1
Posted 07 May 2015 - 04:28 AM
I am writing a program that makes a 3x3 tunnel and I want it to drop the cobblestone, dirt and gravel it collects.

function dump()
 if turtle.getItemDetail().name == "minecraft:cobblestone" or "minecraft:dirt" or "minecraft:gravel" then
   turtle.dropDown()
 end
end
Above is called by:

function full()
if layer == 5 then
  for i = 1, 16 do
   turtle.select(i)
   if turtle.getItemCount() > 0 then
	dump()
   end
  end
  layer = 0
end
end

I made it so that it uses the turtle.getItemDetail() function to get the ID String of the items. However, it drops all items of its items instead of only the specified items.

Pastebin of full program >> http://pastebin.com/7gi79wWL
Lyqyd #2
Posted 07 May 2015 - 05:34 AM
Your if statement is structured incorrectly. As you have it now, it reads (roughly):

if turtle.getItemDetail().name is equal to "minecraft:cobblestone" OR if "minecraft:dirt" is not false/nil OR if "minecraft:gravel" is not false/nil, then

The "or" keyword doesn't extend a single comparison to multiple options; you need to explicitly compare the value the function returns to each of the strings, something like this:


local details = turtle.getItemDetail()
if details.name == "minecraft:cobblestone" or details.name == "minecraft:dirt" or details.name == "minecraft:gravel" then
KingofGamesYami #3
Posted 07 May 2015 - 03:54 PM
An easier way to compare these is to use a table, like this:


local items = {
  ["minecraft:cobblestone"] = true,
  ["minecraft:dirt"] = true,
  ["minecraft:gravel"] = true,
}

if items[ details.name ] then
  --#stuff
end