source: binary-improvements2/7dtd-server-fixes/src/PersistentData/Inventory.cs@ 379

Last change on this file since 379 was 351, checked in by alloc, 6 years ago

Fixed game version compatibility of GamePrefs
Code style cleanup (mostly argument names)

File size: 2.7 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace AllocsFixes.PersistentData {
5 [Serializable]
6 public class Inventory {
7 public List<InvItem> bag;
8 public List<InvItem> belt;
9 public InvItem[] equipment;
10
11 public Inventory () {
12 bag = new List<InvItem> ();
13 belt = new List<InvItem> ();
14 equipment = null;
15 }
16
17 public void Update (PlayerDataFile _pdf) {
18 lock (this) {
19 //Log.Out ("Updating player inventory - player id: " + pdf.id);
20 ProcessInv (bag, _pdf.bag, _pdf.id);
21 ProcessInv (belt, _pdf.inventory, _pdf.id);
22 ProcessEqu (_pdf.equipment, _pdf.id);
23 }
24 }
25
26 private void ProcessInv (List<InvItem> _target, ItemStack[] _sourceFields, int _id) {
27 _target.Clear ();
28 for (int i = 0; i < _sourceFields.Length; i++) {
29 InvItem item = CreateInvItem (_sourceFields [i].itemValue, _sourceFields [i].count, _id);
30 if (item != null && _sourceFields [i].itemValue.Modifications != null) {
31 ProcessParts (_sourceFields [i].itemValue.Modifications, item, _id);
32 }
33
34 _target.Add (item);
35 }
36 }
37
38 private void ProcessEqu (Equipment _sourceEquipment, int _playerId) {
39 equipment = new InvItem[_sourceEquipment.GetSlotCount ()];
40 for (int i = 0; i < _sourceEquipment.GetSlotCount (); i++) {
41 equipment [i] = CreateInvItem (_sourceEquipment.GetSlotItem (i), 1, _playerId);
42 }
43 }
44
45 private void ProcessParts (ItemValue[] _parts, InvItem _item, int _playerId) {
46 InvItem[] itemParts = new InvItem[_parts.Length];
47 for (int i = 0; i < _parts.Length; i++) {
48 InvItem partItem = CreateInvItem (_parts [i], 1, _playerId);
49 if (partItem != null && _parts [i].Modifications != null) {
50 ProcessParts (_parts [i].Modifications, partItem, _playerId);
51 }
52
53 itemParts [i] = partItem;
54 }
55
56 _item.parts = itemParts;
57 }
58
59 private InvItem CreateInvItem (ItemValue _itemValue, int _count, int _playerId) {
60 if (_count <= 0 || _itemValue == null || _itemValue.Equals (ItemValue.None)) {
61 return null;
62 }
63
64 ItemClass itemClass = ItemClass.list [_itemValue.type];
65 int maxAllowed = itemClass.Stacknumber.Value;
66 string name = itemClass.GetItemName ();
67
68 if (_count > maxAllowed) {
69 Log.Out ("Player with ID " + _playerId + " has stack for \"" + name + "\" greater than allowed (" +
70 _count + " > " + maxAllowed + ")");
71 }
72
73 InvItem item;
74 if (_itemValue.HasQuality) {
75 item = new InvItem (name, _count, _itemValue.Quality, _itemValue.MaxUseTimes, _itemValue.UseTimes);
76 } else {
77 item = new InvItem (name, _count, -1, _itemValue.MaxUseTimes, _itemValue.UseTimes);
78 }
79
80 item.icon = itemClass.GetIconName ();
81
82 item.iconcolor = AllocsUtils.ColorToHex (itemClass.GetIconTint ());
83
84 return item;
85 }
86 }
87}
Note: See TracBrowser for help on using the repository browser.