Advertisement
fatboychummy

Modu-move

Nov 4th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.36 KB | None | 0 0
  1. --[[
  2. {"move","transfer","trans"}
  3. ]]
  4.  
  5. return function (input,chests,inventory,tell,dynaStore)
  6.   local function set(name, ... )
  7.     local chestInfo = {...}
  8.     local checks = #chestInfo
  9.     local hits = {}
  10.     for i = 1,#chestInfo do
  11.       for o = 1,#chests do
  12.         local cChest = chests[o]
  13.         if cChest:find(chestInfo[i]) then
  14.           if hits[cChest] == nil then
  15.             hits[cChest] = {}
  16.           end
  17.           hits[cChest][i] = true
  18.         end
  19.       end
  20.     end
  21.  
  22.     local maxHits = 0
  23.     local found = false
  24.     local similar = false
  25.     for k,v in pairs(hits) do
  26.       local flag = true
  27.       for i = 1,checks do
  28.         if not v[i] then
  29.           flag = false
  30.           break
  31.         end
  32.       end
  33.       if flag then
  34.         if found then
  35.           similar = k
  36.         else
  37.           found = k
  38.         end
  39.       end
  40.     end
  41.  
  42.     if similar then
  43.       tell("Not enough information about the specified chest:")
  44.       tell("Got too many results.")
  45.     elseif not found then
  46.       tell("No chest matched specifications.")
  47.     elseif found and not similar then
  48.       tell("Chest found:")
  49.       tell(found)
  50.       tell("Saved as '".. name .."'")
  51.       dynaStore[name] = found
  52.     else
  53.       tell("Yeah this shouldn't happen at all...")
  54.       tell("("..tostring(checks).." "..tostring(c)..")")
  55.     end
  56.   end
  57.  
  58.   local function remove(name)
  59.     dynaStore[name] = nil
  60.     tell("Removed "..name.." successfully.")
  61.   end
  62.  
  63.   local function move(from,to,item,count,damage)
  64.     local from2
  65.     if dynaStore[to] then
  66.       to = dynaStore[to]
  67.     end
  68.     if dynaStore[from] then
  69.       from = dynaStore[from]
  70.     end
  71.     if to == "inventory" then
  72.       to = inventory
  73.     end
  74.     if from == "inventory" then
  75.       from = to
  76.       to = inventory
  77.     end
  78.     local pushPullFlag = false
  79.     local total = 0
  80.     local function push(index)
  81.       return from.pushItems(to,index,math.abs(math.ceil(count-total)))
  82.     end
  83.     local function pull(index)
  84.       return to.pullItems(from,index,math.abs(math.ceil(count-total)))
  85.     end
  86.     if type(to) ~= "table" then
  87.       from = peripheral.wrap(from)
  88.       pushPullFlag = true
  89.     end
  90.     if pushPullFlag then
  91.       if type(from) == "table" then
  92.         local cInv = from.list()
  93.         for i = 1,from.size() do
  94.           if cInv[i] then
  95.             if damage then
  96.               if cInv[i].name:find(item) and cInv[i].damage == damage then
  97.                 total = total + push(i)
  98.               end
  99.             else
  100.               if cInv[i].name:find(item) then
  101.                 total = total + push(i)
  102.               end
  103.             end
  104.           end
  105.           if total >= count then
  106.             tell("Move operation successful")
  107.             tell("Moved "..tostring(total).." items.")
  108.             return
  109.           end
  110.         end
  111.       else
  112.         tell("Failed to wrap inventory "..tostring(from))
  113.       end
  114.       tell("Failed to move some items")
  115.       tell("("..total.."/"..count.." items moved)")
  116.     else
  117.       if type(to) == "table" then
  118.         local cInv = to.list()
  119.         for i = 1,to.size() do
  120.           if cInv[i] then
  121.             if damage then
  122.               if cInv[i].name:find(item) and cInv[i].damage == damage then
  123.                 total = total + pull(i)
  124.               end
  125.             else
  126.               if cInv[i].name:find(item) then
  127.                 total = total + pull(i)
  128.               end
  129.             end
  130.           end
  131.           if total >= count then
  132.             tell("Move operation successful")
  133.             tell("Moved "..tostring(total).." items.")
  134.             return
  135.           end
  136.         end
  137.       else
  138.         tell("Failed to wrap inventory "..tostring(from))
  139.       end
  140.       tell("Failed to move some items")
  141.       tell("("..total.."/"..count.." items moved)")
  142.     end
  143.   end
  144.  
  145.   local function moveFromAll(to,item,count,damage)
  146.     if dynaStore[to] then
  147.       to = dynaStore[to]
  148.     end
  149.     local total = 0
  150.     for i = 1,#chests do
  151.       if chests[i] ~= to then
  152.         total = total + move(chests[i],item,math.abs(math.ceil(count-total)),damage)
  153.       end
  154.       if total >= count then
  155.         tell("Move operation successful")
  156.         tell("Moved "..tostring(total).." items.")
  157.         return
  158.       end
  159.     end
  160.     tell("Failed to move all items")
  161.     tell("("..total.."/"..count.." items moved)")
  162.   end
  163.  
  164.   if input[2] == "set" then
  165.     local name = input[3]
  166.     set(name,table.unpack(input,4))
  167.   elseif input[2] == "remove" then
  168.     local name = input[3]
  169.     if name ~= nil then
  170.       remove(name)
  171.     else
  172.       tell("Expected name for argument 3")
  173.     end
  174.   else
  175.     local item = input[5]
  176.     local count = tonumber(input[6])
  177.     if type(count) ~= "number" then
  178.       count = string.lower(input[6])
  179.       if count == "all" or count == "every" then
  180.         count = 1000000
  181.       else
  182.         tell("Expected item-count for parameter 6.")
  183.         return
  184.       end
  185.     end
  186.     local damage = false
  187.     if input[4] then
  188.       damage = tonumber(input[7])
  189.       if damage and type(damage) ~= "number" and string.lower(damage) ~= "nil" then tell("Expected damage value or nil as input 7") return end
  190.     end
  191.     if input[2] == "push" then
  192.       move(input[3],input[4],item,count,damage)
  193.     elseif input[2] == "pull" then
  194.       move(input[4],input[3],item,count,damage)
  195.     elseif input[2] == "moveall" then
  196.  
  197.     end
  198.   end
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement