PLUGIN.Name = "Admin Commands"; -- What is the plugin name
PLUGIN.Author = "LuaBanana"; -- Author of the plugin
PLUGIN.Description = "A set of default admin commands"; -- The description or purpose of the plugin

function Admin_AddDoor(ply, cmd, args)
	
	local tr = ply:GetEyeTrace()
	local trent = tr.Entity;
	
	if(!IsDoor(trent)) then ply:PrintMessage(3, "You must be looking at a door!"); return; end

	if(table.getn(args) < 1) then ply:PrintMessage(3, "Specify a doorgroup!"); return; end
	
	local pos = trent:GetPos()
	local Door = {}
	Door["x"] = math.ceil(pos.x);
	Door["y"] = math.ceil(pos.y);
	Door["z"] = math.ceil(pos.z);
	Door["group"] = args[1];
	
	table.insert(Doors, Door);
	
	SendChat(ply, "Door added");
	
	local keys = util.TableToKeyValues(Doors);
	file.Write("SSTRP/MapData/" .. game.GetMap() .. ".txt", keys);
	
end

function Admin_Observe( ply, cmd, args )
	
	if( not ply:IsSuperAdmin() ) then return; end

	if( not ply:GetTable().Observe ) then



		ply:GodEnable();
		ply:SetNoDraw( true );
		
		if( ply:GetActiveWeapon() ) then
			ply:GetActiveWeapon():SetNoDraw( true );
		end
		
		ply:SetNotSolid( true );
		ply:SetMoveType( 8 );
		
		ply:GetTable().Observe = true;
		
	else

		ply:GodDisable();
		ply:SetNoDraw( false );
		
		if( ply:GetActiveWeapon() ) then
			ply:GetActiveWeapon():SetNoDraw( false );
		end
		
		ply:SetNotSolid( false );
		ply:SetMoveType( 2 );
		
		ply:GetTable().Observe = false;
		
	end

end

