Advertisement
ingiethingie

extractItem using AE and Turtle

Feb 2nd, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. --[[
  2.   Extract Item
  3.   Written 2014 ingie -> ing@ingie.com
  4.  
  5.   Usage: extractItems Name [Qty] [Fuzzy]
  6.  
  7.   do with it what you will...
  8.   knowledge is free and licences are just beurocracy
  9.   if you want to say who wrote it, that's nice of you...
  10. ]]--
  11.  
  12. -- setup
  13.  
  14. local args = { ... }
  15. local AESide = "left"
  16. local turtleDirection = "south"
  17.  
  18. -- helpers
  19.  
  20. local isMatchingItem = function( _itemName, _search, _fuzzy )
  21.  
  22.   if not _fuzzy then
  23.     return ( _itemName == _search )
  24.   else
  25.     return string.find( string.lower(_itemName) , string.lower(_search) )
  26.   end
  27. end
  28.  
  29. -- functions
  30.  
  31. local extractNamedItem = function( _ae, _search, _direction, _qty, _fuzzy )
  32.  
  33.   local _all = _ae.getAvailableItems()
  34.   local _found = { }
  35.  
  36.   for _, _v in pairs(_all) do
  37.     if isMatchingItem( _v.name, _search, _fuzzy ) then
  38.       local _ex = _ae.extractItem( { id = _v.id, qty = _qty, dmg = _v.dmg } , _direction )
  39.       if _ex then
  40.         table.insert( _found,_v )
  41.       end
  42.  
  43.       if not _fuzzy then break end
  44.     end
  45.   end
  46.  
  47.   return _found
  48. end
  49.  
  50. -- parse arguments
  51.  
  52.   if #args < 1 or #args > 3 then
  53.     print("Usage: extractItems Name [Qty] [Fuzzy]")
  54.     error()
  55.   end
  56.  
  57.   local searchName = args[1]
  58.   local qty = ( #args >= 1 and tonumber(args[2]) ) or 1
  59.   local fuzziness = ( #args >= 2 and string.lower(args[3]) == "true" ) or false
  60.  
  61. -- do the extraction thing
  62.  
  63.   local extracted = extractNamedItem( peripheral.wrap(AESide), searchName, turtleDirection, qty, fuzziness )
  64.  
  65.   if extracted and #extracted > 0 then
  66.     for k,v in ipairs(extracted) do
  67.       print("Extracted : "..tostring(v.name))
  68.     end
  69.   else
  70.     print("Unable to extract item \""..searchName.."\".")
  71.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement