View difference between Paste ID: bGbnyYWZ and SHy4Pw8H
SHOW: | | - or go back to the newest paste.
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
local knownUUIDs = {}
12
-- Bugged UUIDS
13
knownUUIDs["27DFD405"] = {55, 0} -- Chest
14
knownUUIDs["2748D405"] = {280, 0} -- Stick
15
knownUUIDs["27D9D405"] = {58, 0} -- Crafting Table
16
knownUUIDs["2F94D404"] = {4256, 1} -- Floppy Disk
17
knownUUIDs["25A1D405"] = {1226, 0} -- Disk Drive
18
19
-- Just so i can read it :P
20
function DEC_HEX(IN)
21
    local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
22
    while IN>0 do
23
        I=I+1
24
        IN, D= math.floor(IN/B), (IN % B) + 1
25
        OUT=string.sub(K,D,D)..OUT
26
    end
27
    return OUT
28
end
29
30
-- Gets the Unique ID based on the ID and Meta
31
function getID(id, meta)
32
  if meta == nil then -- If meta doesn't exist, it should equal 0
33
    meta = 0
34
  end
35
  meta = bit.bxor(meta, 0x6E6C) -- encode meta, 0x6E6C = 28268
36
  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
37
  return uuid
38
end
39
40
-- Get a stack table from a single uuid and amount
41
-- This does all the math to reverse the unique ID algorithm that RG wrote.
42
-- Valid for version 2.3
43
function getStack(uuid, c)
44
        local sUUID = DEC_HEX(uuid)
45
        local id = 0
46
        local metadata = 0
47
        if knownUUIDs[sUUID] ~= nil then -- If ID is logged:
48
          id = knownUUIDs[sUUID][1]
49
          metadata = knownUUIDs[sUUID][2]
50
        else -- If ID is not logged:
51
          -- Reverse RG's fancy math
52
          local subt = bit.band(uuid, 0xFFFF) -- get last four digits, meta
53
          local dexorm = bit.bxor(subt, 0x3A69) -- decode meta; (a xor n) xor n = a, as long as no digits are lost
54
          metadata = bit.bxor(dexorm, 0x6E6C) -- decode modified id
55
          id = bit.bxor((uuid-subt)/0x8000, 0x4F89) -- seperate id, divide by 0x8000, then decode
56
          -- Confirm id
57
          if uuid ~= getID(id, metadata) then
58
            print("Decode failed!")
59
            print("Original: " .. sUUID)
60
            print("Result: " .. DEC_HEX(getID(id, metadata)))
61
            print("If this is a mod block/item, it means the " ..
62
                   "turtle could not find it's ID and/or metadata.")
63-
            print("Otherwise, report this to TehPers")
63+
64
          end
65
          -- put it in to a nice table
66
          knownUUIDs[sUUID] = {id, metadata}
67
          print("Logging " .. sUUID .. " as " .. id .. ":" .. metadata)
68
        end
69
        local stack = {}
70
        stack.amount = c
71
        stack.id = id
72
        stack.meta = metadata
73
        return stack
74
end
75
 
76
-- Get stacks from an Interactive Sorter
77
-- direction   : the direction of the Interactive Sorter Peripheral
78
-- invDirection: the direction of the inventory from the peripheral
79
-- valid options for invDirection are 0,1,2,3,4,5 ( original values),
80
-- north, south, east, west, up, down, and the +/-X,Y,Z strings.
81
-- (see directions variable)
82
function getStacks(direction, invDirection)
83
        if not peripheral.isPresent(direction) then
84
                return false, "No Peripheral"
85
        end
86
        if peripheral.getType(direction) ~= "interactiveSorter" then
87
                return false, "Not a sorter"
88
        end
89
        local stacks = {}
90
        for uuid,count in pairs(peripheral.call(direction, "list", directions[invDirection])) do
91
                table.insert(stacks, getStack(uuid, count))
92
        end
93
        return true, stacks    
94
end