-
Notifications
You must be signed in to change notification settings - Fork 28
/
CommandKit.cs
178 lines (154 loc) · 6.58 KB
/
CommandKit.cs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using Rocket.API;
using Rocket.Core.Logging;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using System;
using System.Collections.Generic;
using System.Linq;
namespace fr34kyn01535.Kits
{
public class CommandKit : IRocketCommand
{
public string Help
{
get { return "Gives you a kit"; }
}
public string Name
{
get { return "kit"; }
}
public string Syntax
{
get { return "<kit>"; }
}
public bool RunFromConsole
{
get { return false; }
}
public List<string> Aliases
{
get { return new List<string>(); }
}
public AllowedCaller AllowedCaller
{
get { return Rocket.API.AllowedCaller.Player; }
}
public List<string> Permissions
{
get
{
return new List<string>() { "kits.kit" };
}
}
public void Execute(IRocketPlayer caller, string[] command)
{
UnturnedPlayer player = (UnturnedPlayer)caller;
if (command.Length != 1)
{
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_invalid_parameter"));
throw new WrongUsageOfCommandException(caller, this);
}
Kit kit = Kits.Instance.Configuration.Instance.Kits.Where(k => k.Name.ToLower() == command[0].ToLower()).FirstOrDefault();
if (kit == null)
{
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_not_found"));
throw new WrongUsageOfCommandException(caller, this);
}
bool hasPermissions = caller.HasPermission("kit.*") | caller.HasPermission("kit." + kit.Name.ToLower());
if (!hasPermissions)
{
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_no_permissions"));
throw new NoPermissionsForCommandException(caller, this);
}
KeyValuePair<string, DateTime> globalCooldown = Kits.GlobalCooldown.Where(k => k.Key == caller.ToString()).FirstOrDefault();
if (!globalCooldown.Equals(default(KeyValuePair<string, DateTime>)))
{
double globalCooldownSeconds = (DateTime.Now - globalCooldown.Value).TotalSeconds;
if (globalCooldownSeconds < Kits.Instance.Configuration.Instance.GlobalCooldown)
{
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_cooldown_command", (int)(Kits.Instance.Configuration.Instance.GlobalCooldown - globalCooldownSeconds)));
return;
}
}
KeyValuePair<string, DateTime> individualCooldown = Kits.InvididualCooldown.Where(k => k.Key == (caller.ToString() + kit.Name)).FirstOrDefault();
if (!individualCooldown.Equals(default(KeyValuePair<string, DateTime>)))
{
double individualCooldownSeconds = (DateTime.Now - individualCooldown.Value).TotalSeconds;
if (individualCooldownSeconds < kit.Cooldown)
{
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_cooldown_kit", (int)(kit.Cooldown - individualCooldownSeconds)));
return;
}
}
bool cancelBecauseNotEnoughtMoney = false;
if (kit.Money.HasValue && kit.Money.Value != 0)
{
Kits.ExecuteDependencyCode("Uconomy",(IRocketPlugin plugin) =>
{
Uconomy.Uconomy Uconomy = (Uconomy.Uconomy)plugin;
if ((Uconomy.Database.GetBalance(player.CSteamID.ToString()) + kit.Money.Value) < 0)
{
cancelBecauseNotEnoughtMoney = true;
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_no_money", Math.Abs(kit.Money.Value), Uconomy.Configuration.Instance.MoneyName, kit.Name));
return;
}
else
{
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_money", kit.Money.Value, Uconomy.Configuration.Instance.MoneyName, kit.Name));
}
Uconomy.Database.IncreaseBalance(player.CSteamID.ToString(), kit.Money.Value);
});
}
if (cancelBecauseNotEnoughtMoney)
{
throw new WrongUsageOfCommandException(caller, this);
}
foreach (KitItem item in kit.Items)
{
try
{
if (!player.GiveItem(item.ItemId, item.Amount))
{
Logger.Log(Kits.Instance.Translations.Instance.Translate("command_kit_failed_giving_item", player.CharacterName, item.ItemId, item.Amount));
}
}
catch (Exception ex)
{
Logger.LogException(ex, "Failed giving item "+item.ItemId+" to player");
}
}
if (kit.XP.HasValue && kit.XP != 0)
{
player.Experience += kit.XP.Value;
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_xp", kit.XP.Value, kit.Name));
}
if (kit.Vehicle.HasValue) {
try
{
player.GiveVehicle(kit.Vehicle.Value);
}
catch (Exception ex)
{
Logger.LogException(ex, "Failed giving vehicle " + kit.Vehicle.Value + " to player");
}
}
UnturnedChat.Say(caller, Kits.Instance.Translations.Instance.Translate("command_kit_success", kit.Name));
if (Kits.GlobalCooldown.ContainsKey(caller.ToString()))
{
Kits.GlobalCooldown[caller.ToString()] = DateTime.Now;
}
else
{
Kits.GlobalCooldown.Add(caller.ToString(), DateTime.Now);
}
if (Kits.GlobalCooldown.ContainsKey(caller.ToString()))
{
Kits.InvididualCooldown[caller.ToString() + kit.Name] = DateTime.Now;
}
else
{
Kits.InvididualCooldown.Add(caller.ToString() + kit.Name, DateTime.Now);
}
}
}
}