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