Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
daffyyyy committed Dec 5, 2023
1 parent e37f111 commit b079072
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
13 changes: 12 additions & 1 deletion Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private void SetupKnifeMenu()
{
if (Utility.IsPlayerValid(player))
{
if (player == null) return;
var knifeName = option.Text;
var knifeKey = knivesOnly.FirstOrDefault(x => x.Value == knifeName).Key;
if (!string.IsNullOrEmpty(knifeKey))
Expand All @@ -127,14 +128,24 @@ private void SetupKnifeMenu()
player!.PrintToChat(Utility.ReplaceTags(temp));
}
PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Index = (int)player.Index,
SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(),
Name = player?.PlayerName,
IpAddress = player?.IpAddress?.Split(":")[0]
};
g_playersKnife[(int)player!.Index] = knifeKey;
if (player!.PawnIsAlive && g_bCommandsAllowed)
{
RefreshWeapons(player);
}
if (weaponSync != null)
Task.Run(() => weaponSync.SyncKnifeToDatabase((int)player.Index, knifeKey));
Task.Run(async () => await weaponSync.SyncKnifeToDatabase(playerInfo, knifeKey));
}
}
};
Expand Down
2 changes: 2 additions & 0 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private HookResult OnItemPickup(EventItemPickup @event, GameEventInfo info)

RemovePlayerKnife(player, true);
AddTimer(0.3f, () => GiveKnifeToPlayer(player));

}
}
return HookResult.Continue;
Expand Down Expand Up @@ -207,6 +208,7 @@ private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
return HookResult.Continue;
}


private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
g_bCommandsAllowed = false;
Expand Down
5 changes: 4 additions & 1 deletion WeaponAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;

namespace WeaponPaints
{
Expand Down Expand Up @@ -81,7 +82,7 @@ internal static bool PlayerHasKnife(CCSPlayerController? player)
{
if (!_config.Additional.KnifeEnabled) return false;

if (player == null || !player.IsValid || !player.PlayerPawn.IsValid)
if (player == null || !player.IsValid || player.PlayerPawn == null || !player.PlayerPawn.IsValid || !player.PawnIsAlive)
{
return false;
}
Expand Down Expand Up @@ -221,6 +222,7 @@ internal void RefreshWeapons(CCSPlayerController? player)
}
catch (Exception ex)
{
Logger.LogWarning("Refreshing weapons exception");
Console.WriteLine("[WeaponPaints] Refreshing weapons exception");
Console.WriteLine(ex.Message);
}
Expand All @@ -240,6 +242,7 @@ internal void RemovePlayerKnife(CCSPlayerController? player, bool force = false)
if (weapons != null && weapons.Count > 0)
{
CCSPlayer_ItemServices service = new CCSPlayer_ItemServices(player.PlayerPawn.Value.ItemServices.Handle);
//var dropWeapon = VirtualFunction.CreateVoid<nint, nint>(service.Handle, GameData.GetOffset("CCSPlayer_ItemServices_DropActivePlayerWeapon"));

foreach (var weapon in weapons)
{
Expand Down
6 changes: 5 additions & 1 deletion WeaponPaints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Cvars;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Logging;

namespace WeaponPaints;

Expand Down Expand Up @@ -142,7 +143,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
public override string ModuleAuthor => "Nereziel & daffyy";
public override string ModuleDescription => "Skin and knife selector, standalone and web-based";
public override string ModuleName => "WeaponPaints";
public override string ModuleVersion => "1.3c";
public override string ModuleVersion => "1.3d";
public static WeaponPaintsConfig GetWeaponPaintsConfig()
{
return _config;
Expand Down Expand Up @@ -203,6 +204,7 @@ public void OnConfigParsed(WeaponPaintsConfig config)
{
if (config.DatabaseHost.Length < 1 || config.DatabaseName.Length < 1 || config.DatabaseUser.Length < 1)
{
Logger.LogError("You need to setup Database credentials in config!");
throw new Exception("[WeaponPaints] You need to setup Database credentials in config!");
}
}
Expand Down Expand Up @@ -246,10 +248,12 @@ private void GlobalShareConnect()
}
else
{
Logger.LogError("Unable to retrieve serverid from GlobalShare!");
throw new Exception("[WeaponPaints] Unable to retrieve serverid from GlobalShare!");
}
}

Logger.LogInformation("GlobalShare ONLINE!");
Console.WriteLine("[WeaponPaints] GlobalShare ONLINE");
}
}
9 changes: 3 additions & 6 deletions WeaponSynchronization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,17 @@ internal async Task GetWeaponPaintsFromDatabase(PlayerInfo player)
}
}

internal async Task SyncKnifeToDatabase(int playerIndex, string knife)
internal async Task SyncKnifeToDatabase(PlayerInfo player, string knife)
{
if (!_config.Additional.KnifeEnabled) return;
try
{
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid) return;
if (player.AuthorizedSteamID == null) return;
string steamId = player.AuthorizedSteamID.SteamId64.ToString();
if (player.SteamId == null || player.Index == 0) return;

using var connection = new MySqlConnection(_databaseConnectionString);
await connection.OpenAsync();
string query = "INSERT INTO `wp_player_knife` (`steamid`, `knife`) VALUES(@steamid, @newKnife) ON DUPLICATE KEY UPDATE `knife` = @newKnife";
await connection.ExecuteAsync(query, new { steamid = steamId, newKnife = knife });
await connection.ExecuteAsync(query, new { steamid = player.SteamId, newKnife = knife });
await connection.CloseAsync();
}
catch (Exception e)
Expand Down

0 comments on commit b079072

Please sign in to comment.