L2ItemInstance.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model;
  16. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.ADENA_ID;
  17. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.MAX_ADENA;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.util.concurrent.ScheduledFuture;
  22. import java.util.concurrent.locks.ReentrantLock;
  23. import java.util.logging.Level;
  24. import java.util.logging.LogRecord;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.GeoData;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.datatables.ItemTable;
  31. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  32. import com.l2jserver.gameserver.instancemanager.MercTicketManager;
  33. import com.l2jserver.gameserver.model.actor.L2Character;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  35. import com.l2jserver.gameserver.model.actor.knownlist.NullKnownList;
  36. import com.l2jserver.gameserver.model.quest.QuestState;
  37. import com.l2jserver.gameserver.network.SystemMessageId;
  38. import com.l2jserver.gameserver.network.serverpackets.DropItem;
  39. import com.l2jserver.gameserver.network.serverpackets.GetItem;
  40. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  41. import com.l2jserver.gameserver.network.serverpackets.SpawnItem;
  42. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  43. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  44. import com.l2jserver.gameserver.skills.funcs.Func;
  45. import com.l2jserver.gameserver.templates.item.L2Armor;
  46. import com.l2jserver.gameserver.templates.item.L2EtcItem;
  47. import com.l2jserver.gameserver.templates.item.L2Item;
  48. import com.l2jserver.gameserver.templates.item.L2Weapon;
  49. import com.l2jserver.gameserver.util.GMAudit;
  50. /**
  51. * This class manages items.
  52. *
  53. * @version $Revision: 1.4.2.1.2.11 $ $Date: 2005/03/31 16:07:50 $
  54. */
  55. public final class L2ItemInstance extends L2Object
  56. {
  57. protected static final Logger _log = Logger.getLogger(L2ItemInstance.class.getName());
  58. private static final Logger _logItems = Logger.getLogger("item");
  59. /** Enumeration of locations for item */
  60. public static enum ItemLocation {
  61. VOID,
  62. INVENTORY,
  63. PAPERDOLL,
  64. WAREHOUSE,
  65. CLANWH,
  66. PET,
  67. PET_EQUIP,
  68. LEASE,
  69. REFUND,
  70. MAIL
  71. }
  72. /** ID of the owner */
  73. private int _ownerId;
  74. /** ID of who dropped the item last, used for knownlist */
  75. private int _dropperObjectId = 0;
  76. /** Quantity of the item */
  77. private long _count;
  78. /** Initial Quantity of the item */
  79. private long _initCount;
  80. /** Remaining time (in miliseconds) */
  81. private long _time;
  82. /** Quantity of the item can decrease */
  83. private boolean _decrease = false;
  84. /** ID of the item */
  85. private final int _itemId;
  86. /** Object L2Item associated to the item */
  87. private final L2Item _item;
  88. /** Location of the item : Inventory, PaperDoll, WareHouse */
  89. private ItemLocation _loc;
  90. /** Slot where item is stored : Paperdoll slot, inventory order ...*/
  91. private int _locData;
  92. /** Level of enchantment of the item */
  93. private int _enchantLevel;
  94. /** Wear Item */
  95. private boolean _wear;
  96. /** Augmented Item */
  97. private L2Augmentation _augmentation=null;
  98. /** Shadow item */
  99. private int _mana=-1;
  100. private boolean _consumingMana = false;
  101. private static final int MANA_CONSUMPTION_RATE = 60000;
  102. /** Custom item types (used loto, race tickets) */
  103. private int _type1;
  104. private int _type2;
  105. private long _dropTime;
  106. private boolean _published = false;
  107. public static final int CHARGED_NONE = 0;
  108. public static final int CHARGED_SOULSHOT = 1;
  109. public static final int CHARGED_SPIRITSHOT = 1;
  110. public static final int CHARGED_BLESSED_SOULSHOT = 2; // It's a really exists? ;-)
  111. public static final int CHARGED_BLESSED_SPIRITSHOT = 2;
  112. /** Item charged with SoulShot (type of SoulShot) */
  113. private int _chargedSoulshot = CHARGED_NONE;
  114. /** Item charged with SpiritShot (type of SpiritShot) */
  115. private int _chargedSpiritshot = CHARGED_NONE;
  116. private boolean _chargedFishtshot = false;
  117. private boolean _protected;
  118. public static final int UNCHANGED = 0;
  119. public static final int ADDED = 1;
  120. public static final int REMOVED = 3;
  121. public static final int MODIFIED = 2;
  122. private int _lastChange = 2; //1 ??, 2 modified, 3 removed
  123. private boolean _existsInDb; // if a record exists in DB.
  124. private boolean _storedInDb; // if DB data is up-to-date.
  125. private final ReentrantLock _dbLock = new ReentrantLock();
  126. private Elementals _elementals = null;
  127. private ScheduledFuture<?> itemLootShedule = null;
  128. public ScheduledFuture<?> _lifeTimeTask;
  129. /**
  130. * Constructor of the L2ItemInstance from the objectId and the itemId.
  131. * @param objectId : int designating the ID of the object in the world
  132. * @param itemId : int designating the ID of the item
  133. */
  134. public L2ItemInstance(int objectId, int itemId)
  135. {
  136. super(objectId);
  137. setInstanceType(InstanceType.L2ItemInstance);
  138. _itemId = itemId;
  139. _item = ItemTable.getInstance().getTemplate(itemId);
  140. if (_itemId == 0 || _item == null)
  141. throw new IllegalArgumentException();
  142. super.setName(_item.getName());
  143. setCount(1);
  144. _loc = ItemLocation.VOID;
  145. _type1 = 0;
  146. _type2 = 0;
  147. _dropTime = 0;
  148. _mana = _item.getDuration();
  149. _time = _item.getTime() == -1 ? -1 : System.currentTimeMillis() + ((long)_item.getTime()*60*1000);
  150. scheduleLifeTimeTask();
  151. }
  152. /**
  153. * Constructor of the L2ItemInstance from the objetId and the description of the item given by the L2Item.
  154. * @param objectId : int designating the ID of the object in the world
  155. * @param item : L2Item containing informations of the item
  156. */
  157. public L2ItemInstance(int objectId, L2Item item)
  158. {
  159. super(objectId);
  160. setInstanceType(InstanceType.L2ItemInstance);
  161. _itemId = item.getItemId();
  162. _item = item;
  163. if (_itemId == 0)
  164. throw new IllegalArgumentException();
  165. super.setName(_item.getName());
  166. setCount(1);
  167. _loc = ItemLocation.VOID;
  168. _mana = _item.getDuration();
  169. _time = _item.getTime() == -1 ? -1 : System.currentTimeMillis() + ((long)_item.getTime()*60*1000);
  170. scheduleLifeTimeTask();
  171. }
  172. @Override
  173. public void initKnownList()
  174. {
  175. setKnownList(new NullKnownList(this));
  176. }
  177. /**
  178. * Remove a L2ItemInstance from the world and send server->client GetItem packets.<BR><BR>
  179. *
  180. * <B><U> Actions</U> :</B><BR><BR>
  181. * <li>Send a Server->Client Packet GetItem to player that pick up and its _knowPlayers member </li>
  182. * <li>Remove the L2Object from the world</li><BR><BR>
  183. *
  184. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World </B></FONT><BR><BR>
  185. *
  186. * <B><U> Assert </U> :</B><BR><BR>
  187. * <li> this instanceof L2ItemInstance</li>
  188. * <li> _worldRegion != null <I>(L2Object is visible at the beginning)</I></li><BR><BR>
  189. *
  190. * <B><U> Example of use </U> :</B><BR><BR>
  191. * <li> Do Pickup Item : PCInstance and Pet</li><BR><BR>
  192. *
  193. * @param player Player that pick up the item
  194. *
  195. */
  196. public final void pickupMe(L2Character player)
  197. {
  198. assert getPosition().getWorldRegion() != null;
  199. L2WorldRegion oldregion = getPosition().getWorldRegion();
  200. // Create a server->client GetItem packet to pick up the L2ItemInstance
  201. GetItem gi = new GetItem(this, player.getObjectId());
  202. player.broadcastPacket(gi);
  203. synchronized (this)
  204. {
  205. setIsVisible(false);
  206. getPosition().setWorldRegion(null);
  207. }
  208. // if this item is a mercenary ticket, remove the spawns!
  209. int itemId = getItemId();
  210. if (MercTicketManager.getInstance().getTicketCastleId(itemId) > 0)
  211. {
  212. MercTicketManager.getInstance().removeTicket(this);
  213. ItemsOnGroundManager.getInstance().removeObject(this);
  214. }
  215. if (!Config.DISABLE_TUTORIAL && (itemId == 57 || itemId == 6353))
  216. {
  217. L2PcInstance actor = player.getActingPlayer();
  218. if (actor != null)
  219. {
  220. QuestState qs = actor.getQuestState("255_Tutorial");
  221. if (qs != null)
  222. qs.getQuest().notifyEvent("CE"+itemId+"",null, actor);
  223. }
  224. }
  225. // outside of synchronized to avoid deadlocks
  226. // Remove the L2ItemInstance from the world
  227. L2World.getInstance().removeVisibleObject(this, oldregion);
  228. }
  229. /**
  230. * Sets the ownerID of the item
  231. * @param process : String Identifier of process triggering this action
  232. * @param owner_id : int designating the ID of the owner
  233. * @param creator : L2PcInstance Player requesting the item creation
  234. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  235. */
  236. public void setOwnerId(String process, int owner_id, L2PcInstance creator, L2Object reference)
  237. {
  238. setOwnerId(owner_id);
  239. if (Config.LOG_ITEMS)
  240. {
  241. LogRecord record = new LogRecord(Level.INFO, "CHANGE:" + process);
  242. record.setLoggerName("item");
  243. record.setParameters(new Object[]{this, creator, reference});
  244. _logItems.log(record);
  245. }
  246. if (creator != null)
  247. {
  248. if (creator.isGM())
  249. {
  250. String referenceName = "no-reference";
  251. if (reference != null)
  252. {
  253. referenceName = (reference.getName() != null?reference.getName():"no-name");
  254. }
  255. String targetName = (creator.getTarget() != null?creator.getTarget().getName():"no-target");
  256. if (Config.GMAUDIT)
  257. GMAudit.auditGMAction(creator.getName()+" ["+creator.getObjectId()+"]",
  258. process + "(id: "+getItemId()+" name: "+getName()+")", targetName,
  259. "L2Object referencing this action is: " + referenceName);
  260. }
  261. }
  262. }
  263. /**
  264. * Sets the ownerID of the item
  265. * @param owner_id : int designating the ID of the owner
  266. */
  267. public void setOwnerId(int owner_id)
  268. {
  269. if (owner_id == _ownerId) return;
  270. _ownerId = owner_id;
  271. _storedInDb = false;
  272. }
  273. /**
  274. * Returns the ownerID of the item
  275. * @return int : ownerID of the item
  276. */
  277. public int getOwnerId()
  278. {
  279. return _ownerId;
  280. }
  281. /**
  282. * Sets the location of the item
  283. * @param loc : ItemLocation (enumeration)
  284. */
  285. public void setLocation(ItemLocation loc)
  286. {
  287. setLocation(loc, 0);
  288. }
  289. /**
  290. * Sets the location of the item.<BR><BR>
  291. * <U><I>Remark :</I></U> If loc and loc_data different from database, say datas not up-to-date
  292. * @param loc : ItemLocation (enumeration)
  293. * @param loc_data : int designating the slot where the item is stored or the village for freights
  294. */
  295. public void setLocation(ItemLocation loc, int loc_data)
  296. {
  297. if (loc == _loc && loc_data == _locData)
  298. return;
  299. _loc = loc;
  300. _locData = loc_data;
  301. _storedInDb = false;
  302. }
  303. public ItemLocation getLocation()
  304. {
  305. return _loc;
  306. }
  307. /**
  308. * Sets the quantity of the item.<BR><BR>
  309. * @param count the new count to set
  310. */
  311. public void setCount(long count)
  312. {
  313. if (getCount() == count)
  314. {
  315. return;
  316. }
  317. _count = count >= -1 ? count : 0;
  318. _storedInDb = false;
  319. }
  320. /**
  321. * @return Returns the count.
  322. */
  323. public long getCount()
  324. {
  325. return _count;
  326. }
  327. /**
  328. * Sets the quantity of the item.<BR><BR>
  329. * <U><I>Remark :</I></U> If loc and loc_data different from database, say datas not up-to-date
  330. * @param process : String Identifier of process triggering this action
  331. * @param count : int
  332. * @param creator : L2PcInstance Player requesting the item creation
  333. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  334. */
  335. public void changeCount(String process, long count, L2PcInstance creator, L2Object reference)
  336. {
  337. if (count == 0)
  338. {
  339. return;
  340. }
  341. long max = getItemId() == ADENA_ID ? MAX_ADENA : Integer.MAX_VALUE;
  342. if ( count > 0 && getCount() > max - count)
  343. {
  344. setCount(max);
  345. }
  346. else
  347. {
  348. setCount(getCount() + count);
  349. }
  350. if (getCount() < 0)
  351. {
  352. setCount(0);
  353. }
  354. _storedInDb = false;
  355. if (Config.LOG_ITEMS && process != null)
  356. {
  357. LogRecord record = new LogRecord(Level.INFO, "CHANGE:" + process);
  358. record.setLoggerName("item");
  359. record.setParameters(new Object[]{this, creator, reference});
  360. _logItems.log(record);
  361. }
  362. if (creator != null)
  363. {
  364. if (creator.isGM())
  365. {
  366. String referenceName = "no-reference";
  367. if (reference != null)
  368. {
  369. referenceName = (reference.getName() != null?reference.getName():"no-name");
  370. }
  371. String targetName = (creator.getTarget() != null?creator.getTarget().getName():"no-target");
  372. if (Config.GMAUDIT)
  373. GMAudit.auditGMAction(creator.getName()+" ["+creator.getObjectId()+"]",
  374. process + "(id: "+getItemId()+" objId: "+getObjectId()+
  375. " name: "+getName()+" count: "+count+")", targetName,
  376. "L2Object referencing this action is: " + referenceName);
  377. }
  378. }
  379. }
  380. // No logging (function designed for shots only)
  381. public void changeCountWithoutTrace(int count, L2PcInstance creator, L2Object reference)
  382. {
  383. this.changeCount(null, count, creator, reference);
  384. }
  385. /**
  386. * Returns if item is equipable
  387. * @return boolean
  388. */
  389. public boolean isEquipable()
  390. {
  391. return !(_item.getBodyPart() == 0 || _item instanceof L2EtcItem );
  392. }
  393. /**
  394. * Returns if item is equipped
  395. * @return boolean
  396. */
  397. public boolean isEquipped()
  398. {
  399. return _loc == ItemLocation.PAPERDOLL || _loc == ItemLocation.PET_EQUIP;
  400. }
  401. /**
  402. * Returns the slot where the item is stored
  403. * @return int
  404. */
  405. public int getLocationSlot()
  406. {
  407. assert _loc == ItemLocation.PAPERDOLL || _loc == ItemLocation.PET_EQUIP || _loc == ItemLocation.INVENTORY || _loc == ItemLocation.MAIL;
  408. return _locData;
  409. }
  410. /**
  411. * Returns the characteristics of the item
  412. * @return L2Item
  413. */
  414. public L2Item getItem()
  415. {
  416. return _item;
  417. }
  418. public int getCustomType1()
  419. {
  420. return _type1;
  421. }
  422. public int getCustomType2()
  423. {
  424. return _type2;
  425. }
  426. public void setCustomType1(int newtype)
  427. {
  428. _type1=newtype;
  429. }
  430. public void setCustomType2(int newtype)
  431. {
  432. _type2=newtype;
  433. }
  434. public void setDropTime(long time)
  435. {
  436. _dropTime=time;
  437. }
  438. public long getDropTime()
  439. {
  440. return _dropTime;
  441. }
  442. public boolean isWear()
  443. {
  444. return _wear;
  445. }
  446. public void setWear(boolean newwear)
  447. {
  448. _wear=newwear;
  449. }
  450. /**
  451. * Returns the type of item
  452. * @return Enum
  453. */
  454. @SuppressWarnings({ "rawtypes" })
  455. public Enum getItemType()
  456. {
  457. return _item.getItemType();
  458. }
  459. /**
  460. * Returns the ID of the item
  461. * @return int
  462. */
  463. public int getItemId()
  464. {
  465. return _itemId;
  466. }
  467. /**
  468. * Returns true if item is an EtcItem
  469. * @return boolean
  470. */
  471. public boolean isEtcItem()
  472. {
  473. return (_item instanceof L2EtcItem);
  474. }
  475. /**
  476. * Returns true if item is a Weapon/Shield
  477. * @return boolean
  478. */
  479. public boolean isWeapon()
  480. {
  481. return (_item instanceof L2Weapon);
  482. }
  483. /**
  484. * Returns true if item is an Armor
  485. * @return boolean
  486. */
  487. public boolean isArmor()
  488. {
  489. return (_item instanceof L2Armor);
  490. }
  491. /**
  492. * Returns the characteristics of the L2EtcItem
  493. * @return L2EtcItem
  494. */
  495. public L2EtcItem getEtcItem()
  496. {
  497. if (_item instanceof L2EtcItem)
  498. {
  499. return (L2EtcItem) _item;
  500. }
  501. return null;
  502. }
  503. /**
  504. * Returns the characteristics of the L2Weapon
  505. * @return L2Weapon
  506. */
  507. public L2Weapon getWeaponItem()
  508. {
  509. if (_item instanceof L2Weapon)
  510. {
  511. return (L2Weapon) _item;
  512. }
  513. return null;
  514. }
  515. /**
  516. * Returns the characteristics of the L2Armor
  517. * @return L2Armor
  518. */
  519. public L2Armor getArmorItem()
  520. {
  521. if (_item instanceof L2Armor)
  522. {
  523. return (L2Armor) _item;
  524. }
  525. return null;
  526. }
  527. /**
  528. * Returns the quantity of crystals for crystallization
  529. *
  530. * @return int
  531. */
  532. public final int getCrystalCount()
  533. {
  534. return _item.getCrystalCount(_enchantLevel);
  535. }
  536. /**
  537. * Returns the reference price of the item
  538. * @return int
  539. */
  540. public int getReferencePrice()
  541. {
  542. return _item.getReferencePrice();
  543. }
  544. /**
  545. * Returns the name of the item
  546. * @return String
  547. */
  548. public String getItemName()
  549. {
  550. return _item.getName();
  551. }
  552. /**
  553. * Returns the last change of the item
  554. * @return int
  555. */
  556. public int getLastChange()
  557. {
  558. return _lastChange;
  559. }
  560. /**
  561. * Sets the last change of the item
  562. * @param lastChange : int
  563. */
  564. public void setLastChange(int lastChange)
  565. {
  566. _lastChange = lastChange;
  567. }
  568. /**
  569. * Returns if item is stackable
  570. * @return boolean
  571. */
  572. public boolean isStackable()
  573. {
  574. return _item.isStackable();
  575. }
  576. /**
  577. * Returns if item is dropable
  578. * @return boolean
  579. */
  580. public boolean isDropable()
  581. {
  582. return isAugmented() ? false : _item.isDropable();
  583. }
  584. /**
  585. * Returns if item is destroyable
  586. * @return boolean
  587. */
  588. public boolean isDestroyable()
  589. {
  590. return _item.isDestroyable();
  591. }
  592. /**
  593. * Returns if item is tradeable
  594. * @return boolean
  595. */
  596. public boolean isTradeable()
  597. {
  598. return isAugmented() ? false : _item.isTradeable();
  599. }
  600. /**
  601. * Returns if item is sellable
  602. * @return boolean
  603. */
  604. public boolean isSellable()
  605. {
  606. return isAugmented() ? false : _item.isSellable();
  607. }
  608. /**
  609. * Returns if item can be deposited in warehouse or freight
  610. * @return boolean
  611. */
  612. public boolean isDepositable(boolean isPrivateWareHouse)
  613. {
  614. // equipped, hero and quest items
  615. if (isEquipped() || !_item.isDepositable())
  616. return false;
  617. if (!isPrivateWareHouse)
  618. {
  619. // augmented not tradeable
  620. if (!isTradeable() || isShadowItem())
  621. return false;
  622. }
  623. return true;
  624. }
  625. /**
  626. * Returns if item is consumable
  627. * @return boolean
  628. */
  629. public boolean isConsumable()
  630. {
  631. return _item.isConsumable();
  632. }
  633. public boolean isHeroItem()
  634. {
  635. return _item.isHeroItem();
  636. }
  637. public boolean isCommonItem()
  638. {
  639. return _item.isCommon();
  640. }
  641. /**
  642. * Returns whether this item is pvp or not
  643. * @return boolean
  644. */
  645. public boolean isPvp()
  646. {
  647. return _item.isPvpItem();
  648. }
  649. public boolean isOlyRestrictedItem()
  650. {
  651. return (Config.LIST_OLY_RESTRICTED_ITEMS.contains(_itemId));
  652. }
  653. /**
  654. * Returns if item is available for manipulation
  655. * @return boolean
  656. */
  657. public boolean isAvailable(L2PcInstance player, boolean allowAdena, boolean allowNonTradeable)
  658. {
  659. return (
  660. (!isEquipped()) // Not equipped
  661. && (getItem().getType2() != L2Item.TYPE2_QUEST) // Not Quest Item
  662. && (getItem().getType2() != L2Item.TYPE2_MONEY || getItem().getType1() != L2Item.TYPE1_SHIELD_ARMOR) // not money, not shield
  663. && (player.getPet() == null || getObjectId() != player.getPet().getControlObjectId()) // Not Control item of currently summoned pet
  664. && (player.getActiveEnchantItem() != this) // Not momentarily used enchant scroll
  665. && (allowAdena || getItemId() != 57) // Not adena
  666. && (player.getCurrentSkill() == null || player.getCurrentSkill().getSkill().getItemConsumeId() != getItemId())
  667. && (!player.isCastingSimultaneouslyNow() || player.getLastSimultaneousSkillCast() == null || player.getLastSimultaneousSkillCast().getItemConsumeId() != getItemId())
  668. && (allowNonTradeable || isTradeable())
  669. );
  670. }
  671. /**
  672. * Returns the level of enchantment of the item
  673. * @return int
  674. */
  675. public int getEnchantLevel()
  676. {
  677. return _enchantLevel;
  678. }
  679. /**
  680. * Sets the level of enchantment of the item
  681. * @param int
  682. */
  683. public void setEnchantLevel(int enchantLevel)
  684. {
  685. if (_enchantLevel == enchantLevel)
  686. return;
  687. _enchantLevel = enchantLevel;
  688. _storedInDb = false;
  689. }
  690. /**
  691. * Returns the physical defense of the item
  692. * @return int
  693. */
  694. public int getPDef()
  695. {
  696. if (_item instanceof L2Armor)
  697. return ((L2Armor)_item).getPDef();
  698. return 0;
  699. }
  700. /**
  701. * Returns whether this item is augmented or not
  702. * @return true if augmented
  703. */
  704. public boolean isAugmented()
  705. {
  706. return _augmentation == null ? false : true;
  707. }
  708. /**
  709. * Returns the augmentation object for this item
  710. * @return augmentation
  711. */
  712. public L2Augmentation getAugmentation()
  713. {
  714. return _augmentation;
  715. }
  716. /**
  717. * Sets a new augmentation
  718. * @param augmentation
  719. * @return return true if sucessfull
  720. */
  721. public boolean setAugmentation(L2Augmentation augmentation)
  722. {
  723. // there shall be no previous augmentation..
  724. if (_augmentation != null)
  725. return false;
  726. _augmentation = augmentation;
  727. updateItemAttributes();
  728. return true;
  729. }
  730. /**
  731. * Remove the augmentation
  732. *
  733. */
  734. public void removeAugmentation()
  735. {
  736. if (_augmentation == null)
  737. return;
  738. _augmentation = null;
  739. Connection con = null;
  740. try
  741. {
  742. con = L2DatabaseFactory.getInstance().getConnection();
  743. PreparedStatement statement = null;
  744. if (_elementals != null)
  745. {
  746. // Item still has elemental enchant, only update the DB
  747. statement = con.prepareStatement("UPDATE item_attributes SET augAttributes = -1, augSkillId = -1, augSkillLevel = -1 WHERE itemId = ?");
  748. }
  749. else
  750. {
  751. // Remove the entry since the item also has no elemental enchant
  752. statement = con.prepareStatement("DELETE FROM item_attributes WHERE itemId = ?");
  753. }
  754. statement.setInt(1, getObjectId());
  755. statement.executeUpdate();
  756. statement.close();
  757. }
  758. catch (Exception e)
  759. {
  760. _log.log(Level.SEVERE, "Could not remove augmentation for item: "+this+" from DB:", e);
  761. }
  762. finally
  763. {
  764. L2DatabaseFactory.close(con);
  765. }
  766. }
  767. public void restoreAttributes()
  768. {
  769. Connection con = null;
  770. try
  771. {
  772. con = L2DatabaseFactory.getInstance().getConnection();
  773. PreparedStatement statement = con.prepareStatement("SELECT augAttributes,augSkillId,augSkillLevel,elemType,elemValue FROM item_attributes WHERE itemId=?");
  774. statement.setInt(1, getObjectId());
  775. ResultSet rs = statement.executeQuery();
  776. if (rs.next())
  777. {
  778. int aug_attributes = rs.getInt(1);
  779. int aug_skillId = rs.getInt(2);
  780. int aug_skillLevel = rs.getInt(3);
  781. byte elem_type = rs.getByte(4);
  782. int elem_value = rs.getInt(5);
  783. if (elem_type != -1 && elem_value != -1)
  784. _elementals = new Elementals(elem_type, elem_value);
  785. if (aug_attributes != -1 && aug_skillId != -1 && aug_skillLevel != -1)
  786. _augmentation = new L2Augmentation(rs.getInt("augAttributes"), rs.getInt("augSkillId"), rs.getInt("augSkillLevel"));
  787. }
  788. rs.close();
  789. statement.close();
  790. }
  791. catch (Exception e)
  792. {
  793. _log.log(Level.SEVERE, "Could not restore augmentation and elemental data for item " + this + " from DB: "+e.getMessage(), e);
  794. }
  795. finally
  796. {
  797. L2DatabaseFactory.close(con);
  798. }
  799. }
  800. public void updateItemAttributes()
  801. {
  802. Connection con = null;
  803. try
  804. {
  805. con = L2DatabaseFactory.getInstance().getConnection();
  806. PreparedStatement statement = con.prepareStatement("REPLACE INTO item_attributes VALUES(?,?,?,?,?,?)");
  807. statement.setInt(1, getObjectId());
  808. if (_augmentation == null)
  809. {
  810. statement.setInt(2, -1);
  811. statement.setInt(3, -1);
  812. statement.setInt(4, -1);
  813. }
  814. else
  815. {
  816. statement.setInt(2, _augmentation.getAttributes());
  817. if(_augmentation.getSkill() == null)
  818. {
  819. statement.setInt(3, 0);
  820. statement.setInt(4, 0);
  821. }
  822. else
  823. {
  824. statement.setInt(3, _augmentation.getSkill().getId());
  825. statement.setInt(4, _augmentation.getSkill().getLevel());
  826. }
  827. }
  828. if (_elementals == null)
  829. {
  830. statement.setByte(5, (byte) -1);
  831. statement.setInt(6, -1);
  832. }
  833. else
  834. {
  835. statement.setByte(5, _elementals.getElement());
  836. statement.setInt(6, _elementals.getValue());
  837. }
  838. statement.executeUpdate();
  839. statement.close();
  840. }
  841. catch (Exception e)
  842. {
  843. _log.log(Level.SEVERE, "Could not remove elemental enchant for item: "+this+" from DB:", e);
  844. }
  845. finally
  846. {
  847. L2DatabaseFactory.close(con);
  848. }
  849. }
  850. public Elementals getElementals()
  851. {
  852. return _elementals;
  853. }
  854. public byte getAttackElementType()
  855. {
  856. if (!isWeapon())
  857. return -2;
  858. else if (getItem().getElementals() != null)
  859. return getItem().getElementals().getElement();
  860. else if (_elementals != null)
  861. return _elementals.getElement();
  862. return -2;
  863. }
  864. public int getAttackElementPower()
  865. {
  866. if (!isWeapon())
  867. return 0;
  868. else if (getItem().getElementals() != null)
  869. return getItem().getElementals().getValue();
  870. else if (_elementals != null)
  871. return _elementals.getValue();
  872. return 0;
  873. }
  874. public int getElementDefAttr(byte element)
  875. {
  876. if (!isArmor())
  877. return 0;
  878. else if (getItem().getElementals() != null && getItem().getElementals().getElement() == element)
  879. return getItem().getElementals().getValue();
  880. else if (_elementals != null && _elementals.getElement() == element)
  881. return _elementals.getValue();
  882. return 0;
  883. }
  884. public void setElementAttr(byte element, int value)
  885. {
  886. if (_elementals == null)
  887. {
  888. _elementals = new Elementals(element, value);
  889. }
  890. else
  891. {
  892. _elementals.setElement(element);
  893. _elementals.setValue(value);
  894. }
  895. updateItemAttributes();
  896. }
  897. public void clearElementAttr()
  898. {
  899. if (_elementals != null)
  900. {
  901. _elementals = null;
  902. }
  903. Connection con = null;
  904. try
  905. {
  906. con = L2DatabaseFactory.getInstance().getConnection();
  907. PreparedStatement statement = null;
  908. if (_augmentation != null)
  909. {
  910. // Item still has augmentation, only update the DB
  911. statement = con.prepareStatement("UPDATE item_attributes SET elemType = -1, elemValue = -1 WHERE itemId = ?");
  912. }
  913. else
  914. {
  915. // Remove the entry since the item also has no augmentation
  916. statement = con.prepareStatement("DELETE FROM item_attributes WHERE itemId = ?");
  917. }
  918. statement.setInt(1, getObjectId());
  919. statement.executeUpdate();
  920. statement.close();
  921. }
  922. catch (Exception e)
  923. {
  924. _log.log(Level.SEVERE, "Could not remove elemental enchant for item: "+this+" from DB:", e);
  925. }
  926. finally
  927. {
  928. L2DatabaseFactory.close(con);
  929. }
  930. }
  931. /**
  932. * Used to decrease mana
  933. * (mana means life time for shadow items)
  934. */
  935. public class ScheduleConsumeManaTask implements Runnable
  936. {
  937. private final L2ItemInstance _shadowItem;
  938. public ScheduleConsumeManaTask(L2ItemInstance item)
  939. {
  940. _shadowItem = item;
  941. }
  942. public void run()
  943. {
  944. try
  945. {
  946. // decrease mana
  947. if (_shadowItem != null)
  948. _shadowItem.decreaseMana(true);
  949. }
  950. catch (Exception e)
  951. {
  952. _log.log(Level.SEVERE, "", e);
  953. }
  954. }
  955. }
  956. /**
  957. * Returns true if this item is a shadow item
  958. * Shadow items have a limited life-time
  959. * @return
  960. */
  961. public boolean isShadowItem()
  962. {
  963. return (_mana >= 0);
  964. }
  965. /**
  966. * Returns the remaining mana of this shadow item
  967. * @return lifeTime
  968. */
  969. public int getMana()
  970. {
  971. return _mana;
  972. }
  973. /**
  974. * Decreases the mana of this shadow item,
  975. * sends a inventory update
  976. * schedules a new consumption task if non is running
  977. * optionally one could force a new task
  978. * @param forces a new consumption task if item is equipped
  979. */
  980. public void decreaseMana(boolean resetConsumingMana)
  981. {
  982. decreaseMana(resetConsumingMana, 1);
  983. }
  984. /**
  985. * Decreases the mana of this shadow item,
  986. * sends a inventory update
  987. * schedules a new consumption task if non is running
  988. * optionally one could force a new task
  989. * @param forces a new consumption task if item is equipped
  990. * @param count how much mana decrease
  991. */
  992. public void decreaseMana(boolean resetConsumingMana, int count)
  993. {
  994. if (!isShadowItem())
  995. return;
  996. if (_mana - count >= 0)
  997. _mana -= count;
  998. else
  999. _mana = 0;
  1000. if (_storedInDb)
  1001. _storedInDb = false;
  1002. if (resetConsumingMana)
  1003. _consumingMana = false;
  1004. final L2PcInstance player = L2World.getInstance().getPlayer(getOwnerId());
  1005. if (player != null)
  1006. {
  1007. SystemMessage sm;
  1008. switch (_mana)
  1009. {
  1010. case 10:
  1011. sm = new SystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_10);
  1012. sm.addItemName(_item);
  1013. player.sendPacket(sm);
  1014. break;
  1015. case 5:
  1016. sm = new SystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_5);
  1017. sm.addItemName(_item);
  1018. player.sendPacket(sm);
  1019. break;
  1020. case 1:
  1021. sm = new SystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_1);
  1022. sm.addItemName(_item);
  1023. player.sendPacket(sm);
  1024. break;
  1025. }
  1026. if (_mana == 0) // The life time has expired
  1027. {
  1028. sm = new SystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_0);
  1029. sm.addItemName(_item);
  1030. player.sendPacket(sm);
  1031. // unequip
  1032. if (isEquipped())
  1033. {
  1034. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInSlotAndRecord(getLocationSlot());
  1035. InventoryUpdate iu = new InventoryUpdate();
  1036. for (L2ItemInstance item: unequiped)
  1037. {
  1038. player.checkSShotsMatch(null, item);
  1039. iu.addModifiedItem(item);
  1040. }
  1041. player.sendPacket(iu);
  1042. player.broadcastUserInfo();
  1043. }
  1044. if (getLocation() != ItemLocation.WAREHOUSE)
  1045. {
  1046. // destroy
  1047. player.getInventory().destroyItem("L2ItemInstance", this, player, null);
  1048. // send update
  1049. InventoryUpdate iu = new InventoryUpdate();
  1050. iu.addRemovedItem(this);
  1051. player.sendPacket(iu);
  1052. StatusUpdate su = new StatusUpdate(player);
  1053. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  1054. player.sendPacket(su);
  1055. }
  1056. else
  1057. {
  1058. player.getWarehouse().destroyItem("L2ItemInstance", this, player, null);
  1059. }
  1060. // delete from world
  1061. L2World.getInstance().removeObject(this);
  1062. }
  1063. else
  1064. {
  1065. // Reschedule if still equipped
  1066. if (!_consumingMana && isEquipped())
  1067. {
  1068. scheduleConsumeManaTask();
  1069. }
  1070. if (getLocation() != ItemLocation.WAREHOUSE)
  1071. {
  1072. InventoryUpdate iu = new InventoryUpdate();
  1073. iu.addModifiedItem(this);
  1074. player.sendPacket(iu);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. public void scheduleConsumeManaTask()
  1080. {
  1081. if (_consumingMana)
  1082. return;
  1083. _consumingMana = true;
  1084. ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleConsumeManaTask(this), MANA_CONSUMPTION_RATE);
  1085. }
  1086. /**
  1087. * Returns false cause item can't be attacked
  1088. * @return boolean false
  1089. */
  1090. @Override
  1091. public boolean isAutoAttackable(L2Character attacker)
  1092. {
  1093. return false;
  1094. }
  1095. /**
  1096. * Returns the type of charge with SoulShot of the item.
  1097. * @return int (CHARGED_NONE, CHARGED_SOULSHOT)
  1098. */
  1099. public int getChargedSoulshot()
  1100. {
  1101. return _chargedSoulshot;
  1102. }
  1103. /**
  1104. * Returns the type of charge with SpiritShot of the item
  1105. * @return int (CHARGED_NONE, CHARGED_SPIRITSHOT, CHARGED_BLESSED_SPIRITSHOT)
  1106. */
  1107. public int getChargedSpiritshot()
  1108. {
  1109. return _chargedSpiritshot;
  1110. }
  1111. public boolean getChargedFishshot()
  1112. {
  1113. return _chargedFishtshot;
  1114. }
  1115. /**
  1116. * Sets the type of charge with SoulShot of the item
  1117. * @param type : int (CHARGED_NONE, CHARGED_SOULSHOT)
  1118. */
  1119. public void setChargedSoulshot(int type)
  1120. {
  1121. _chargedSoulshot = type;
  1122. }
  1123. /**
  1124. * Sets the type of charge with SpiritShot of the item
  1125. * @param type : int (CHARGED_NONE, CHARGED_SPIRITSHOT, CHARGED_BLESSED_SPIRITSHOT)
  1126. */
  1127. public void setChargedSpiritshot(int type)
  1128. {
  1129. _chargedSpiritshot = type;
  1130. }
  1131. public void setChargedFishshot(boolean type)
  1132. {
  1133. _chargedFishtshot = type;
  1134. }
  1135. /**
  1136. * This function basically returns a set of functions from
  1137. * L2Item/L2Armor/L2Weapon, but may add additional
  1138. * functions, if this particular item instance is enhanched
  1139. * for a particular player.
  1140. * @param player : L2Character designating the player
  1141. * @return Func[]
  1142. */
  1143. public Func[] getStatFuncs(L2Character player)
  1144. {
  1145. return getItem().getStatFuncs(this, player);
  1146. }
  1147. /**
  1148. * Updates the database.<BR>
  1149. */
  1150. public void updateDatabase()
  1151. {
  1152. this.updateDatabase(false);
  1153. }
  1154. /**
  1155. * Updates the database.<BR>
  1156. *
  1157. * @param force if the update should necessarilly be done.
  1158. */
  1159. public void updateDatabase(boolean force)
  1160. {
  1161. if (isWear()) //avoid saving weared items
  1162. {
  1163. return;
  1164. }
  1165. _dbLock.lock();
  1166. try
  1167. {
  1168. if (_existsInDb)
  1169. {
  1170. if (_ownerId == 0
  1171. || _loc == ItemLocation.VOID
  1172. || _loc == ItemLocation.REFUND
  1173. || (getCount() == 0 && _loc != ItemLocation.LEASE))
  1174. {
  1175. removeFromDb();
  1176. }
  1177. else if (!Config.LAZY_ITEMS_UPDATE || force)
  1178. {
  1179. updateInDb();
  1180. }
  1181. }
  1182. else
  1183. {
  1184. if (_ownerId == 0
  1185. || _loc == ItemLocation.VOID
  1186. || _loc == ItemLocation.REFUND
  1187. || (getCount() == 0 && _loc != ItemLocation.LEASE))
  1188. {
  1189. return;
  1190. }
  1191. insertIntoDb();
  1192. }
  1193. }
  1194. finally
  1195. {
  1196. _dbLock.unlock();
  1197. }
  1198. }
  1199. /**
  1200. * Returns a L2ItemInstance stored in database from its objectID
  1201. * @param objectId : int designating the objectID of the item
  1202. * @return L2ItemInstance
  1203. */
  1204. public static L2ItemInstance restoreFromDb(int ownerId, ResultSet rs)
  1205. {
  1206. L2ItemInstance inst = null;
  1207. int objectId, item_id, loc_data, enchant_level, custom_type1, custom_type2, manaLeft;
  1208. long time, count;
  1209. ItemLocation loc;
  1210. try
  1211. {
  1212. objectId = rs.getInt(1);
  1213. item_id = rs.getInt("item_id");
  1214. count = rs.getLong("count");
  1215. loc = ItemLocation.valueOf(rs.getString("loc"));
  1216. loc_data = rs.getInt("loc_data");
  1217. enchant_level = rs.getInt("enchant_level");
  1218. custom_type1 = rs.getInt("custom_type1");
  1219. custom_type2 = rs.getInt("custom_type2");
  1220. manaLeft = rs.getInt("mana_left");
  1221. time = rs.getLong("time");
  1222. } catch (Exception e) {
  1223. _log.log(Level.SEVERE, "Could not restore an item owned by "+ownerId+" from DB:", e);
  1224. return null;
  1225. }
  1226. L2Item item = ItemTable.getInstance().getTemplate(item_id);
  1227. if (item == null) {
  1228. _log.severe("Item item_id="+item_id+" not known, object_id="+objectId);
  1229. return null;
  1230. }
  1231. inst = new L2ItemInstance(objectId, item);
  1232. inst._ownerId = ownerId;
  1233. inst.setCount(count);
  1234. inst._enchantLevel = enchant_level;
  1235. inst._type1 = custom_type1;
  1236. inst._type2 = custom_type2;
  1237. inst._loc = loc;
  1238. inst._locData = loc_data;
  1239. inst._existsInDb = true;
  1240. inst._storedInDb = true;
  1241. // Setup life time for shadow weapons
  1242. inst._mana = manaLeft;
  1243. inst._time = time;
  1244. //load augmentation and elemental enchant
  1245. if (inst.isEquipable())
  1246. inst.restoreAttributes();
  1247. return inst;
  1248. }
  1249. /**
  1250. * Init a dropped L2ItemInstance and add it in the world as a visible object.<BR><BR>
  1251. *
  1252. * <B><U> Actions</U> :</B><BR><BR>
  1253. * <li>Set the x,y,z position of the L2ItemInstance dropped and update its _worldregion </li>
  1254. * <li>Add the L2ItemInstance dropped to _visibleObjects of its L2WorldRegion</li>
  1255. * <li>Add the L2ItemInstance dropped in the world as a <B>visible</B> object</li><BR><BR>
  1256. *
  1257. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T ADD the object to _allObjects of L2World </B></FONT><BR><BR>
  1258. *
  1259. * <B><U> Assert </U> :</B><BR><BR>
  1260. * <li> _worldRegion == null <I>(L2Object is invisible at the beginning)</I></li><BR><BR>
  1261. *
  1262. * <B><U> Example of use </U> :</B><BR><BR>
  1263. * <li> Drop item</li>
  1264. * <li> Call Pet</li><BR>
  1265. *
  1266. */
  1267. public class ItemDropTask implements Runnable
  1268. {
  1269. private int _x,_y,_z;
  1270. private final L2Character _dropper;
  1271. private final L2ItemInstance _itm;
  1272. public ItemDropTask(L2ItemInstance item, L2Character dropper, int x, int y, int z)
  1273. {
  1274. _x = x;
  1275. _y = y;
  1276. _z = z;
  1277. _dropper = dropper;
  1278. _itm = item;
  1279. }
  1280. public final void run()
  1281. {
  1282. assert _itm.getPosition().getWorldRegion() == null;
  1283. if (Config.GEODATA > 0 && _dropper != null)
  1284. {
  1285. Location dropDest = GeoData.getInstance().moveCheck(_dropper.getX(), _dropper.getY(), _dropper.getZ(), _x, _y, _z, _dropper.getInstanceId());
  1286. _x = dropDest.getX();
  1287. _y = dropDest.getY();
  1288. _z = dropDest.getZ();
  1289. }
  1290. if(_dropper != null)
  1291. setInstanceId(_dropper.getInstanceId()); // Inherit instancezone when dropped in visible world
  1292. else
  1293. setInstanceId(0); // No dropper? Make it a global item...
  1294. synchronized (_itm)
  1295. {
  1296. // Set the x,y,z position of the L2ItemInstance dropped and update its _worldregion
  1297. _itm.setIsVisible(true);
  1298. _itm.getPosition().setWorldPosition(_x, _y ,_z);
  1299. _itm.getPosition().setWorldRegion(L2World.getInstance().getRegion(getPosition().getWorldPosition()));
  1300. // Add the L2ItemInstance dropped to _visibleObjects of its L2WorldRegion
  1301. }
  1302. _itm.getPosition().getWorldRegion().addVisibleObject(_itm);
  1303. _itm.setDropTime(System.currentTimeMillis());
  1304. _itm.setDropperObjectId(_dropper != null ? _dropper.getObjectId() : 0); //Set the dropper Id for the knownlist packets in sendInfo
  1305. // this can synchronize on others instancies, so it's out of
  1306. // synchronized, to avoid deadlocks
  1307. // Add the L2ItemInstance dropped in the world as a visible object
  1308. L2World.getInstance().addVisibleObject(_itm, _itm.getPosition().getWorldRegion());
  1309. if (Config.SAVE_DROPPED_ITEM)
  1310. ItemsOnGroundManager.getInstance().save(_itm);
  1311. _itm.setDropperObjectId(0); //Set the dropper Id back to 0 so it no longer shows the drop packet
  1312. }
  1313. }
  1314. public final void dropMe(L2Character dropper, int x, int y, int z)
  1315. {
  1316. ThreadPoolManager.getInstance().executeTask(new ItemDropTask(this, dropper, x, y, z));
  1317. }
  1318. /**
  1319. * Update the database with values of the item
  1320. */
  1321. private void updateInDb()
  1322. {
  1323. assert _existsInDb;
  1324. if (_wear)
  1325. return;
  1326. if (_storedInDb)
  1327. return;
  1328. Connection con = null;
  1329. PreparedStatement statement = null;
  1330. try
  1331. {
  1332. con = L2DatabaseFactory.getInstance().getConnection();
  1333. statement = con.prepareStatement(
  1334. "UPDATE items SET owner_id=?,count=?,loc=?,loc_data=?,enchant_level=?,custom_type1=?,custom_type2=?,mana_left=?,time=? " +
  1335. "WHERE object_id = ?");
  1336. statement.setInt(1, _ownerId);
  1337. statement.setLong(2, getCount());
  1338. statement.setString(3, _loc.name());
  1339. statement.setInt(4, _locData);
  1340. statement.setInt(5, getEnchantLevel());
  1341. statement.setInt(6, getCustomType1());
  1342. statement.setInt(7, getCustomType2());
  1343. statement.setInt(8, getMana());
  1344. statement.setLong(9, getTime());
  1345. statement.setInt(10, getObjectId());
  1346. statement.executeUpdate();
  1347. _existsInDb = true;
  1348. _storedInDb = true;
  1349. statement.close();
  1350. }
  1351. catch (Exception e)
  1352. {
  1353. _log.log(Level.SEVERE, "Could not update item "+this+" in DB: Reason: "+e.getMessage(), e);
  1354. }
  1355. finally
  1356. {
  1357. L2DatabaseFactory.close(con);
  1358. }
  1359. }
  1360. /**
  1361. * Insert the item in database
  1362. */
  1363. private void insertIntoDb()
  1364. {
  1365. assert !_existsInDb && getObjectId() != 0;
  1366. if (_wear)
  1367. return;
  1368. Connection con = null;
  1369. PreparedStatement statement = null;
  1370. try
  1371. {
  1372. con = L2DatabaseFactory.getInstance().getConnection();
  1373. statement = con.prepareStatement(
  1374. "INSERT INTO items (owner_id,item_id,count,loc,loc_data,enchant_level,object_id,custom_type1,custom_type2,mana_left,time) " +
  1375. "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
  1376. statement.setInt(1, _ownerId);
  1377. statement.setInt(2, _itemId);
  1378. statement.setLong(3, getCount());
  1379. statement.setString(4, _loc.name());
  1380. statement.setInt(5, _locData);
  1381. statement.setInt(6, getEnchantLevel());
  1382. statement.setInt(7, getObjectId());
  1383. statement.setInt(8, _type1);
  1384. statement.setInt(9, _type2);
  1385. statement.setInt(10, getMana());
  1386. statement.setLong(11, getTime());
  1387. statement.executeUpdate();
  1388. _existsInDb = true;
  1389. _storedInDb = true;
  1390. statement.close();
  1391. }
  1392. catch (Exception e)
  1393. {
  1394. _log.log(Level.SEVERE, "Could not insert item "+this+" into DB: Reason: "+e.getMessage(), e);
  1395. }
  1396. finally
  1397. {
  1398. L2DatabaseFactory.close(con);
  1399. }
  1400. if (_elementals != null)
  1401. updateItemAttributes();
  1402. }
  1403. /**
  1404. * Delete item from database
  1405. */
  1406. private void removeFromDb()
  1407. {
  1408. assert _existsInDb;
  1409. if (_wear)
  1410. return;
  1411. Connection con = null;
  1412. PreparedStatement statement = null;
  1413. try
  1414. {
  1415. con = L2DatabaseFactory.getInstance().getConnection();
  1416. statement = con.prepareStatement("DELETE FROM items WHERE object_id=?");
  1417. statement.setInt(1, getObjectId());
  1418. statement.executeUpdate();
  1419. _existsInDb = false;
  1420. _storedInDb = false;
  1421. statement.close();
  1422. statement = con.prepareStatement("DELETE FROM item_attributes WHERE itemId = ?");
  1423. statement.setInt(1, getObjectId());
  1424. statement.executeUpdate();
  1425. statement.close();
  1426. }
  1427. catch (Exception e)
  1428. {
  1429. _log.log(Level.SEVERE, "Could not delete item "+this+" in DB: "+e.getMessage(), e);
  1430. }
  1431. finally
  1432. {
  1433. L2DatabaseFactory.close(con);
  1434. }
  1435. }
  1436. /**
  1437. * Returns the item in String format
  1438. * @return String
  1439. */
  1440. @Override
  1441. public String toString()
  1442. {
  1443. return _item+"["+getObjectId()+"]";
  1444. }
  1445. public void resetOwnerTimer()
  1446. {
  1447. if(itemLootShedule != null)
  1448. itemLootShedule.cancel(true);
  1449. itemLootShedule = null;
  1450. }
  1451. public void setItemLootShedule(ScheduledFuture<?> sf)
  1452. {
  1453. itemLootShedule = sf;
  1454. }
  1455. public ScheduledFuture<?> getItemLootShedule()
  1456. {
  1457. return itemLootShedule;
  1458. }
  1459. public void setProtected(boolean is_protected)
  1460. {
  1461. _protected = is_protected;
  1462. }
  1463. public boolean isProtected()
  1464. {
  1465. return _protected;
  1466. }
  1467. public boolean isNightLure()
  1468. {
  1469. return ((_itemId >= 8505 && _itemId <= 8513) || _itemId == 8485);
  1470. }
  1471. public void setCountDecrease(boolean decrease)
  1472. {
  1473. _decrease = decrease;
  1474. }
  1475. public boolean getCountDecrease()
  1476. {
  1477. return _decrease;
  1478. }
  1479. public void setInitCount(int InitCount)
  1480. {
  1481. _initCount = InitCount;
  1482. }
  1483. public long getInitCount()
  1484. {
  1485. return _initCount;
  1486. }
  1487. public void restoreInitCount()
  1488. {
  1489. if(_decrease)
  1490. setCount(_initCount);
  1491. }
  1492. public boolean isTimeLimitedItem()
  1493. {
  1494. return (_time > 0);
  1495. }
  1496. /**
  1497. * Returns (current system time + time) of this time limited item
  1498. * @return Time
  1499. */
  1500. public long getTime()
  1501. {
  1502. return _time;
  1503. }
  1504. public long getRemainingTime()
  1505. {
  1506. return _time - System.currentTimeMillis();
  1507. }
  1508. public void endOfLife()
  1509. {
  1510. L2PcInstance player = L2World.getInstance().getPlayer(getOwnerId());
  1511. if (player != null)
  1512. {
  1513. if (isEquipped())
  1514. {
  1515. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInSlotAndRecord(getLocationSlot());
  1516. InventoryUpdate iu = new InventoryUpdate();
  1517. for (L2ItemInstance item: unequiped)
  1518. {
  1519. player.checkSShotsMatch(null, item);
  1520. iu.addModifiedItem(item);
  1521. }
  1522. player.sendPacket(iu);
  1523. player.broadcastUserInfo();
  1524. }
  1525. if (getLocation() != ItemLocation.WAREHOUSE)
  1526. {
  1527. // destroy
  1528. player.getInventory().destroyItem("L2ItemInstance", this, player, null);
  1529. // send update
  1530. InventoryUpdate iu = new InventoryUpdate();
  1531. iu.addRemovedItem(this);
  1532. player.sendPacket(iu);
  1533. StatusUpdate su = new StatusUpdate(player);
  1534. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  1535. player.sendPacket(su);
  1536. }
  1537. else
  1538. {
  1539. player.getWarehouse().destroyItem("L2ItemInstance", this, player, null);
  1540. }
  1541. player.sendPacket(new SystemMessage(SystemMessageId.TIME_LIMITED_ITEM_DELETED));
  1542. // delete from world
  1543. L2World.getInstance().removeObject(this);
  1544. }
  1545. }
  1546. public void scheduleLifeTimeTask()
  1547. {
  1548. if (!isTimeLimitedItem()) return;
  1549. if (getRemainingTime() <= 0)
  1550. endOfLife();
  1551. else
  1552. {
  1553. if (_lifeTimeTask != null)
  1554. _lifeTimeTask.cancel(false);
  1555. _lifeTimeTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleLifeTimeTask(this), getRemainingTime());
  1556. }
  1557. }
  1558. public class ScheduleLifeTimeTask implements Runnable
  1559. {
  1560. private final L2ItemInstance _limitedItem;
  1561. public ScheduleLifeTimeTask(L2ItemInstance item)
  1562. {
  1563. _limitedItem = item;
  1564. }
  1565. public void run()
  1566. {
  1567. try
  1568. {
  1569. if (_limitedItem != null)
  1570. _limitedItem.endOfLife();
  1571. }
  1572. catch (Exception e)
  1573. {
  1574. _log.log(Level.SEVERE, "", e);
  1575. }
  1576. }
  1577. }
  1578. public void updateElementAttrBonus(L2PcInstance player)
  1579. {
  1580. if (_elementals == null)
  1581. return;
  1582. _elementals.updateBonus(player, isArmor());
  1583. }
  1584. public void setDropperObjectId(int id)
  1585. {
  1586. _dropperObjectId = id;
  1587. }
  1588. @Override
  1589. public void sendInfo(L2PcInstance activeChar)
  1590. {
  1591. if (_dropperObjectId != 0)
  1592. activeChar.sendPacket(new DropItem(this, _dropperObjectId));
  1593. else
  1594. activeChar.sendPacket(new SpawnItem(this));
  1595. }
  1596. public boolean isPublished()
  1597. {
  1598. return _published;
  1599. }
  1600. public void publish()
  1601. {
  1602. _published = true;
  1603. }
  1604. /* (non-Javadoc)
  1605. * @see com.l2jserver.gameserver.model.L2Object#decayMe()
  1606. */
  1607. @Override
  1608. public void decayMe()
  1609. {
  1610. if (Config.SAVE_DROPPED_ITEM)
  1611. ItemsOnGroundManager.getInstance().removeObject(this);
  1612. super.decayMe();
  1613. }
  1614. }