L2ItemInstance.java 46 KB

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