2
0

L2ItemInstance.java 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  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 isHeroItem()
  639. {
  640. return _item.isHeroItem();
  641. }
  642. public boolean isCommonItem()
  643. {
  644. return _item.isCommon();
  645. }
  646. /**
  647. * Returns whether this item is pvp or not
  648. * @return boolean
  649. */
  650. public boolean isPvp()
  651. {
  652. return _item.isPvpItem();
  653. }
  654. public boolean isOlyRestrictedItem()
  655. {
  656. return (Config.LIST_OLY_RESTRICTED_ITEMS.contains(_itemId));
  657. }
  658. /**
  659. * Returns if item is available for manipulation
  660. * @return boolean
  661. */
  662. public boolean isAvailable(L2PcInstance player, boolean allowAdena, boolean allowNonTradeable)
  663. {
  664. return (
  665. (!isEquipped()) // Not equipped
  666. && (getItem().getType2() != L2Item.TYPE2_QUEST) // Not Quest Item
  667. && (getItem().getType2() != L2Item.TYPE2_MONEY || getItem().getType1() != L2Item.TYPE1_SHIELD_ARMOR) // not money, not shield
  668. && (player.getPet() == null || getObjectId() != player.getPet().getControlObjectId()) // Not Control item of currently summoned pet
  669. && (player.getActiveEnchantItem() != this) // Not momentarily used enchant scroll
  670. && (allowAdena || getItemId() != 57) // Not adena
  671. && (player.getCurrentSkill() == null || player.getCurrentSkill().getSkill().getItemConsumeId() != getItemId())
  672. && (!player.isCastingSimultaneouslyNow() || player.getLastSimultaneousSkillCast() == null || player.getLastSimultaneousSkillCast().getItemConsumeId() != getItemId())
  673. && (allowNonTradeable || isTradeable()
  674. && (!(getItem().getItemType() == L2EtcItemType.PET_COLLAR && player.havePetInvItems())))
  675. );
  676. }
  677. /**
  678. * Returns the level of enchantment of the item
  679. * @return int
  680. */
  681. public int getEnchantLevel()
  682. {
  683. return _enchantLevel;
  684. }
  685. /**
  686. * Sets the level of enchantment of the item
  687. * @param int
  688. */
  689. public void setEnchantLevel(int enchantLevel)
  690. {
  691. if (_enchantLevel == enchantLevel)
  692. return;
  693. _enchantLevel = enchantLevel;
  694. _storedInDb = false;
  695. }
  696. /**
  697. * Returns whether this item is augmented or not
  698. * @return true if augmented
  699. */
  700. public boolean isAugmented()
  701. {
  702. return _augmentation == null ? false : true;
  703. }
  704. /**
  705. * Returns the augmentation object for this item
  706. * @return augmentation
  707. */
  708. public L2Augmentation getAugmentation()
  709. {
  710. return _augmentation;
  711. }
  712. /**
  713. * Sets a new augmentation
  714. * @param augmentation
  715. * @return return true if sucessfull
  716. */
  717. public boolean setAugmentation(L2Augmentation augmentation)
  718. {
  719. // there shall be no previous augmentation..
  720. if (_augmentation != null)
  721. return false;
  722. _augmentation = augmentation;
  723. updateItemAttributes(null);
  724. return true;
  725. }
  726. /**
  727. * Remove the augmentation
  728. *
  729. */
  730. public void removeAugmentation()
  731. {
  732. if (_augmentation == null)
  733. return;
  734. _augmentation = null;
  735. Connection con = null;
  736. try
  737. {
  738. con = L2DatabaseFactory.getInstance().getConnection();
  739. PreparedStatement statement = null;
  740. // Remove the entry
  741. statement = con.prepareStatement("DELETE FROM item_attributes WHERE itemId = ?");
  742. statement.setInt(1, getObjectId());
  743. statement.executeUpdate();
  744. statement.close();
  745. }
  746. catch (Exception e)
  747. {
  748. _log.log(Level.SEVERE, "Could not remove augmentation for item: "+this+" from DB:", e);
  749. }
  750. finally
  751. {
  752. L2DatabaseFactory.close(con);
  753. }
  754. }
  755. public void restoreAttributes()
  756. {
  757. Connection con = null;
  758. try
  759. {
  760. con = L2DatabaseFactory.getInstance().getConnection();
  761. PreparedStatement statement = con.prepareStatement("SELECT augAttributes,augSkillId,augSkillLevel FROM item_attributes WHERE itemId=?");
  762. statement.setInt(1, getObjectId());
  763. ResultSet rs = statement.executeQuery();
  764. if (rs.next())
  765. {
  766. int aug_attributes = rs.getInt(1);
  767. int aug_skillId = rs.getInt(2);
  768. int aug_skillLevel = rs.getInt(3);
  769. if (aug_attributes != -1 && aug_skillId != -1 && aug_skillLevel != -1)
  770. _augmentation = new L2Augmentation(rs.getInt("augAttributes"), rs.getInt("augSkillId"), rs.getInt("augSkillLevel"));
  771. }
  772. rs.close();
  773. statement.close();
  774. statement = con.prepareStatement("SELECT elemType,elemValue FROM item_elementals WHERE itemId=?");
  775. statement.setInt(1, getObjectId());
  776. rs = statement.executeQuery();
  777. while (rs.next())
  778. {
  779. byte elem_type = rs.getByte(1);
  780. int elem_value = rs.getInt(2);
  781. if (elem_type != -1 && elem_value != -1)
  782. applyAttribute(elem_type, elem_value);
  783. }
  784. rs.close();
  785. statement.close();
  786. }
  787. catch (Exception e)
  788. {
  789. _log.log(Level.SEVERE, "Could not restore augmentation and elemental data for item " + this + " from DB: "+e.getMessage(), e);
  790. }
  791. finally
  792. {
  793. L2DatabaseFactory.close(con);
  794. }
  795. }
  796. private void updateItemAttributes(Connection pooledCon)
  797. {
  798. Connection con = null;
  799. try
  800. {
  801. con = pooledCon == null ? L2DatabaseFactory.getInstance().getConnection() : pooledCon;
  802. PreparedStatement statement = con.prepareStatement("REPLACE INTO item_attributes VALUES(?,?,?,?)");
  803. statement.setInt(1, getObjectId());
  804. if (_augmentation == null)
  805. {
  806. statement.setInt(2, -1);
  807. statement.setInt(3, -1);
  808. statement.setInt(4, -1);
  809. }
  810. else
  811. {
  812. statement.setInt(2, _augmentation.getAttributes());
  813. if(_augmentation.getSkill() == null)
  814. {
  815. statement.setInt(3, 0);
  816. statement.setInt(4, 0);
  817. }
  818. else
  819. {
  820. statement.setInt(3, _augmentation.getSkill().getId());
  821. statement.setInt(4, _augmentation.getSkill().getLevel());
  822. }
  823. }
  824. statement.executeUpdate();
  825. statement.close();
  826. }
  827. catch (SQLException e)
  828. {
  829. _log.log(Level.SEVERE, "Could not update atributes for item: "+this+" from DB:", e);
  830. }
  831. finally
  832. {
  833. if (pooledCon == null)
  834. L2DatabaseFactory.close(con);
  835. }
  836. }
  837. private void updateItemElements(Connection pooledCon)
  838. {
  839. Connection con = null;
  840. try
  841. {
  842. con = pooledCon == null ? L2DatabaseFactory.getInstance().getConnection() : pooledCon;
  843. PreparedStatement statement = con.prepareStatement("DELETE FROM item_elementals WHERE itemId = ?");
  844. statement.setInt(1, getObjectId());
  845. statement.executeUpdate();
  846. statement.close();
  847. if (_elementals == null)
  848. return;
  849. statement = con.prepareStatement("INSERT INTO item_elementals VALUES(?,?,?)");
  850. for (Elementals elm : _elementals)
  851. {
  852. statement.setInt(1, getObjectId());
  853. statement.setByte(2, elm.getElement());
  854. statement.setInt(3, elm.getValue());
  855. statement.executeUpdate();
  856. statement.clearParameters();
  857. }
  858. statement.close();
  859. }
  860. catch (SQLException e)
  861. {
  862. _log.log(Level.SEVERE, "Could not update elementals for item: "+this+" from DB:", e);
  863. }
  864. finally
  865. {
  866. if (pooledCon == null)
  867. L2DatabaseFactory.close(con);
  868. }
  869. }
  870. public Elementals[] getElementals()
  871. {
  872. return _elementals;
  873. }
  874. public Elementals getElemental(byte attribute)
  875. {
  876. if (_elementals == null)
  877. return null;
  878. for (Elementals elm : _elementals)
  879. {
  880. if (elm.getElement() == attribute)
  881. return elm;
  882. }
  883. return null;
  884. }
  885. public byte getAttackElementType()
  886. {
  887. if (!isWeapon())
  888. return -2;
  889. else if (getItem().getElementals() != null)
  890. return getItem().getElementals()[0].getElement();
  891. else if (_elementals != null)
  892. return _elementals[0].getElement();
  893. return -2;
  894. }
  895. public int getAttackElementPower()
  896. {
  897. if (!isWeapon())
  898. return 0;
  899. else if (getItem().getElementals() != null)
  900. return getItem().getElementals()[0].getValue();
  901. else if (_elementals != null)
  902. return _elementals[0].getValue();
  903. return 0;
  904. }
  905. public int getElementDefAttr(byte element)
  906. {
  907. if (!isArmor())
  908. return 0;
  909. else if (getItem().getElementals() != null)
  910. {
  911. Elementals elm = getItem().getElemental(element);
  912. if (elm != null)
  913. return elm.getValue();
  914. }
  915. else if (_elementals != null)
  916. {
  917. Elementals elm = getElemental(element);
  918. if (elm != null )
  919. return elm.getValue();
  920. }
  921. return 0;
  922. }
  923. private void applyAttribute(byte element, int value)
  924. {
  925. if (_elementals == null)
  926. {
  927. _elementals = new Elementals[1];
  928. _elementals[0] = new Elementals(element, value);
  929. }
  930. else
  931. {
  932. Elementals elm = getElemental(element);
  933. if (elm != null)
  934. {
  935. elm.setValue(value);
  936. }
  937. else
  938. {
  939. elm = new Elementals(element, value);
  940. Elementals[] array = new Elementals[_elementals.length+1];
  941. System.arraycopy(_elementals, 0, array, 0, _elementals.length);
  942. array[_elementals.length] = elm;
  943. _elementals = array;
  944. }
  945. }
  946. }
  947. /**
  948. * Add elemental attribute to item and save to db
  949. * @param element
  950. * @param value
  951. */
  952. public void setElementAttr(byte element, int value)
  953. {
  954. applyAttribute(element, value);
  955. updateItemElements(null);
  956. }
  957. /**
  958. * Remove elemental from item
  959. * @param element byte element to remove, -1 for all elementals remove
  960. */
  961. public void clearElementAttr(byte element)
  962. {
  963. if (getElemental(element) == null && element != -1)
  964. {
  965. return;
  966. }
  967. Elementals[] array = null;
  968. if (element != -1 && _elementals != null && _elementals.length > 1)
  969. {
  970. array = new Elementals[_elementals.length - 1];
  971. int i = 0;
  972. for (Elementals elm : _elementals)
  973. {
  974. if (elm.getElement() != element)
  975. {
  976. array[i++] = elm;
  977. }
  978. }
  979. }
  980. _elementals = array;
  981. Connection con = null;
  982. try
  983. {
  984. con = L2DatabaseFactory.getInstance().getConnection();
  985. PreparedStatement statement = null;
  986. if (element != -1)
  987. {
  988. //Item can have still others
  989. statement = con.prepareStatement("DELETE FROM item_elementals WHERE itemId = ? AND elemType = ?");
  990. statement.setInt(2, element);
  991. }
  992. else
  993. {
  994. // Remove the entries
  995. statement = con.prepareStatement("DELETE FROM item_elementals WHERE itemId = ?");
  996. }
  997. statement.setInt(1, getObjectId());
  998. statement.executeUpdate();
  999. statement.close();
  1000. }
  1001. catch (Exception e)
  1002. {
  1003. _log.log(Level.SEVERE, "Could not remove elemental enchant for item: "+this+" from DB:", e);
  1004. }
  1005. finally
  1006. {
  1007. L2DatabaseFactory.close(con);
  1008. }
  1009. }
  1010. /**
  1011. * Used to decrease mana
  1012. * (mana means life time for shadow items)
  1013. */
  1014. public static class ScheduleConsumeManaTask implements Runnable
  1015. {
  1016. private final L2ItemInstance _shadowItem;
  1017. public ScheduleConsumeManaTask(L2ItemInstance item)
  1018. {
  1019. _shadowItem = item;
  1020. }
  1021. public void run()
  1022. {
  1023. try
  1024. {
  1025. // decrease mana
  1026. if (_shadowItem != null)
  1027. _shadowItem.decreaseMana(true);
  1028. }
  1029. catch (Exception e)
  1030. {
  1031. _log.log(Level.SEVERE, "", e);
  1032. }
  1033. }
  1034. }
  1035. /**
  1036. * Returns true if this item is a shadow item
  1037. * Shadow items have a limited life-time
  1038. * @return
  1039. */
  1040. public boolean isShadowItem()
  1041. {
  1042. return (_mana >= 0);
  1043. }
  1044. /**
  1045. * Returns the remaining mana of this shadow item
  1046. * @return lifeTime
  1047. */
  1048. public int getMana()
  1049. {
  1050. return _mana;
  1051. }
  1052. /**
  1053. * Decreases the mana of this shadow item,
  1054. * sends a inventory update
  1055. * schedules a new consumption task if non is running
  1056. * optionally one could force a new task
  1057. * @param forces a new consumption task if item is equipped
  1058. */
  1059. public void decreaseMana(boolean resetConsumingMana)
  1060. {
  1061. decreaseMana(resetConsumingMana, 1);
  1062. }
  1063. /**
  1064. * Decreases the mana of this shadow item,
  1065. * sends a inventory update
  1066. * schedules a new consumption task if non is running
  1067. * optionally one could force a new task
  1068. * @param forces a new consumption task if item is equipped
  1069. * @param count how much mana decrease
  1070. */
  1071. public void decreaseMana(boolean resetConsumingMana, int count)
  1072. {
  1073. if (!isShadowItem())
  1074. return;
  1075. if (_mana - count >= 0)
  1076. _mana -= count;
  1077. else
  1078. _mana = 0;
  1079. if (_storedInDb)
  1080. _storedInDb = false;
  1081. if (resetConsumingMana)
  1082. _consumingMana = false;
  1083. final L2PcInstance player = L2World.getInstance().getPlayer(getOwnerId());
  1084. if (player != null)
  1085. {
  1086. SystemMessage sm;
  1087. switch (_mana)
  1088. {
  1089. case 10:
  1090. sm = SystemMessage.getSystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_10);
  1091. sm.addItemName(_item);
  1092. player.sendPacket(sm);
  1093. break;
  1094. case 5:
  1095. sm = SystemMessage.getSystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_5);
  1096. sm.addItemName(_item);
  1097. player.sendPacket(sm);
  1098. break;
  1099. case 1:
  1100. sm = SystemMessage.getSystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_1);
  1101. sm.addItemName(_item);
  1102. player.sendPacket(sm);
  1103. break;
  1104. }
  1105. if (_mana == 0) // The life time has expired
  1106. {
  1107. sm = SystemMessage.getSystemMessage(SystemMessageId.S1S_REMAINING_MANA_IS_NOW_0);
  1108. sm.addItemName(_item);
  1109. player.sendPacket(sm);
  1110. // unequip
  1111. if (isEquipped())
  1112. {
  1113. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInSlotAndRecord(getLocationSlot());
  1114. InventoryUpdate iu = new InventoryUpdate();
  1115. for (L2ItemInstance item: unequiped)
  1116. {
  1117. player.checkSShotsMatch(null, item);
  1118. iu.addModifiedItem(item);
  1119. }
  1120. player.sendPacket(iu);
  1121. player.broadcastUserInfo();
  1122. }
  1123. if (getLocation() != ItemLocation.WAREHOUSE)
  1124. {
  1125. // destroy
  1126. player.getInventory().destroyItem("L2ItemInstance", this, player, null);
  1127. // send update
  1128. InventoryUpdate iu = new InventoryUpdate();
  1129. iu.addRemovedItem(this);
  1130. player.sendPacket(iu);
  1131. StatusUpdate su = new StatusUpdate(player);
  1132. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  1133. player.sendPacket(su);
  1134. }
  1135. else
  1136. {
  1137. player.getWarehouse().destroyItem("L2ItemInstance", this, player, null);
  1138. }
  1139. // delete from world
  1140. L2World.getInstance().removeObject(this);
  1141. }
  1142. else
  1143. {
  1144. // Reschedule if still equipped
  1145. if (!_consumingMana && isEquipped())
  1146. {
  1147. scheduleConsumeManaTask();
  1148. }
  1149. if (getLocation() != ItemLocation.WAREHOUSE)
  1150. {
  1151. InventoryUpdate iu = new InventoryUpdate();
  1152. iu.addModifiedItem(this);
  1153. player.sendPacket(iu);
  1154. }
  1155. }
  1156. }
  1157. }
  1158. public void scheduleConsumeManaTask()
  1159. {
  1160. if (_consumingMana)
  1161. return;
  1162. _consumingMana = true;
  1163. ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleConsumeManaTask(this), MANA_CONSUMPTION_RATE);
  1164. }
  1165. /**
  1166. * Returns false cause item can't be attacked
  1167. * @return boolean false
  1168. */
  1169. @Override
  1170. public boolean isAutoAttackable(L2Character attacker)
  1171. {
  1172. return false;
  1173. }
  1174. /**
  1175. * Returns the type of charge with SoulShot of the item.
  1176. * @return int (CHARGED_NONE, CHARGED_SOULSHOT)
  1177. */
  1178. public int getChargedSoulshot()
  1179. {
  1180. return _chargedSoulshot;
  1181. }
  1182. /**
  1183. * Returns the type of charge with SpiritShot of the item
  1184. * @return int (CHARGED_NONE, CHARGED_SPIRITSHOT, CHARGED_BLESSED_SPIRITSHOT)
  1185. */
  1186. public int getChargedSpiritshot()
  1187. {
  1188. return _chargedSpiritshot;
  1189. }
  1190. public boolean getChargedFishshot()
  1191. {
  1192. return _chargedFishtshot;
  1193. }
  1194. /**
  1195. * Sets the type of charge with SoulShot of the item
  1196. * @param type : int (CHARGED_NONE, CHARGED_SOULSHOT)
  1197. */
  1198. public void setChargedSoulshot(int type)
  1199. {
  1200. _chargedSoulshot = type;
  1201. }
  1202. /**
  1203. * Sets the type of charge with SpiritShot of the item
  1204. * @param type : int (CHARGED_NONE, CHARGED_SPIRITSHOT, CHARGED_BLESSED_SPIRITSHOT)
  1205. */
  1206. public void setChargedSpiritshot(int type)
  1207. {
  1208. _chargedSpiritshot = type;
  1209. }
  1210. public void setChargedFishshot(boolean type)
  1211. {
  1212. _chargedFishtshot = type;
  1213. }
  1214. /**
  1215. * This function basically returns a set of functions from
  1216. * L2Item/L2Armor/L2Weapon, but may add additional
  1217. * functions, if this particular item instance is enhanched
  1218. * for a particular player.
  1219. * @param player : L2Character designating the player
  1220. * @return Func[]
  1221. */
  1222. public Func[] getStatFuncs(L2Character player)
  1223. {
  1224. return getItem().getStatFuncs(this, player);
  1225. }
  1226. /**
  1227. * Updates the database.<BR>
  1228. */
  1229. public void updateDatabase()
  1230. {
  1231. this.updateDatabase(false);
  1232. }
  1233. /**
  1234. * Updates the database.<BR>
  1235. *
  1236. * @param force if the update should necessarilly be done.
  1237. */
  1238. public void updateDatabase(boolean force)
  1239. {
  1240. _dbLock.lock();
  1241. try
  1242. {
  1243. if (_existsInDb)
  1244. {
  1245. if (_ownerId == 0
  1246. || _loc == ItemLocation.VOID
  1247. || _loc == ItemLocation.REFUND
  1248. || (getCount() == 0 && _loc != ItemLocation.LEASE))
  1249. {
  1250. removeFromDb();
  1251. }
  1252. else if (!Config.LAZY_ITEMS_UPDATE || force)
  1253. {
  1254. updateInDb();
  1255. }
  1256. }
  1257. else
  1258. {
  1259. if (_ownerId == 0
  1260. || _loc == ItemLocation.VOID
  1261. || _loc == ItemLocation.REFUND
  1262. || (getCount() == 0 && _loc != ItemLocation.LEASE))
  1263. {
  1264. return;
  1265. }
  1266. insertIntoDb();
  1267. }
  1268. }
  1269. finally
  1270. {
  1271. _dbLock.unlock();
  1272. }
  1273. }
  1274. /**
  1275. * Returns a L2ItemInstance stored in database from its objectID
  1276. * @param objectId : int designating the objectID of the item
  1277. * @return L2ItemInstance
  1278. */
  1279. public static L2ItemInstance restoreFromDb(int ownerId, ResultSet rs)
  1280. {
  1281. L2ItemInstance inst = null;
  1282. int objectId, item_id, loc_data, enchant_level, custom_type1, custom_type2, manaLeft;
  1283. long time, count;
  1284. ItemLocation loc;
  1285. try
  1286. {
  1287. objectId = rs.getInt(1);
  1288. item_id = rs.getInt("item_id");
  1289. count = rs.getLong("count");
  1290. loc = ItemLocation.valueOf(rs.getString("loc"));
  1291. loc_data = rs.getInt("loc_data");
  1292. enchant_level = rs.getInt("enchant_level");
  1293. custom_type1 = rs.getInt("custom_type1");
  1294. custom_type2 = rs.getInt("custom_type2");
  1295. manaLeft = rs.getInt("mana_left");
  1296. time = rs.getLong("time");
  1297. } catch (Exception e) {
  1298. _log.log(Level.SEVERE, "Could not restore an item owned by "+ownerId+" from DB:", e);
  1299. return null;
  1300. }
  1301. L2Item item = ItemTable.getInstance().getTemplate(item_id);
  1302. if (item == null) {
  1303. _log.severe("Item item_id="+item_id+" not known, object_id="+objectId);
  1304. return null;
  1305. }
  1306. inst = new L2ItemInstance(objectId, item);
  1307. inst._ownerId = ownerId;
  1308. inst.setCount(count);
  1309. inst._enchantLevel = enchant_level;
  1310. inst._type1 = custom_type1;
  1311. inst._type2 = custom_type2;
  1312. inst._loc = loc;
  1313. inst._locData = loc_data;
  1314. inst._existsInDb = true;
  1315. inst._storedInDb = true;
  1316. // Setup life time for shadow weapons
  1317. inst._mana = manaLeft;
  1318. inst._time = time;
  1319. //load augmentation and elemental enchant
  1320. if (inst.isEquipable())
  1321. inst.restoreAttributes();
  1322. return inst;
  1323. }
  1324. /**
  1325. * Init a dropped L2ItemInstance and add it in the world as a visible object.<BR><BR>
  1326. *
  1327. * <B><U> Actions</U> :</B><BR><BR>
  1328. * <li>Set the x,y,z position of the L2ItemInstance dropped and update its _worldregion </li>
  1329. * <li>Add the L2ItemInstance dropped to _visibleObjects of its L2WorldRegion</li>
  1330. * <li>Add the L2ItemInstance dropped in the world as a <B>visible</B> object</li><BR><BR>
  1331. *
  1332. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T ADD the object to _allObjects of L2World </B></FONT><BR><BR>
  1333. *
  1334. * <B><U> Assert </U> :</B><BR><BR>
  1335. * <li> _worldRegion == null <I>(L2Object is invisible at the beginning)</I></li><BR><BR>
  1336. *
  1337. * <B><U> Example of use </U> :</B><BR><BR>
  1338. * <li> Drop item</li>
  1339. * <li> Call Pet</li><BR>
  1340. *
  1341. */
  1342. public class ItemDropTask implements Runnable
  1343. {
  1344. private int _x,_y,_z;
  1345. private final L2Character _dropper;
  1346. private final L2ItemInstance _itm;
  1347. public ItemDropTask(L2ItemInstance item, L2Character dropper, int x, int y, int z)
  1348. {
  1349. _x = x;
  1350. _y = y;
  1351. _z = z;
  1352. _dropper = dropper;
  1353. _itm = item;
  1354. }
  1355. public final void run()
  1356. {
  1357. assert _itm.getPosition().getWorldRegion() == null;
  1358. if (Config.GEODATA > 0 && _dropper != null)
  1359. {
  1360. Location dropDest = GeoData.getInstance().moveCheck(_dropper.getX(), _dropper.getY(), _dropper.getZ(), _x, _y, _z, _dropper.getInstanceId());
  1361. _x = dropDest.getX();
  1362. _y = dropDest.getY();
  1363. _z = dropDest.getZ();
  1364. }
  1365. if(_dropper != null)
  1366. setInstanceId(_dropper.getInstanceId()); // Inherit instancezone when dropped in visible world
  1367. else
  1368. setInstanceId(0); // No dropper? Make it a global item...
  1369. synchronized (_itm)
  1370. {
  1371. // Set the x,y,z position of the L2ItemInstance dropped and update its _worldregion
  1372. _itm.setIsVisible(true);
  1373. _itm.getPosition().setWorldPosition(_x, _y ,_z);
  1374. _itm.getPosition().setWorldRegion(L2World.getInstance().getRegion(getPosition().getWorldPosition()));
  1375. // Add the L2ItemInstance dropped to _visibleObjects of its L2WorldRegion
  1376. }
  1377. _itm.getPosition().getWorldRegion().addVisibleObject(_itm);
  1378. _itm.setDropTime(System.currentTimeMillis());
  1379. _itm.setDropperObjectId(_dropper != null ? _dropper.getObjectId() : 0); //Set the dropper Id for the knownlist packets in sendInfo
  1380. // this can synchronize on others instancies, so it's out of
  1381. // synchronized, to avoid deadlocks
  1382. // Add the L2ItemInstance dropped in the world as a visible object
  1383. L2World.getInstance().addVisibleObject(_itm, _itm.getPosition().getWorldRegion());
  1384. if (Config.SAVE_DROPPED_ITEM)
  1385. ItemsOnGroundManager.getInstance().save(_itm);
  1386. _itm.setDropperObjectId(0); //Set the dropper Id back to 0 so it no longer shows the drop packet
  1387. }
  1388. }
  1389. public final void dropMe(L2Character dropper, int x, int y, int z)
  1390. {
  1391. ThreadPoolManager.getInstance().executeTask(new ItemDropTask(this, dropper, x, y, z));
  1392. }
  1393. /**
  1394. * Update the database with values of the item
  1395. */
  1396. private void updateInDb()
  1397. {
  1398. assert _existsInDb;
  1399. if (_wear)
  1400. return;
  1401. if (_storedInDb)
  1402. return;
  1403. Connection con = null;
  1404. PreparedStatement statement = null;
  1405. try
  1406. {
  1407. con = L2DatabaseFactory.getInstance().getConnection();
  1408. statement = con.prepareStatement(
  1409. "UPDATE items SET owner_id=?,count=?,loc=?,loc_data=?,enchant_level=?,custom_type1=?,custom_type2=?,mana_left=?,time=? " +
  1410. "WHERE object_id = ?");
  1411. statement.setInt(1, _ownerId);
  1412. statement.setLong(2, getCount());
  1413. statement.setString(3, _loc.name());
  1414. statement.setInt(4, _locData);
  1415. statement.setInt(5, getEnchantLevel());
  1416. statement.setInt(6, getCustomType1());
  1417. statement.setInt(7, getCustomType2());
  1418. statement.setInt(8, getMana());
  1419. statement.setLong(9, getTime());
  1420. statement.setInt(10, getObjectId());
  1421. statement.executeUpdate();
  1422. _existsInDb = true;
  1423. _storedInDb = true;
  1424. statement.close();
  1425. }
  1426. catch (Exception e)
  1427. {
  1428. _log.log(Level.SEVERE, "Could not update item "+this+" in DB: Reason: "+e.getMessage(), e);
  1429. }
  1430. finally
  1431. {
  1432. L2DatabaseFactory.close(con);
  1433. }
  1434. }
  1435. /**
  1436. * Insert the item in database
  1437. */
  1438. private void insertIntoDb()
  1439. {
  1440. assert !_existsInDb && getObjectId() != 0;
  1441. if (_wear)
  1442. return;
  1443. Connection con = null;
  1444. PreparedStatement statement = null;
  1445. try
  1446. {
  1447. con = L2DatabaseFactory.getInstance().getConnection();
  1448. statement = con.prepareStatement(
  1449. "INSERT INTO items (owner_id,item_id,count,loc,loc_data,enchant_level,object_id,custom_type1,custom_type2,mana_left,time) " +
  1450. "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
  1451. statement.setInt(1, _ownerId);
  1452. statement.setInt(2, _itemId);
  1453. statement.setLong(3, getCount());
  1454. statement.setString(4, _loc.name());
  1455. statement.setInt(5, _locData);
  1456. statement.setInt(6, getEnchantLevel());
  1457. statement.setInt(7, getObjectId());
  1458. statement.setInt(8, _type1);
  1459. statement.setInt(9, _type2);
  1460. statement.setInt(10, getMana());
  1461. statement.setLong(11, getTime());
  1462. statement.executeUpdate();
  1463. _existsInDb = true;
  1464. _storedInDb = true;
  1465. statement.close();
  1466. if (_augmentation != null)
  1467. updateItemAttributes(con);
  1468. if (_elementals != null)
  1469. updateItemElements(con);
  1470. }
  1471. catch (Exception e)
  1472. {
  1473. _log.log(Level.SEVERE, "Could not insert item " + this + " into DB: Reason: " + e.getMessage(), e);
  1474. }
  1475. finally
  1476. {
  1477. L2DatabaseFactory.close(con);
  1478. }
  1479. }
  1480. /**
  1481. * Delete item from database
  1482. */
  1483. private void removeFromDb()
  1484. {
  1485. assert _existsInDb;
  1486. if (_wear)
  1487. return;
  1488. Connection con = null;
  1489. PreparedStatement statement = null;
  1490. try
  1491. {
  1492. con = L2DatabaseFactory.getInstance().getConnection();
  1493. statement = con.prepareStatement("DELETE FROM items WHERE object_id=?");
  1494. statement.setInt(1, getObjectId());
  1495. statement.executeUpdate();
  1496. _existsInDb = false;
  1497. _storedInDb = false;
  1498. statement.close();
  1499. statement = con.prepareStatement("DELETE FROM item_attributes WHERE itemId = ?");
  1500. statement.setInt(1, getObjectId());
  1501. statement.executeUpdate();
  1502. statement.close();
  1503. }
  1504. catch (Exception e)
  1505. {
  1506. _log.log(Level.SEVERE, "Could not delete item "+this+" in DB: "+e.getMessage(), e);
  1507. }
  1508. finally
  1509. {
  1510. L2DatabaseFactory.close(con);
  1511. }
  1512. }
  1513. /**
  1514. * Returns the item in String format
  1515. * @return String
  1516. */
  1517. @Override
  1518. public String toString()
  1519. {
  1520. return _item+"["+getObjectId()+"]";
  1521. }
  1522. public void resetOwnerTimer()
  1523. {
  1524. if(itemLootShedule != null)
  1525. itemLootShedule.cancel(true);
  1526. itemLootShedule = null;
  1527. }
  1528. public void setItemLootShedule(ScheduledFuture<?> sf)
  1529. {
  1530. itemLootShedule = sf;
  1531. }
  1532. public ScheduledFuture<?> getItemLootShedule()
  1533. {
  1534. return itemLootShedule;
  1535. }
  1536. public void setProtected(boolean is_protected)
  1537. {
  1538. _protected = is_protected;
  1539. }
  1540. public boolean isProtected()
  1541. {
  1542. return _protected;
  1543. }
  1544. public boolean isNightLure()
  1545. {
  1546. return ((_itemId >= 8505 && _itemId <= 8513) || _itemId == 8485);
  1547. }
  1548. public void setCountDecrease(boolean decrease)
  1549. {
  1550. _decrease = decrease;
  1551. }
  1552. public boolean getCountDecrease()
  1553. {
  1554. return _decrease;
  1555. }
  1556. public void setInitCount(int InitCount)
  1557. {
  1558. _initCount = InitCount;
  1559. }
  1560. public long getInitCount()
  1561. {
  1562. return _initCount;
  1563. }
  1564. public void restoreInitCount()
  1565. {
  1566. if(_decrease)
  1567. setCount(_initCount);
  1568. }
  1569. public boolean isTimeLimitedItem()
  1570. {
  1571. return (_time > 0);
  1572. }
  1573. /**
  1574. * Returns (current system time + time) of this time limited item
  1575. * @return Time
  1576. */
  1577. public long getTime()
  1578. {
  1579. return _time;
  1580. }
  1581. public long getRemainingTime()
  1582. {
  1583. return _time - System.currentTimeMillis();
  1584. }
  1585. public void endOfLife()
  1586. {
  1587. L2PcInstance player = L2World.getInstance().getPlayer(getOwnerId());
  1588. if (player != null)
  1589. {
  1590. if (isEquipped())
  1591. {
  1592. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInSlotAndRecord(getLocationSlot());
  1593. InventoryUpdate iu = new InventoryUpdate();
  1594. for (L2ItemInstance item: unequiped)
  1595. {
  1596. player.checkSShotsMatch(null, item);
  1597. iu.addModifiedItem(item);
  1598. }
  1599. player.sendPacket(iu);
  1600. player.broadcastUserInfo();
  1601. }
  1602. if (getLocation() != ItemLocation.WAREHOUSE)
  1603. {
  1604. // destroy
  1605. player.getInventory().destroyItem("L2ItemInstance", this, player, null);
  1606. // send update
  1607. InventoryUpdate iu = new InventoryUpdate();
  1608. iu.addRemovedItem(this);
  1609. player.sendPacket(iu);
  1610. StatusUpdate su = new StatusUpdate(player);
  1611. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  1612. player.sendPacket(su);
  1613. }
  1614. else
  1615. {
  1616. player.getWarehouse().destroyItem("L2ItemInstance", this, player, null);
  1617. }
  1618. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.TIME_LIMITED_ITEM_DELETED));
  1619. // delete from world
  1620. L2World.getInstance().removeObject(this);
  1621. }
  1622. }
  1623. public void scheduleLifeTimeTask()
  1624. {
  1625. if (!isTimeLimitedItem()) return;
  1626. if (getRemainingTime() <= 0)
  1627. endOfLife();
  1628. else
  1629. {
  1630. if (_lifeTimeTask != null)
  1631. _lifeTimeTask.cancel(false);
  1632. _lifeTimeTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleLifeTimeTask(this), getRemainingTime());
  1633. }
  1634. }
  1635. public static class ScheduleLifeTimeTask implements Runnable
  1636. {
  1637. private final L2ItemInstance _limitedItem;
  1638. public ScheduleLifeTimeTask(L2ItemInstance item)
  1639. {
  1640. _limitedItem = item;
  1641. }
  1642. public void run()
  1643. {
  1644. try
  1645. {
  1646. if (_limitedItem != null)
  1647. _limitedItem.endOfLife();
  1648. }
  1649. catch (Exception e)
  1650. {
  1651. _log.log(Level.SEVERE, "", e);
  1652. }
  1653. }
  1654. }
  1655. public void updateElementAttrBonus(L2PcInstance player)
  1656. {
  1657. if (_elementals == null)
  1658. return;
  1659. for (Elementals elm : _elementals)
  1660. elm.updateBonus(player, isArmor());
  1661. }
  1662. public void removeElementAttrBonus(L2PcInstance player)
  1663. {
  1664. if (_elementals == null)
  1665. return;
  1666. for (Elementals elm : _elementals)
  1667. elm.removeBonus(player);
  1668. }
  1669. public void setDropperObjectId(int id)
  1670. {
  1671. _dropperObjectId = id;
  1672. }
  1673. @Override
  1674. public void sendInfo(L2PcInstance activeChar)
  1675. {
  1676. if (_dropperObjectId != 0)
  1677. activeChar.sendPacket(new DropItem(this, _dropperObjectId));
  1678. else
  1679. activeChar.sendPacket(new SpawnItem(this));
  1680. }
  1681. public boolean isPublished()
  1682. {
  1683. return _published;
  1684. }
  1685. public void publish()
  1686. {
  1687. _published = true;
  1688. }
  1689. /* (non-Javadoc)
  1690. * @see com.l2jserver.gameserver.model.L2Object#decayMe()
  1691. */
  1692. @Override
  1693. public void decayMe()
  1694. {
  1695. if (Config.SAVE_DROPPED_ITEM)
  1696. ItemsOnGroundManager.getInstance().removeObject(this);
  1697. super.decayMe();
  1698. }
  1699. public boolean isQuestItem()
  1700. {
  1701. return getItem().isQuestItem();
  1702. }
  1703. }