Advertisement
NanoBob

get items

Aug 24th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. function getItems()
  2.   items={}
  3.   slots={}
  4.   turtle.turnRight()
  5.   turtle.turnRight()
  6.   chestOne=peripheral.wrap("right")
  7.   allItems={}
  8.   for i=1,27 do
  9.     slotItems=chestOne.getStackInSlot(i)
  10.     if slotItems~=nil then
  11.       slotItems['slot']=i
  12.       allItems[#allItems+1]=slotItems
  13.     end
  14.   end
  15.   for nothing,itemData in pairs(allItems) do
  16.      if items[itemData['id']]~=nil then
  17.        items[itemData['id']]={['name']=itemData['name'],['count']=itemData['qty']+items[itemData['id']]['count']}
  18.     else
  19.        items[itemData['id']]={['name']=itemData['name'],['count']=itemData['qty']}
  20.     end
  21.   end
  22.   for nothing,itemData in pairs(allItems) do
  23.      if slots[itemData['slot']]~=nil then
  24.        slots[itemData['slot']]={['name']=itemData['name'],['count']=itemData['qty']+slots[itemData['id']]['count'],['id']=itemData['id']}
  25.     else
  26.        slots[itemData['slot']]={['name']=itemData['name'],['count']=itemData['qty'],['id']=itemData['id']}
  27.     end
  28.   end
  29.   turtle.turnRight()
  30.   turtle.turnRight()
  31.   chestOne=peripheral.wrap("right")
  32.   allItems={}
  33.   for i=1,27 do
  34.     slotItems=chestOne.getStackInSlot(i)
  35.     if slotItems~=nil then
  36.       slotItems['slot']=i
  37.       allItems[#allItems+1]=slotItems
  38.     end
  39.   end
  40.   for nothing,itemData in pairs(allItems) do
  41.     if items[itemData['id']]~=nil then
  42.        items[itemData['id']]={['name']=itemData['name'],['count']=itemData['qty']+items[itemData['id']]['count']}
  43.     else
  44.        items[itemData['id']]={['name']=itemData['name'],['count']=itemData['qty']}
  45.     end
  46.   end
  47.   for nothing,itemData in pairs(allItems) do
  48.      if slots[itemData['slot']]~=nil then
  49.        slots[itemData['slot']]={['name']=itemData['name'],['count']=itemData['qty']+slots[itemData['id']]['count'],['id']=itemData['id']}
  50.     else
  51.        slots[itemData['slot']]={['name']=itemData['name'],['count']=itemData['qty'],['id']=itemData['id']}
  52.     end
  53.   end
  54.   for id,data in pairs(items) do
  55.     print(id.." ("..data['name']..") : "..data['count'])
  56.   end
  57.   for id,data in pairs(slots) do
  58.     print(id.." ("..data['name']..") : "..data['count'])
  59.   end
  60.   return items,slots
  61. end
  62.  
  63. function getsideItemsInSide(side)
  64.   sidesideItems={}
  65.   if side=="north" then
  66.     chestOne=peripheral.wrap("right")
  67.     allsideItems={}
  68.     slots={}
  69.     for i=1,27 do
  70.       slotsideItems=chestOne.getStackInSlot(i)
  71.       if slotsideItems~=nil then
  72.         slotsideItems['slot']=i
  73.         allsideItems[#allsideItems+1]=slotsideItems
  74.       end
  75.     end
  76.     for nothing,itemData in pairs(allsideItems) do
  77.       if sideItems[itemData['id']]~=nil then
  78.          sideItems[itemData['id']]={['name']=itemData['name'],['count']=itemData['qty']+sideItems[itemData['id']]['count']}
  79.       else
  80.          sideItems[itemData['id']]={['name']=itemData['name'],['count']=itemData['qty']}
  81.       end
  82.     end
  83.     for nothing,itemData in pairs(allsideItems) do
  84.        if slots[itemData['slot']]~=nil then
  85.          slots[itemData['slot']]={['name']=itemData['name'],['count']=itemData['qty']+slots[itemData['id']]['count'],['id']=itemData['id']}
  86.       else
  87.          slots[itemData['slot']]={['name']=itemData['name'],['count']=itemData['qty'],['id']=itemData['id']}
  88.       end
  89.     end
  90.   elseif side=="south" then
  91.     turtle.turnRight()
  92.     turtle.turnRight()
  93.     chestOne=peripheral.wrap("right")
  94.     allsideItems={}
  95.     slots={}
  96.     for i=1,27 do
  97.       slotsideItems=chestOne.getStackInSlot(i)
  98.       if slotsideItems~=nil then
  99.         slotsideItems['slot']=i
  100.         allsideItems[#allsideItems+1]=slotsideItems
  101.       end
  102.     end
  103.     for nothing,itemData in pairs(allsideItems) do
  104.       if sideItems[itemData['id']]~=nil then
  105.          sideItems[itemData['id']]={['name']=itemData['name'],['count']=itemData['qty']+sideItems[itemData['id']]['count']}
  106.       else
  107.          sideItems[itemData['id']]={['name']=itemData['name'],['count']=itemData['qty']}
  108.       end
  109.     end
  110.     for nothing,itemData in pairs(allsideItems) do
  111.        if slots[itemData['slot']]~=nil then
  112.          slots[itemData['slot']]={['name']=itemData['name'],['count']=itemData['qty']+slots[itemData['id']]['count'],['id']=itemData['id']}
  113.       else
  114.          slots[itemData['slot']]={['name']=itemData['name'],['count']=itemData['qty'],['id']=itemData['id']}
  115.       end
  116.     end
  117.     turtle.turnRight()
  118.     turtle.turnRight()  
  119.   end
  120.   return sidesideItems,slots
  121. end
  122.  
  123. function getSlotsWithItem(slots,id)
  124.     slotsWithItem={}
  125.     if slots~=nil then
  126.       for slot,data in pairs(slots) do
  127.           if data['id']==id then
  128.             slotsWithItem[slot]=data['count']
  129.           end
  130.       end
  131.       return slotsWithItem
  132.     end
  133. end
  134.  
  135. function getSpecificItemCount(id)
  136.   allItems,allSlots=getItems()
  137.   count=items[id]['count']
  138.   return count
  139. end
  140.  
  141. function pullItem(id,count,side)
  142.   sideItems,sideSlots=getItemsInSide(side)
  143.   if sideItems[id]~=nil then
  144.     available=sideItems[id]['count']
  145.     slotsWithitem=getSlotsWithItem(sideSlots,id)
  146.     if side=="north" then
  147.       for slot,availableCount in pairs(slotsWithItem) do
  148.         if availableCount>count then
  149.           temp=peripheral.wrap("right")
  150.           temp.pullItem("south",slot,count,1)
  151.           turtle.dropDown(1)
  152.         else
  153.           temp=peripheral.wrap("right")
  154.           temp.pullItem("south",slot,availableCount,1)
  155.           turtle.dropDown(1)
  156.           count=count-availableCount
  157.         end
  158.       end
  159.     elseif side=="south" then
  160.       turtle.turnRight()
  161.       turtle.turnRight()
  162.       temp=peripheral.wrap("right")
  163.       temp.pullItem()
  164.       turtle.dropDown(1)
  165.       turtle.turnRight()
  166.       turtle.turnRight()         
  167.     end
  168.   end
  169.   return count  
  170. end
  171.  
  172.  
  173.  
  174. getItems()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement