Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- the remotes
- local InventoryRemotes = game.ReplicatedStorage.Shared.Remotes.Inventory
- local Function = InventoryRemotes.Function
- local Event = InventoryRemotes.Event
- -- modules
- local inventorySettings = require(game.ServerStorage.ServerSettings.InventorySettings)
- -- inventory object
- local inventory = {}
- inventory.__index = inventory
- function inventory:debug_logSlots()
- for i = 1,self.size do
- local slot = self.slots[i]
- local amt = not slot and "NIL" or slot.amt
- local id = not slot and "NIL" or slot.id
- print("@Slot#" .. i .. "; id : " .. id .. "; amt : " .. amt)
- end
- end
- function inventory:PullItem(id,amt)
- for i = 1, self.size do
- local slot = self.slots[i]
- if slot and slot.id == id then
- local take = amt >= slot.amt and slot.amt or amt
- amt = amt - take
- slot.amt = slot.amt - take
- if slot.amt <= 0 then
- self.slots[i] = nil
- self:client_emptySlot(i)
- elseif take > 0 then
- self:client_updateSlot(i,slot.id,slot.amt)
- end
- if amt <= 0 then
- break
- end
- end
- end
- end
- function changeSize(size)
- -- if size is less we need to do some shit
- end
- function inventory:PushItem(id,amt) -- returns left over
- local slotCount = 0
- for i = 1,self.size do
- if amt <= 0 then
- return 0
- end
- local slot = self.slots[i]
- slotCount = not (slot == nil) and slotCount + 1 or slotCount
- if slot and slot.id == id then
- local left = inventorySettings.MAXSTACKSIZE - slot.amt
- local fill = amt >= left and left or amt
- slot.amt = slot.amt + fill
- amt = amt - fill
- if fill > 0 then
- self:client_updateSlot(i,slot.id,slot.amt)
- end
- end
- end
- for i = 1, self.size do
- if slotCount >= self.size then
- break
- end
- if amt <= 0 then
- return 0
- end
- if not self.slots[i] then
- slotCount = slotCount + 1
- local fill = amt >= inventorySettings.MAXSTACKSIZE and inventorySettings.MAXSTACKSIZE or amt
- amt = amt - fill
- local newSlot = {id = id,amt = fill}
- self.slots[i] = newSlot
- -- update client
- self:client_newSlot(i,id,newSlot.amt)
- end
- end
- return amt
- end
- --
- function inventory:client_pushAllSlots()
- --print("sending")
- Event:FireClient(self.plyr,"ALLSLOTS",self.slots)
- end
- function inventory:client_newSlot(i,id,amt)
- --print("sending")
- Event:FireClient(self.plyr,"NEWSLOT",i,id,amt)
- end
- function inventory:client_emptySlot(i)
- Event:FireClient(self.plyr,"EMPTYSLOT",i)
- end
- function inventory:client_updateSlot(i,id,amt)
- --print("sending")
- Event:FireClient(self.plyr,"UPDATESLOT",i,id,amt)
- end
- function inventory:client_updateSize(size)
- end
- function inventory:client_getContents()
- return self.slots
- end
- function inventory.new(plyr)
- local i = {}
- setmetatable(i,inventory)
- i.plyr = plyr
- i.slots = {}
- i.size = 10 -- number of slots
- return i
- end
- -- way to interact with all player inventories for all server
- inventoryHandler = {}
- -- inventory handler data
- inventoryHandler.PlayerInventories = {}
- -- inventory handler functions
- function inventoryHandler:GetInventory(plyr)
- return self.PlayerInventories[plyr] or warn("Could not find inventory for player upon server request")
- end
- function inventoryHandler:GiveClientContents(plyr)
- local inventory = self:GetInventory(plyr)
- if inventory then
- return inventory.slots
- end
- end
- function inventoryHandler:GiveClientInfo(plyr)
- local inventory = self:GetInventory(plyr)
- if inventory then
- return inventory.slots,inventory.size
- end
- end
- function inventoryHandler:NewPlayer(plyr)
- local i = inventory.new(plyr)
- self.PlayerInventories[plyr] = i
- return i
- end
- function inventoryHandler:RemovePlayer(plyr)
- self.PlayerInventories[plyr] = nil
- end
- function inventoryHandler.FunctionInvoke(plyr,req)
- print("invoked")
- if req == "REQCONTENT" then
- return inventoryHandler:GiveClientContents(plyr)
- elseif req == "REQINFO" then
- return inventoryHandler:GiveClientInfo(plyr)
- end
- end
- function inventoryHandler:RemoteInit()
- Function.OnServerInvoke = self.FunctionInvoke
- end
- return inventoryHandler
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement