-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemSystem.cs
More file actions
463 lines (410 loc) · 20.1 KB
/
ItemSystem.cs
File metadata and controls
463 lines (410 loc) · 20.1 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
extern alias UnityEngineCoreModule;
using System;
using System.Collections.Generic;
using System.Linq;
using Rocket.Core.Logging;
using Rocket.Unturned.Enumerations;
using Rocket.Unturned.Player;
using SDG.Unturned;
namespace HordeServer
{
class ItemSystem
{
static public Dictionary<UnturnedPlayer, WeaponLoadout> primaryWeapon = [];
static public Dictionary<UnturnedPlayer, WeaponLoadout> secondaryWeapon = [];
static private List<KeyValuePair<UnturnedPlayer, Item>> itemSwapped = [];
// Player / tickrate
public static readonly Dictionary<UnturnedPlayer, uint> ignoredRefunds = [];
static bool SwapTickReset = false;
static bool ClearItemsNextTick = false;
static public List<KeyValuePair<UnturnedPlayer, WeaponLoadout>> weaponEquipNextTick = [];
static private List<KeyValuePair<UnturnedPlayer, Item>> weaponReplaceNextTick = [];
// Variable to ignore next player receive item handling
private static readonly List<UnturnedPlayer> weaponInventoryIgnoreNextTick = [];
// Player / tickrate for weaponInventoryIgnoreNextTick be removed, when tickrate is 0 it will be removed from both variables
private static readonly Dictionary<UnturnedPlayer, uint> weaponInventoryResetIgnoreNextTickOnNextTick = [];
private static List<UnturnedPlayer> refreshPrimaryWeaponNextTick = [];
private static List<UnturnedPlayer> refreshSecondaryWeaponNextTick = [];
private static List<KeyValuePair<UnturnedPlayer, Item>> removeItemNextTick = [];
static private void RemovePreviouslyAmmo(UnturnedPlayer player, int ammoId)
{
for (byte page = 0; page < PlayerInventory.PAGES; page++)
{
try
{
for (byte j = 0; j < player.Inventory.getItemCount(page); j++)
{
if (player.Inventory.getItem(page, j).item.id == ammoId)
{
player.Inventory.removeItem(page, j);
j--;
}
}
}
catch (Exception) { }
}
}
static public void OnInventoryAdded(UnturnedPlayer player, InventoryGroup inventoryGroup, byte inventoryIndex, ItemJar P)
{
if (IsDisabledInventoryItem(P))
{
removeItemNextTick.Add(new(player, new(inventoryGroup, inventoryIndex, P)));
return;
}
// Check if the items is swapped
for (int i = itemSwapped.Count - 1; i >= 0; i--)
{
var entry = itemSwapped[i];
if (entry.Key == player && entry.Value.item.item.id == P.item.id)
{
// Check if the player removed the mains weapon to the inventory
foreach (WeaponLoadout weaponLoadout in HordeServerPlugin.instance!.Configuration.Instance.AvailableWeaponsToPurchase)
{
// Yes the player put the weapon in inventory
if (weaponLoadout.weapondId == P.item.id)
{
weaponReplaceNextTick.Add(new(player, new(inventoryGroup, inventoryIndex, P)));
break;
}
}
return;
}
}
// Checking if is some sort of powerup
foreach (PowerupLoadout powerUpLoadout in HordeServerPlugin.instance!.Configuration.Instance.AvailablePowerupsToPurchase)
{
if (powerUpLoadout.itemId == P.item.id)
{
for (byte page = 0; page < PlayerInventory.PAGES; page++)
{
try
{
for (byte j = 0; j < player.Inventory.getItemCount(page); j++)
{
if (player.Inventory.getItem(page, j).item.id == P.item.id)
{
PowerupSystem.GivePlayerPowerupByType(player, powerUpLoadout.powerupType);
player.Inventory.removeItem(page, j);
return;
}
}
}
catch (Exception) { }
}
}
}
// Ignore weapon receive for this event
if (weaponInventoryIgnoreNextTick.Contains(player))
{
weaponInventoryIgnoreNextTick.Remove(player);
return;
}
// In this situation the item is purchased
foreach (WeaponLoadout weaponLoadout in HordeServerPlugin.instance!.Configuration.Instance.AvailableWeaponsToPurchase)
{
// If is the first weapon give it the ammo for that weapon
if (P.item.id == weaponLoadout.weapondId)
{
// Remove previously equipped weapon
{
ItemJar? equippedWeapon;
if (weaponLoadout.primary)
equippedWeapon = player.Inventory.getItem(0, 0);
else
equippedWeapon = player.Inventory.getItem(1, 0);
// Check if exists (in theory is not necessary but...)
if (equippedWeapon != null)
{
// Check if the weapon id is different from the equipped id
if (equippedWeapon.item.id != P.item.id)
{
// Getting the ammo id
foreach (WeaponLoadout checkLoadout in HordeServerPlugin.instance!.Configuration.Instance.AvailableWeaponsToPurchase)
{
if (checkLoadout.weapondId == equippedWeapon.item.id)
{
// Removing the ammo and the weapon
RemovePreviouslyAmmo(player, checkLoadout.ammoId);
if (weaponLoadout.primary) player.Inventory.removeItem(0, 0);
else player.Inventory.removeItem(1, 0);
break;
}
}
}
}
}
// Ignore the next: 2 ticks, before detecting ammo refunds
// This is necessary on first buy so the system does not refund for the ammo received
// in first buy
ignoredRefunds.Remove(player);
ignoredRefunds.Add(player, 4);
// For some reason in this function the item add is not yet in the inventory, we need to equip in the next tick
// And for another reason the player receives ammo of the weapon in the horde purchase volume
// and we need to remove it for handling the ammo system in the next tick
weaponEquipNextTick.Add(new(player, weaponLoadout));
if (weaponLoadout.primary)
{
refreshPrimaryWeaponNextTick.Add(player);
PowerupSystem.ResetPlayerPrimaryPackAPunch(player);
}
else
{
refreshSecondaryWeaponNextTick.Add(player);
PowerupSystem.ResetPlayerSecondaryPackAPunch(player);
}
}
// If the player receives ammo, is because he already have the weapon lets refresh the inventory
if (P.item.id == weaponLoadout.ammoId)
{
if (weaponLoadout.ammoRefundValue > 0 && !ignoredRefunds.ContainsKey(player))
{
player.Experience += weaponLoadout.ammoRefundValue;
ChatManager.serverSendMessage(
HordeServerPlugin.instance!.Translate("refund_ammo", weaponLoadout.ammoRefundValue),
new UnityEngineCoreModule.UnityEngine.Color(0, 255, 0),
null,
player.SteamPlayer(),
EChatMode.SAY,
HordeServerPlugin.instance!.Configuration.Instance.ChatIconURL,
true
);
}
RemovePreviouslyAmmo(player, weaponLoadout.ammoId);
player.GiveItem(weaponLoadout.ammoId, weaponLoadout.ammoRefilQuantity);
}
}
}
static public void OnInventoryRemoved(UnturnedPlayer player, InventoryGroup inventoryGroup, byte inventoryIndex, ItemJar P)
{
// Future detection for swapped items
ItemJar? item = player.Inventory.getItem((byte)inventoryGroup, inventoryIndex);
if (item != null)
{
itemSwapped.Add(new KeyValuePair<UnturnedPlayer, Item>(player, new Item(inventoryGroup, inventoryIndex, P)));
SwapTickReset = true;
}
}
static public void OnItemDropped(PlayerInventory _, SDG.Unturned.Item __, ref bool ___)
{
ClearItemsNextTick = true;
}
static public void RefreshPrimaryLoadout(UnturnedPlayer player)
{
ItemJar item = player.Player.inventory.getItem(0, 0);
if (item?.item == null)
{
primaryWeapon.Remove(player);
return;
}
foreach (WeaponLoadout loadout in HordeServerPlugin.instance!.Configuration.Instance.AvailableWeaponsToPurchase)
{
if (loadout.weapondId == item.item.id && loadout.primary)
{
primaryWeapon[player] = loadout;
return;
}
}
}
static public void RefreshSecondaryLoadout(UnturnedPlayer player)
{
ItemJar item = player.Player.inventory.getItem(1, 0);
if (item?.item == null)
{
secondaryWeapon.Remove(player);
return;
}
foreach (WeaponLoadout loadout in HordeServerPlugin.instance!.Configuration.Instance.AvailableWeaponsToPurchase)
{
if (loadout.weapondId == item.item.id && !loadout.primary)
{
secondaryWeapon[player] = loadout;
return;
}
}
}
static public bool IsDisabledInventoryItem(ItemJar P)
{
if (HordeServerPlugin.instance!.Configuration.Instance.DisabledInventoryIds.Contains(P.item.id))
return true;
else
return false;
}
static public void Update()
{
{ // weaponInventoryResetIgnoreNextTickOnNextTick handler
// If is 0 remove it from both lists
foreach (KeyValuePair<UnturnedPlayer, uint> keyValuePair in weaponInventoryResetIgnoreNextTickOnNextTick.ToList())
{
if (keyValuePair.Value == 0)
{
weaponInventoryIgnoreNextTick.Remove(keyValuePair.Key);
weaponInventoryResetIgnoreNextTickOnNextTick.Remove(keyValuePair.Key);
}
else weaponInventoryResetIgnoreNextTickOnNextTick[keyValuePair.Key]--;
}
}
if (ignoredRefunds.Count > 0)
{
foreach (var key in ignoredRefunds.Keys.ToList())
{
ignoredRefunds[key]--;
if (ignoredRefunds[key] <= 0)
{
ignoredRefunds.Remove(key);
}
}
}
if (SwapTickReset)
itemSwapped = [];
if (ClearItemsNextTick)
ItemManager.askClearAllItems();
if (weaponEquipNextTick.Count > 0)
{
// Try equip items
for (int i = weaponEquipNextTick.Count - 1; i >= 0; i--)
{
var entry = weaponEquipNextTick[i];
UnturnedPlayer player = entry.Key;
bool equipSuccess = false;
for (byte page = 0; page < PlayerInventory.PAGES; page++)
{
try
{
byte itemCount = player.Inventory.getItemCount(page);
for (byte itemIndex = 0; itemIndex < itemCount; itemIndex++)
{
ItemJar? item = player.Inventory.getItem(page, itemIndex);
if (item == null) continue;
if (item.item.id == entry.Value.weapondId)
{
// If the weapon is not on primary or secondary slot, add to it
if (page != 0 && page != 1)
{
player.Inventory.removeItem(page, itemIndex);
if (entry.Value.primary) player.Inventory.tryAddItem(item.item, 0, 0, 0, 0);
else player.Inventory.tryAddItem(item.item, 0, 0, 1, 0);
equipSuccess = true;
}
// If the weapon is already on the primary or secondary slot equip it
else
{
// If is not a primary weapon and it is equipped on primary, we need to remove it
// and place on secondary
if (!entry.Value.primary && page == 0)
{
player.Inventory.removeItem(0, 0);
player.Inventory.removeItem(1, 0);
player.Inventory.tryAddItem(item.item, 0, 0, 1, 0);
break;
}
player.Inventory.player.equipment.tryEquip(page, item.x, item.y);
// Only give ammo if weaponInventory is not ignored
if (!weaponInventoryIgnoreNextTick.Contains(player))
{
RemovePreviouslyAmmo(player, entry.Value.ammoId);
player.GiveItem(entry.Value.ammoId, entry.Value.ammoRefilQuantity);
weaponEquipNextTick.RemoveAt(i);
// Why you give ammo 2 times in a row?
// Simple the game code is bugged, the first time you give ammo it will multiply by a strange amount
// The second time is normal
RemovePreviouslyAmmo(player, entry.Value.ammoId);
player.GiveItem(entry.Value.ammoId, entry.Value.ammoRefilQuantity);
}
else
{
weaponInventoryIgnoreNextTick.Remove(player);
weaponEquipNextTick.RemoveAt(i);
}
}
break;
}
}
}
catch (Exception) { }
if (equipSuccess) break;
}
}
// Insane bug or exploit treatment
if (weaponEquipNextTick.Count > 2)
{
Logger.LogWarning($"Something is strange in weapon equip system, {weaponEquipNextTick.Count}");
weaponEquipNextTick = [];
}
}
if (weaponReplaceNextTick.Count > 0)
{
for (int i = weaponReplaceNextTick.Count - 1; i >= 0; i--)
{
var entry = weaponReplaceNextTick[i];
weaponInventoryIgnoreNextTick.Remove(entry.Key);
weaponInventoryIgnoreNextTick.Add(entry.Key);
weaponInventoryResetIgnoreNextTickOnNextTick.Add(entry.Key, 2); // Ignore for 2 ticks
foreach (WeaponLoadout weaponLoadout in HordeServerPlugin.instance!.Configuration.Instance.AvailableWeaponsToPurchase)
{
if (weaponLoadout.weapondId == entry.Value.item.item.id)
{
byte[] itemMetadata = entry.Value.item.item.metadata;
entry.Key.Inventory.removeItem(entry.Value.inventoryPage, entry.Value.inventoryIndex);
SDG.Unturned.Item itemToRespawn = new(entry.Value.item.item.id, true)
{
amount = 1,
metadata = itemMetadata
};
if (weaponLoadout.primary) entry.Key.Inventory.tryAddItem(itemToRespawn, 0, 0, 0, 0);
else entry.Key.Inventory.tryAddItem(itemToRespawn, 0, 0, 1, 0);
weaponEquipNextTick.Add(new(entry.Key, weaponLoadout));
continue;
}
}
}
weaponReplaceNextTick = [];
}
if (refreshPrimaryWeaponNextTick.Count > 0)
{
foreach (UnturnedPlayer player in refreshPrimaryWeaponNextTick)
{
RefreshPrimaryLoadout(player);
}
refreshPrimaryWeaponNextTick = [];
}
if (refreshSecondaryWeaponNextTick.Count > 0)
{
foreach (UnturnedPlayer player in refreshSecondaryWeaponNextTick)
{
RefreshSecondaryLoadout(player);
}
refreshSecondaryWeaponNextTick = [];
}
if (removeItemNextTick.Count > 0)
{
foreach (KeyValuePair<UnturnedPlayer, Item> keyValue in removeItemNextTick)
keyValue.Key.Inventory.removeItem(keyValue.Value.inventoryPage, keyValue.Value.inventoryIndex);
removeItemNextTick = [];
}
SwapTickReset = false;
ClearItemsNextTick = false;
}
}
}
class Item
{
public byte inventoryPage;
public byte inventoryIndex;
public ItemJar item;
public Item(InventoryGroup inventoryPage, byte inventoryIndex, ItemJar item)
{
switch (inventoryPage)
{
case InventoryGroup.Primary: this.inventoryPage = 0; break;
case InventoryGroup.Secondary: this.inventoryPage = 1; break;
case InventoryGroup.Hands: this.inventoryPage = 2; break;
case InventoryGroup.Backpack: this.inventoryPage = 3; break;
case InventoryGroup.Vest: this.inventoryPage = 4; break;
case InventoryGroup.Shirt: this.inventoryPage = 5; break;
case InventoryGroup.Pants: this.inventoryPage = 6; break;
case InventoryGroup.Storage: this.inventoryPage = 7; break;
}
this.inventoryIndex = inventoryIndex;
this.item = item;
}
}