Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --cc:Tweaked: Turtle Inventory Management
- --pastebin get 8JaJH3g2 inventory.lua
- --inv=require("inventory")
- --chest=inv.wrap("left")
- --chest.count()
- --require("inventory").wrap("left").find("minecraft:redstone")
- local inventory = {}
- function stacksEqual(a,b)
- if a==nil or b==nil then return false end
- if type(a) == "string" then a = {name=a} end
- if type(b) == "string" then b = {name=b} end
- if a.damage == nil then a["damage"] = 0 end
- if b.damage == nil then b["damage"] = 0 end
- return a.damage == b.damage and a.name == b.name
- end
- local function extend(_count, _size, _detail, _list)
- base = {}
- function base.list()
- return _list()
- end
- function base.size()
- return _size()
- end
- function base.item(slot)
- return _list()[slot]
- end
- function base.itemDetail(slot)
- return _detail(slot)
- end
- function base.counts()
- counts = {}
- for slot,item in pairs(_list()) do
- local count = counts[item.name]
- if count == nil then
- counts[item.name] = item.count
- else
- counts[item.name] = item.count + count
- end
- end
- return counts
- end
- --item == nil: # of slots which contain items
- --item == number: count of items in slot
- --item == string: total count of items
- --item == {item}: total count of items
- function base.find(item)
- for slot,cursor in pairs(_list()) do
- if stacksEqual(item, cursor) then return true, slot, cursor.count end
- end
- return false
- end
- function base.isFull()
- return base.count() == _size()
- end
- function base.count(item)
- local count = 0
- if item == nil then
- return #_list()
- elseif
- type(item) == "number" then return _count(item)
- else
- for _,cursor in pairs(_list()) do
- if stacksEqual(cursor, item) then
- count = count + cursor.count
- end
- end
- return count
- end
- end
- return base
- end
- function inventory.wrap(side)
- local __wrapper = peripheral.wrap(side)
- if __wrapper == nil then return nil end
- local chest = extend(
- function(slot) local items = __wrapper.list() if items[slot] == nil then return 0 else return items[slot].count end end,
- function() return __wrapper.size() end,
- function(slot) return __wrapper.getItemDetail(slot) end,
- function() return __wrapper.list() end
- )
- chest.name = side
- --Moves an item from this inventory to another.
- --[item | slot] = item can be item name or slot
- function chest.push(to, item, count, toSlot)
- if type(to) ~= "string" then to = to.name end
- local total = 0
- if type(item) == "number" then
- --push to slot
- moved = __wrapper.pushItems(to,item,count,toSlot)
- if(count == nil) then
- return true
- else
- total = total + moved
- return count == total,total
- end
- end
- local counted = base.count(item)
- if count == nil then count = counted end
- repeat
- local found, slot, stack = base.find(item)
- if(not found) then return false, total end
- if stack > count - total then stack = count - total end
- total = total + __wrapper.pushItems(to,slot,stack,toSlot)
- until count == total
- return true, total
- end
- function chest.pull(from,item,count,toSlot)
- if type(from) == "string" then from = inventory.wrap(from) end
- return from.push(chest,item,count,toSlot)
- end
- --Turtle can't interact directly with chests in this way in CC:Tweaked. Modem required.
- --https://github.com/cc-tweaked/CC-Tweaked/discussions/601
- function chest.put(item, count, toSlot)
- error("Not Supported")
- end
- function chest.get(item, count)
- error("Not implemented")
- end
- return chest
- end
- if turtle == nil then return inventory end
- local function tlist()
- list={}
- for s=1,16 do
- local count = turtle.getItemCount(s)
- if count > 0 then
- list[s] = turtle.getItemDetail(s, false)
- end
- end
- return list
- end
- local t_inventory = extend(
- turtle.getItemCount,
- function () return 16 end,
- turtle.getItemDetail,
- tlist
- )
- function t_inventory.compact()
- local source = t_inventory.size()
- local dest = 1
- local count = t_inventory.count()
- for s=t_inventory.size(),2,-1 do
- local source = turtle.getItemDetail(s)
- if turtle.getItemCount(s) > 0 then
- for d=1,s-1 do
- if turtle.getItemSpace(d) > 0 then
- local dest = turtle.getItemDetail(d)
- if dest == nil or stacksEqual(source,dest) then
- turtle.select(s)
- if turtle.transferTo(d) then
- if turtle.getItemCount() == 0 then break end
- end
- end
- end
- end
- end
- end
- return t_inventory.count() ~= count
- end
- function t_inventory.selected()
- return turtle.getItemDetail()
- end
- function t_inventory.depositAll(side)
- local dropCount = 0
- local chest = inventory.getInventory(side)
- if chest == nil then return 0 end
- for n=1,INV_COUNT do
- if inventory.getItemCount(n) > 0 then
- turtle.select(n)
- if not turtle.drop() then
- return false
- end
- end
- end
- return true
- end
- turtle.inventory = t_inventory
Add Comment
Please, Sign In to add comment