L2ItemInstance.java 48 KB

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