L2ItemInstance.java 38 KB

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