392 posts
Location
Christchurch, New Zealand
Posted 08 January 2013 - 07:43 PM
Hello ComputerCraft forums!
I present to you an API that allows simplified reading of stacks in an Interactive Sorter from Misc Peripherals, It returns an array of tables, each with the following fields:
- id The ID of the item stack, the actual ID, not the Unique Identifier
- meta The metadata of the item, this is nil if the item takes damage
- amount The size of the stack of items.
I hope this helps people write amazing sorting programs, I know I'll be using it for my turtles.
Here's the code:
http://pastebin.com/dUh038dfBasically you call getStacks with the direction of the Interactive Sorter and then the direction of the inventory, for example
getStacks("front", "up") and that will return an array of stack tables from the inventory above the peripheral, with id, metadata and count.
Should be self explanatory, ENJOY!
I've also updated the source to have a getUID function that will return the uuid for the id and meta, as I believe you need it for the extract
-NeverCast
(ps. don't forget to up vote me if you likey, thanks )
Updated a couple times thanks to the people commenting below with fixes, You're wonderful!
3 posts
Posted 11 January 2013 - 03:42 AM
Excellent piece :D/> I'm going to try to use this.
Because it's so well defined, I would shorten the "directions" object to something like "d" to save typing time.
392 posts
Location
Christchurch, New Zealand
Posted 13 January 2013 - 10:39 PM
Thanks for the tip medicdude :)/>
I hope this serves you well!
Also before line 33 you may want to add metadata = bit.band(metadata, 0x7FFF)
That'll stop metadata from going out of range :)/> Which is something I overlooked.
3 posts
Posted 14 January 2013 - 04:19 PM
I tried using your API to find out the item id's because I wanted turtles to auto-craft without knowing exactly what goes into their inventory :D/>
It mostly worked, but failed a few times, so I looked into it, and found why.
Sometimes the item id, when being encoded into the UUII, overflows into the metadata. Try sticks: The ID is 280, so it becomes 0x27488000 before adding the metadata (which is 0). When you add it, the metadata turns from 0x5405 to 0xD405. That changes the metadata by a lot, turning the output from 0 to decimal 32,768.
Also, at line 14 and 30, shouldn't those numbers be 28268, or 0x6E6C?
Other than that, good job! It works amazingly well, and it seems the only bug I found is actually in the sorter code, sadly. But hopefully you can find a way around it.
392 posts
Location
Christchurch, New Zealand
Posted 15 January 2013 - 09:21 PM
Thanks a lot for the bug spots there, I did have some issues trying to get it working with diamonds which are around the 280 area, an overflow would make perfect sense.
As for lines 14 and 30, I'm not entirely sure, I think I pulled most the constants verbatim from Misc Peripherals. If you can confirm that it should be 0x6E6C instead of 0x6E66 then I'll change it :)/>
Glad it's working for you mostly, If you're interested I wrote a crafting program that uses the computer controlled crafter to automatically craft things based on an items and recipes database. It'll auto magically break down a recipe in to it's core components, request them and build what you need. I've got it making me Solar Panels. It knows the recipes for a Furnace and Electric Circuits etc. So it makes those firsts as they are 'dependencies'. I'm kind of proud of it but am shy when it comes to showing my code.
3 posts
Posted 16 January 2013 - 12:53 PM
That's awesome! Eventually I'm gonna have to make a crafter for the program I'm working on.
I also looked directly at the code and didn't see the number 0x6E66 anywhere. I found the number 28268, or 0x6E6C, as the default metadata value.
It could just be the decompiler though.
For the bug, I just ran through the items/blocks that I needed, found the ones that bugged, and added them to a list of UUIDs. Here's my modified version of your code that I'm using. The UUID list is incomplete, btw:
http://pastebin.com/bGbnyYWZYou may have to change some of the UUIDs for them to work because you could have different ID's for the mod items.
Also, for the sorter that I'm using, I am using an automatic crafting table instead of an interactive sorter because whenever I'd try to get the list of items in the turtles, it'd return a table that was out of order and was unusable for finding what each slot held. With the crafting table, list(side) will list the items in the table's buffer, so I had it place the item in, get the ID, and retrieve it.
2 posts
Posted 21 January 2013 - 10:48 AM
Just registered to post this. On line 27, in the getStack() function, change
local subt = bit.band(uuid, 0xffff)
to
local subt = bit.band(uuid, 0x7fff)
and you will not have those weird items that are off by a little. Dug around the mod myself to see why this was, and when it's multiplied by 32768, that's only 15 bits over, not 16. So, you don't need that one last bit, hence changing from F to 7. That should also eliminate needing to do the bit.band with 7FFF later on as well.
392 posts
Location
Christchurch, New Zealand
Posted 21 January 2013 - 10:53 AM
Oh thanks mleo2003!
I don't know if you'll ever return but thanks for registering to help out :)/>
Also thanks for the few fixes there TehPers
12 posts
Posted 30 January 2013 - 12:52 AM
Thanks for this
But I am getting
sorterAPI:56: bad argument #1 to pairs (table expected, got nil)
My code is
local tArgs = {...}
local sortdir = tArgs[1]
local invdir = tArgs[2]
if sortdir == nil or invdir == nil then
print("Type icount <direction to sorter> <direction to inventory>")
else
os.loadAPI("sorterAPI")
items = sorterAPI.getStacks(sortdir, invdir)
end
Edit: Nevermind, have figured out the chest needs to be above the sorter.
Edited on 30 January 2013 - 03:24 AM
57 posts
Posted 03 February 2013 - 03:19 AM
I think this not work anymore in the last version of miscperipheral…
12 posts
Posted 03 February 2013 - 10:15 AM
I think this not work anymore in the last version of miscperipheral…
Correct. Will need reverse engineering again.
392 posts
Location
Christchurch, New Zealand
Posted 05 February 2013 - 12:06 PM
I just hope RichardG hasn't used a hash or something.
22 posts
Posted 06 February 2013 - 02:15 AM
He didn't. Added random salts to every property, but thats it. You can figure it out by inspecting a few items, and their IDs
4 posts
Posted 13 February 2013 - 09:59 PM
So uhh, I'm a little confused. I want to use the interactive sorter but I must be missing some knowledge that allows all of this to make sense.
I have a computer with an interactive sorted adjacent to it such that when I'm looking at the computer's front the sorter is on the left. I assume that therefore the direction variable is "left". A chest is above the sorter, invDirection being "up".
I'm using SavageCore's script from above except I included a textutils.tabulate(items) to print the result. I type "icount left up" and nothing is printed.
What's going on here?
12 posts
Posted 23 February 2013 - 05:04 AM
@lost_RD
Did you figure out how to do things? This API is no longer really required to interact as real IDs are shown.
local stacks = {}
for item,count in pairs(peripheral.call("left", "list", 0)) do
itemId = bit.band(item, 0x7fff)
damageValue = bit.brshift(item, 15)
local stack = {}
stack.amount = tonumber(count)
stack.id = tonumber(itemId)
stack.meta = tonumber(damageValue)
stack.uuid = tonumber(item)
table.insert(stacks, stack)
end
This is a snippet of code I am working on to count items from a sorter on the left of a computer with a chest underneath said sorter.
The bit operations convert the new uuids for items with meta data. Stone will return 1 where as pink wool would return 196643 (I think) rather than 35:6.
Have a play and PM me or create a new thread in Ask a Pro is probably the best way to get help now.
1 posts
Posted 02 March 2013 - 02:03 PM
I'm getting,
(name):90: bad argument: table expected, got nil
i have no clue how to fix this.
Any help?
7508 posts
Location
Australia
Posted 02 March 2013 - 04:06 PM
I'm getting,
(name):90: bad argument: table expected, got nil
i have no clue how to fix this.
Any help?
Interesting……… How are you getting and error on line 90 when there is 60 lines in the program…