Advertisement
moomoomoo309

DefragAE 1.2

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