Skip to content

Commit

Permalink
Merge pull request #7 from HvH-gg/custom-votes-support
Browse files Browse the repository at this point in the history
Added FakeConVars
  • Loading branch information
imi-tat0r authored Mar 9, 2024
2 parents 0c10a82 + 1804c5a commit 17011d7
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 33 deletions.
8 changes: 7 additions & 1 deletion CS2-Essentials/CS2-Essentials.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.167" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.191" />
</ItemGroup>

<ItemGroup>
<Reference Include="CS2-CustomVotes.Shared">
<HintPath>..\..\CS2-CustomVotes\CS2-CustomVotes.Shared\bin\Release\net7.0\CS2-CustomVotes.Shared.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
35 changes: 34 additions & 1 deletion CS2-Essentials/Features/FriendlyFire.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CS2_CustomVotes.Shared.Models;

namespace hvhgg_essentials.Features;

public class FriendlyFire
{
private readonly Plugin _plugin;
public static readonly FakeConVar<bool> hvh_unmatched_friendlyfire = new("hvh_unmatched_friendlyfire", "Enables unmatched.gg style friendly fire (utility only)", true, ConVarFlags.FCVAR_REPLICATED);

public FriendlyFire(Plugin plugin)
{
_plugin = plugin;
_plugin.RegisterFakeConVars(this);
hvh_unmatched_friendlyfire.Value = _plugin.Config.UnmatchedFriendlyFire;
}

private static readonly string[] AllowedDamageInflicter =
{
"inferno", "hegrenade_projectile", "flashbang_projectile", "smokegrenade_projectile", "decoy_projectile",
"planted_c4"
};

public static void RegisterCustomVotes(Plugin plugin)
{
if (!plugin.Config.CustomVoteSettings.FriendlyFireVote)
return;

var defaultOption = plugin.Config.UnmatchedFriendlyFire ? "Enable" : "Disable";

Plugin.CustomVotesApi.Get()?.AddCustomVote(
"friendlyfire",
new List<string> {
"ff"
},
"unmatched.gg friendly fire",
defaultOption,
30,
new Dictionary<string, VoteOption>
{
{ "Enable", new("{Green}Enable", new List<string> { "hvh_unmatched_friendlyfire 0" })},
{ "Disable", new("{Red}Disable", new List<string> { "hvh_unmatched_friendlyfire 1" })},
},
plugin.Config.CustomVoteSettings.Style);
}
public static void UnregisterCustomVotes(Plugin plugin)
{
Plugin.CustomVotesApi.Get()?.RemoveCustomVote("friendlyfire");
}

public HookResult OnTakeDamage(DynamicHook hook)
{
Expand Down
9 changes: 7 additions & 2 deletions CS2-Essentials/Features/RageQuit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Cvars.Validators;
using CounterStrikeSharp.API.Modules.Utils;

namespace hvhgg_essentials.Features;

public class RageQuit
{
private readonly Plugin _plugin;
public static readonly FakeConVar<bool> hvh_ragequit = new("hvh_ragequit", "Enables the rage quit feature", true, ConVarFlags.FCVAR_REPLICATED);

public RageQuit(Plugin plugin)
{
_plugin = plugin;
_plugin.RegisterFakeConVars(this);
hvh_ragequit.Value = _plugin.Config.AllowRageQuit;
}

[ConsoleCommand("rq", "Rage quit")]
[ConsoleCommand("ragequit", "Rage quit")]
[ConsoleCommand("css_rq", "Rage quit")]
[ConsoleCommand("css_ragequit", "Rage quit")]
public void OnRageQuit(CCSPlayerController? player, CommandInfo inf)
{
if (!_plugin.Config.AllowRageQuit)
Expand Down
50 changes: 49 additions & 1 deletion CS2-Essentials/Features/RapidFireFix.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Cvars.Validators;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Utils;
using CS2_CustomVotes.Shared.Models;
using hvhgg_essentials.Enums;

namespace hvhgg_essentials.Features;
Expand All @@ -13,10 +16,56 @@ public class RapidFire
private readonly Dictionary<uint, float> _rapidFireBlockWarnings = new();

private readonly Plugin _plugin;
public static readonly FakeConVar<int> hvh_restrict_rapidfire = new("hvh_restrict_rapidfire", "Restrict rapid fire", 0, ConVarFlags.FCVAR_REPLICATED, new RangeValidator<int>(0, 3));
public static readonly FakeConVar<float> hvh_rapidfire_reflect_scale = new("hvh_rapidfire_reflect_scale", "Reflect scale", 1, ConVarFlags.FCVAR_REPLICATED, new RangeValidator<float>(0, 1));

public RapidFire(Plugin plugin)
{
_plugin = plugin;
_plugin.RegisterFakeConVars(this);
hvh_restrict_rapidfire.Value = (int) _plugin.Config.RapidFireFixMethod;
hvh_rapidfire_reflect_scale.Value = _plugin.Config.RapidFireReflectScale;
}

public static void RegisterCustomVotes(Plugin plugin)
{
var defaultOption = plugin.Config.RapidFireFixMethod switch
{
FixMethod.Allow => "Allow",
FixMethod.Ignore => "Block",
FixMethod.Reflect => "Reflect",
FixMethod.ReflectSafe => "Reflect (safe)",
_ => "Allow"
};

var options = new Dictionary<string, VoteOption>();

if (plugin.Config.CustomVoteSettings.RapidFireVote != "off")
{
options.Add("Allow", new VoteOption("{Green}Allow", new List<string> { "hvh_restrict_rapidfire 0" }));
options.Add("Block", new VoteOption("{Red}Block", new List<string> { "hvh_restrict_rapidfire 1" }));

if (plugin.Config.CustomVoteSettings.RapidFireVote == "full")
{
options.Add("Reflect", new VoteOption("{Orange}Reflect", new List<string> { "hvh_restrict_rapidfire 2" }));
options.Add("Reflect (safe)", new VoteOption("{Orange}Reflect (safe)", new List<string> { "hvh_restrict_rapidfire 3" }));
}
}

Plugin.CustomVotesApi.Get()?.AddCustomVote(
"rapidfire",
new List<string> {
"rf"
},
"Rapid fire",
defaultOption,
30,
options,
plugin.Config.CustomVoteSettings.Style);
}
public static void UnregisterCustomVotes(Plugin plugin)
{
Plugin.CustomVotesApi.Get()?.RemoveCustomVote("rapidfire");
}

public HookResult OnWeaponFire(EventWeaponFire eventWeaponFire, GameEventInfo info)
Expand All @@ -27,7 +76,6 @@ public HookResult OnWeaponFire(EventWeaponFire eventWeaponFire, GameEventInfo in
var firedWeapon = eventWeaponFire.Userid.Pawn.Value?.WeaponServices?.ActiveWeapon.Value;
var weaponData = firedWeapon?.GetVData<CCSWeaponBaseVData>();

var nextPrimaryAttackTick = firedWeapon?.NextPrimaryAttackTick ?? 0;
var index = eventWeaponFire.Userid.Pawn.Index;

if (!_lastPlayerShotTick.TryGetValue(index, out var lastShotTick))
Expand Down
8 changes: 6 additions & 2 deletions CS2-Essentials/Features/ResetScore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Utils;

namespace hvhgg_essentials.Features;

public class ResetScore
{
private readonly Plugin _plugin;
public static readonly FakeConVar<bool> hvh_resetscore = new("hvh_resetscore", "Enables the reset score feature", true, ConVarFlags.FCVAR_REPLICATED);

public ResetScore(Plugin plugin)
{
_plugin = plugin;
_plugin.RegisterFakeConVars(this);
hvh_resetscore.Value = _plugin.Config.AllowResetScore;
}

[ConsoleCommand("rs", "Reset score")]
[ConsoleCommand("resetscore", "Reset score")]
[ConsoleCommand("css_rs", "Reset score")]
[ConsoleCommand("css_resetscore", "Reset score")]
public void OnResetScore(CCSPlayerController? player, CommandInfo inf)
{
if (!_plugin.Config.AllowResetScore)
Expand Down
29 changes: 29 additions & 0 deletions CS2-Essentials/Features/TeleportFix.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Runtime.CompilerServices;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Utils;
using CS2_CustomVotes.Shared.Models;
using hvhgg_essentials.Extensions;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -12,10 +14,37 @@ public class TeleportFix
{
private readonly Plugin _plugin;
private readonly Dictionary<uint, float> _teleportBlockWarnings = new();
public static readonly FakeConVar<bool> hvh_restrict_teleport = new("hvh_restrict_teleport", "Restricts players from teleporting/airstucking and crashing the server", true, ConVarFlags.FCVAR_REPLICATED);

public TeleportFix(Plugin plugin)
{
_plugin = plugin;
_plugin.RegisterFakeConVars(this);
hvh_restrict_teleport.Value = _plugin.Config.RestrictTeleport;
}

public static void RegisterCustomVotes(Plugin plugin)
{
if (!plugin.Config.CustomVoteSettings.TeleportFixVote)
return;

var defaultOption = plugin.Config.RestrictTeleport ? "Block" : "Allow";

Plugin.CustomVotesApi.Get()?.AddCustomVote(
"teleport",
"Teleport/Airstuck",
defaultOption,
30,
new Dictionary<string, VoteOption>
{
{ "Allow", new("{Green}Allow", new List<string> { "hvh_restrict_teleport 0" })},
{ "Block", new("{Red}Block", new List<string> { "hvh_restrict_teleport 1" })},
},
plugin.Config.CustomVoteSettings.Style);
}
public static void UnregisterCustomVotes(Plugin plugin)
{
Plugin.CustomVotesApi.Get()?.RemoveCustomVote("teleport");
}

public HookResult RunCommand(DynamicHook h)
Expand Down
9 changes: 9 additions & 0 deletions CS2-Essentials/Features/WeaponRestrict.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Cvars.Validators;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CounterStrikeSharp.API.Modules.Utils;
Expand All @@ -17,10 +19,17 @@ public class WeaponRestrict
{ "weapon_g3sg1", new Tuple<ItemDefinition, int>(ItemDefinition.G3SG1, 5000) },
};
private readonly Dictionary<uint, float> _lastWeaponRestrictPrint = new();
public static readonly FakeConVar<int> hvh_restrict_awp = new("hvh_restrict_awp", "Restrict awp to X per team", -1, ConVarFlags.FCVAR_REPLICATED, new RangeValidator<int>(-1, int.MaxValue));
public static readonly FakeConVar<int> hvh_restrict_scout = new("hvh_restrict_scout", "Restrict scout to X per team", -1, ConVarFlags.FCVAR_REPLICATED, new RangeValidator<int>(-1, int.MaxValue));
public static readonly FakeConVar<int> hvh_restrict_auto = new("hvh_restrict_auto", "Restrict autosniper to X per team", -1, ConVarFlags.FCVAR_REPLICATED, new RangeValidator<int>(-1, int.MaxValue));

public WeaponRestrict(Plugin plugin)
{
_plugin = plugin;
_plugin.RegisterFakeConVars(this);
hvh_restrict_awp.Value = _plugin.Config.AllowedAwpCount;
hvh_restrict_scout.Value = _plugin.Config.AllowedScoutCount;
hvh_restrict_auto.Value = _plugin.Config.AllowedAutoSniperCount;
}

public HookResult OnWeaponCanUse(DynamicHook hook)
Expand Down
64 changes: 42 additions & 22 deletions CS2-Essentials/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,25 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Cvars.Validators;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using CS2_CustomVotes.Shared;
using hvhgg_essentials.Enums;
using hvhgg_essentials.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace hvhgg_essentials;

public partial class Cs2EssentialsConfig : BasePluginConfig
{
[JsonPropertyName("RapidFireFixMethod")] public FixMethod RapidFireFixMethod { get; set; } = FixMethod.Ignore;
[JsonPropertyName("RapidFireReflectScale")] public float RapidFireReflectScale { get; set; } = 1f;
[JsonPropertyName("AllowedAwpCount")] public int AllowedAwpCount { get; set; } = -1;
[JsonPropertyName("AllowedScoutCount")] public int AllowedScoutCount { get; set; } = -1;
[JsonPropertyName("AllowedAutoSniperCount")] public int AllowedAutoSniperCount { get; set; } = -1;
[JsonPropertyName("UnmatchedFriendlyFire")] public bool UnmatchedFriendlyFire { get; set; } = true;
[JsonPropertyName("RestrictTeleport")] public bool RestrictTeleport { get; set; } = true;
[JsonPropertyName("AllowAdPrint")] public bool AllowAdPrint { get; set; } = true;
[JsonPropertyName("AllowSettingsPrint")] public bool AllowSettingsPrint { get; set; } = true;
[JsonPropertyName("AllowResetScore")] public bool AllowResetScore { get; set; } = true;
[JsonPropertyName("AllowRageQuit")] public bool AllowRageQuit { get; set; } = true;
[JsonPropertyName("ChatPrefix")] public string ChatPrefix { get; set; } = "[{Red}Hv{DarkRed}H{Default}.gg]";
[JsonPropertyName("ConfigVersion")] public override int Version { get; set; } = 3;
}

public class Plugin : BasePlugin, IPluginConfig<Cs2EssentialsConfig>
{
public override string ModuleName => "HvH.gg - Essentials";
public override string ModuleVersion => "1.1.3";
public override string ModuleVersion => "1.2.0";
public override string ModuleAuthor => "imi-tat0r";
public override string ModuleDescription => "Essential features for CS2 HvH servers";
public Cs2EssentialsConfig Config { get; set; } = new();
Expand All @@ -47,6 +34,9 @@ public class Plugin : BasePlugin, IPluginConfig<Cs2EssentialsConfig>

public required MemoryFunctionVoid<CCSPlayer_MovementServices, IntPtr> RunCommand;

public static PluginCapability<ICustomVoteApi> CustomVotesApi { get; } = new("custom_votes:api");
private bool _isCustomVotesLoaded = false;

public void OnConfigParsed(Cs2EssentialsConfig config)
{
Config = config;
Expand Down Expand Up @@ -102,10 +92,32 @@ public override void Load(bool hotReload)
UseResetScore();
UseMisc();
UseTeleport();

Console.WriteLine("[HvH.gg] Finished loading HvH.gg Essentials plugin");
}


public override void OnAllPluginsLoaded(bool hotReload)
{
base.OnAllPluginsLoaded(hotReload);

try
{
if (CustomVotesApi.Get() is null)
return;
}
catch (Exception e)
{
Console.WriteLine($"[HvH.gg] CS2-CustomVotes plugin not found. Custom votes will not be registered.");
return;
}

_isCustomVotesLoaded = true;
Console.WriteLine("[HvH.gg] Registering custom votes");
RapidFire.RegisterCustomVotes(this);
FriendlyFire.RegisterCustomVotes(this);
TeleportFix.RegisterCustomVotes(this);
}

private void UseTeleport()
{
var teleportFix = _serviceProvider!.GetRequiredService<TeleportFix>();
Expand Down Expand Up @@ -198,8 +210,6 @@ public void OnReloadConfigCommand(CCSPlayerController? player, CommandInfo info)

public override void Unload(bool hotReload)
{
base.Unload(hotReload);

if (_serviceProvider is null)
return;

Expand All @@ -216,5 +226,15 @@ public override void Unload(bool hotReload)
var teleportFix = _serviceProvider.GetRequiredService<TeleportFix>();
RunCommand.Unhook(teleportFix.RunCommand, HookMode.Pre);

if (!_isCustomVotesLoaded)
return;

Console.WriteLine("[HvH.gg] Unregistering custom votes");

RapidFire.UnregisterCustomVotes(this);
FriendlyFire.UnregisterCustomVotes(this);
TeleportFix.UnregisterCustomVotes(this);

base.Unload(hotReload);
}
}
Loading

0 comments on commit 17011d7

Please sign in to comment.