PLUGIN.Name = "Limits"; -- What is the plugin name
PLUGIN.Author = "LuaBanana"; -- Author of the plugin
PLUGIN.Description = "Limit prop, ragdoll, vehicle, and effect limits on a player by player basis. Useful for donators."; -- The description or purpose of the plugin

SpawnTable = {};

function MaxProps(ply)

	return tonumber(ConVars[ "PropLimit" ]) + tonumber(GetPlayerField(ply, "extraprops"));
	
end

function MaxRagdolls(ply)

	return tonumber(ConVars[ "RagdollLimit" ]) + tonumber(GetPlayerField(ply, "extraragdolls"));
	
end

function MaxVehicles(ply)

	return tonumber(ConVars[ "VehicleLimit" ]) + tonumber(GetPlayerField(ply, "extravehicles"));
	
end

function MaxEffects(ply)

	return tonumber(ConVars[ "EffectLimit" ]) + tonumber(GetPlayerField(ply, "extraeffects"));
	
end

function CreateSpawnTable(ply)
	
	SpawnTable[FormatSteamID(ply:SteamID())] = {};
	
	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	spawntable.props = {};
	spawntable.ragdolls = {};
	spawntable.vehicles = {};
	spawntable.effects = {};

end

function GM:PlayerSpawnProp(ply, mdl)

	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	
	if(GetPlayerField(ply, "proptrust") != 1) then
	
		SendChat(ply, "You are not allowed to spawn anything!");
		return false;
		
	end
	
	if(spawntable != nil) then
	
		local spawned = 0;
		
		for k, v in pairs(spawntable.props) do
		
			if(v != nil and v:IsValid()) then
			
				spawned = spawned + 1;
			
			else
			
				spawntable.props[k] = nil; -- No longer exists. Wipe it out.
			
			end
			
		end
		
		if(spawned >= MaxProps(ply)) then
		
			SendChat(ply, "You have reached your limit! (" .. MaxProps(ply) .. ")");
			return false;
			
		else
		
			return true;
			
		end
		
	else

		CreateSpawnTable(ply)
		return true;
		
	end
	
end

function GM:PlayerSpawnRagdoll(ply, mdl)

	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	
	if(GetPlayerField(ply, "proptrust") != 1) then
	
		SendChat(ply, "You are not allowed to spawn anything!");
		return false;
		
	end
	
	if(spawntable != nil) then
	
		local spawned = 0;
		
		for k, v in pairs(spawntable.ragdolls) do
		
			if(v != nil and v:IsValid()) then
			
				spawned = spawned + 1;
			
			else
			
				spawntable.ragdolls[k] = nil; -- No longer exists. Wipe it out.
			
			end
			
		end
		
		if(spawned >= MaxRagdolls(ply)) then
		
			SendChat(ply, "You have reached your limit! (" .. MaxRagdolls(ply) .. ")");
			return false;
			
		else
		
			return true;
			
		end
		
	else

		CreateSpawnTable(ply)
		return true;
		
	end
	
end

function GM:PlayerSpawnVehicle(ply)

	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	
	if(GetPlayerField(ply, "proptrust") != 1) then
	
		SendChat(ply, "You are not allowed to spawn anything!");
		return false;
		
	end
	
	if(spawntable != nil) then
	
		local spawned = 0;
		
		for k, v in pairs(spawntable.vehicles) do
		
			if(v != nil and v:IsValid()) then
			
				spawned = spawned + 1;
			
			else
			
				spawntable.vehicles[k] = nil; -- No longer exists. Wipe it out.
			
			end
			
		end
		
		if(spawned >= MaxVehicles(ply)) then
		
			SendChat(ply, "You have reached your limit! (" .. MaxVehicles(ply) .. ")");
			return false;
			
		else
		
			return true;
			
		end
		
	else

		CreateSpawnTable(ply)
		return true;
		
	end
	
end

function GM:PlayerSpawnEffect(ply, mdl)

	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	
	if(GetPlayerField(ply, "proptrust") != 1) then
	
		SendChat(ply, "You are not allowed to spawn anything!");
		return false;
		
	end
	
	if(spawntable != nil) then
	
		local spawned = 0;
		
		for k, v in pairs(spawntable.effects) do
		
			if(v != nil and v:IsValid()) then
			
				spawned = spawned + 1;
			
			else
			
				spawntable.effects[k] = nil; -- No longer exists. Wipe it out.
			
			end
			
		end
		
		if(spawned >= MaxEffects(ply)) then
		
			SendChat(ply, "You have reached your limit! (" .. MaxEffects(ply) .. ")");
			return false;
			
		else
		
			return true;
			
		end
		
	else

		CreateSpawnTable(ply)
		return true;
		
	end
	
end

