Advertisement
poopman123

server side inv system

Nov 14th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1. -- the remotes
  2. local InventoryRemotes = game.ReplicatedStorage.Shared.Remotes.Inventory
  3. local Function = InventoryRemotes.Function
  4. local Event = InventoryRemotes.Event
  5. -- modules
  6. local inventorySettings = require(game.ServerStorage.ServerSettings.InventorySettings)
  7. -- inventory object
  8. local inventory = {}
  9. inventory.__index = inventory
  10.  
  11. function inventory:debug_logSlots()
  12.     for i = 1,self.size do
  13.         local slot = self.slots[i]
  14.         local amt = not slot and "NIL" or slot.amt
  15.         local id = not slot and "NIL" or slot.id
  16.         print("@Slot#" .. i .. "; id : " .. id .. "; amt : " .. amt)
  17.     end
  18. end
  19.  
  20.  
  21.  
  22. function inventory:PullItem(id,amt)
  23.     for i = 1, self.size do
  24.         local slot = self.slots[i]
  25.         if slot and slot.id == id then
  26.             local take = amt >= slot.amt and slot.amt or amt
  27.             amt = amt - take
  28.             slot.amt = slot.amt - take
  29.             if slot.amt <= 0 then
  30.                 self.slots[i] = nil
  31.                 self:client_emptySlot(i)
  32.             elseif take > 0 then
  33.                 self:client_updateSlot(i,slot.id,slot.amt)
  34.             end
  35.             if amt <= 0 then
  36.                 break
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. function changeSize(size)
  43. -- if size is less we need to do some shit
  44. end
  45.  
  46. function inventory:PushItem(id,amt) -- returns left over
  47.     local slotCount = 0
  48.     for i = 1,self.size do
  49.         if amt <= 0 then
  50.             return 0
  51.         end
  52.         local slot = self.slots[i]
  53.         slotCount = not (slot == nil) and slotCount + 1 or slotCount
  54.         if slot and slot.id == id then
  55.             local left = inventorySettings.MAXSTACKSIZE - slot.amt
  56.             local fill = amt >= left and left or amt
  57.             slot.amt = slot.amt + fill
  58.             amt = amt - fill
  59.             if fill > 0 then
  60.                 self:client_updateSlot(i,slot.id,slot.amt)
  61.             end
  62.         end
  63.        
  64.     end
  65.     for i = 1, self.size do
  66.         if slotCount >= self.size then
  67.             break
  68.         end
  69.         if amt <= 0 then
  70.             return 0
  71.         end
  72.         if not self.slots[i] then
  73.             slotCount = slotCount + 1
  74.             local fill = amt >= inventorySettings.MAXSTACKSIZE and inventorySettings.MAXSTACKSIZE or amt
  75.             amt = amt - fill
  76.             local newSlot = {id = id,amt = fill}
  77.             self.slots[i] = newSlot
  78.             -- update client
  79.             self:client_newSlot(i,id,newSlot.amt)
  80.         end
  81.     end
  82.     return amt
  83. end
  84.  
  85. --
  86. function inventory:client_pushAllSlots()
  87.     --print("sending")
  88.     Event:FireClient(self.plyr,"ALLSLOTS",self.slots)
  89. end
  90.  
  91. function inventory:client_newSlot(i,id,amt)
  92.     --print("sending")
  93.     Event:FireClient(self.plyr,"NEWSLOT",i,id,amt)
  94. end
  95.  
  96. function inventory:client_emptySlot(i)
  97.     Event:FireClient(self.plyr,"EMPTYSLOT",i)
  98. end
  99.  
  100. function inventory:client_updateSlot(i,id,amt)
  101.     --print("sending")
  102.     Event:FireClient(self.plyr,"UPDATESLOT",i,id,amt)
  103. end
  104.  
  105. function inventory:client_updateSize(size)
  106.    
  107. end
  108.  
  109. function inventory:client_getContents()
  110.     return self.slots
  111. end
  112.  
  113. function inventory.new(plyr)
  114.     local i = {}
  115.     setmetatable(i,inventory)
  116.     i.plyr = plyr
  117.     i.slots = {}
  118.     i.size = 10 -- number of slots
  119.     return i
  120. end
  121. -- way to interact with all player inventories for all server
  122. inventoryHandler = {}
  123. -- inventory handler data
  124. inventoryHandler.PlayerInventories = {}
  125. -- inventory handler functions
  126. function inventoryHandler:GetInventory(plyr)
  127.     return self.PlayerInventories[plyr] or warn("Could not find inventory for player upon server request")
  128. end
  129.  
  130. function inventoryHandler:GiveClientContents(plyr)
  131.     local inventory = self:GetInventory(plyr)
  132.     if inventory then
  133.         return inventory.slots
  134.     end
  135. end
  136.  
  137. function inventoryHandler:GiveClientInfo(plyr)
  138.     local inventory = self:GetInventory(plyr)
  139.     if inventory then
  140.         return inventory.slots,inventory.size
  141.     end
  142. end
  143.  
  144. function inventoryHandler:NewPlayer(plyr)
  145.     local i = inventory.new(plyr)
  146.     self.PlayerInventories[plyr] = i
  147.     return i
  148. end
  149.  
  150. function inventoryHandler:RemovePlayer(plyr)
  151.     self.PlayerInventories[plyr] = nil
  152. end
  153.  
  154. function inventoryHandler.FunctionInvoke(plyr,req)
  155.     print("invoked")
  156.     if req == "REQCONTENT" then
  157.         return inventoryHandler:GiveClientContents(plyr)
  158.     elseif req == "REQINFO" then
  159.         return inventoryHandler:GiveClientInfo(plyr)
  160.     end
  161. end
  162.  
  163. function inventoryHandler:RemoteInit()
  164.     Function.OnServerInvoke = self.FunctionInvoke
  165. end
  166.  
  167. return inventoryHandler
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement