Guest User

MiscPeripherals UUID Reader

a guest
Jan 20th, 2013
2,558
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[ Code by NeverCast
  2.      This should be loaded like a typical api
  3.      Feel free to name it what you like
  4. --]]
  5. 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}
  6. directions.south = directions["+Z"]
  7. directions.east = directions["+X"]
  8. directions.north = directions["-Z"]
  9. directions.west = directions["-X"]
  10.  
  11. -- Gets the Unique ID based on the ID and Meta
  12. function getID(id, meta)
  13.   if meta == nil then
  14.     meta = 27268
  15.   else
  16.     meta = bit.bxor(meta, 0x6E6C)
  17.   end
  18.   local uuid = bit.bxor(id, 0x4F89) * 0x8000 + bit.bxor(meta, 0x3A69)
  19.   return uuid
  20. end
  21.  
  22. -- Get a stack table from a single uuid and amount
  23. -- This does all the math to reverse the unique ID algorithm that RG wrote.
  24. -- Valid for version 2.3
  25. function getStack(uuid, c)
  26.         -- Reverse RG's fancy math
  27.         local subt = bit.band(uuid, 0x7fff)
  28.         local dexorm = bit.bxor(subt, 0x3a69)
  29.         local metadata = nil
  30.         if dexorm ~= 28262 then -- item takes dmg
  31.                 metadata = bit.bxor(dexorm, 0x6e6c)
  32.         end
  33.         local id = bit.bxor((uuid-subt)/0x8000, 0x4f89)
  34.         -- put it in to a nice table
  35.         local stack = {}
  36.         stack.amount = c
  37.         stack.id = id
  38.         stack.meta = metadata
  39.         return stack
  40. end
  41.  
  42. -- Get stacks from an Interactive Sorter
  43. -- direction   : the direction of the Interactive Sorter Peripheral
  44. -- invDirection: the direction of the inventory from the peripheral
  45. -- valid options for invDirection are 0,1,2,3,4,5 ( original values),
  46. -- north, south, east, west, up, down, and the +/-X,Y,Z strings.
  47. -- (see directions variable)
  48. function getStacks(direction, invDirection)
  49.         if not peripheral.isPresent(direction) then
  50.                 return false, "No Peripheral"
  51.         end
  52.         if peripheral.getType(direction) ~= "interactiveSorter" then
  53.                 return false, "Not a sorter"
  54.         end
  55.         local stacks = {}
  56.         for uuid,count in pairs(peripheral.call(direction, "list", directions[invDirection])) do
  57.                 table.insert(stacks, getStack(uuid, count))
  58.         end
  59.         return true, stacks    
  60. end
RAW Paste Data