L2ItemInstance.java 40 KB

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