L2ItemInstance.java 48 KB

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