L2ItemInstance.java 49 KB

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