L2ItemInstance.java 47 KB

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