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

[QUESTION] Turtles, GetItemDetail() and Fuel

Started by Gargoylegrgs, 21 November 2016 - 12:32 AM
Gargoylegrgs #1
Posted 21 November 2016 - 01:32 AM
Hey guys,

So… I have a turtle that will check through all slots and when it finds coal, it will run the turtle's refuel program. Now, I decided to do something, and that was to test planks, sticks, wooden tools, etc and they worked… This is not good for me as I would have to add lots and lots of "and"s in my if statement, or use a switch statement or something, but my question is… "Is there an easy way to check if the item in a selected slot is fuel?". Without listing every single item there is.
Here is my function at the moment (That only works with coal)

function refuel()
	for i = 1, 16 do
		turtle.select(i);
		if turtle.getItemCount() > 0 then
			if turtle.getItemDetail(i).name == "minecraft:coal" then
				turtle.refuel();
				print("Successfully added fuel!");
				break;
			end
		elseif i == 16 then
			print("No fuel found, please add fuel!");
			break;
		end
	end
end
Lyqyd #2
Posted 21 November 2016 - 01:45 AM
I believe in most recent versions turtle.refuel(0) will return true if the item could be consumed for fuel, but does not consume the item.
Gargoylegrgs #3
Posted 21 November 2016 - 01:51 AM
I figured out how to do it. Totally derped out here. Here is the new code. Thanks for your response though.

function refuel()
    for i = 1, 16 do
    turtle.select(i);
    if turtle.getItemCount() > 0 then
        if turtle.refuel() then
            print("Successfully added fuel!");
            break;
        end
        elseif i == 16 then
            print("No fuel found, please add fuel!");
            break;
        end
    end
end
Gargoylegrgs #4
Posted 21 November 2016 - 04:18 AM
Hey guys,
So I am having a problem with my turtle refueling everything that's NOT a sapling. If you look at the code below, ot should do just that, but instead, it just executes the elseif statement. If I take away the NOT infront of getItemDetail() it will only use the sapling as fuel, which WORKS… I have no idea why. Please… help…


Edited on 21 November 2016 - 03:23 AM
Dog #5
Posted 21 November 2016 - 04:29 AM
Instead of "if not" substitute ~= for == and see if that works.

if turtle.getItemDetail().name ~= "minecraft:sapling" and turtle.refuel() then
Edited on 21 November 2016 - 03:30 AM
Gargoylegrgs #6
Posted 21 November 2016 - 04:41 AM
Ah, of course. That worked like a charm! Thank you =)

Bomb Bloke #7
Posted 21 November 2016 - 05:01 AM
Threads merged; please stick to the one for the one project.
Gargoylegrgs #8
Posted 21 November 2016 - 05:15 AM
Threads merged; please stick to the one for the one project.

Ah, alright. Will do in future =)