Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("InventoryCache")
- Crafter = {}
- function Crafter:new(o)
- o = o or {}
- setmetatable(o, self)
- self.__index = self
- self.items = {}
- self.quants = {}
- self.slots = {}
- self.inventoryCache = InventoryCache:new()
- return o
- end
- function Crafter:AddIngredient(item, slots)
- table.insert(self.items, item)
- table.insert(self.quants, table.getn(slots))
- table.insert(self.slots, slots)
- end
- function Crafter:Craft()
- print("Crafting...")
- --Update cache
- self.inventoryCache:Update()
- if(table.getn(self.items) == 0) then
- print("No Items added")
- end
- local noncraftSlots = {4,8,12,13,14,15,16}
- for i=1,table.getn(self.items) do
- --Get first item
- item1 = self.inventoryCache:FindItem(self.items[i])
- if(item1 == -1) then
- print(self.items[i] .. " not found")
- return
- end
- print(self.items[i] .. " found in slot " .. item1)
- --Check for a good spot out of the grid if applicable
- goodSpot = 0
- for j, v in pairs(noncraftSlots) do
- --check if its already in a good place
- if(v == item1) then
- break
- end
- invCache = self.inventoryCache:IsSlotOpen(v)
- if(invCache) then
- goodSpot = v
- break
- end
- end
- if(goodSpot ~= 0) then
- self.inventoryCache:MoveItem(item1, goodSpot)
- end
- end
- for i=1,table.getn(self.items) do
- item1 = self.inventoryCache:FindItem(self.items[i])
- if(item1 ~= -1) then
- cnt = turtle.getItemCount(item1)
- if (cnt < self.quants[i]) then
- print("Not enough to craft")
- return
- end
- cntPer = cnt / self.quants[i]
- for j, v in pairs(self.slots[i]) do
- self.inventoryCache:MoveItem(item1, v, cntPer)
- end
- end
- end
- print("Crafting")
- turtle.select(1)
- turtle.craft()
- end
Add Comment
Please, Sign In to add comment