L2ItemInstance.java 37 KB

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