L2ItemInstance.java 48 KB

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