-- rp_admin kick "name" "reason"
function Admin_Kick( ply, cmd, args )

	if( #args != 2 ) then
	
		SendChat( ply, "Invalid number of arguments! ( rp_admin kick \"name\" \"reason\" )" );
		return;
		
	end
	
	local plyname = args[ 1 ];
	local reason = args[ 2 ];
	
	local pl = FindPlayer( plyname );
	
	if( pl:IsValid( ) and pl:IsPlayer( ) ) then
	
		local UniqueID = pl:UserID( );
		
		game.ConsoleCommand( "kickid " .. UniqueID .. " \"" .. reason .. "\"\n" );
		
		AnnounceAction( ply, "kicked " .. pl:Name( ) );
		
	else
	
		SendChat( ply, "Cannot find " .. plyname .. "!" );
		
	end
	
end

-- rp_admin ban "name" "reason" minutes
function Admin_Ban( ply, cmd, args )

	if( #args != 3 ) then
	
		SendChat( ply, "Invalid number of arguments! ( rp_admin ban \"name\" \"reason\" minutes )" );
		return;
		
	end
	
	local plyname = args[ 1 ];
	local reason = args[ 2 ];
	local mins = tonumber( args[ 3 ] );
	
	if(mins > ConVars[ "MaxBan" ]) then
	
		SendChat( ply, "Max minutes is " .. ConVars[ "MaxBan" ] .. " for regular ban. Use superban.");
		return;
	
	end
	
	local pl = FindPlayer( plyname );
	
	if( pl != nil and pl:IsValid( ) and pl:IsPlayer( ) ) then
	
		local UniqueID = pl:UserID( );
		
		-- This bans, then kicks, then writes their ID to the file.
		game.ConsoleCommand( "banid " .. mins .. " " .. UniqueID .. "\n" );
		game.ConsoleCommand( "kickid " .. UniqueID .. " \"Banned for " .. mins .. " mins ( " .. reason .. " )\"\n" );
		game.ConsoleCommand( "writeid\n" );
		
		AnnounceAction( ply, "banned " .. pl:Name( ) );
		
	else
	
		SendChat( ply, "Cannot find " .. plyname .. "!" );
		
	end
	
end

function Admin_SetModel( ply, cmd, args)
	if( #args != 2 ) then
		SendChat( ply, "Invalid # of arguments! ( rp_admin setmodel name model " )
		return
	end
	
	local plyname = args[1]
	local model =  args[2]
	
	local pl = FindPlayer(plyname)
	if( pl != nil and pl:IsValid( ) and pl:IsPlayer( ) ) then
		pl:SetModel( "" .. args[2] .. "" )
	end
end

function Admin_SetPermanentModel( ply, cmd, args)
	if( #args != 2 ) then
		SendChat( ply, "Invalid # of arguments! ( rp_admin setmodel name model " )
		return
	end
	
	local plyname = args[1]
	local model =  args[2]
	
	local pl = FindPlayer(plyname)
	if( pl != nil and pl:IsValid( ) and pl:IsPlayer( ) ) then
		SetCharField(pl, "model", "" .. args[2] .. "" );
	end
end


-- rp_admin superban "name" "reason" minutes
function Admin_SuperBan( ply, cmd, args )

	if( #args != 3 ) then
	
		SendChat( ply, "Invalid number of arguments! ( rp_admin superban \"name\" \"reason\" minutes )" );
		return;
		
	end
	
	local plyname = args[ 1 ];
	local reason = args[ 2 ];
	local mins = tonumber( args[ 3 ] );
	
	local pl = FindPlayer( plyname );
	
	if( pl != nil and pl:IsValid( ) and pl:IsPlayer( ) ) then
	
		local UniqueID = pl:UserID( );
		
		-- This bans, then kicks, then writes their ID to the file.
		game.ConsoleCommand( "banid " .. mins .. " " .. UniqueID .. "\n" );
		
		if( mins == 0 ) then
		
			game.ConsoleCommand( "kickid " .. UniqueID .. " \"Permanently banned ( " .. reason .. " )\"\n" );
			AnnounceAction( ply, "permabanned " .. pl:Name( ) );
	
		else
		
			game.ConsoleCommand( "kickid " .. UniqueID .. " \"Banned for " .. mins .. " mins ( " .. reason .. " )\"\n" );
			AnnounceAction( ply, "banned " .. pl:Name( ) );
			
		end
		
		game.ConsoleCommand( "writeid\n" );
		
	else
	
		SendChat( ply, "Cannot find " .. plyname .. "!" );
		
	end
	
end

function Admin_SetConVar( ply, cmd, args )

	if( #args != 2 ) then
	
		SendChat( ply, "Invalid number of arguments! ( rp_admin setvar \"varname\" \"value\" )" );
		return;
		
	end
	
	if( ConVars[ args[ 1 ] ] ) then
	
		local vartype = type( ConVars[ args [ 1 ] ] );
		
		if( vartype == "string" ) then
		
			ConVars[ args[ 1 ] ] = tostring(args[ 2 ]);
			
		elseif( vartype == "number" ) then
		
			ConVars[ args[ 1 ] ] = NilFix(tonumber(args[ 2 ]), 0); -- Don't set a fkn string for a number, dumbass! >:<
		
		elseif( vartype == "table" ) then
		
			SendChat( ply, args[ 1 ] .. " cannot be changed, it is a table." ); -- This is kind of like.. impossible.. kinda. (Or I'm just a lazy fuck)
			return;
			
		end
		
		SendChat( ply, args[ 1 ] .. " set to " .. args[ 2 ] );
		CallHook( "SetConVar", ply, args[ 1 ], args[ 2 ] );
		
	else
	
		SendChat( ply, args[ 1 ] .. " is not a valid convar! Use rp_admin listvars" );
		
	end
	
end

function Admin_ListVars( ply, cmd, args )

	SendChat( ply, "---List of ConVars---" );
	
	for k, v in pairs( ConVars ) do
		
		SendChat( ply, k .. " - " .. tostring(v) );
		
	end
	
end

function Admin_SetFlags( ply, cmd, args)
	
	local target = FindPlayer(args[1])
	
	if(target != nil and target:IsValid() and target:IsPlayer()) then
		-- klol
	else
		SendChat( ply, "Target not found!" );
		return;
	end
	
	table.remove(args, 1); -- Get rid of the name
	
	SetCharField(target, "flags", args); -- KLOL!
	
	SendChat( ply, target:Name() .. "'s flags were set to \"" .. table.concat(args, " ") .. "\"" );
	SendChat( target, "Your flags were set to \"" .. table.concat(args, " ") .. "\" by " .. ply:Name());
	
end

function Admin_Help( ply, cmd, args )

	SendChat( ply, "---List of Admin Commands---" );
	
	for cmdname, cmd in pairs( AdminCommands ) do
	
		local s = cmdname .. " \"" .. cmd.desc .. "\"";
		
		if(cmd.CanRunFromConsole) then
		
			s = s .. " console";

		else
		
			s = s .. " noconsole";
			
		end
		
		if(cmd.CanRunFromAdmin) then
		
			s = s .. " admin";
			
		end
		
		if(cmd.SuperOnly) then
		
			s = s .. " superonly";
			
		end
		
		SendChat( ply, s );
		
	end
	
end
	
-- Let's make some ADMIN COMMANDS!
function PLUGIN.Init( )

	ConVars[ "MaxBan" ] = 110101010101010; -- What is the maximum ban limit for regular admins?
	
	AdminCommand( "help", Admin_Help, "List of all admin commands", true, true, false );
	AdminCommand( "kick", Admin_Kick, "Kick someone on the server", true, true, false );
	AdminCommand( "ban", Admin_Ban, "Ban someone on the server", true, true, false );
	AdminCommand( "superban", Admin_SuperBan, "Ban someone on the server ( Permanent allowed )", true, true, true );
	AdminCommand( "setconvar", Admin_SetConVar, "Set a Convar", true, true, true );
	AdminCommand( "listvars", Admin_ListVars, "List convars", true, true, true );
	AdminCommand( "setflags", Admin_SetFlags, "Set a players flags", true, true, false );
	AdminCommand( "adddoor", Admin_AddDoor, "Add a door to a doorgroup", true, true, true );
	AdminCommand( "setmodel", Admin_SetModel, "Set a players TEMPORARY MODEL", true, true, false );
	AdminCommand( "setpermamodel", Admin_SetPermanentModel, "Set a players PERMANENT MODEL", true, true, false );
	AdminCommand( "observe", Admin_Observe, "Look around LOL", true, true, false )
end

