-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMenuHelper.cs
More file actions
67 lines (61 loc) · 2.28 KB
/
MenuHelper.cs
File metadata and controls
67 lines (61 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Menu;
namespace SimpleGunMenuPlugin;
public partial class SimpleGunMenuPlugin
{
private static class MenuHelper
{
private static Dictionary<string, Weapon> _weapons;
private static Dictionary<string, Weapon> _weaponCheckers;
static MenuHelper()
{
var res = WeaponHelper.LoadWeapons();
_weapons = res.Weapons;
_weaponCheckers = res.WeaponCheckers;
}
internal static void GetGuns(ChatMenu gunMenu, WeaponType? type = null)
{
Dictionary<string, Weapon> weapons = _weapons;
if (type != null)
{
weapons = _weapons.Where(x => x.Value.Type == type.Value)
.ToDictionary(x => x.Key, y => y.Value);
}
foreach (var item in weapons)
{
gunMenu.AddMenuOption(item.Key, GiveSelectedItem);
}
}
private static void GiveSelectedItem(CCSPlayerController player, ChatMenuOption option)
{
if (player == null
|| player.IsValid == false
|| player.IsBot == true
|| player?.PlayerPawn?.Value?.WeaponServices?.MyWeapons == null)
{
return;
}
if (_weapons.TryGetValue(option.Text, out var selectedWeapon))
{
RemoveCurrentWeapon(player, selectedWeapon);
player.GiveNamedItem(selectedWeapon.GiveName);
}
}
private static void RemoveCurrentWeapon(CCSPlayerController? player, Weapon selectedWeapon)
{
foreach (var weapon in player!.PlayerPawn.Value!.WeaponServices!.MyWeapons)
{
if (weapon.Value != null &&
string.IsNullOrWhiteSpace(weapon.Value.DesignerName) == false &&
weapon.Value.DesignerName != "[null]" &&
_weaponCheckers.TryGetValue(weapon.Value.DesignerName, out var currentWeapon))
{
if (currentWeapon.Type == selectedWeapon.Type)
{
weapon.Value.Remove();
}
}
}
}
}
}