
local meta = FindMetaTable( "Player" )

// Return if there's nothing to add on to
if (!meta) then return end

g_SBoxObjects = {}


function meta:CheckLimit( str )

	// No limits in single player
	if (SinglePlayer()) then return true end

	local c = server_settings.Int( "sbox_max"..str, 0 )
	
	if ( c < 0 ) then return true end
	if ( self:GetCount( str ) > c-1 ) then self:LimitHit( str ) return false end

	return true

end

function meta:GetCount( str )

	local key = self:UniqueID()
	local tab = g_SBoxObjects[ key ]
	
	if ( !tab || !tab[ str ] ) then return 0 end
	
	local c = 0
	
	for k, v in pairs ( tab[ str ] ) do
	
		if (v:IsValid()) then 
			c = c + 1
		else
			tab[ str ][ k ] = nil
		end
	
	end

	return c

end

function meta:AddCount( str, ent )

	local key = self:UniqueID()
	g_SBoxObjects[ key ] = g_SBoxObjects[ key ] or {}
	g_SBoxObjects[ key ][ str ] = g_SBoxObjects[ key ][ str ] or {}
	
	local tab = g_SBoxObjects[ key ][ str ]
	
	table.insert( tab, ent )

end

function meta:LimitHit( str )

	self:SendLua( "GAMEMODE:LimitHit( '".. str .."' )" )

end

function meta:AddCleanup( type, ent )

	cleanup.Add( self, type, ent )
	
end

if (SERVER) then

	function meta:SendHint( str, delay )

		self:GetTable().Hints = self:GetTable().Hints or {}
		if (self:GetTable().Hints[ str ]) then return end
		
		self:SendLua( "GAMEMODE:AddHint( '"..str.."', "..delay.." )" )
		self:GetTable().Hints[ str ] = true

	end
	
	function meta:SuppressHint( str )

		self:GetTable().Hints = self:GetTable().Hints or {}
		if (self:GetTable().Hints[ str ]) then return end
		
		self:SendLua( "GAMEMODE:SuppressHint( '"..str.."' )" )
		self:GetTable().Hints[ str ] = true

	end

end

	// This returns a player trace - but the trace is cached

	function meta:GetEyeTrace()
	
		if ( self:GetTable().LastPlayertTrace == CurTime() ) then
			return self:GetTable().PlayerTrace
		end

		self:GetTable().PlayerTrace = util.TraceLine( util.GetPlayerTrace( self, self:GetCursorAimVector() ) )
		self:GetTable().LastPlayertTrace = CurTime()
		
		return self:GetTable().PlayerTrace

	end
	