L2ItemInstance.java 40 KB

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