L2ItemInstance.java 47 KB

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