L2ItemInstance.java 46 KB

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