Advertisement
moomoomoo309

DefragAE

Jan 17th, 2015
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1. --Uncomment to use given pipe side.
  2. --PipeSide="bottom"
  3. --Uncomment to use given monitor side(s). Put commas between for multiple sides, keep the curly braces.
  4. --MonitorSide={"top"}
  5. sleepTime=60 --In seconds. Default is 60.
  6. itemsPerTick=10 --How many items it should search per tick. Default is 10.
  7. checkCount=false --Should it check the amount of items?
  8. itemThreshold=64 --The max amount of items it can have and still be requested. Default is 64.
  9. writeToConsole=true --Should it write to the computer itself?
  10. writeToMonitor=true --Should it write to connected monitors?
  11. nameWhitelist=true --Should it filter based on a whitelist or a blacklist?
  12. nameList={} --Unlocalized name(s) of filtered items.
  13. idWhitelist=true --Should it filter based on a whitelist or a blacklist?
  14. idList={52} --IDs of filtered items. Supports ID:Meta.
  15. modWhitelist=true --Should it filter based on a whitelist or a blacklist?
  16. modList={}
  17. checkDurability=false --Should it look at durability?
  18. durabilityPercentThreshold=50 --Items this percentage or lower will be pulled out if checkDurability is true.
  19. local function getPeripherals(type)
  20.   PerTbl={}
  21.   local Peripherals=peripheral.getNames()
  22.   wildcardBeginning=string.sub(type,1,1)=="*"
  23.   wildcardEnd=string.sub(type,#type)=="*"
  24.   for i=1,#Peripherals do
  25.     index1,index2=string.find(peripheral.getType(Peripherals[i]),type:gsub("*",""))
  26.     if wildcardBeginning and wildcardEnd and index1 then
  27.       table.insert(PerTbl,Peripherals[i])
  28.     elseif wildcardBeginning then
  29.       if index2==#peripheral.getType(Peripherals[i]) then
  30.         table.insert(PerTbl,Peripherals[i])
  31.       end
  32.     elseif wildcardEnd then
  33.       if index1==1 then
  34.         table.insert(PerTbl,Peripherals[i])
  35.       end
  36.     elseif peripheral.getType(Peripherals[i])==type then
  37.       table.insert(PerTbl,Peripherals[i])
  38.     end
  39.   end
  40.   return PerTbl
  41. end
  42.  
  43. local function findPeripheral(type)
  44.   local Peripherals=peripheral.getNames()
  45.   wildcardBeginning=string.sub(type,1,1)=="*"
  46.   wildcardEnd=string.sub(type,#type)=="*"
  47.   for i=1,#Peripherals do
  48.     currentType=peripheral.getType(Peripherals[i])
  49.     index1,index2=string.find(currentType,type:gsub("*",""))
  50.     if wildcardBeginning and wildcardEnd and index1 then
  51.       return Peripherals[i]
  52.     elseif wildcardBeginning then
  53.       if index2==#currentType then
  54.         return Peripherals[i]
  55.       end
  56.     elseif wildcardEnd then
  57.       if index1==1 then
  58.         return Peripherals[i]
  59.       end
  60.     elseif currentType==type then
  61.       return Peripherals[i]
  62.     end
  63.   end
  64. end
  65.  
  66. local function draw(str)
  67.   if writeToMonitor then
  68.     MonTbl=MonitorSide or getPeripherals("monitor")
  69.     for i=1,#MonTbl do
  70.       peripheral.call(MonTbl[i],"write",str)
  71.     end
  72.   end
  73.   if writeToConsole then
  74.     term.write(str)
  75.   end
  76. end
  77.  
  78. local function Clear()
  79.   if writeToMonitor then
  80.     MonTbl=MonitorSide or getPeripherals("monitor")
  81.     for i=1,#MonTbl do
  82.       peripheral.call(MonTbl[i],"clear")
  83.     end
  84.   end
  85.   if writeToConsole then
  86.     term.clear()
  87.   end
  88. end
  89.  
  90. local function CursorPos(x,y)
  91.   if writeToMonitor then
  92.     MonTbl=MonitorSide or getPeripherals("monitor")
  93.     for i=1,#MonTbl do
  94.       peripheral.call(MonTbl[i],"setCursorPos",x,y)
  95.     end
  96.   end
  97.   if writeToConsole then
  98.     term.setCursorPos(x,y)
  99.   end
  100. end
  101.  
  102. function countDown()
  103.   for i=(sleepTime*10),0,-1 do
  104.     CursorPos(1,1)
  105.     draw(clr)
  106.     CursorPos(1,1)
  107.     draw(i/10)
  108.     draw("s until next search.")
  109.     sleep(0.1)
  110.   end
  111. end
  112.  
  113. local function request(id,amt,Count)
  114.   Count=Count+1
  115.   m.makeRequest(id,amt)
  116.   return Count
  117. end
  118.  
  119. --Setup for the main loop
  120. local clr=""
  121. local MonTbl=MonitorSide or getPeripherals("monitor")
  122. local searchTime=0
  123. x,y=term.getSize()
  124. for i=1,#MonTbl do
  125.   local p=peripheral.wrap(MonTbl[i])
  126.   local x2,y2=p.getSize()
  127.   x=math.max(x,x2)
  128.   y=math.max(y,y2)
  129. end
  130. for i=1,x do
  131.   clr=clr.." " --String that will clear the whole line of a monitor.
  132. end
  133. m={}
  134. if PipeSide and peripheral.getType(PipeSide)=="LogisticsPipes:Request" then
  135.   m=peripheral.wrap(PipeSide)
  136. else
  137.   if peripheral.find then --Checks to see if peripheral.find exists at all
  138.     m=peripheral.find("LogisticsPipes:Request")
  139.   else
  140.     m=findPeripheral("LogisticsPipes:Request") --This isn't as nice as peripheral.find(), but it'll work.
  141.   end
  142. end
  143. if not m then
  144.   error("No request pipe found.")
  145. end
  146.  
  147. --Main loop
  148. function main()
  149.   local Items=m.getAvailableItems()
  150.   local Count=0
  151.   searchTime=0
  152.   if #Items>itemsPerTick then
  153.     CursorPos(1,2)
  154.     draw("Searching...")
  155.   end
  156.   for k,v in pairs(Items) do      
  157.     local id=v.getValue1()
  158.     local amt=m.getItemAmount(id)
  159.     if checkCount and amt<=itemThreshold then
  160.       Count=request(id,amt,Count)
  161.     end
  162.     for i=1,#nameList do
  163.       if nameWhitelist and id.getName()==nameList[i] then
  164.         Count=request(id,amt,Count)
  165.       elseif not nameWhitelist and id.getName()~=nameList[i] then
  166.         Count=request(id,amt,Count)
  167.       end
  168.     end
  169.     for i=1,#idList do
  170.       findColon,findColonEnd=string.find(idList[i],":")
  171.       if findColon then
  172.         if idWhitelist and id.getId()==string.sub(idList[i],1,findColon-1) and id.getData()==string.sub(idList[i],findColon+1) then
  173.           Count=request(id,amt,Count)
  174.         elseif not idWhitelist and id.getId()~=idList[i] then
  175.           Count=request(id,amt,Count)
  176.         end
  177.       else
  178.         if idWhitelist and id.getId()==idList[i] then
  179.           Count=request(id,amt,Count)
  180.         elseif not idWhitelist and id.getId()~=idList[i] then
  181.           Count=request(id,amt,Count)
  182.         end
  183.       end
  184.     end
  185.     for i=1,#modList do
  186.       if modWhitelist and id.getModName()==modList[i] then
  187.         Count=request(id,amt,Count)
  188.       elseif not modWhitelist and id.getModName()~=modList[i] then
  189.         Count=request(id,amt,Count)
  190.       end
  191.     end
  192.     if checkDurability and id.isDamageable() and id.getData()/id.getUndamaged().getData()<=durabilityPercentThreshold/100 then
  193.       Count=request(id,amt,Count)
  194.     end
  195.     if k%itemsPerTick==0 then
  196.       searchTime=searchTime+.05
  197.       sleep(0.05)
  198.     end
  199.   end
  200.   CursorPos(1,2)
  201.   draw(clr)
  202.   CursorPos(1,2)
  203.   if Count==1 then
  204.     draw(Count.." item pulled.")
  205.   else
  206.     draw(Count.." items pulled.")
  207.   end
  208. end
  209.  
  210. --"Real" main loop
  211. while true do
  212.   parallel.waitForAll(countDown,main)
  213. end
  214. --Program by moomoomoo3O9
  215.  
  216. --LP ItemIdentifier functions (accessed with m.getAvailableItems()[x].getValue1()
  217. --getValue2() returns an integer for the LP documentation's functions
  218. --[[
  219. getID()
  220. getUndamaged()
  221. isFluidContainer()
  222. getName()
  223. hasTagCompound()
  224. getTagCompound()
  225. getModName()
  226. isDamageable()
  227. getType()
  228. getData()
  229. getFluidContainer()
  230. makeStack()
  231. getIgnoringNBT()
  232. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement