L2ItemInstance.java 48 KB

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