Advertisement
Alakazard12

Sort

Oct 15th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1. local Cheststart = 12
  2. local InputChest = 11
  3. local ChestAmount = 6
  4. local Extra = 6
  5. local ChestType = "iron_chest_"
  6. local Side = "east"
  7. local SideO = "west"
  8.  
  9. local List = {
  10.   ["Raw Beef"] = 3;
  11.   ["Computer"] = 2;
  12.   ["Networking Cable"] = 2;
  13.   ["Chest"] = 4;
  14.   ["Apple"] = 3;
  15.   [""] = 3;
  16.   ["Dirt"] = 1;
  17.   ["Cobblestone"] = 1;
  18.   ["Iron Ingot"] = 5;
  19. }
  20.  
  21. local Chests = {}
  22. local MoveItem
  23.  
  24. local Input = peripheral.wrap("iron_chest_11")
  25.  
  26. for i = 1, ChestAmount do
  27.   Chests[i] = peripheral.wrap(ChestType .. i + Cheststart - 1)
  28. end
  29.  
  30. -- Input.push("west", 0, 64)
  31. local function Distribute()
  32.   for i = 0, (Input.getSizeInventory() - 1) do
  33.     local t = Input.getStackInSlot(i)
  34.     if t then
  35.       local Match
  36.       for i,v in pairs(List) do
  37.         if i == t["name"] then
  38.           Match = v
  39.           break
  40.         end
  41.         if string.sub(i, 1, 1) == "-" then
  42.           print("Yeah")
  43.           if string.find(t["name"]:lower(), i:sub(2):lower()) then
  44.             Match = v
  45.             break
  46.           end
  47.         end
  48.       end
  49.       if Match then
  50.         print("Moving " .. t["name"] .. " to " .. Match .. " from Input (Distributing)")
  51.         if Match == 1 then
  52.           Input.push(Side, i, 64)
  53.         else
  54.           Input.pushIntoSlot(Side, i, 64, 53)
  55.           MoveItem(1, 53, Match)
  56.         end
  57.       else
  58.         print("Moving " .. t["name"] .. " to " .. Extra .. " from input (Misc.)")
  59.         if Extra == 1 then
  60.           Input.push(Side, i, 64)
  61.         else
  62.           Input.pushIntoSlot(Side, i, 64, 53)
  63.           MoveItem(1, 53, Extra)
  64.         end
  65.       end
  66.     end
  67.   end
  68. end
  69.  
  70. local function CheckOther()
  71.   for i,v in pairs(Chests) do
  72.     for t = 0, v.getSizeInventory() - 1 do
  73.       -- print("Item " .. t)
  74.       local Item = v.getStackInSlot(t)
  75.       if Item then
  76.         local Match
  77.         for r,c in pairs(List) do
  78.           if r == Item["name"] then
  79.             Match = c
  80.             break
  81.           end
  82.           if string.sub(r, 1, 1) == "-" then
  83.             if string.find(Item["name"]:lower(), r:sub(2, #r):lower()) then
  84.               Match = c
  85.               break
  86.             end
  87.           end
  88.         end
  89.         if Match then
  90.           if Match ~= i then
  91.             print("Moving " .. Item["name"] .. " to " .. Match .. " from " .. i .. " (Wrong chest)")
  92.             MoveItem(i, t, Match)
  93.           end
  94.         else
  95.           if i ~= Extra then
  96.             print("Moving " .. Item["name"] .. " to " .. Extra .. " from " .. i .. " (Wrong chest)")
  97.             MoveItem(i, t, Extra)
  98.           end
  99.         end
  100.       end
  101.     end
  102.   end
  103. end
  104.      
  105.  
  106. MoveItem = function(Chest, Slot, NewChest)
  107.   local MSide = Side
  108.   if NewChest < Chest then
  109.     MSide = SideO
  110.   end
  111.  
  112.   if Chest - NewChest == 1 or Chest - NewChest == -1 then
  113.     Chests[Chest].push(MSide, Slot, 64)
  114.     return
  115.   end
  116.  
  117.   local ToGo = NewChest - Chest
  118.   local Der = 1
  119.   local Ha = 0
  120.   if ToGo < 0 then
  121.     Ha = 2
  122.     Der = -1
  123.   end
  124.  
  125.   Chests[Chest].pushIntoSlot(MSide, Slot, 64, 53)
  126.  
  127.   for i = Chest + Der, Chest + ToGo - 1 + Ha, Der do
  128.     if i == Chest + ToGo - 1 + Ha then
  129.       Chests[i].push(MSide, 53, 64)
  130.     else
  131.       Chests[i].pushIntoSlot(MSide, 53, 64, 53)
  132.     end
  133.   end
  134. end
  135.  
  136. while true do
  137.   sleep(5)
  138.   Distribute()
  139.   CheckOther()
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement