forked from doozzik/DropManager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DropManager.cs
379 lines (335 loc) · 16.2 KB
/
DropManager.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using Rocket.Core.Plugins;
using Rocket.Unturned.Player;
using Rocket.Core.Logging;
using Rocket.Unturned.Events;
using SDG.Unturned;
using System;
using System.Collections.Generic;
using Rocket.API;
namespace DropManager
{
public class DropManager : RocketPlugin<DropManagerConfiguration>
{
private readonly int _invPages = PlayerInventory.PAGES - 1;
private readonly Random _rand = new Random();
protected override void Load()
{
UnturnedPlayerEvents.OnPlayerDeath += Drop;
}
protected override void Unload()
{
UnturnedPlayerEvents.OnPlayerDeath -= Drop;
}
private void Drop(UnturnedPlayer player, EDeathCause cause, ELimb limb, Steamworks.CSteamID murderer)
{
bool showWarnings = Configuration.Instance.ShowWarnings;
string leftOtherDrop = Configuration.Instance.LeftOtherDrop;
string blackListIds = Configuration.Instance.BlackListIds;
if (player.HasPermission("dropmanager.alwaysclear")) // if we want to clear players inventory before death
{
ClearAllItems(player, showWarnings);
ClearAllClothes(player, showWarnings);
}
else
{
List<ushort> blackList = new List<ushort>();
BlackListController(blackListIds, blackList, showWarnings); // convert string to ushort list
ClearBlackListedItems(player, blackList, showWarnings); // remove only items, which contains in blacklist
Logger.Log(blackList.Count.ToString());
int allItemsAmount = 0;
for (byte page = 0; page < _invPages; page++)
{
allItemsAmount += player.Player.inventory.getItemCount(page); // set allItemsAmount
}
var amountOfItemsToClear = CalculateAmountOfRandomItemsToClear(player, allItemsAmount, leftOtherDrop, showWarnings);
if (amountOfItemsToClear == allItemsAmount && allItemsAmount != 0)
{
ClearAllItems(player, showWarnings);
ClearAllClothes(player, showWarnings);
}
else
{
ClearRandomItems(player, amountOfItemsToClear, showWarnings);
}
DropAllItems(player, showWarnings); // we will drop all items for get a free space to new items
ClearBlackListedClothes(player, blackList, showWarnings);
}
AddItems(player, showWarnings);
}
private void BlackListController(string configBlackList, List<ushort> blackList, bool showWarnings)
{
configBlackList = configBlackList.Replace(" ", "");
string[] array = configBlackList.Split(',');
foreach (string str in array)
{
try
{
blackList.Add(Convert.ToUInt16(str));
}
catch (Exception e)
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Cant convert your BlackListIds string to integer.");
Logger.LogWarning(@"[DropManager] Warning: You can use only: numbers 0-9, whitespaces ("" ""), and commas ("","")");
Logger.LogWarning(@"[DropManager] Warning: For example: 12, 28, 300, 301, 312");
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
}
}
}
}
private void ClearAllItems(UnturnedPlayer player, bool showWarnings)
{
for (byte page = 0; page < _invPages; page++)
{
byte itemCount = player.Player.inventory.getItemCount(page);
for (byte index = 0; index < itemCount; index++)
{
try
{
player.Player.inventory.removeItem(page, index);
index--;
itemCount--;
}
catch (Exception e)
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Cant clean inventory for player " + player.CharacterName);
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
}
}
}
}
}
private void ClearBlackListedItems(UnturnedPlayer player, List<ushort> blackList, bool showWarnings)
{
for (byte page = 0; page < _invPages; page++)
{
byte itemCount = player.Player.inventory.getItemCount(page);
for (byte index = 0; index < itemCount; index++)
{
ushort id = player.Player.inventory.getItem(page, index).item.id;
if (blackList.Contains(id))
{
try
{
player.Player.inventory.removeItem(page, index);
index--;
itemCount--;
}
catch (Exception e)
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Cant clean BlackListed item for player " + player.CharacterName);
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
}
}
}
}
}
}
// Thanks to ZaupClearInventoryLib
private void ClearAllClothes(UnturnedPlayer player, bool showWarnings)
{
try
{
player.Player.clothing.askWearBackpack(0, 0, new byte[0], true);
ClearAllItems(player, showWarnings);
player.Player.clothing.askWearGlasses(0, 0, new byte[0], true);
ClearAllItems(player, showWarnings);
player.Player.clothing.askWearHat(0, 0, new byte[0], true);
ClearAllItems(player, showWarnings);
player.Player.clothing.askWearMask(0, 0, new byte[0], true);
ClearAllItems(player, showWarnings);
player.Player.clothing.askWearPants(0, 0, new byte[0], true);
ClearAllItems(player, showWarnings);
player.Player.clothing.askWearShirt(0, 0, new byte[0], true);
ClearAllItems(player, showWarnings);
player.Player.clothing.askWearVest(0, 0, new byte[0], true);
ClearAllItems(player, showWarnings);
}
catch (Exception e)
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Cant clean clothes for player " + player.CharacterName);
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
}
}
}
private void ClearBlackListedClothes(UnturnedPlayer player, List<ushort> blackList, bool showWarnings)
{
player.Player.clothing.askWearBackpack(0, 0, new byte[0], true);
ClearBlackListedItems(player, blackList, showWarnings);
DropAllItems(player, showWarnings);
player.Player.clothing.askWearGlasses(0, 0, new byte[0], true);
ClearBlackListedItems(player, blackList, showWarnings);
DropAllItems(player, showWarnings);
player.Player.clothing.askWearHat(0, 0, new byte[0], true);
ClearBlackListedItems(player, blackList, showWarnings);
DropAllItems(player, showWarnings);
player.Player.clothing.askWearMask(0, 0, new byte[0], true);
ClearBlackListedItems(player, blackList, showWarnings);
DropAllItems(player, showWarnings);
player.Player.clothing.askWearPants(0, 0, new byte[0], true);
ClearBlackListedItems(player, blackList, showWarnings);
DropAllItems(player, showWarnings);
player.Player.clothing.askWearShirt(0, 0, new byte[0], true);
ClearBlackListedItems(player, blackList, showWarnings);
DropAllItems(player, showWarnings);
player.Player.clothing.askWearVest(0, 0, new byte[0], true);
ClearBlackListedItems(player, blackList, showWarnings);
DropAllItems(player, showWarnings);
}
private void ClearRandomItems(UnturnedPlayer player, int amount, bool showWarnings)
{
while (amount > 0)
{
byte page = Convert.ToByte(_rand.Next(0, _invPages));
byte itemsCountOnPage = player.Player.inventory.getItemCount(page);
if (itemsCountOnPage > 0)
{
byte index = Convert.ToByte(_rand.Next(0, itemsCountOnPage));
try
{
player.Player.inventory.removeItem(page, index);
amount--;
}
catch (Exception e)
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Cant ClearRandomItem for player " + player.CharacterName);
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
}
}
}
}
}
private int CalculateAmountOfRandomItemsToClear(UnturnedPlayer player, int allItemsAmount, string percentage, bool showWarnings)
{
string sourcePercentage = percentage;
percentage = percentage.Trim();
percentage = percentage.Trim('%');
int finalPercentage;
if (percentage == "true")
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Seems, like you have updated this plugin, but dont remove config before this.");
Logger.LogWarning(@"[DropManager] Warning: Cant CalculateAmountOfRandomItemsToClear.");
Logger.LogWarning(@"[DropManager] Warning: You set DropPercentage to: " + sourcePercentage);
Logger.LogWarning(@"[DropManager] Warning: DropPercentage can be, for example: 0% (clear all drop), 15%, 50%, 100% (left all drop)");
Logger.LogWarning(@"[DropManager] Warning: We will set DropPercentage to: 100%");
}
finalPercentage = 100;
}
if (percentage == "false")
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Seems, like you have updated this plugin, but dont remove config before this.");
Logger.LogWarning(@"[DropManager] Warning: Cant CalculateAmountOfRandomItemsToClear.");
Logger.LogWarning(@"[DropManager] Warning: You set DropPercentage to: " + sourcePercentage);
Logger.LogWarning(@"[DropManager] Warning: DropPercentage can be, for example: 0% (clear all drop), 15%, 50%, 100% (left all drop)");
Logger.LogWarning(@"[DropManager] Warning: We will set DropPercentage to: 0%");
}
finalPercentage = 0;
}
try
{
finalPercentage = Convert.ToInt32(percentage);
}
catch (Exception e)
{
Logger.LogWarning(@"[DropManager] Warning: Cant CalculateAmountOfRandomItemsToClear: cant convert your value to integer");
Logger.LogWarning(@"[DropManager] Warning: You set DropPercentage to: " + sourcePercentage);
Logger.LogWarning(@"[DropManager] Warning: DropPercentage can be, for example: 0% (clear all drop), 15%, 50%, 100% (left all drop)");
Logger.LogWarning(@"[DropManager] Warning: We will set DropPercentage to: 100%");
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
finalPercentage = 100;
}
if (finalPercentage < 0 || finalPercentage > 100)
{
Logger.LogWarning(@"[DropManager] Warning: Cant CalculateAmountOfRandomItemsToClear: your value is less than 0% or bigger than 100%");
Logger.LogWarning(@"[DropManager] Warning: You set DropPercentage to: " + sourcePercentage);
Logger.LogWarning(@"[DropManager] Warning: DropPercentage can be, for example: 0% (clear all drop), 15%, 50%, 100% (left all drop)");
Logger.LogWarning(@"[DropManager] Warning: We will set DropPercentage to: 100%");
finalPercentage = 100;
}
if (finalPercentage == 100)
{
return 0;
}
if (finalPercentage == 0)
{
return allItemsAmount;
}
return Convert.ToInt32(allItemsAmount - Math.Ceiling(allItemsAmount * finalPercentage / (double)100));
}
private void DropAllItems(UnturnedPlayer player, bool showWarnings)
{
for (byte page = 0; page < _invPages; page++)
{
byte itemCount = player.Player.inventory.getItemCount(page);
for (byte index = 0; index < itemCount; index++)
{
byte posX = player.Player.inventory.getItem(page, index).x;
byte posY = player.Player.inventory.getItem(page, index).y;
try
{
player.Player.inventory.askDropItem(player.CSteamID, page, posX, posY);
index--;
itemCount--;
}
catch (Exception e)
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Cant drop item for player " + player.CharacterName);
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
}
}
}
}
}
private void AddItems(UnturnedPlayer player, bool showWarnings)
{
foreach (Item item in Configuration.Instance.Items) // maybe hard to understand, sorry
{
int random = _rand.Next(item.Min, item.Max + 1);
try
{
SDG.Unturned.Item itemToAdd = new SDG.Unturned.Item(item.ID, true);
for (int i = random; i > 0; i--)
{
try
{
player.Player.inventory.tryAddItem(itemToAdd, true);
}
catch (Exception e)
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Cant add item to player " + player.CharacterName);
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
}
}
DropAllItems(player, showWarnings);
}
}
catch (Exception e)
{
if (showWarnings)
{
Logger.LogWarning(@"[DropManager] Warning: Cant find item by this id: " + item.ID);
Logger.LogWarning(@"[DropManager] Warning: Check config and site, where you get Unturned items id list");
Logger.LogWarning(@"[DropManager] Warning: Full problem description: " + e.Message);
}
}
}
}
}
}