Skip to content

Commit

Permalink
Merge pull request #33 from daffyyyy/enhancement/config-expansion
Browse files Browse the repository at this point in the history
Added an additional section to the config
  • Loading branch information
Nereziel authored Nov 15, 2023
2 parents b5a4577 + c4ad17a commit 843ae34
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
20 changes: 19 additions & 1 deletion Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,24 @@ public class Messages
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 override int Version { get; set; } = 2;
public override int Version { get; set; } = 3;

[JsonPropertyName("DatabaseHost")]
public string DatabaseHost { get; set; } = "";
Expand Down Expand Up @@ -51,6 +66,9 @@ public class WeaponPaintsConfig : BasePluginConfig

[JsonPropertyName("Messages")]
public Messages Messages { get; set; } = new Messages();

[JsonPropertyName("Additional")]
public Additional Additional { get; set; } = new Additional();
}

}
41 changes: 31 additions & 10 deletions WeaponPaints.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities;
Expand All @@ -13,6 +14,7 @@


namespace WeaponPaints;
[MinimumApiVersion(52)]
public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{
public override string ModuleName => "WeaponPaints";
Expand Down Expand Up @@ -74,7 +76,8 @@ public override void Load(bool hotReload)
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
//RegisterEventHandler<EventRoundPrestart>(OnRoundPreStart);
SetupMenus();
if (Config.Additional.KnifeEnabled)
SetupMenus();
}
public void OnConfigParsed(WeaponPaintsConfig config)
{
Expand Down Expand Up @@ -112,6 +115,7 @@ private void RemoveGlobalExceptionHandler()
}
private void OnMapStart(string mapName)
{
if (!Config.Additional.KnifeEnabled) return;
// TODO
// needed for now
base.AddTimer(2.0f, () => {
Expand All @@ -124,15 +128,17 @@ private void OnClientPutInServer(int playerSlot)
{
int playerIndex = playerSlot + 1;
Task.Run(async () =>
{
await GetKnifeFromDatabase(playerIndex);
{
if (Config.Additional.KnifeEnabled)
await GetKnifeFromDatabase(playerIndex);
await GetWeaponPaintsFromDatabase(playerIndex);
});
}
private void OnClientDisconnect(int playerSlot)
{
// 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)
Expand All @@ -143,17 +149,22 @@ private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
return HookResult.Continue;
}

GiveKnifeToPlayer(player);
if (Config.Additional.KnifeEnabled)
GiveKnifeToPlayer(player);

// Check the best slot and set it. Weird solution but works xD
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"));
if (Config.Additional.SkinVisibilityFix)
{
// Check the best slot and set it. Weird solution but works xD
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;
}
private void OnEntitySpawned(CEntityInstance entity)
{
if (!Config.Additional.SkinEnabled) return;
var designerName = entity.DesignerName;
if (!weaponList.Contains(designerName)) return;
bool isKnife = false;
Expand Down Expand Up @@ -199,6 +210,7 @@ private void OnEntitySpawned(CEntityInstance entity)
}
public void GiveKnifeToPlayer(CCSPlayerController player)
{
if (!Config.Additional.KnifeEnabled) return;
if (player.IsBot)
{
player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife");
Expand All @@ -219,6 +231,7 @@ public void GiveKnifeToPlayer(CCSPlayerController player)
}
public void RemoveKnifeFromPlayer(CCSPlayerController player)
{
if (!Config.Additional.KnifeEnabled) return;
if (!g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value)) return;
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
foreach (var weapon in weapons)
Expand All @@ -234,8 +247,9 @@ public void RemoveKnifeFromPlayer(CCSPlayerController player)
}
}
}
public static bool PlayerHasKnife(CCSPlayerController player)
public bool PlayerHasKnife(CCSPlayerController player)
{
if (!Config.Additional.KnifeEnabled) return true;
var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
foreach (var weapon in weapons)
{
Expand All @@ -251,6 +265,7 @@ public static bool PlayerHasKnife(CCSPlayerController player)
}
private void SetupMenus()
{
if (!Config.Additional.KnifeEnabled) return;
var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle));
var handleGive = (CCSPlayerController player, ChatMenuOption option) =>
{
Expand All @@ -275,6 +290,7 @@ private void SetupMenus()
[ConsoleCommand("css_wp", "refreshskins")]
public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
{
if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled) return;
if (player == null) return;
string temp = "";
int playerIndex = (int)player.EntityIndex!.Value.Value;
Expand All @@ -296,6 +312,7 @@ public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
[ConsoleCommand("css_ws", "weaponskins")]
public void OnCommandWS(CCSPlayerController? player, CommandInfo command)
{
if (!Config.Additional.SkinEnabled) return;
if (player == null) return;

string temp = "";
Expand All @@ -308,6 +325,7 @@ public void OnCommandWS(CCSPlayerController? player, CommandInfo command)
temp = $"{Config.Prefix} {Config.Messages.SynchronizeMessageCommand}";
player.PrintToChat(ReplaceTags(temp));
}
if (!Config.Additional.KnifeEnabled) return;
if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand)) {
temp = $"{Config.Prefix} {Config.Messages.KnifeMessageCommand}";
player.PrintToChat(ReplaceTags(temp));
Expand All @@ -320,6 +338,7 @@ public static CSkeletonInstance GetSkeletonInstance(CGameSceneNode node)
}
private async Task GetWeaponPaintsFromDatabase(int playerIndex)
{
if (!Config.Additional.SkinEnabled) return;
try
{
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
Expand Down Expand Up @@ -364,6 +383,7 @@ private async Task GetWeaponPaintsFromDatabase(int playerIndex)
}
private async Task GetKnifeFromDatabase(int playerIndex)
{
if (!Config.Additional.KnifeEnabled) return;
try
{
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
Expand Down Expand Up @@ -395,6 +415,7 @@ private async Task GetKnifeFromDatabase(int playerIndex)
}
private async Task SyncKnifeToDatabase(int playerIndex, string knife)
{
if (!Config.Additional.KnifeEnabled) return;
try
{
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
Expand Down

0 comments on commit 843ae34

Please sign in to comment.