L2ItemInstance.java 46 KB

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