🚀 Initial commit

This commit is contained in:
Tobias Thiele 2023-12-13 09:46:00 +01:00
commit 01b43dcdef
8 changed files with 1031 additions and 0 deletions

173
Flashlight/Flashlight.cs Normal file
View file

@ -0,0 +1,173 @@
using System.Drawing;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using Vector = CounterStrikeSharp.API.Modules.Utils.Vector;
namespace Flashlight;
public class Flashlight : BasePlugin
{
public override string ModuleAuthor => "creazy.eth";
public override string ModuleName => "Flashlight";
public override string ModuleDescription => "Flashlight for Counter-Strike 2";
public override string ModuleVersion => "0.0.1";
private static string ModuleDisplayName => "Flashlight";
// TODO: Add config and make light entity values configurable
// TODO: Maybe replace light_omni2 with light_rect or something else
public static Flashlight? Instance { get; private set; }
private readonly List<CCSPlayerController> _connectedPlayers = new();
private readonly Dictionary<CCSPlayerController, bool> _playerUsingFlashlight = new();
private readonly Dictionary<CCSPlayerController, bool> _playerCanToggle = new();
private readonly Dictionary<CCSPlayerController, COmniLight> _playerFlashlight = new();
public override void Load(bool hotReload)
{
LogHelper.LogToConsole(ConsoleColor.Green, $"{ModuleName} version {ModuleVersion} loaded");
Instance = this;
RegisterListener<Listeners.OnTick>(() =>
{
foreach (var player in _connectedPlayers.Where(player => player is { IsValid: true, IsBot: false, PawnIsAlive: true }))
{
ToggleFlashlight(player, null);
if (_playerCanToggle[player] == false) continue;
if ((player.Buttons & PlayerButtons.Use) != 0)
{
_playerCanToggle[player] = false;
if (_playerUsingFlashlight[player] == false)
{
_playerUsingFlashlight[player] = true;
Instance?.AddTimer(0.25f, () =>
{
_playerCanToggle[player] = true;
});
}
else
{
_playerUsingFlashlight[player] = false;
Instance?.AddTimer(0.25f, () =>
{
_playerCanToggle[player] = true;
});
}
}
}
});
LogHelper.LogToChatAll($"{ModuleDisplayName} v{ModuleVersion} loaded!");
}
[GameEventHandler]
public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info)
{
var player = @event.Userid;
if (!player.IsValid || player.IsBot) return HookResult.Continue;
_connectedPlayers.Add(player);
_playerUsingFlashlight[player] = false;
_playerCanToggle[player] = true;
LogHelper.LogToConsole(ConsoleColor.Green, $"{player.PlayerName} connected");
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo info)
{
var player = @event.Userid;
if (!player.IsValid || player.IsBot) return HookResult.Continue;
_connectedPlayers.Remove(player);
_playerUsingFlashlight.Remove(player);
_playerCanToggle.Remove(player);
LogHelper.LogToConsole(ConsoleColor.Green, $"{player.PlayerName} disconnected");
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
var player = @event.Userid;
if (!player.IsValid || player.IsBot) return HookResult.Continue;
_playerUsingFlashlight[player] = false;
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
var player = @event.Userid;
if (!player.IsValid || player.IsBot) return HookResult.Continue;
_playerUsingFlashlight[player] = false;
return HookResult.Continue;
}
[ConsoleCommand("fl_toggle", "Toggles the flashlight")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void ToggleFlashlight(CCSPlayerController caller, CommandInfo? info)
{
if (_playerUsingFlashlight[caller] == false)
{
if (_playerFlashlight.TryGetValue(caller, out var value))
{
value.Remove();
_playerFlashlight.Remove(caller);
}
return;
}
var entity = _playerFlashlight.TryGetValue(caller, out var flashlight) ? flashlight : Utilities.CreateEntityByName<COmniLight>("light_omni2");
if (entity == null || !entity.IsValid)
{
LogHelper.LogToConsole("Failed to create entity!");
return;
}
entity.DirectLight = 3;
entity.Teleport(
new Vector(
caller.PlayerPawn.Value!.AbsOrigin!.X,
caller.PlayerPawn.Value!.AbsOrigin!.Y,
caller.PlayerPawn.Value!.AbsOrigin!.Z + 64.03f
),
caller.PlayerPawn.Value!.EyeAngles,
caller.PlayerPawn.Value!.AbsVelocity
);
entity.OuterAngle = 45f;
entity.Enabled = true;
entity.Color = Color.White;
entity.ColorTemperature = 6500;
entity.Brightness = 1f;
entity.Range = 5000f;
entity.DispatchSpawn();
_playerFlashlight[caller] = entity;
}
}

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.124" />
</ItemGroup>
</Project>

56
Flashlight/LogHelper.cs Normal file
View file

@ -0,0 +1,56 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
namespace Flashlight;
public static class LogHelper
{
public static void LogToConsole(string messageToLog)
{
Console.WriteLine($"[{Flashlight.Instance?.ModuleName}] -> {messageToLog}");
}
public static void LogToConsole(ConsoleColor color, string messageToLog)
{
Console.ForegroundColor = color;
Console.WriteLine($"[{Flashlight.Instance?.ModuleName}] -> {messageToLog}");
Console.ResetColor();
}
public static void LogToChat(CCSPlayerController? player, string messageToLog)
{
player?.PrintToChat($"[{ChatColors.Purple}{Flashlight.Instance?.ModuleName}{ChatColors.Default}]{ReplaceTags(messageToLog + "{DEFAULT}")}");
}
public static void LogToChatAll(string messageToLog)
{
Server.PrintToChatAll($"[{ChatColors.Purple}{Flashlight.Instance?.ModuleName}{ChatColors.Default}]{ReplaceTags(messageToLog + "{DEFAULT}")}");
}
public static string ReplaceTags(this string text)
{
if (text.StartsWith("{")) text = $" {text}";
text = text.Replace("{DEFAULT}", $"{ChatColors.Default}");
text = text.Replace("{WHITE}", $"{ChatColors.White}");
text = text.Replace("{DARKRED}", $"{ChatColors.Darkred}");
text = text.Replace("{GREEN}", $"{ChatColors.Green}");
text = text.Replace("{LIGHTYELLOW}", $"{ChatColors.LightYellow}");
text = text.Replace("{LIGHTBLUE}", $"{ChatColors.LightBlue}");
text = text.Replace("{OLIVE}", $"{ChatColors.Olive}");
text = text.Replace("{LIME}", $"{ChatColors.Lime}");
text = text.Replace("{RED}", $"{ChatColors.Red}");
text = text.Replace("{PURPLE}", $"{ChatColors.Purple}");
text = text.Replace("{GREY}", $"{ChatColors.Grey}");
text = text.Replace("{YELLOW}", $"{ChatColors.Yellow}");
text = text.Replace("{GOLD}", $"{ChatColors.Gold}");
text = text.Replace("{SILVER}", $"{ChatColors.Silver}");
text = text.Replace("{BLUE}", $"{ChatColors.Blue}");
text = text.Replace("{DARKBLUE}", $"{ChatColors.DarkBlue}");
text = text.Replace("{BLUEGREY}", $"{ChatColors.BlueGrey}");
text = text.Replace("{MAGENTA}", $"{ChatColors.Magenta}");
text = text.Replace("{LIGHTRED}", $"{ChatColors.LightRed}");
return text;
}
}