mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-03-28 22:57:45 +00:00
Update WeaponPaints.cs
add async mysql query add command !wp with 2min cooldown (for now hardcoded)
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
using CounterStrikeSharp.API;
|
using CounterStrikeSharp.API;
|
||||||
using CounterStrikeSharp.API.Core;
|
using CounterStrikeSharp.API.Core;
|
||||||
|
using CounterStrikeSharp.API.Core.Attributes.Registration;
|
||||||
|
using CounterStrikeSharp.API.Modules.Commands;
|
||||||
using CounterStrikeSharp.API.Modules.Entities;
|
using CounterStrikeSharp.API.Modules.Entities;
|
||||||
|
using CounterStrikeSharp.API.Modules.Memory;
|
||||||
using Nexd.MySQL;
|
using Nexd.MySQL;
|
||||||
|
|
||||||
namespace WeaponPaints;
|
namespace WeaponPaints;
|
||||||
@@ -9,8 +12,9 @@ public class WeaponPaints : BasePlugin
|
|||||||
public override string ModuleName => "WeaponPaints";
|
public override string ModuleName => "WeaponPaints";
|
||||||
public override string ModuleDescription => "Connector for web-based player chosen wepaon paints.";
|
public override string ModuleDescription => "Connector for web-based player chosen wepaon paints.";
|
||||||
public override string ModuleAuthor => "Nereziel";
|
public override string ModuleAuthor => "Nereziel";
|
||||||
public override string ModuleVersion => "0.4";
|
public override string ModuleVersion => "0.5";
|
||||||
MySqlDb? MySql = null;
|
MySqlDb? MySql = null;
|
||||||
|
public DateTime[] commandCooldown = new DateTime[Server.MaxPlayers];
|
||||||
private Dictionary<ulong, Dictionary<nint, int>> g_playersSkins = new Dictionary<ulong, Dictionary<nint, int>>();
|
private Dictionary<ulong, Dictionary<nint, int>> g_playersSkins = new Dictionary<ulong, Dictionary<nint, int>>();
|
||||||
|
|
||||||
public override void Load(bool hotReload)
|
public override void Load(bool hotReload)
|
||||||
@@ -18,10 +22,21 @@ public class WeaponPaints : BasePlugin
|
|||||||
new Cfg().CheckConfig(ModuleDirectory);
|
new Cfg().CheckConfig(ModuleDirectory);
|
||||||
MySql = new MySqlDb(Cfg.config.DatabaseHost!, Cfg.config.DatabaseUser!, Cfg.config.DatabasePassword!, Cfg.config.DatabaseName!, (int)Cfg.config.DatabasePort);
|
MySql = new MySqlDb(Cfg.config.DatabaseHost!, Cfg.config.DatabaseUser!, Cfg.config.DatabasePassword!, Cfg.config.DatabaseName!, (int)Cfg.config.DatabasePort);
|
||||||
RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawned);
|
RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawned);
|
||||||
RegisterListener<Listeners.OnClientPutInServer>(OnClientConnect);
|
RegisterListener<Listeners.OnClientConnect>(OnClientConnect);
|
||||||
|
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);
|
||||||
|
}
|
||||||
|
private void OnClientConnect(int playerSlot, string name, string ipAddress)
|
||||||
|
{
|
||||||
|
int slot = playerSlot;
|
||||||
|
Server.NextFrame(() =>
|
||||||
|
{
|
||||||
|
Task.Run(() => GetWeaponPaintsFromDatabase(slot));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
private void OnClientDisconnect(int playerSlot)
|
||||||
|
{
|
||||||
|
// Clean up after player
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnEntitySpawned(CEntityInstance entity)
|
private void OnEntitySpawned(CEntityInstance entity)
|
||||||
{
|
{
|
||||||
var designerName = entity.DesignerName;
|
var designerName = entity.DesignerName;
|
||||||
@@ -34,15 +49,10 @@ public class WeaponPaints : BasePlugin
|
|||||||
if (!weapon.IsValid) return;
|
if (!weapon.IsValid) return;
|
||||||
var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex((int)weapon.OwnerEntity.Value.EntityIndex!.Value.Value));
|
var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex((int)weapon.OwnerEntity.Value.EntityIndex!.Value.Value));
|
||||||
if (!pawn.IsValid) return;
|
if (!pawn.IsValid) return;
|
||||||
|
|
||||||
var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value;
|
var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value;
|
||||||
|
|
||||||
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
|
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
|
||||||
|
|
||||||
if (player == null || !player.IsValid) return;
|
if (player == null || !player.IsValid) return;
|
||||||
|
|
||||||
var steamId = new SteamID(player.SteamID);
|
var steamId = new SteamID(player.SteamID);
|
||||||
|
|
||||||
if (g_playersSkins.TryGetValue(steamId.SteamId64, out var weaponIDs))
|
if (g_playersSkins.TryGetValue(steamId.SteamId64, out var weaponIDs))
|
||||||
{
|
{
|
||||||
if (weaponIDs.TryGetValue(weapon.AttributeManager.Item.ItemDefinitionIndex, out var weaponPaint))
|
if (weaponIDs.TryGetValue(weapon.AttributeManager.Item.ItemDefinitionIndex, out var weaponPaint))
|
||||||
@@ -52,43 +62,58 @@ public class WeaponPaints : BasePlugin
|
|||||||
weapon.FallbackPaintKit = weaponPaint;
|
weapon.FallbackPaintKit = weaponPaint;
|
||||||
weapon.FallbackSeed = 0;
|
weapon.FallbackSeed = 0;
|
||||||
weapon.FallbackWear = 0.0001f;
|
weapon.FallbackWear = 0.0001f;
|
||||||
|
if (weapon.AttributeManager.Item.AccountID > 0 && weapon.CBodyComponent != null && weapon.CBodyComponent.SceneNode != null)
|
||||||
|
{
|
||||||
|
var skeleton = GetSkeletonInstance(weapon.CBodyComponent.SceneNode);
|
||||||
|
skeleton.ModelState.MeshGroupMask = 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
private static void Log(string message)
|
[ConsoleCommand("css_wp", "refreshskins")]
|
||||||
|
public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
|
||||||
{
|
{
|
||||||
Console.BackgroundColor = ConsoleColor.DarkGray;
|
if (player == null) return;
|
||||||
Console.ForegroundColor = ConsoleColor.DarkMagenta;
|
int playerSlot = (int)player.EntityIndex!.Value.Value - 1;
|
||||||
Console.WriteLine(message);
|
if (DateTime.UtcNow >= commandCooldown[playerSlot].AddMinutes(2))
|
||||||
Console.ResetColor();
|
{
|
||||||
|
commandCooldown[playerSlot] = DateTime.UtcNow;
|
||||||
|
Task.Run(async () => await GetWeaponPaintsFromDatabase(playerSlot));
|
||||||
|
player.PrintToChat("Refreshed weapon paints.");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
private void OnClientConnect(int playerSlot)
|
player.PrintToChat("You can't refresh weapon paints right now.");
|
||||||
|
}
|
||||||
|
public CSkeletonInstance GetSkeletonInstance(CGameSceneNode node)
|
||||||
|
{
|
||||||
|
Func<nint, nint> GetSkeletonInstance = VirtualFunction.Create<nint, nint>(node.Handle, 8);
|
||||||
|
return new CSkeletonInstance(GetSkeletonInstance(node.Handle));
|
||||||
|
}
|
||||||
|
private async Task GetWeaponPaintsFromDatabase(int playerSlot)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot);
|
CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot);
|
||||||
|
|
||||||
if (player == null || !player.IsValid) return;
|
if (player == null || !player.IsValid) return;
|
||||||
|
|
||||||
var steamId = new SteamID(player.SteamID);
|
var steamId = new SteamID(player.SteamID);
|
||||||
|
|
||||||
MySqlQueryCondition conditions = new MySqlQueryCondition()
|
MySqlQueryCondition conditions = new MySqlQueryCondition()
|
||||||
.Add("steamid", "=", steamId.SteamId64.ToString());
|
.Add("steamid", "=", steamId.SteamId64.ToString());
|
||||||
|
|
||||||
MySqlQueryResult result = MySql!.Table("wp_player_skins").Where(conditions).Select();
|
MySqlQueryResult result = await MySql!.Table("wp_player_skins").Where(conditions).SelectAsync();
|
||||||
|
|
||||||
result.ToList().ForEach(pair =>
|
result.ToList().ForEach(pair =>
|
||||||
{
|
{
|
||||||
int weponId = result.Get<int>(pair.Key, "weapon_defindex");
|
int weaponId = result.Get<int>(pair.Key, "weapon_defindex");
|
||||||
int weponPaint = result.Get<int>(pair.Key, "weapon_paint_id");
|
int weaponPaint = result.Get<int>(pair.Key, "weapon_paint_id");
|
||||||
|
|
||||||
if (!g_playersSkins.ContainsKey(steamId.SteamId64))
|
if (!g_playersSkins.ContainsKey(steamId.SteamId64))
|
||||||
{
|
{
|
||||||
g_playersSkins[steamId.SteamId64] = new Dictionary<nint, int>();
|
g_playersSkins[steamId.SteamId64] = new Dictionary<nint, int>();
|
||||||
}
|
}
|
||||||
|
|
||||||
g_playersSkins[steamId.SteamId64][weponId] = weponPaint;
|
g_playersSkins[steamId.SteamId64][weaponId] = weaponPaint;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user