L2ItemInstance.java 47 KB

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