Reisyukaku

Untitled

Sep 29th, 2022 (edited)
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. require("InventoryCache")
  2.  
  3. Crafter = {}
  4.  
  5. function Crafter:new(o)
  6. o = o or {}
  7. setmetatable(o, self)
  8. self.__index = self
  9. self.items = {}
  10. self.quants = {}
  11. self.slots = {}
  12. self.inventoryCache = InventoryCache:new()
  13. return o
  14. end
  15.  
  16. function Crafter:AddIngredient(item, slots)
  17. table.insert(self.items, item)
  18. table.insert(self.quants, table.getn(slots))
  19. table.insert(self.slots, slots)
  20. end
  21.  
  22. function Crafter:Craft()
  23. print("Crafting...")
  24. --Update cache
  25. self.inventoryCache:Update()
  26.  
  27. if(table.getn(self.items) == 0) then
  28. print("No Items added")
  29. end
  30.  
  31. local noncraftSlots = {4,8,12,13,14,15,16}
  32. for i=1,table.getn(self.items) do
  33. --Get first item
  34. item1 = self.inventoryCache:FindItem(self.items[i])
  35. if(item1 == -1) then
  36. print(self.items[i] .. " not found")
  37. return
  38. end
  39. print(self.items[i] .. " found in slot " .. item1)
  40. --Check for a good spot out of the grid if applicable
  41. goodSpot = 0
  42. for j, v in pairs(noncraftSlots) do
  43. --check if its already in a good place
  44. if(v == item1) then
  45. break
  46. end
  47. invCache = self.inventoryCache:IsSlotOpen(v)
  48. if(invCache) then
  49. goodSpot = v
  50. break
  51. end
  52. end
  53.  
  54. if(goodSpot ~= 0) then
  55. self.inventoryCache:MoveItem(item1, goodSpot)
  56. end
  57. end
  58.  
  59. for i=1,table.getn(self.items) do
  60. item1 = self.inventoryCache:FindItem(self.items[i])
  61. if(item1 ~= -1) then
  62. cnt = turtle.getItemCount(item1)
  63. if (cnt < self.quants[i]) then
  64. print("Not enough to craft")
  65. return
  66. end
  67. cntPer = cnt / self.quants[i]
  68. for j, v in pairs(self.slots[i]) do
  69. self.inventoryCache:MoveItem(item1, v, cntPer)
  70. end
  71. end
  72. end
  73.  
  74. print("Crafting")
  75. turtle.select(1)
  76. turtle.craft()
  77. end
Add Comment
Please, Sign In to add comment