Merge pull request #33 from daffyyyy/enhancement/config-expansion

Added an additional section to the config
This commit is contained in:
Nereziel
2023-11-16 00:38:33 +01:00
committed by GitHub
2 changed files with 50 additions and 11 deletions

View File

@@ -21,9 +21,24 @@ namespace WeaponPaints
public string KnifeMenuTitle { get; set; } = "Knife Menu."; public string KnifeMenuTitle { get; set; } = "Knife Menu.";
} }
public class Additional
{
[JsonPropertyName("SkinVisibilityFix")]
public bool SkinVisibilityFix { get; set; } = true;
[JsonPropertyName("KnifeEnabled")]
public bool KnifeEnabled { get; set; } = true;
[JsonPropertyName("SkinEnabled")]
public bool SkinEnabled { get; set; } = true;
[JsonPropertyName("CommandWpEnabled")]
public bool CommandWpEnabled { get; set; } = true;
}
public class WeaponPaintsConfig : BasePluginConfig public class WeaponPaintsConfig : BasePluginConfig
{ {
public override int Version { get; set; } = 2; public override int Version { get; set; } = 3;
[JsonPropertyName("DatabaseHost")] [JsonPropertyName("DatabaseHost")]
public string DatabaseHost { get; set; } = ""; public string DatabaseHost { get; set; } = "";
@@ -51,6 +66,9 @@ namespace WeaponPaints
[JsonPropertyName("Messages")] [JsonPropertyName("Messages")]
public Messages Messages { get; set; } = new Messages(); public Messages Messages { get; set; } = new Messages();
[JsonPropertyName("Additional")]
public Additional Additional { get; set; } = new Additional();
} }
} }

View File

