L2ItemInstance.java 49 KB

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