Josqus

chest_system

Nov 2nd, 2023 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. -- chest_system.lua
  2. ChestDatabase = {}
  3. ChestDatabase.__index = ChestDatabase
  4.  
  5. Chest = {}
  6. Chest.__index = Chest
  7.  
  8. function Chest.new(chestNumber, x, y, z)
  9. z = z or 0
  10. local self = setmetatable({}, Chest)
  11. self.chestNumber = chestNumber
  12. self.slots = {}
  13. self.position = {x = x, y = y, z = z}
  14. return self
  15. end
  16.  
  17. function Chest:addItem(slot, itemName, quantity)
  18. if self.slots[slot] and self.slots[slot].itemName == itemName then
  19. self.slots[slot].quantity = self.slots[slot].quantity + quantity
  20. else
  21. self.slots[slot] = {itemName = itemName, quantity = quantity}
  22. end
  23. end
  24.  
  25. function Chest:removeItems(slot, quantity)
  26. if self.slots[slot] then
  27. self.slots[slot].quantity = self.slots[slot].quantity - quantity
  28. if self.slots[slot].quantity <= 0 then
  29. self.slots[slot] = nil
  30. end
  31. end
  32. end
  33.  
  34. function Chest:findSlotForItem(itemName)
  35. for slot, record in pairs(self.slots) do
  36. if record.itemName == itemName then
  37. return slot, record.quantity
  38. end
  39. end
  40. return nil, 0
  41. end
  42.  
  43. function Chest:findEmptySlot()
  44. for slot = 1, 27 do -- Chests have 27 slots
  45. if not self.slots[slot] then
  46. return slot
  47. end
  48. end
  49. return nil
  50. end
  51.  
  52. function Chest:printInfo()
  53. print("Chest: " .. self.chestNumber .. " at position (" .. self.position.x .. ", " .. self.position.y .. ", " .. self.position.z .. ")")
  54. for slot, record in pairs(self.slots) do
  55. print(" Slot: " .. slot .. ", Item: " .. record.itemName .. ", Quantity: " .. record.quantity)
  56. end
  57. end
  58.  
  59. -- DataBase
  60.  
  61. function ChestDatabase.new()
  62. local self = setmetatable({}, ChestDatabase)
  63. self.chests = {}
  64. return self
  65. end
  66.  
  67. function ChestDatabase:addChest(chestNumber, x, y, z)
  68. self.chests[chestNumber] = Chest.new(chestNumber, x, y, z)
  69. end
  70.  
  71. function ChestDatabase:addItemToChest(chestNumber, slot, itemName, quantity)
  72. if self.chests[chestNumber] then
  73. self.chests[chestNumber]:addItem(slot, itemName, quantity)
  74. else
  75. print("Chest number " .. chestNumber .. " does not exist.")
  76. end
  77. end
  78.  
  79. function ChestDatabase:removeItemsFromChest(chestNumber, slot, quantity)
  80. if self.chests[chestNumber] then
  81. self.chests[chestNumber]:removeItems(slot, quantity)
  82. else
  83. print("Chest number " .. chestNumber .. " does not exist.")
  84. end
  85. end
  86.  
  87. function ChestDatabase:findChestAndSlotForItem(itemName)
  88. for chestNumber, chest in pairs(self.chests) do
  89. local slot, quantity = chest:findSlotForItem(itemName)
  90. if slot then
  91. return chestNumber, slot, quantity
  92. end
  93. end
  94. return nil, nil, 0
  95. end
  96.  
  97. function ChestDatabase:findChestWithEmptySlot()
  98. for chestNumber, chest in pairs(self.chests) do
  99. local emptySlot = chest:findEmptySlot()
  100. if emptySlot then
  101. return chestNumber, emptySlot
  102. end
  103. end
  104. return nil, nil
  105. end
  106.  
  107. function ChestDatabase:addItemAutomatically(itemName, quantity)
  108. local chestNumber, slot = self:findChestWithEmptySlot()
  109. if chestNumber and slot then
  110. self:addItemToChest(chestNumber, slot, itemName, quantity)
  111. else
  112. print("No empty slot available to add " .. itemName)
  113. end
  114. end
  115.  
  116. function ChestDatabase:printDatabase()
  117. for chestNumber, chest in pairs(self.chests) do
  118. chest:printInfo()
  119. end
  120. end
  121.  
  122.  
  123. return {
  124. ChestDatabase = ChestDatabase,
  125. Chest = Chest
  126. }
  127.  
Add Comment
Please, Sign In to add comment