Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- chest_system.lua
- ChestDatabase = {}
- ChestDatabase.__index = ChestDatabase
- Chest = {}
- Chest.__index = Chest
- function Chest.new(chestNumber, x, y, z)
- z = z or 0
- local self = setmetatable({}, Chest)
- self.chestNumber = chestNumber
- self.slots = {}
- self.position = {x = x, y = y, z = z}
- return self
- end
- function Chest:addItem(slot, itemName, quantity)
- if self.slots[slot] and self.slots[slot].itemName == itemName then
- self.slots[slot].quantity = self.slots[slot].quantity + quantity
- else
- self.slots[slot] = {itemName = itemName, quantity = quantity}
- end
- end
- function Chest:removeItems(slot, quantity)
- if self.slots[slot] then
- self.slots[slot].quantity = self.slots[slot].quantity - quantity
- if self.slots[slot].quantity <= 0 then
- self.slots[slot] = nil
- end
- end
- end
- function Chest:findSlotForItem(itemName)
- for slot, record in pairs(self.slots) do
- if record.itemName == itemName then
- return slot, record.quantity
- end
- end
- return nil, 0
- end
- function Chest:findEmptySlot()
- for slot = 1, 27 do -- Chests have 27 slots
- if not self.slots[slot] then
- return slot
- end
- end
- return nil
- end
- function Chest:printInfo()
- print("Chest: " .. self.chestNumber .. " at position (" .. self.position.x .. ", " .. self.position.y .. ", " .. self.position.z .. ")")
- for slot, record in pairs(self.slots) do
- print(" Slot: " .. slot .. ", Item: " .. record.itemName .. ", Quantity: " .. record.quantity)
- end
- end
- -- DataBase
- function ChestDatabase.new()
- local self = setmetatable({}, ChestDatabase)
- self.chests = {}
- return self
- end
- function ChestDatabase:addChest(chestNumber, x, y, z)
- self.chests[chestNumber] = Chest.new(chestNumber, x, y, z)
- end
- function ChestDatabase:addItemToChest(chestNumber, slot, itemName, quantity)
- if self.chests[chestNumber] then
- self.chests[chestNumber]:addItem(slot, itemName, quantity)
- else
- print("Chest number " .. chestNumber .. " does not exist.")
- end
- end
- function ChestDatabase:removeItemsFromChest(chestNumber, slot, quantity)
- if self.chests[chestNumber] then
- self.chests[chestNumber]:removeItems(slot, quantity)
- else
- print("Chest number " .. chestNumber .. " does not exist.")
- end
- end
- function ChestDatabase:findChestAndSlotForItem(itemName)
- for chestNumber, chest in pairs(self.chests) do
- local slot, quantity = chest:findSlotForItem(itemName)
- if slot then
- return chestNumber, slot, quantity
- end
- end
- return nil, nil, 0
- end
- function ChestDatabase:findChestWithEmptySlot()
- for chestNumber, chest in pairs(self.chests) do
- local emptySlot = chest:findEmptySlot()
- if emptySlot then
- return chestNumber, emptySlot
- end
- end
- return nil, nil
- end
- function ChestDatabase:addItemAutomatically(itemName, quantity)
- local chestNumber, slot = self:findChestWithEmptySlot()
- if chestNumber and slot then
- self:addItemToChest(chestNumber, slot, itemName, quantity)
- else
- print("No empty slot available to add " .. itemName)
- end
- end
- function ChestDatabase:printDatabase()
- for chestNumber, chest in pairs(self.chests) do
- chest:printInfo()
- end
- end
- return {
- ChestDatabase = ChestDatabase,
- Chest = Chest
- }
Add Comment
Please, Sign In to add comment