L2ItemInstance.java 48 KB

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