Robear9992

inventory

Jul 2nd, 2022 (edited)
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.27 KB | None | 0 0
  1. --cc:Tweaked: Turtle Inventory Management
  2. --pastebin get 8JaJH3g2 inventory.lua
  3. --inv=require("inventory")
  4. --chest=inv.wrap("left")
  5. --chest.count()
  6. --require("inventory").wrap("left").find("minecraft:redstone")
  7.  
  8. local inventory = {}
  9.  
  10. function stacksEqual(a,b)
  11.     if a==nil or b==nil then return false end
  12.  
  13.     if type(a) == "string" then a = {name=a} end
  14.     if type(b) == "string" then b = {name=b} end
  15.    
  16.     if a.damage == nil then a["damage"] = 0 end
  17.     if b.damage == nil then b["damage"] = 0 end
  18.    
  19.     return a.damage == b.damage and a.name == b.name
  20. end
  21.  
  22. local function extend(_count, _size, _detail, _list)
  23.     local base = {}
  24.  
  25.     function base.list()
  26.         return _list()
  27.     end
  28.    
  29.     function base.size()
  30.         return _size()
  31.     end
  32.    
  33.     function base.item(slot)
  34.         return _list()[slot]
  35.     end
  36.    
  37.     function base.itemDetail(slot)
  38.         return _detail(slot)
  39.     end
  40.    
  41.     function base.counts()
  42.         counts = {}
  43.         for slot,item in pairs(_list()) do
  44.             if item ~= nil then
  45.                 local count = counts[item.name]
  46.                 if count == nil then
  47.                     counts[item.name] = item.count
  48.                 else
  49.                     counts[item.name] = item.count + count
  50.                 end
  51.             end
  52.         end
  53.         return counts
  54.     end
  55.    
  56.     --item == nil: # of slots which contain items
  57.     --item == number: count of items in slot
  58.     --item == string: total count of items
  59.     --item == {item}: total count of items
  60.     function base.find(item)
  61.         for slot,cursor in pairs(_list()) do
  62.             if stacksEqual(item, cursor) then return true, slot, cursor.count end
  63.         end
  64.         return false
  65.     end
  66.  
  67.     function base.isFull()
  68.         return base.count() == _size()
  69.     end
  70.    
  71.     function base.count(item)
  72.         local count = 0
  73.  
  74.         if item == nil then
  75.             for slot,cursor in pairs(_list()) do
  76.                 count = count + 1
  77.             end
  78.             return count
  79.         elseif
  80.             type(item) == "number" then return _count(item)
  81.         else
  82.             for _,cursor in pairs(_list()) do
  83.                 if stacksEqual(cursor, item) then
  84.                     count = count + cursor.count
  85.                 end
  86.             end
  87.             return count
  88.         end
  89.     end
  90.    
  91.     return base
  92. end
  93.  
  94. function inventory.wrap(side)
  95.  
  96.     local __wrapper = peripheral.wrap(side)
  97.     if __wrapper == nil then return nil end
  98.  
  99.     local chest = extend(
  100.         function(slot) local items = __wrapper.list() if items[slot] == nil then return 0 else return items[slot].count end end,
  101.         function() return __wrapper.size() end,
  102.         function(slot) return __wrapper.getItemDetail(slot) end,
  103.         function() return __wrapper.list() end
  104.     )
  105.    
  106.     chest.name = side
  107.    
  108.     --Moves an item from this inventory to another.
  109.     --[item | slot] = item can be item name or slot
  110.     --returns:
  111.     --  Boolean - Entire stack moved and slot is now empty
  112.     --  Number  - Number of items in stack moved
  113.     function chest.push(to, item, count, toSlot)
  114.         if type(to) ~= "string" then to = to.name end
  115.        
  116.         if type(item) == "number" then
  117.             --push to slot
  118.             if(count == nil) then count = chest.count(item) end
  119.             local moved = __wrapper.pushItems(to,item,count,toSlot)
  120.             return count == moved,total
  121.         end
  122.        
  123.         local counted =  chest.count(item)
  124.         if count == nil then count = counted end
  125.        
  126.         local total = 0
  127.         repeat
  128.             local found, slot, stack = chest.find(item)
  129.             if(not found) then return false, total end
  130.             if stack > count - total then stack = count - total end
  131.             total = total + __wrapper.pushItems(to,slot,stack,toSlot)
  132.         until count == total
  133.         return true, total
  134.     end
  135.    
  136.     function chest.pull(from,item,count,toSlot)
  137.         if type(from) == "string" then from = inventory.wrap(from) end
  138.         return from.push(chest,item,count,toSlot)
  139.     end
  140.    
  141.     --Turtle can't interact directly with chests in this way in CC:Tweaked. Modem required.
  142.     --https://github.com/cc-tweaked/CC-Tweaked/discussions/601
  143.     function chest.put(item, count, toSlot)
  144.         error("Not Supported")
  145.     end
  146.  
  147.     function chest.get(item, count)
  148.         error("Not implemented")
  149.     end
  150.    
  151.     return chest
  152. end
  153.  
  154. if turtle == nil then return inventory end
  155.  
  156. local function tlist()
  157.     list={}
  158.     for s=1,16 do
  159.         local count = turtle.getItemCount(s)
  160.         if count > 0 then
  161.             list[s] = turtle.getItemDetail(s, false)
  162.         end
  163.     end
  164.     return list
  165. end
  166.  
  167. turtle.items = extend(
  168.     turtle.getItemCount,
  169.     function () return 16 end,
  170.     turtle.getItemDetail,
  171.     tlist
  172. )
  173.  
  174. function turtle.items.compact()
  175.     local source = turtle.items.size()
  176.     local dest = 1
  177.     local count = turtle.items.count()
  178.    
  179.     for s=turtle.items.size(),2,-1 do
  180.         local source = turtle.getItemDetail(s)
  181.         if turtle.getItemCount(s) > 0 then
  182.             for d=1,s-1 do
  183.                 if turtle.getItemSpace(d) > 0 then
  184.                     local dest = turtle.getItemDetail(d)
  185.                     if dest == nil or stacksEqual(source,dest) then
  186.                         turtle.select(s)
  187.                         if turtle.transferTo(d) then
  188.                             if turtle.getItemCount() == 0 then break end
  189.                         end
  190.                     end
  191.                 end
  192.             end
  193.         end
  194.     end
  195.    
  196.     return turtle.items.count() ~= count
  197. end
  198.  
  199. function turtle.items.selected()
  200.     return turtle.getItemDetail()
  201. end
  202.  
  203. function turtle.items.select(item)
  204.     local found,slot,count = turtle.items.find(item)
  205.     if found then turtle.select(slot) end
  206.     return found,slot,count
  207. end
  208.  
  209. function turtle.items.depositAll(side)
  210.     local dropCount = 0
  211.    
  212.     local chest = inventory.wrap(side)
  213.     if chest == nil then return 0 end  
  214.    
  215.     for n=1,turtle.items.size() do
  216.         if turtle.items.count(n) > 0 then
  217.             turtle.select(n)
  218.             if not turtle.drop() then
  219.                 return false
  220.             end
  221.         end
  222.     end
  223.    
  224.     return true
  225. end
  226.  
  227. return inventory
  228.  
Add Comment
Please, Sign In to add comment