forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2Item.cs
More file actions
265 lines (223 loc) · 7.63 KB
/
Copy pathL2Item.cs
File metadata and controls
265 lines (223 loc) · 7.63 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
using System;
using System.Linq;
using System.Threading.Tasks;
using L2dotNET.DataContracts;
using L2dotNET.DataContracts.Shared.Enums;
using L2dotNET.Models.Player;
using L2dotNET.Network.serverpackets;
using L2dotNET.Services.Contracts;
using L2dotNET.Tables;
using L2dotNET.Tools;
using L2dotNET.World;
using static L2dotNET.Models.Inventory.Inventory;
namespace L2dotNET.Models.Items
{
public class L2Item : L2Object
{
public ItemTemplate Template;
public int Count;
public short IsEquipped { get; set; }
public bool Equipped => IsEquipped > 0;
public int Enchant;
public int AugmentationId = 0;
public int Durability;
public ItemLocation Location;
public int PaperdollSlot = -1;
public int PetId = -1;
public int Dropper;
public int SlotLocation;
public bool ExistsInDb { get; set; }
public int OwnerId { get; set; }
public short AttrAttackType = -2;
public short AttrAttackValue = 0;
public bool Blocked = false;
public bool TempBlock = false;
private readonly ICrudService<ItemContract> _itemCrudService;
private readonly IdFactory _idFactory;
public L2Item(ICrudService<ItemContract> itemCrudService, IdFactory idFactory, ItemTemplate template , int objectId) : base(objectId)
{
_itemCrudService = itemCrudService;
_idFactory = idFactory;
ObjectId = objectId != 0 ? objectId : _idFactory.NextId();
Template = template;
Count = 1;
Location = ItemLocation.Void;
}
public void GenId()
{
ObjectId = _idFactory.NextId();
}
public void ChangeCount(int count, L2Player creator)
{
if (count == 0)
return;
if ((count > 0) && (Count > (int.MaxValue - count)))
Count = int.MaxValue;
else
Count = Count + count;
if (Count < 0)
Count = 0;
}
public void Unequip(L2Player owner)
{
PaperdollSlot = -1;
Location = ItemLocation.Inventory;
SlotLocation = -1;
PaperdollSlot = -1;
IsEquipped = 0;
}
public void Equip(L2Player owner)
{
Location = ItemLocation.Paperdoll;
SlotLocation = GetPaperdollIndex((int) Template.BodyPart);
PaperdollSlot = GetPaperdollIndex((int) Template.BodyPart);
IsEquipped = 1;
}
public void NotifyStats(L2Player owner)
{
}
private void TryEquipSecondary(L2Player owner) { }
public void DropMe(int x, int y, int z, L2Character dropper, L2Character killer, int seconds)
{
X = x;
Y = y;
Z = z;
DropItem pk = new DropItem(this);
if (dropper != null)
Dropper = dropper.ObjectId;
Location = ItemLocation.Void;
killer?.AddKnownObject(this, pk, true);
L2World.AddObject(this);
}
public void DropMe(int x, int y, int z)
{
DropMe(x, y, z, null, null, 0);
}
public override async Task OnActionAsync(L2Player player)
{
double dis = Calcs.CalculateDistance(this, player, true);
await player.SendMessageAsync($"{AsString()} dis {(int)dis}");
if (dis < 80)
{
foreach (L2Player o in KnownObjects.Values.OfType<L2Player>())
{
await o.SendPacketAsync(new GetItem(player.ObjectId, ObjectId, X, Y, Z));
await o.SendPacketAsync(new DeleteObject(ObjectId));
}
player.OnPickUp(this);
L2World.RemoveObject(this);
}
else
await player.CharMovement.MoveTo(X, Y, Z);
}
public override async Task OnForcedAttackAsync(L2Player player)
{
await player.SendActionFailedAsync();
}
public void UpdateDatabase()
{
if (ExistsInDb)
{
//if(OwnerId == 0 || Location == ItemLocation.Void || (Count == 0 && Location != ItemLocation.Lease))
// RemoveFromDb();
//else
UpdateInDb();
}
else
{
if ((OwnerId == 0) || (Location == ItemLocation.Void) || ((Count == 0) && (Location != ItemLocation.Lease)))
return;
InsertInDb();
ExistsInDb = true;
}
}
private void UpdateInDb()
{
ItemContract contract = MapItemModel();
_itemCrudService.Update(contract);
}
private void InsertInDb()
{
Location = ItemLocation.Inventory;
ItemContract contract = MapItemModel();
_itemCrudService.Add(contract);
}
private ItemContract MapItemModel()
{
ItemContract contract = new ItemContract
{
ObjectId = ObjectId,
ItemId = Template.ItemId,
Count = Count,
CustomType1 = CustomType1,
CustomType2 = CustomType2,
Enchant = Enchant,
LocationData = SlotLocation,
Location = Location,
CharacterId = OwnerId,
ManaLeft = 0,
Time = 0,
TimeOfUse = null
};
return contract;
}
private bool _lifeTimeEndEnabled;
private DateTime _lifeTimeEndTime;
public int CustomType1;
public int CustomType2;
public bool Soulshot = false,
Spiritshot = false,
BlessSpiritshot = false;
public int LifeTimeEnd()
{
if (!_lifeTimeEndEnabled)
return -9999;
TimeSpan ts = _lifeTimeEndTime - DateTime.Now;
return (int)ts.TotalSeconds;
}
public void AddLimitedHour(int hours)
{
if (_lifeTimeEndEnabled)
_lifeTimeEndTime = _lifeTimeEndTime.AddHours(hours);
else
{
_lifeTimeEndEnabled = true;
_lifeTimeEndTime = DateTime.Now.AddHours(hours);
}
}
public void SetLimitedHour(string str)
{
if (str == "-1")
return;
string[] x1 = str.Split(' ');
int yy = Convert.ToInt32(x1[0].Split('-')[0]);
int mm = Convert.ToInt32(x1[0].Split('-')[1]);
int dd = Convert.ToInt32(x1[0].Split('-')[2]);
int hh = Convert.ToInt32(x1[1].Split('-')[0]);
int m = Convert.ToInt32(x1[1].Split('-')[1]);
int ss = Convert.ToInt32(x1[1].Split('-')[2]);
DateTime dt = new DateTime(yy, mm, dd, hh, m, ss);
if (dt <= DateTime.Now)
return;
_lifeTimeEndEnabled = true;
_lifeTimeEndTime = dt;
//TODO delete me
}
public bool IsEquipable()
{
return !(Template.BodyPart == 0);
}
public override string AsString()
{
return $"L2Item:{Template.ItemId}; count {Count}; enchant {Enchant}; id {ObjectId}";
}
public bool NotForTrade()
{
return !Template.Tradeable || (AugmentationId > 0) || (IsEquipped == 1);
}
public bool NotForSale()
{
return !Template.Tradeable || (IsEquipped == 1);
}
}
}