alesandreo

lib/ale/turtle/inventory.lua

Aug 11th, 2021 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.06 KB | None | 0 0
  1.  
  2. -- https://pastebin.com/Xt9Z2Dk8
  3.  
  4. TurtleInventorySlot = {}
  5. TurtleInventorySlot.__index = TurtleInventorySlot
  6.  
  7. function TurtleInventorySlot:new(slot_number)
  8.   local o = {}
  9.   setmetatable(o, self)
  10.   o.slot_number = slot_number
  11.   return o
  12. end
  13.  
  14. function TurtleInventorySlot:select()
  15.   turtle.select(self.slot_number)
  16. end
  17.  
  18. function TurtleInventorySlot:space()
  19.   return turtle.getItemSpace(self.slot_number)
  20. end
  21.  
  22. function TurtleInventorySlot:getName()
  23.   if turtle.getItemDetail(self.slot_number) then
  24.     return turtle.getItemDetail(self.slot_number).name
  25.   else
  26.     return nil
  27.   end
  28. end
  29.  
  30. function TurtleInventorySlot:count()
  31.   return turtle.getItemCount(self.slot_number)
  32. end
  33.  
  34. function TurtleInventorySlot:contains(block_name, quantity)
  35.   local block_data
  36.   quantity = quantity or 1
  37.   block_data = turtle.getItemDetail(self.slot_number)
  38.   if block_data and block_data.name == block_name and block_data.count >= quantity then
  39.     return true
  40.   end
  41. end
  42.  
  43. function TurtleInventorySlot:copyFrom(source_slot)
  44.   source_slot:select()
  45.   return turtle.transferTo(self.slot_number, self:space())
  46. end
  47.  
  48. function TurtleInventorySlot:copyTo(destination_slot)
  49.   self:select()
  50.   return turtle.transferTo(destination_slot.slot_number, destination_slot:space())
  51. end
  52.  
  53. function TurtleInventorySlot:not_empty()
  54.   return not ( turtle.getItemDetail(self.slot_number) == nil )
  55. end
  56.  
  57. function TurtleInventorySlot:is_full()
  58.   if self:not_empty() then
  59.     return self:space() == 0
  60.   else
  61.     return nil
  62.   end
  63. end
  64.  
  65. TurtleInventory = {
  66.   slots = {}
  67. }
  68.  
  69. TurtleInventory.__index = TurtleInventory
  70.  
  71. function TurtleInventory:new()
  72.   local o = {}
  73.   setmetatable(o, self)
  74.   for i = 1, 16, 1 do
  75.     table.insert(o.slots, TurtleInventorySlot:new(i))
  76.   end
  77.   return o
  78. end
  79.  
  80. function TurtleInventory:getSlot(block_name, quantity)
  81.   quantity = quantity or 1
  82.   for k, slot in ipairs(self.slots) do
  83.     if slot:contains(block_name, quantity) then
  84.       return slot
  85.     end
  86.   end
  87.   return nil
  88. end
  89.  
  90. function TurtleInventory:findInBlockList(blocklist, quantity)
  91.   quantity = quantity or 1
  92.   for block_name, bool_val in pairs(blocklist.blocks) do
  93.     local slot = self:getSlot(block_name, quantity)
  94.     if slot then
  95.       return slot
  96.     end
  97.   end
  98.   return nil
  99. end
  100.  
  101. function TurtleInventory:consolidate()
  102.   local inventory
  103.   inventory = {}
  104.   for k, slot in ipairs(self.slots) do
  105.     if slot:not_empty() then
  106.       local name = slot:getName()
  107.       if name then
  108.         if not inventory[name] then
  109.           inventory[name] = {}
  110.         end
  111.         table.insert(inventory[name], slot)
  112.       end
  113.     end
  114.   end
  115.   for name, slots in pairs(inventory) do
  116.     for k, slot in ipairs(slots) do
  117.       for key, slot2 in ipairs(slots) do
  118.         if (key > k) then
  119.           if slot:space() and slot:space() > 0 then
  120.             slot2:copyTo(slot, slot:space())
  121.           end
  122.         end
  123.       end
  124.     end
  125.   end
  126. end
  127.  
  128. function TurtleInventory:is_full()
  129.   for k, slot in ipairs(self.slots) do
  130.     if slot:count() == 0 then
  131.       return false
  132.     end
  133.   end
  134.   return true
  135. end
Add Comment
Please, Sign In to add comment