L2ItemInstance.java 42 KB

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