Skip to content

Commit

Permalink
Update WeaponPaints.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
daffyyyy authored Nov 16, 2023
1 parent 118931f commit 9e2dac5
Showing 1 changed file with 107 additions and 24 deletions.
131 changes: 107 additions & 24 deletions WeaponPaints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@


namespace WeaponPaints;
[MinimumApiVersion(52)]
[MinimumApiVersion(50)]
public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{
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, and standalone for knife.";
public override string ModuleAuthor => "Nereziel";
public override string ModuleVersion => "0.8";
public override string ModuleVersion => "0.9";

public WeaponPaintsConfig Config { get; set; } = new();

Expand Down Expand Up @@ -78,6 +78,8 @@ public override void Load(bool hotReload)
//RegisterEventHandler<EventRoundPrestart>(OnRoundPreStart);
if (Config.Additional.KnifeEnabled)
SetupMenus();

RegisterCommands();
}
public void OnConfigParsed(WeaponPaintsConfig config)
{
Expand Down Expand Up @@ -113,6 +115,19 @@ private void RemoveGlobalExceptionHandler()
{
AppDomain.CurrentDomain.FirstChanceException -= this.GlobalExceptionHandler;
}
public void RegisterCommands()
{
AddCommand($"css_{Config.Additional.CommandSkin}", "Skins info", (player, info) => { if (player == null) return; OnCommandWS(player, info); });
AddCommand($"css_{Config.Additional.CommandRefresh}", "Skins refresh", (player, info) => { if (player == null) return; OnCommandRefresh(player, info); });
if (Config.Additional.CommandKillEnabled) {
AddCommand($"css_{Config.Additional.CommandKill}", "kill yourself", (player, info) => {
if(player == null || !player.IsValid || !player.PlayerPawn.IsValid)
return;

player.PlayerPawn.Value.CommitSuicide(true, false);
});
}
}
private void OnMapStart(string mapName)
{
if (!Config.Additional.KnifeEnabled) return;
Expand All @@ -131,6 +146,7 @@ private void OnClientPutInServer(int playerSlot)
{
if (Config.Additional.KnifeEnabled)
await GetKnifeFromDatabase(playerIndex);
if (Config.Additional.SkinEnabled)
await GetWeaponPaintsFromDatabase(playerIndex);
});
}
Expand All @@ -149,15 +165,18 @@ private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
return HookResult.Continue;
}

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

AddTimer(0.1f, () => RefreshPlayerKnife(player));
}

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"));
AddTimer(0.2f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot3"));
AddTimer(0.32f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot2"));
AddTimer(0.42f, () => NativeAPI.IssueClientCommand((int)player.EntityIndex!.Value.Value - 1, "slot1"));
}

return HookResult.Continue;
Expand Down Expand Up @@ -211,17 +230,18 @@ 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");
return;
}

if (!PlayerHasKnife(player))
if (g_playersKnife.TryGetValue((int)player.EntityIndex!.Value.Value, out var knife))
{
if (g_playersKnife.TryGetValue((int)player.EntityIndex!.Value.Value, out var knife))
player.GiveNamedItem(knife);
}
else
{
if (Config.Additional.GiveRandomKnife)
{
player.GiveNamedItem(knife);
Random random = new Random();
int index = random.Next(knifeTypes.Count);
player.GiveNamedItem(knifeTypes.Values.ElementAt(index));
}
else
{
Expand All @@ -231,8 +251,6 @@ 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 @@ -242,15 +260,75 @@ public void RemoveKnifeFromPlayer(CCSPlayerController player)
if (weapon.Value.DesignerName.Contains("knife"))
{
weapon.Value.Remove();
break;
return;
}
}
}
}
public void RefreshPlayerKnife(CCSPlayerController player)
{
if (!PlayerHasKnife(player))
GiveKnifeToPlayer(player);
}
// Unused method, only for testing
// public void RefreshPlayerKnife(CCSPlayerController player)
// {
// if (!Config.Additional.KnifeEnabled) return;
// // if (!g_playersKnife.ContainsKey((int)player.EntityIndex!.Value.Value)) return;

// // if (!PlayerHasKnife(player))
// // player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? "weapon_knife_t" : "weapon_knife");

// var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
// foreach (var weapon in weapons)
// {
// if (weapon.IsValid && weapon.Value.IsValid)
// {
// //if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59)
// if (weapon.Value.DesignerName.Contains("knife"))
// {
// weapon.Value.Remove();
// break;
// }
// }
// }

// weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
// foreach (var weapon in weapons)
// {
// if (weapon.IsValid && weapon.Value.IsValid)
// {
// //if (weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 42 || weapon.Value.AttributeManager.Item.ItemDefinitionIndex == 59)
// if (weapon.Value.DesignerName.Contains("knife"))
// {
// var temp = weapon.Value;
// var steamId = new SteamID(player.SteamID);
// if (!gPlayerWeaponPaints.ContainsKey(steamId.SteamId64)) return;
// if (!gPlayerWeaponPaints[steamId.SteamId64].ContainsKey(temp.AttributeManager.Item.ItemDefinitionIndex)) return;
// //Log($"Apply on {weapon.DesignerName}({weapon.AttributeManager.Item.ItemDefinitionIndex}) paint {gPlayerWeaponPaints[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} seed {gPlayerWeaponSeed[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} wear {gPlayerWeaponWear[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]}");
// temp.AttributeManager.Item.ItemID = 16384;
// temp.AttributeManager.Item.ItemIDLow = 16384 & 0xFFFFFFFF;
// temp.AttributeManager.Item.ItemIDHigh = temp.AttributeManager.Item.ItemIDLow >> 32;
// temp.FallbackPaintKit = gPlayerWeaponPaints[steamId.SteamId64][temp.AttributeManager.Item.ItemDefinitionIndex];
// temp.FallbackSeed = gPlayerWeaponSeed[steamId.SteamId64][temp.AttributeManager.Item.ItemDefinitionIndex];
// temp.FallbackWear = gPlayerWeaponWear[steamId.SteamId64][temp.AttributeManager.Item.ItemDefinitionIndex];
// break;
// }
// }
// }
// }

public bool PlayerHasKnife(CCSPlayerController player)
{
if (!Config.Additional.KnifeEnabled) return true;

if (!player.IsValid || !player.PlayerPawn.IsValid)
{
return true;
}

var weapons = player.PlayerPawn.Value.WeaponServices!.MyWeapons;
if (weapons == null) return false;
foreach (var weapon in weapons)
{
if (weapon.IsValid && weapon.Value.IsValid)
Expand All @@ -269,25 +347,30 @@ private void SetupMenus()
var giveItemMenu = new ChatMenu(ReplaceTags(Config.Messages.KnifeMenuTitle));
var handleGive = (CCSPlayerController player, ChatMenuOption option) =>
{
string temp = "";
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];
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenu)) {
string temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", option.Text);
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenu}".Replace("{KNIFE}", option.Text);
player.PrintToChat(ReplaceTags(temp));
}
if (!string.IsNullOrEmpty(Config.Messages.ChosenKnifeMenuKill) && Config.Additional.CommandKillEnabled) {
temp = $"{Config.Prefix} {Config.Messages.ChosenKnifeMenuKill}";
player.PrintToChat(ReplaceTags(temp));
}
Task.Run(() => SyncKnifeToDatabase((int)player.EntityIndex!.Value.Value, knife));
RemoveKnifeFromPlayer(player);
GiveKnifeToPlayer(player);
AddTimer(0.1f, () => GiveKnifeToPlayer(player));
}
};
foreach (var knife in knifeTypes)
{
giveItemMenu.AddMenuOption(knife.Key, handleGive);
}
AddCommand("css_knife", "Knife Menu", (player, info) => { if (player == null) return; ChatMenus.OpenMenu(player, giveItemMenu); });
AddCommand($"css_{Config.Additional.CommandKnife}", "Knife Menu", (player, info) => { if (player == null) return; ChatMenus.OpenMenu(player, giveItemMenu); });
}
[ConsoleCommand("css_wp", "refreshskins")]
// [ConsoleCommand($"css_{Config.Additional.CommandRefresh}", "refreshskins")]
public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
{
if (!Config.Additional.CommandWpEnabled || !Config.Additional.SkinEnabled) return;
Expand All @@ -311,7 +394,7 @@ public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
player.PrintToChat(ReplaceTags(temp));
}
}
[ConsoleCommand("css_ws", "weaponskins")]
// [ConsoleCommand($"css_{Config.Additional.CommandSkin}", "weaponskins")]
public void OnCommandWS(CCSPlayerController? player, CommandInfo command)
{
if (!Config.Additional.SkinEnabled) return;
Expand Down

0 comments on commit 9e2dac5

Please sign in to comment.