aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/player_useitem.lua
blob: 128146a52765471a32cb0c803ad3c6c11adbab69 (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
local meta = FindMetaTable("Player")

if (SERVER) then
	util.AddNetworkString("UseItem")
	util.AddNetworkString("UseItemReq")
	
	function meta:UseItem(item,bAccount)
		if ((!bAccount and !self:HasItem(item)) or (bAccount and !self:HasAccountItem(item))) then return end
		
		local IT = GetItemByName(item)
		
		if (IT.OnUse) then
			IT:OnUse(self)
			
			net.Start("UseItem")
				net.WriteEntity(self)
				net.WriteString(item)
			net.Broadcast()
		end
	end
	
	net.Receive("UseItemReq",function(seq,pl) pl:UseItem(net.ReadString(),util.tobool(net.ReadBit())) end)
else
	net.Receive("UseItem",function()
		local pl = net.ReadEntity()
		local it = GetItemByName(net.ReadString())
		
		if (it.OnUse) then it:OnUse(pl) end
	end)
	
	function RequestUseItem(item,bAccount)
		net.Start("UseItemReq")
			net.WriteString(item)
			net.WriteBit(bAccount)
		net.SendToServer()
	end
end