Skip to content

Commit

Permalink
Merge pull request #30 from daffyyyy/improvement-config_messages
Browse files Browse the repository at this point in the history
Add messages to config
  • Loading branch information
Nereziel authored Nov 15, 2023
2 parents dc7fb18 + 9f982d1 commit d88ce55
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
34 changes: 29 additions & 5 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core;
using System.Text.Json.Serialization;

namespace WeaponPaints
{
public class Messages
{
[JsonPropertyName("WebsiteMessageCommand")]
public string WebsiteMessageCommand { get; set; } = "Visit {WEBSITE} where you can change skins.";
[JsonPropertyName("SynchronizeMessageCommand")]
public string SynchronizeMessageCommand { get; set; } = "Type !wp to synchronize chosen skins.";
[JsonPropertyName("KnifeMessageCommand")]
public string KnifeMessageCommand { get; set; } = "Type !knife to open knife menu.";
[JsonPropertyName("CooldownRefreshCommand")]
public string CooldownRefreshCommand { get; set; } = "You can't refresh weapon paints right now.";
[JsonPropertyName("SuccessRefreshCommand")]
public string SuccessRefreshCommand { get; set; } = "Refreshing weapon paints.";
[JsonPropertyName("ChosenKnifeMenu")]
public string ChosenKnifeMenu { get; set; } = "You have chosen {KNIFE} as your knife.";
[JsonPropertyName("KnifeMenuTitle")]
public string KnifeMenuTitle { get; set; } = "Knife Menu.";
}

public class WeaponPaintsConfig : BasePluginConfig
{
public override int Version { get; set; } = 1;
public override int Version { get; set; } = 2;

[JsonPropertyName("DatabaseHost")]
public string DatabaseHost { get; set; } = "";
Expand All @@ -25,8 +43,14 @@ public class WeaponPaintsConfig : BasePluginConfig
[JsonPropertyName("CmdRefreshCooldownSeconds")]
public int CmdRefreshCooldownSeconds { get; set; } = 60;

[JsonPropertyName("WebSite")]
public string WebSite { get; set; } = "http://wp.example.com";

[JsonPropertyName("Prefix")]
public string Prefix { get; set; } = "[WeaponPaints]";

[JsonPropertyName("Website")]
public string Website { get; set; } = "example.com/skins";

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

}
62 changes: 53 additions & 9 deletions WeaponPaints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Nexd.MySQL;
using System.Runtime.ExceptionServices;
using static CounterStrikeSharp.API.Core.Listeners;
using System.Reflection;


namespace WeaponPaints;
public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
Expand All @@ -22,7 +24,6 @@ public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>

MySqlDb? MySql = null;
private DateTime[] commandCooldown = new DateTime[Server.MaxPlayers];
private static string PluginPrefix = $" {ChatColors.Green}[WeaponPaints]{ChatColors.White}";
private Dictionary<ulong, Dictionary<nint, int>> gPlayerWeaponPaints = new();
private Dictionary<ulong, Dictionary<nint, int>> gPlayerWeaponSeed = new();
private Dictionary<ulong, Dictionary<nint, float>> gPlayerWeaponWear = new();
Expand Down Expand Up @@ -243,14 +244,17 @@ public static bool PlayerHasKnife(CCSPlayerController player)
}
private void SetupMenus()
{
var giveItemMenu = new ChatMenu("Knife Menu");
var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle));
var handleGive = (CCSPlayerController player, ChatMenuOption option) =>
{
if (knifeTypes.TryGetValue(option.Text, out var knife))
{
Task.Run(() => SyncKnifeToDatabase((int)player.EntityIndex!.Value.Value, knife));
g_playersKnife[(int)player.EntityIndex!.Value.Value] = knifeTypes[option.Text];
player.PrintToChat($"You have chosen {option.Text} as your knife.");
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenu)) {
string temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", option.Text);
player.PrintToChat(ReplaceTags(temp));
}
RemoveKnifeFromPlayer(player);
}
};
Expand All @@ -264,23 +268,42 @@ private void SetupMenus()
public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
{
if (player == null) return;
string temp = "";
int playerIndex = (int)player.EntityIndex!.Value.Value;
if (DateTime.UtcNow >= commandCooldown[playerIndex].AddSeconds(Config.CmdRefreshCooldownSeconds))
{
commandCooldown[playerIndex] = DateTime.UtcNow;
Task.Run(async () => await GetWeaponPaintsFromDatabase(playerIndex));
player.PrintToChat($"{PluginPrefix} Refreshing weapon paints.");
if (!string.IsNullOrEmpty(Config.Messages.SuccessRefreshCommand)) {
temp = $"{Config.Prefix} {Config.Messages.SuccessRefreshCommand}";
player.PrintToChat(ReplaceTags(temp));
}
return;
}
player.PrintToChat($"{PluginPrefix} You can't refresh weapon paints right now.");
if (!string.IsNullOrEmpty(Config.Messages.CooldownRefreshCommand)) {
temp = $"{Config.Prefix} {Config.Messages.CooldownRefreshCommand}";
player.PrintToChat(ReplaceTags(temp));
}
}
[ConsoleCommand("css_ws", "weaponskins")]
public void OnCommandWS(CCSPlayerController? player, CommandInfo command)
{
if (player == null) return;
player.PrintToChat($"{PluginPrefix} Visit {ChatColors.Purple}{Config.WebSite} {ChatColors.White}where you can change skins.");
player.PrintToChat($"{PluginPrefix} Type {ChatColors.Purple}!wp {ChatColors.White}in chat to synchronize chosen skins.");
player.PrintToChat($"{PluginPrefix} Type {ChatColors.Purple}!knife {ChatColors.White}in chat to open knife menu.");

string temp = "";

if (!string.IsNullOrEmpty(Config.Messages.WebsiteMessageCommand)) {
temp = $"{Config.Prefix} {Config.Messages.WebsiteMessageCommand}";
player.PrintToChat(ReplaceTags(temp));
}
if (!string.IsNullOrEmpty(Config.Messages.SynchronizeMessageCommand)) {
temp = $"{Config.Prefix} {Config.Messages.SynchronizeMessageCommand}";
player.PrintToChat(ReplaceTags(temp));
}
if (!string.IsNullOrEmpty(Config.Messages.KnifeMessageCommand)) {
temp = $"{Config.Prefix} {Config.Messages.KnifeMessageCommand}";
player.PrintToChat(ReplaceTags(temp));
}
}
public static CSkeletonInstance GetSkeletonInstance(CGameSceneNode node)
{
Expand Down Expand Up @@ -377,11 +400,32 @@ private async Task SyncKnifeToDatabase(int playerIndex, string knife)
return;
}
}

private string ReplaceTags(string message)
{
if (message.Contains('{'))
{
string modifiedValue = message;
modifiedValue = modifiedValue.Replace("{WEBSITE}", Config.Website);
foreach (FieldInfo field in typeof(ChatColors).GetFields())
{
string pattern = $"{{{field.Name}}}";
if (message.Contains(pattern, StringComparison.OrdinalIgnoreCase))
{
modifiedValue = modifiedValue.Replace(pattern, field.GetValue(null)!.ToString(), StringComparison.OrdinalIgnoreCase);
}
}
return modifiedValue;
}

return message;
}

private static void Log(string message)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(message);
Console.ResetColor();
}
}
}

0 comments on commit d88ce55

Please sign in to comment.