L2ItemInstance.java 49 KB

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