Advertisement
fatboychummy

modu-core

Oct 15th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.72 KB | None | 0 0
  1. --[[
  2. {"get","grab","remove"}
  3. The syntax of ALL module files: line 2: table of module arguments, line 5: start of return <function> statement.  Make sure this portion is commented or it will error.
  4. ]]
  5. return function (input,chests,inventory,tell) --Functions will ALWAYS be passed these four values.
  6.   local arg = input[1]
  7.   local item = input[2]
  8.   local count = tonumber(input[3])
  9.   local damage = input[4]
  10.   if not damage then
  11.     damage = false
  12.   else
  13.     damage = tonumber(damage)
  14.   end
  15.   --As the input still contains the first command argument, your "module" can contain multiple localized functions within this main function
  16.   --useful if your module has a "get" and "remove", like this module does.
  17.  
  18.   if input[3] == "all" or input[3] == "every" then
  19.     count = 100000
  20.   end
  21.   if type(count) ~= "number" then
  22.     tell("Expected an item count for argument #3")
  23.     return 0
  24.   elseif count <= 0 then
  25.     tell("Expected an item count greater than 0 for argument #3")
  26.     return 0
  27.   end
  28.   if type(damage) ~= "number" and type(damage) ~= "boolean" then
  29.     tell("Expected damage value or nil for argument #4")
  30.     return 0
  31.   end
  32.  
  33.   local function get()
  34.     local pushed = 0
  35.     for i = 1,#chests do                      --For each chest...
  36.       local cur = peripheral.wrap(chests[i])  --wrap current chest for search...
  37.       if cur then                             --Sometimes errors if you dont do this.
  38.         local cInv = cur.list()               --Lists the items in chests in a way readable by CC
  39.         for o = 1,cur.size() do              --For every slot in the chest...
  40.           if cInv[o] then                     --If there is an item in slot o...
  41.             if damage then
  42.  
  43.               if cInv[o].name:find(item) and cInv[o].damage == damage then --input 2 is the name of the item to find...
  44.                 local tomove = math.abs(count-pushed)
  45.                 tell("Attempting to push "..tostring(tomove).." items.")
  46.                 pushed = pushed + inventory.pullItems(chests[i],o,tomove)      --push the items and keep track of how many items were pushed
  47.                 if pushed >= count then                                       --if we've pushed how many was wanted we stop.
  48.                   tell("Sent "..tostring(pushed).."/"..tostring(count).." items.")
  49.                   return
  50.                 end
  51.               end
  52.             else
  53.               if cInv[o].name:find(item) then --input 2 is the name of the item to find...
  54.                 local tomove = math.abs(count-pushed)
  55.                 tell("Attempting to push "..tostring(tomove).." items.")
  56.                 pushed = pushed + inventory.pullItems(chests[i],o,tomove)
  57.                 if pushed >= count then
  58.                   tell("Sent "..tostring(pushed).."/"..tostring(count).." items.")
  59.                   return
  60.                 end
  61.               end
  62.             end
  63.           end
  64.         end
  65.       end
  66.     end
  67.     tell("Sent "..tostring(pushed).."/"..tostring(count).." items.")
  68.   end
  69.   local function remove()
  70.     local lastEmpty = false  --Something for speedup
  71.     local function push(count,index) --push items from inventory to chest
  72.       if lastEmpty then --if we've cached the last empty chest...
  73.         for i = lastEmpty,#chests do
  74.           for i = 1,#chests do
  75.             local a = inventory.pushItems(chests[i],count,index)
  76.             if a and a > 0 then
  77.               lastEmpty = i
  78.               return a
  79.             end
  80.           end
  81.           return 0
  82.         end
  83.       else--if we don't know the last empty chest...
  84.         for i = 1,#chests do
  85.           local a = inventory.pushItems(chests[i],count,index)
  86.           if a and a > 0 then
  87.             lastEmpty = i
  88.             return a
  89.           end
  90.         end
  91.         return 0
  92.       end
  93.     end
  94.     local pushed = 0
  95.     local cInv = inventory.list()
  96.     for i = 1,inventory.size() do
  97.       if cInv[i] then
  98.         local c = cInv[i].name
  99.         local d = cInv[i].damage
  100.         if c:find(item) then
  101.           local tomove = math.abs(count-pushed)
  102.           if damage and damage == d then
  103.             tell("Attempting to push "..tostring(tomove).." items.")
  104.             pushed = pushed + push(i,tomove)
  105.           elseif not damage then
  106.             tell("Attempting to push "..tostring(tomove).." items.")
  107.             pushed = pushed + push(i,tomove)
  108.           end
  109.         end
  110.       end
  111.     end
  112.     tell("Pushed "..tostring(pushed).."/"..tostring(count).." items.")
  113.   end
  114.  
  115.   --the below statement will iterate through the first argument and run whatever is needed.
  116.   if input[1] == "get" or input[1] == "grab" then
  117.     get()
  118.   elseif input[1] == "remove" then
  119.     remove()
  120.   else
  121.     tell("This module has no parameter, "..input[1])
  122.   end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement