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

Get access to same info as turtle.compare() can

Started by Evil_Bengt, 05 March 2018 - 09:01 PM
Evil_Bengt #1
Posted 05 March 2018 - 10:01 PM
I'm building a mining turtle program that will try to avoid certain block types (stone etc). I know I could hardcode this but that's not gonna happen, I want the user to be able to configure which blocks to ignore. For this, I have tried having the user place the blocks on top of the turtle and have the turtle run inspect() and store the name in a table. Though I since noticed that the names isn't even remotely unique to block types. For example, stone, granite and whatnot is all called minecraft:stone. Which isn't helpful at all. The same happens with getItemDetail(), and then all I get is name and "damage". Which isn't always damage… Stone is 0, granite is 1 and so on… But inspect() returns alot more information. I know that logs are even messier with different directions and whatnot.

So… I've tried some scenarios with turtle.compare(), and it seems fine. Where does it get it's information from to know that granite ~= stone etc. when it's comparing an item (remember getItemDetail() only returns "name", "damage" and "count") to a block (inspect() return all kinds of goodies). Is there any way for me to do the same comparison reliably with information coming from items in the inventory? Having the user place blocks on top of the turtle seems a bit inconvenient…

Thanks! :D/>
Lupus590 #2
Posted 05 March 2018 - 10:32 PM
Short of creating a database to look up items to find their (placed in the world) block equivalents, it can't be done. This is due to how Minecraft does things and CC can't do anything about it without breaking every other mod by, practically, rewriting the game.
Edited on 05 March 2018 - 09:33 PM
SquidDev #3
Posted 05 March 2018 - 10:41 PM
Short of creating a database to look up items to find their (placed in the world) block equivalents, it can't be done. This is due to how Minecraft does things and CC can't do anything about it without breaking every other mod by, practically, rewriting the game.
That's technically not true. If you presume damage and meta values are equivalent (which they generally are) it's possible to convert items into block states (and vice versa). That being said, it's a bit hacky for a couple of reasons:
  • Block states include additional information: Items have no concept of which direction a chest is facing, but the block state does.
  • Items are not blocks, and blocks are not items: whilst some items are capable of placing blocks, and some blocks can be broken to give an item, it's not fair to assume the two are the convertible. There's always going to be some edge case which breaks things.
Basically there isn't a "one size fits all" solution. Your best bet is just to hard-code the damage values where needed.
Evil_Bengt #4
Posted 05 March 2018 - 11:01 PM
Short of creating a database to look up items to find their (placed in the world) block equivalents, it can't be done. This is due to how Minecraft does things and CC can't do anything about it without breaking every other mod by, practically, rewriting the game.

Well it has to be possible for CC to do it because turtle.compare() does just what I want… I understand that it's not always going to be perfect so maybe I'll just go with storing returns from turtle.inspect() instead, after all, the user will only have to do this once and then it's stored in a file in a "server" for all turtles to enjoy :D/>

Short of creating a database to look up items to find their (placed in the world) block equivalents, it can't be done. This is due to how Minecraft does things and CC can't do anything about it without breaking every other mod by, practically, rewriting the game.
That's technically not true. If you presume damage and meta values are equivalent (which they generally are) it's possible to convert items into block states (and vice versa). That being said, it's a bit hacky for a couple of reasons:
  • Block states include additional information: Items have no concept of which direction a chest is facing, but the block state does.
  • Items are not blocks, and blocks are not items: whilst some items are capable of placing blocks, and some blocks can be broken to give an item, it's not fair to assume the two are the convertible. There's always going to be some edge case which breaks things.
Basically there isn't a "one size fits all" solution. Your best bet is just to hard-code the damage values where needed.

Yeeeeah… That's what I'm thinking too, there's no real connection between blocks and their eventually corresponding items. I'm just curious to know how turtle.compare does it as the level of accuracy I get from turtle.compare is more than enough for me :D/> otherwise as I said, I'll just go with turtle.inspect() the kinda ugly way :D/>
Evil_Bengt #5
Posted 05 March 2018 - 11:07 PM
Oh by the way, maybe I can have the user put all the blocks in the inventory, click something and then have the turtle cycle through the inventory, placing every block and then running turtle.inspect on them so that the user don't have to place them all manually! Why make it easy when you can make it hard? :D/>
KingofGamesYami #6
Posted 06 March 2018 - 05:38 AM
Here's how turtle.compare does it. You can't use the black magic that is reflection inside the Lua sanbox though.