function GM:PlayerSpawnedProp(ply, mdl, ent)

	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	table.insert(spawntable.props, ent);
	
end

function GM:PlayerSpawnedRagdoll(ply, mdl, ent)

	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	table.insert(spawntable.ragdolls, ent);
	
end

function GM:PlayerSpawnedVehicle(ply, ent)

	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	table.insert(spawntable.vehicles, ent);
	
end

function GM:PlayerSpawnedEffect(ply, mdl, ent)

	local spawntable = SpawnTable[FormatSteamID(ply:SteamID())];
	table.insert(spawntable.effects, ent);
	
end

function Admin_ExtraProps(ply, cmd, args)

	if(#args != 2) then
	
		SendChat( ply, "Invalid number of arguments! ( rp_admin extraprops \"name\" amount )" );
		return;

	end
	
	local target = FindPlayer(args[1])
	
	if(target != nil and target:IsValid() and target:IsPlayer()) then
		-- klol
	else
		SendChat( ply, "Target not found!" );
		return;
	end
	
	SetPlayerField(target, "extraprops", tonumber(args[2]));
	SendChat( target, "Your extra props has been set to " .. args[2] .. " by " .. ply:Name());
	SendChat( ply, target:Name() .. "'s extra prop limit has been set to " .. args[2] );
	
end

function Admin_ExtraRagdolls(ply, cmd, args)

	if(#args != 2) then
	
		SendChat( ply, "Invalid number of arguments! ( rp_admin extraragdolls \"name\" amount )" );
		return;

	end
	
	local target = FindPlayer(args[1])
	
	if(target != nil and target:IsValid() and target:IsPlayer()) then
		-- klol
	else
		SendChat( ply, "Target not found!" );
		return;
	end
	
	SetPlayerField(target, "extraragdolls", tonumber(args[2]));
	SendChat( target, "Your extra ragdolls has been set to " .. args[2] .. " by " .. ply:Name());
	SendChat( ply, target:Name() .. "'s extra ragdolls  has been set to " .. args[2] );
	
end

function Admin_ExtraVehicles(ply, cmd, args)

	if(#args != 2) then
	
		SendChat( ply, "Invalid number of arguments! ( rp_admin extravehicles \"name\" amount )" );
		return;

	end
	
	local target = FindPlayer(args[1])
	
	if(target != nil and target:IsValid() and target:IsPlayer()) then
		-- klol
	else
		SendChat( ply, "Target not found!" );
		return;
	end
	
	SetPlayerField(target, "extravehicles", tonumber(args[2]));
	SendChat( target, "Your extra vehicles has been set to " .. args[2] .. " by " .. ply:Name());
	SendChat( ply, target:Name() .. "'s extra vehicle limit has been set to " .. args[2] );
	
end

function Admin_ExtraEffects(ply, cmd, args)

	if(#args != 2) then
	
		SendChat( ply, "Invalid number of arguments! ( rp_admin extraprops \"name\" amount )" );
		return;

	end
	
	local target = FindPlayer(args[1])
	
	if(target != nil and target:IsValid() and target:IsPlayer()) then
		-- klol
	else
		SendChat( ply, "Target not found!" );
		return;
	end
	
	SetPlayerField(target, "extraeffects", tonumber(args[2]));
	SendChat( target, "Your extra effects has been set to " .. args[2] .. " by " .. ply:Name());
	SendChat( ply, target:Name() .. "'s extra effects limit has been set to " .. args[2] );
	
end

function PLUGIN.Init()

	ConVars[ "Default_Extraprops" ] = 0;
	ConVars[ "Default_Extraragdolls" ] = 0;
	ConVars[ "Default_Extravehicles" ] = 0;
	ConVars[ "Default_Extraeffects" ] = 0;
	
	ConVars[ "PropLimit" ] = 30;
	ConVars[ "RagdollLimit" ] = 0;
	ConVars[ "VehicleLimit" ] = 0;
	ConVars[ "EffectLimit" ] = 0;
	
	AddDataField( 1, "extraprops", ConVars[ "Default_Extraprops" ] );
	AddDataField( 1, "extraragdolls", ConVars[ "Default_Extraragdolls" ] );
	AddDataField( 1, "extravehicles", ConVars[ "Default_Extravehicles" ] );
	AddDataField( 1, "extraeffects", ConVars[ "Default_Extraeffects" ] );
	
	AdminCommand( "extraprops", Admin_ExtraProps, "Change someones extra props limit", true, true, false );
	AdminCommand( "extraragdolls", Admin_ExtraRagdolls, "Change someones extra ragdolls limit", true, true, false );
	AdminCommand( "extravehicles", Admin_ExtraVehicles, "Change someones extra vehicles limit", true, true, false );
	AdminCommand( "extraeffects", Admin_ExtraEffects, "Change someones extra effects limit", true, true, false );
	
end