Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Code by NeverCast
- This should be loaded like a typical api
- Feel free to name it what you like
- --]]
- local directions = { [0]=0,[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,["down"] = 0, ["up"] = 1, ["-Z"] = 2, ["+Z"] = 3, ["-X"] = 4, ["+X"] = 5, ["+Y"] = 1, ["-Y"] = 0}
- directions.south = directions["+Z"]
- directions.east = directions["+X"]
- directions.north = directions["-Z"]
- directions.west = directions["-X"]
- local knownUUIDs = {}
- -- Bugged UUIDS
- knownUUIDs["27DFD405"] = {55, 0} -- Chest
- knownUUIDs["2748D405"] = {280, 0} -- Stick
- knownUUIDs["27D9D405"] = {58, 0} -- Crafting Table
- knownUUIDs["2F94D404"] = {4256, 1} -- Floppy Disk
- knownUUIDs["25A1D405"] = {1226, 0} -- Disk Drive
- -- Just so i can read it :P
- function DEC_HEX(IN)
- local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
- while IN>0 do
- I=I+1
- IN, D= math.floor(IN/B), (IN % B) + 1
- OUT=string.sub(K,D,D)..OUT
- end
- return OUT
- end
- -- Gets the Unique ID based on the ID and Meta
- function getID(id, meta)
- if meta == nil then -- If meta doesn't exist, it should equal 0
- meta = 0
- end
- meta = bit.bxor(meta, 0x6E6C) -- encode meta, 0x6E6C = 28268
- local uuid = bit.bxor(id, 0x4F89) * 0x8000 + bit.bxor(meta, 0x3A69) -- encode id, add space for metadata, encode metadata. result: IIIIMMMM where I = id and M = meta
- return uuid
- end
- -- Get a stack table from a single uuid and amount
- -- This does all the math to reverse the unique ID algorithm that RG wrote.
- -- Valid for version 2.3
- function getStack(uuid, c)
- local sUUID = DEC_HEX(uuid)
- local id = 0
- local metadata = 0
- if knownUUIDs[sUUID] ~= nil then -- If ID is logged:
- id = knownUUIDs[sUUID][1]
- metadata = knownUUIDs[sUUID][2]
- else -- If ID is not logged:
- -- Reverse RG's fancy math
- local subt = bit.band(uuid, 0xFFFF) -- get last four digits, meta
- local dexorm = bit.bxor(subt, 0x3A69) -- decode meta; (a xor n) xor n = a, as long as no digits are lost
- metadata = bit.bxor(dexorm, 0x6E6C) -- decode modified id
- id = bit.bxor((uuid-subt)/0x8000, 0x4F89) -- seperate id, divide by 0x8000, then decode
- -- Confirm id
- if uuid ~= getID(id, metadata) then
- print("Decode failed!")
- print("Original: " .. sUUID)
- print("Result: " .. DEC_HEX(getID(id, metadata)))
- print("If this is a mod block/item, it means the " ..
- "turtle could not find it's ID and/or metadata.")
- error()
- end
- -- put it in to a nice table
- knownUUIDs[sUUID] = {id, metadata}
- print("Logging " .. sUUID .. " as " .. id .. ":" .. metadata)
- end
- local stack = {}
- stack.amount = c
- stack.id = id
- stack.meta = metadata
- return stack
- end
- -- Get stacks from an Interactive Sorter
- -- direction : the direction of the Interactive Sorter Peripheral
- -- invDirection: the direction of the inventory from the peripheral
- -- valid options for invDirection are 0,1,2,3,4,5 ( original values),
- -- north, south, east, west, up, down, and the +/-X,Y,Z strings.
- -- (see directions variable)
- function getStacks(direction, invDirection)
- if not peripheral.isPresent(direction) then
- return false, "No Peripheral"
- end
- if peripheral.getType(direction) ~= "interactiveSorter" then
- return false, "Not a sorter"
- end
- local stacks = {}
- for uuid,count in pairs(peripheral.call(direction, "list", directions[invDirection])) do
- table.insert(stacks, getStack(uuid, count))
- end
- return true, stacks
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement