Guest User

L2ItemInstance

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