summaryrefslogtreecommitdiff
path: root/UsefullLuaSnippets/shared_inventory.lua
blob: 124c585912394261bbef4ae4cc6be2be2a1780d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--> Inherited from Old Age 2 scripts made by The Maw. <--

local meta  = FindMetaTable("Player")
local Slots = 16

if (SERVER) then
	util.AddNetworkString("SetSlotItem")
	util.AddNetworkString("SetupInventory")

	function meta:AddItem(item)
		local ID = nil
		if (!self.Inventory) then self.Inventory = {} ID = 1
		else for i = 1,Slots do if (!self.Inventory[i]) then ID = i break end end end
		
		if (ID) then
			self.Inventory[ID] = item
			
			net.Start("SetSlotItem")
				net.WriteEntity(self)
				net.WriteByte(ID)
				net.WriteString(item)
			net.Send(self)
		else return false end --We are returning false if there isn't any available slots left.
	end
	
	function meta:SetupInventory(dat) --We are using a table of items, using the indexes as the slotID
		self.Inventory = dat
		
		net.Start("SetupInventory")
			net.WriteEntity(self)
			net.WriteTable(dat)
		net.Send(self)
	end
else
	net.Receive("SetSlotItem",function(size)
		local Ply = net.ReadEntity()
		
		if (!Ply.Inventory) then Ply.Inventory = {} end
		Ply.Inventory[net.ReadByte()] = net.ReadString()
	end)
	
	net.Receive("SetupInventory",function(size) net.ReadEntity().Inventory = net.ReadTable() end)
end

function meta:GetSlot(id)
	if (!self.Inventory) then return nil end
	return self.Inventory[id] or nil
end

function meta:GetInventory(id)
	return self.Inventory or {}
end

function meta:HasRoom()
	if (!self.Inventory) then return true end
	for i = 1,Slots do if (!self.Inventory[i]) then return true end end
	return false
end