@@ -1,5 +1,6 @@
using CounterStrikeSharp.API; using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Entities;
@@ -13,6 +14,7 @@ using System.Reflection;
namespace WeaponPaints; namespace WeaponPaints;
[MinimumApiVersion(52)]
public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig> public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{ {
public override string ModuleName => "WeaponPaints"; public override string ModuleName => "WeaponPaints";
@@ -74,7 +76,8 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
RegisterListener<Listeners.OnMapStart>(OnMapStart); RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn); RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
//RegisterEventHandler<EventRoundPrestart>(OnRoundPreStart); //RegisterEventHandler<EventRoundPrestart>(OnRoundPreStart);
SetupMenus(); if (Config.Additional.KnifeEnabled)
SetupMenus();
} }
public void OnConfigParsed(WeaponPaintsConfig config) public void OnConfigParsed(WeaponPaintsConfig config)
{ {
@@ -112,6 +115,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
private void OnMapStart(string mapName) private void OnMapStart(string mapName)
{ {
if (!Config.Additional.KnifeEnabled) return;
// TODO // TODO
// needed for now // needed for now
base.AddTimer(2.0f, () => { base.AddTimer(2.0f, () => {
@@ -124,15 +128,17 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{ {
int playerIndex = playerSlot + 1; int playerIndex = playerSlot + 1;
Task.Run(async () => Task.Run(async () =>
{ {
await GetKnifeFromDatabase(playerIndex); if (Config.Additional.KnifeEnabled)
await GetKnifeFromDatabase(playerIndex);
await GetWeaponPaintsFromDatabase(playerIndex); await GetWeaponPaintsFromDatabase(playerIndex);
}); });
} }
private void OnClientDisconnect(int playerSlot) private void OnClientDisconnect(int playerSlot)
{ {
// TODO: Clean up after player // TODO: Clean up after player
g_playersKnife.Remove(playerSlot+1); if (Config.Additional.KnifeEnabled)
g_playersKnife.Remove(playerSlot+1);
} }
private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info) private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
@@ -143,17 +149,22 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
return HookResult.Continue; return HookResult.Continue;
} }
GiveKnifeToPlayer(player); if (Config.Additional.KnifeEnabled)
GiveKnifeToPlayer(player);
// Check the best slot and set it. Weird solution but works xD if (Config.Additional.SkinVisibilityFix)
AddTimer(0.1f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3")); {
AddTimer(0.25f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2")); // Check the best slot and set it. Weird solution but works xD
AddTimer(0.35f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot1")); AddTimer(0.1f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"));
AddTimer(0.25f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2"));
AddTimer(0.35f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot1"));
}
return HookResult.Continue; return HookResult.Continue;
} }
private void OnEntitySpawned(CEntityInstance entity) private void OnEntitySpawned(CEntityInstance entity)
{ {
if (!Config.Additional.SkinEnabled) return;
var designerName = entity.DesignerName; var designerName = entity.DesignerName;
if (!weaponList.Contains(designerName)) return; if (!weaponList.Contains(designerName)) return;
bool isKnife = false; bool isKnife = false;
@@ -199,6 +210,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
public void GiveKnifeToPlayer(CCSPlayerController player) public void GiveKnifeToPlayer(CCSPlayerController player)
{ {
if (!Config.Additional.KnifeEnabled) return;
if (player.IsBot) if (player.IsBot)
{ {
player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife"); player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife");
@@ -219,6 +231,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
public void RemoveKnifeFromPlayer(CCSPlayerController player) public void RemoveKnifeFromPlayer(CCSPlayerController player)
{ {
if (!Config.Additional.KnifeEnabled) return;
if (!g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value)) return; if (!g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value)) return;
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons; var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
foreach (var weapon in weapons) foreach (var weapon in weapons)
@@ -234,8 +247,9 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
} }
} }
public static bool PlayerHasKnife(CCSPlayerController player) public bool PlayerHasKnife(CCSPlayerController player)
{ {
if (!Config.Additional.KnifeEnabled) return true;
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons; var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
foreach (var weapon in weapons) foreach (var weapon in weapons)
{ {
@@ -251,6 +265,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
private void SetupMenus() private void SetupMenus()
{ {
if (!Config.Additional.KnifeEnabled) return;
var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle)); var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle));
var handleGive = (CCSPlayerController player, ChatMenuOption option) => var handleGive = (CCSPlayerController player, ChatMenuOption option) =>
{ {
@@ -275,6 +290,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
[ConsoleCommand("css_wp", "refreshskins")] [ConsoleCommand("css_wp", "refreshskins")]
public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command) public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
{ {
if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled) return;
if (player == null) return; if (player == null) return;
string temp = ""; string temp = "";
int playerIndex = (int)player.EntityIndex!.Value.Value; int playerIndex = (int)player.EntityIndex!.Value.Value;
@@ -296,6 +312,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
[ConsoleCommand("css_ws", "weaponskins")] [ConsoleCommand("css_ws", "weaponskins")]
public void OnCommandWS(CCSPlayerController? player, CommandInfo command) public void OnCommandWS(CCSPlayerController? player, CommandInfo command)
{ {
if (!Config.Additional.SkinEnabled) return;
if (player == null) return; if (player == null) return;
string temp = ""; string temp = "";
@@ -308,6 +325,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
temp = $"{Config.Prefix} {Config.Messages.SynchronizeMessageCommand}"; temp = $"{Config.Prefix} {Config.Messages.SynchronizeMessageCommand}";
player.PrintToChat(ReplaceTags(temp)); player.PrintToChat(ReplaceTags(temp));
} }
if (!Config.Additional.KnifeEnabled) return;
if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand)) { if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand)) {
temp = $"{Config.Prefix} {Config.Messages.KnifeMessageCommand}"; temp = $"{Config.Prefix} {Config.Messages.KnifeMessageCommand}";
player.PrintToChat(ReplaceTags(temp)); player.PrintToChat(ReplaceTags(temp));
@@ -320,6 +338,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
private async Task GetWeaponPaintsFromDatabase(int playerIndex) private async Task GetWeaponPaintsFromDatabase(int playerIndex)
{ {
if (!Config.Additional.SkinEnabled) return;
try try
{ {
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
@@ -364,6 +383,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
private async Task GetKnifeFromDatabase(int playerIndex) private async Task GetKnifeFromDatabase(int playerIndex)
{ {
if (!Config.Additional.KnifeEnabled) return;
try try
{ {
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
@@ -395,6 +415,7 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
} }
private async Task SyncKnifeToDatabase(int playerIndex, string knife) private async Task SyncKnifeToDatabase(int playerIndex, string knife)
{ {
if (!Config.Additional.KnifeEnabled) return;
try try
{ {
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);