L2ItemInstance.java 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model;
  16. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.ADENA_ID;
  17. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.MAX_ADENA;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.util.concurrent.ScheduledFuture;
  23. import java.util.concurrent.locks.ReentrantLock;
  24. import java.util.logging.Level;
  25. import java.util.logging.LogRecord;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.GeoData;
  30. import com.l2jserver.gameserver.ThreadPoolManager;
  31. import com.l2jserver.gameserver.datatables.ItemTable;
  32. import com.l2jserver.gameserver.instancemanager.ItemsOnGroundManager;
  33. import com.l2jserver.gameserver.instancemanager.MercTicketManager;
  34. import com.l2jserver.gameserver.model.actor.L2Character;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  36. import com.l2jserver.gameserver.model.actor.knownlist.NullKnownList;
  37. import com.l2jserver.gameserver.model.quest.QuestState;
  38. import com.l2jserver.gameserver.network.SystemMessageId;
  39. import com.l2jserver.gameserver.network.serverpackets.DropItem;
  40. import com.l2jserver.gameserver.network.serverpackets.GetItem;
  41. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  42. import com.l2jserver.gameserver.network.serverpackets.SpawnItem;
  43. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  44. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  45. import com.l2jserver.gameserver.skills.funcs.Func;
  46. import com.l2jserver.gameserver.templates.item.L2Armor;
  47. import com.l2jserver.gameserver.templates.item.L2EtcItem;
  48. import com.l2jserver.gameserver.templates.item.L2EtcItemType;
  49. import com.l2jserver.gameserver.templates.item.L2Item;
  50. import com.l2jserver.gameserver.templates.item.L2ItemType;
  51. import com.l2jserver.gameserver.templates.item.L2Weapon;
  52. import com.l2jserver.gameserver.util.GMAudit;
  53. /**
  54. * This class manages items.
  55. *
  56. * @version $Revision: 1.4.2.1.2.11 $ $Date: 2005/03/31 16:07:50 $
  57. */
  58. public final class L2ItemInstance extends L2Object
  59. {
  60. protected static final Logger _log = Logger.getLogger(L2ItemInstance.class.getName());
  61. private static final Logger _logItems = Logger.getLogger("item");
  62. /** Enumeration of locations for item */
  63. public static enum ItemLocation
  64. {
  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><BR>
  185. *
  186. * <B><U> Actions</U> :</B><BR><BR>
  187. * <li>Send a Server->Client Packet GetItem to player that pick up and its _knowPlayers member </li>
  188. * <li>Remove the L2Object from the world</li><BR><BR>
  189. *
  190. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World </B></FONT><BR><BR>
  191. *
  192. * <B><U> Assert </U> :</B><BR><BR>
  193. * <li> this instanceof L2ItemInstance</li>
  194. * <li> _worldRegion != null <I>(L2Object is visible at the beginning)</I></li><BR><BR>
  195. *
  196. * <B><U> Example of use </U> :</B><BR><BR>
  197. * <li> Do Pickup Item : PCInstance and Pet</li><BR><BR>
  198. *
  199. * @param player Player that pick up the item
  200. *
  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 == 57 || 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[]{this, creator, reference});
  254. _logItems.log(record);
  255. }
  256. }
  257. if (creator != null)
  258. {
  259. if (creator.isGM())
  260. {
  261. String referenceName = "no-reference";
  262. if (reference instanceof L2Object)
  263. {
  264. referenceName = (((L2Object)reference).getName() != null?((L2Object)reference).getName():"no-name");
  265. }
  266. else if (reference instanceof String)
  267. referenceName = (String)reference;
  268. String targetName = (creator.getTarget() != null?creator.getTarget().getName():"no-target");
  269. if (Config.GMAUDIT)
  270. GMAudit.auditGMAction(creator.getName()+" ["+creator.getObjectId()+"]",
  271. process + "(id: "+getItemId()+" name: "+getName()+")", targetName,
  272. "L2Object referencing this action is: " + referenceName);
  273. }
  274. }
  275. }
  276. /**
  277. * Sets the ownerID of the item
  278. * @param owner_id : int designating the ID of the owner
  279. */
  280. public void setOwnerId(int owner_id)
  281. {
  282. if (owner_id == _ownerId) return;
  283. _ownerId = owner_id;
  284. _storedInDb = false;
  285. }
  286. /**
  287. * Returns the ownerID of the item
  288. * @return int : ownerID of the item
  289. */
  290. public int getOwnerId()
  291. {
  292. return _ownerId;
  293. }
  294. /**
  295. * Sets the location of the item
  296. * @param loc : ItemLocation (enumeration)
  297. */
  298. public void setLocation(ItemLocation loc)
  299. {
  300. setLocation(loc, 0);
  301. }
  302. /**
  303. * Sets the location of the item.<BR><BR>
  304. * <U><I>Remark :</I></U> If loc and loc_data different from database, say datas not up-to-date
  305. * @param loc : ItemLocation (enumeration)
  306. * @param loc_data : int designating the slot where the item is stored or the village for freights
  307. */
  308. public void setLocation(ItemLocation loc, int loc_data)
  309. {
  310. if (loc == _loc && loc_data == _locData)
  311. return;
  312. _loc = loc;
  313. _locData = loc_data;
  314. _storedInDb = false;
  315. }
  316. public ItemLocation getLocation()
  317. {
  318. return _loc;
  319. }
  320. /**
  321. * Sets the quantity of the item.<BR><BR>
  322. * @param count the new count to set
  323. */
  324. public void setCount(long count)
  325. {
  326. if (getCount() == count)
  327. {
  328. return;
  329. }
  330. _count = count >= -1 ? count : 0;
  331. _storedInDb = false;
  332. }
  333. /**
  334. * @return Returns the count.
  335. */
  336. public long getCount()
  337. {
  338. return _count;
  339. }
  340. /**
  341. * Sets the quantity of the item.<BR><BR>
  342. * <U><I>Remark :</I></U> If loc and loc_data different from database, say datas not up-to-date
  343. * @param process : String Identifier of process triggering this action
  344. * @param count : int
  345. * @param creator : L2PcInstance Player requesting the item creation
  346. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  347. */
  348. public void changeCount(String process, long count, L2PcInstance creator, Object reference)
  349. {
  350. if (count == 0)
  351. {
  352. return;
  353. }
  354. long old = getCount();
  355. long max = getItemId() == ADENA_ID ? MAX_ADENA : Integer.MAX_VALUE;
  356. if ( count > 0 && getCount() > max - count)
  357. {
  358. setCount(max);
  359. }
  360. else
  361. {
  362. setCount(getCount() + count);
  363. }
  364. if (getCount() < 0)
  365. {
  366. setCount(0);
  367. }
  368. _storedInDb = false;
  369. if (Config.LOG_ITEMS && process != null)
  370. {
  371. if (!Config.LOG_ITEMS_SMALL_LOG || (Config.LOG_ITEMS_SMALL_LOG && (_item.isEquipable() || _item.getItemId() == ADENA_ID)))
  372. {
  373. LogRecord record = new LogRecord(Level.INFO, "CHANGE:" + process);
  374. record.setLoggerName("item");
  375. record.setParameters(new Object[]{this, "PrevCount("+old+")",creator, reference});
  376. _logItems.log(record);
  377. }
  378. }
  379. if (creator != null)
  380. {
  381. if (creator.isGM())
  382. {
  383. String referenceName = "no-reference";
  384. if (reference instanceof L2Object)
  385. {
  386. referenceName = (((L2Object)reference).getName() != null?((L2Object)reference).getName():"no-name");
  387. }
  388. else if (reference instanceof String)
  389. referenceName = (String)reference;
  390. String targetName = (creator.getTarget() != null?creator.getTarget().getName():"no-target");
  391. if (Config.GMAUDIT)
  392. GMAudit.auditGMAction(creator.getName()+" ["+creator.getObjectId()+"]",
  393. process + "(id: "+getItemId()+" objId: "+getObjectId()+
  394. " name: "+getName()+" count: "+count+")", targetName,
  395. "L2Object referencing this action is: " + referenceName);
  396. }
  397. }
  398. }
  399. // No logging (function designed for shots only)
  400. public void changeCountWithoutTrace(int count, L2PcInstance creator, Object reference)
  401. {
  402. this.changeCount(null, count, creator, reference);
  403. }
  404. /**
  405. * Returns if item is equipable
  406. * @return boolean
  407. */
  408. public boolean isEquipable()
  409. {
  410. return !(_item.getBodyPart() == 0 || _item.getItemType() == L2EtcItemType.ARROW || _item.getItemType() == L2EtcItemType.BOLT || _item.getItemType() == L2EtcItemType.LURE );
  411. }
  412. /**
  413. * Returns if item is equipped
  414. * @return boolean
  415. */
  416. public boolean isEquipped()
  417. {
  418. return _loc == ItemLocation.PAPERDOLL || _loc == ItemLocation.PET_EQUIP;
  419. }
  420. /**
  421. * Returns the slot where the item is stored
  422. * @return int
  423. */
  424. public int getLocationSlot()
  425. {
  426. assert _loc == ItemLocation.PAPERDOLL || _loc == ItemLocation.PET_EQUIP || _loc == ItemLocation.INVENTORY || _loc == ItemLocation.MAIL || _loc == ItemLocation.FREIGHT;
  427. return _locData;
  428. }
  429. /**
  430. * Returns the characteristics of the item
  431. * @return L2Item
  432. */
  433. public L2Item getItem()
  434. {
  435. return _item;
  436. }
  437. public int getCustomType1()
  438. {
  439. return _type1;
  440. }
  441. public int getCustomType2()
  442. {
  443. return _type2;
  444. }
  445. public void setCustomType1(int newtype)
  446. {
  447. _type1=newtype;
  448. }
  449. public void setCustomType2(int newtype)
  450. {
  451. _type2=newtype;
  452. }
  453. public void setDropTime(long time)
  454. {
  455. _dropTime=time;
  456. }
  457. public long getDropTime()
  458. {
  459. return _dropTime;
  460. }
  461. /**
  462. * Returns the type of item
  463. * @return Enum
  464. */
  465. public L2ItemType getItemType()
  466. {
  467. return _item.getItemType();
  468. }
  469. /**
  470. * Returns the ID of the item
  471. * @return int
  472. */
  473. public int getItemId()
  474. {
  475. return _itemId;
  476. }
  477. /**
  478. * Returns true if item is an EtcItem
  479. * @return boolean
  480. */
  481. public boolean isEtcItem()
  482. {
  483. return (_item instanceof L2EtcItem);
  484. }
  485. /**
  486. * Returns true if item is a Weapon/Shield
  487. * @return boolean
  488. */
  489. public boolean isWeapon()
  490. {
  491. return (_item instanceof L2Weapon);
  492. }
  493. /**
  494. * Returns true if item is an Armor
  495. * @return boolean
  496. */
  497. public boolean isArmor()
  498. {
  499. return (_item instanceof L2Armor);
  500. }
  501. /**
  502. * Returns the characteristics of the L2EtcItem
  503. * @return L2EtcItem
  504. */
  505. public L2EtcItem getEtcItem()
  506. {
  507. if (_item instanceof L2EtcItem)
  508. {
  509. return (L2EtcItem) _item;
  510. }
  511. return null;
  512. }
  513. /**
  514. * Returns the characteristics of the L2Weapon
  515. * @return L2Weapon
  516. */
  517. public L2Weapon getWeaponItem()
  518. {
  519. if (_item instanceof L2Weapon)
  520. {
  521. return (L2Weapon) _item;
  522. }
  523. return null;
  524. }
  525. /**
  526. * Returns the characteristics of the L2Armor
  527. * @return L2Armor
  528. */
  529. public L2Armor getArmorItem()
  530. {
  531. if (_item instanceof L2Armor)
  532. {
  533. return (L2Armor) _item;
  534. }
  535. return null;
  536. }
  537. /**
  538. * Returns the quantity of crystals for crystallization
  539. *
  540. * @return int
  541. */
  542. public final int getCrystalCount()
  543. {
  544. return _item.getCrystalCount(_enchantLevel);
  545. }
  546. /**
  547. * Returns the reference price of the item
  548. * @return int
  549. */
  550. public int getReferencePrice()
  551. {
  552. return _item.getReferencePrice();
  553. }
  554. /**
  555. * Returns the name of the item
  556. * @return String
  557. */
  558. public String getItemName()
  559. {
  560. return _item.getName();
  561. }
  562. /**
  563. * Returns the last change of the item
  564. * @return int
  565. */
  566. public int getLastChange()
  567. {
  568. return _lastChange;
  569. }
  570. /**
  571. * Sets the last change of the item
  572. * @param lastChange : int
  573. */
  574. public void setLastChange(int lastChange)
  575. {
  576. _lastChange = lastChange;
  577. }
  578. /**
  579. * Returns if item is stackable
  580. * @return boolean
  581. */
  582. public boolean isStackable()
  583. {
  584. return _item.isStackable();
  585. }
  586. /**
  587. * Returns if item is dropable
  588. * @return boolean
  589. */
  590. public boolean isDropable()
  591. {
  592. return isAugmented() ? false : _item.isDropable();
  593. }
  594. /**
  595. * Returns if item is destroyable
  596. * @return boolean
  597. */
  598. public boolean isDestroyable()
  599. {
  600. return _item.isDestroyable();
  601. }
  602. /**
  603. * Returns if item is tradeable
  604. * @return boolean
  605. */
  606. public boolean isTradeable()
  607. {
  608. return isAugmented() ? false : _item.isTradeable();
  609. }
  610. /**
  611. * Returns if item is sellable
  612. * @return boolean
  613. */
  614. public boolean isSellable()
  615. {
  616. return isAugmented() ? false : _item.isSellable();
  617. }
  618. /**
  619. * @param isPrivateWareHouse
  620. * @return if item can be deposited in warehouse or freight
  621. */
  622. public boolean isDepositable(boolean isPrivateWareHouse)
  623. {
  624. // equipped, hero and quest items
  625. if (isEquipped() || !_item.isDepositable())
  626. return false;
  627. if (!isPrivateWareHouse)
  628. {
  629. // augmented not tradeable
  630. if (!isTradeable() || isShadowItem())
  631. return false;
  632. }
  633. return true;
  634. }
  635. /**
  636. * Returns if item is consumable
  637. * @return boolean
  638. */
  639. public boolean isConsumable()
  640. {
  641. return _item.isConsumable();
  642. }
  643. public boolean isPotion()
  644. {
  645. return _item.isPotion();
  646. }
  647. public boolean isElixir()
  648. {
  649. return _item.isElixir();
  650. }
  651. public boolean isHeroItem()
  652. {
  653. return _item.isHeroItem();
  654. }
  655. public boolean isCommonItem()
  656. {
  657. return _item.isCommon();
  658. }
  659. /**
  660. * Returns whether this item is pvp or not
  661. * @return boolean
  662. */
  663. public boolean isPvp()
  664. {
  665. return _item.isPvpItem();
  666. }
  667. public boolean isOlyRestrictedItem()
  668. {
  669. return (Config.LIST_OLY_RESTRICTED_ITEMS.contains(_itemId));
  670. }
  671. /**
  672. * @param player
  673. * @param allowAdena
  674. * @param allowNonTradeable
  675. * @return if item is available for manipulation
  676. */
  677. public boolean isAvailable(L2PcInstance player, boolean allowAdena, boolean allowNonTradeable)
  678. {
  679. return (
  680. (!isEquipped()) // Not equipped
  681. && (getItem().getType2() != L2Item.TYPE2_QUEST) // Not Quest Item
  682. && (getItem().getType2() != L2Item.TYPE2_MONEY || getItem().getType1() != L2Item.TYPE1_SHIELD_ARMOR) // not money, not shield
  683. && (player.getPet() == null || getObjectId() != player.getPet().getControlObjectId()) // Not Control item of currently summoned pet
  684. && (player.getActiveEnchantItem() != this) // Not momentarily used enchant scroll
  685. && (allowAdena || getItemId() != 57) // Not adena
  686. && (player.getCurrentSkill() == null || player.getCurrentSkill().getSkill().getItemConsumeId() != getItemId())
  687. && (!player.isCastingSimultaneouslyNow() || player.getLastSimultaneousSkillCast() == null || player.getLastSimultaneousSkillCast().getItemConsumeId() != getItemId())
  688. && (allowNonTradeable || isTradeable()
  689. && (!(getItem().getItemType() == L2EtcItemType.PET_COLLAR && player.havePetInvItems())))
  690. );
  691. }
  692. /**
  693. * Returns the level of enchantment of the item
  694. * @return int
  695. */
  696. public int getEnchantLevel()
  697. {
  698. return _enchantLevel;
  699. }
  700. /**
  701. * @param enchantLevel the enchant value to set
  702. */
  703. public void setEnchantLevel(int enchantLevel)
  704. {
  705. if (_enchantLevel == enchantLevel)
  706. return;
  707. _enchantLevel = enchantLevel;
  708. _storedInDb = false;
  709. }
  710. /**
  711. * Returns whether this item is augmented or not
  712. * @return true if augmented
  713. */
  714. public boolean isAugmented()
  715. {
  716. return _augmentation == null ? false : true;
  717. }
  718. /**
  719. * Returns the augmentation object for this item
  720. * @return augmentation
  721. */
  722. public L2Augmentation getAugmentation()
  723. {
  724. return _augmentation;
  725. }
  726. /**
  727. * Sets a new augmentation
  728. * @param augmentation
  729. * @return return true if sucessfull
  730. */
  731. public boolean setAugmentation(L2Augmentation augmentation)
  732. {
  733. // there shall be no previous augmentation..
  734. if (_augmentation != null)
  735. return false;
  736. _augmentation = augmentation;
  737. updateItemAttributes(null);
  738. return true;
  739. }
  740. /**
  741. * Remove the augmentation
  742. *
  743. */
  744. public void removeAugmentation()
  745. {
  746. if (_augmentation == null)
  747. return;
  748. _augmentation = null;
  749. Connection con = null;
  750. try
  751. {
  752. con = L2DatabaseFactory.getInstance().getConnection();
  753. PreparedStatement statement = null;
  754. // Remove the entry
  755. statement = con.prepareStatement("DELETE FROM item_attributes WHERE itemId = ?");
  756. statement.setInt(1, getObjectId());
  757. statement.executeUpdate();
  758. statement.close();
  759. }
  760. catch (Exception e)
  761. {
  762. _log.log(Level.SEVERE, "Could not remove augmentation for item: "+this+" from DB:", e);
  763. }
  764. finally
  765. {
  766. L2DatabaseFactory.close(con);
  767. }
  768. }
  769. public void restoreAttributes()
  770. {
  771. Connection con = null;
  772. try
  773. {
  774. con = L2DatabaseFactory.getInstance().getConnection();
  775. PreparedStatement statement = con.prepareStatement("SELECT augAttributes,augSkillId,augSkillLevel FROM item_attributes WHERE itemId=?");
  776. statement.setInt(1, getObjectId());
  777. ResultSet rs = statement.executeQuery();
  778. if (rs.next())
  779. {
  780. int aug_attributes = rs.getInt(1);
  781. int aug_skillId = rs.getInt(2);
  782. int aug_skillLevel = rs.getInt(3);
  783. if (aug_attributes != -1 && aug_skillId != -1 && aug_skillLevel != -1)
  784. _augmentation = new L2Augmentation(rs.getInt("augAttributes"), rs.getInt("augSkillId"), rs.getInt("augSkillLevel"));
  785. }
  786. rs.close();
  787. statement.close();
  788. statement = con.prepareStatement("SELECT elemType,elemValue FROM item_elementals WHERE itemId=?");
  789. statement.setInt(1, getObjectId());
  790. rs = statement.executeQuery();
  791. while (rs.next())
  792. {
  793. byte elem_type = rs.getByte(1);
  794. int elem_value = rs.getInt(2);
  795. if (elem_type != -1 && elem_value != -1)
  796. applyAttribute(elem_type, elem_value);
  797. }
  798. rs.close();
  799. statement.close();
  800. }
  801. catch (Exception e)
  802. {
  803. _log.log(Level.SEVERE, "Could not restore augmentation and elemental data for item " + this + " from DB: "+e.getMessage(), e);
  804. }
  805. finally
  806. {
  807. L2DatabaseFactory.close(con);
  808. }
  809. }
  810. private void updateItemAttributes(Connection pooledCon)
  811. {
  812. Connection con = null;
  813. try
  814. {
  815. con = pooledCon == null ? L2DatabaseFactory.getInstance().getConnection() : pooledCon;
  816. PreparedStatement statement = con.prepareStatement("REPLACE INTO item_attributes VALUES(?,?,?,?)");
  817. statement.setInt(1, getObjectId());
  818. if (_augmentation == null)
  819. {
  820. statement.setInt(2, -1);
  821. statement.setInt(3, -1);
  822. statement.setInt(4, -1);
  823. }
  824. else
  825. {
  826. statement.setInt(2, _augmentation.getAttributes());
  827. if(_augmentation.getSkill() == null)
  828. {
  829. statement.setInt(3, 0);
  830. statement.setInt(4, 0);
  831. }
  832. else
  833. {
  834. statement.setInt(3, _augmentation.getSkill().getId());
  835. statement.setInt(4, _augmentation.getSkill().getLevel());
  836. }
  837. }
  838. statement.executeUpdate();
  839. statement.close();
  840. }
  841. catch (SQLException e)
  842. {
  843. _log.log(Level.SEVERE, "Could not update atributes for item: "+this+" from DB:", e);
  844. }
  845. finally
  846. {
  847. if (pooledCon == null)
  848. L2DatabaseFactory.close(con);
  849. }
  850. }
  851. private void updateItemElements(Connection pooledCon)
  852. {
  853. Connection con = null;
  854. try
  855. {
  856. con = pooledCon == null ? L2DatabaseFactory.getInstance().getConnection() : pooledCon;
  857. PreparedStatement statement = con.prepareStatement("DELETE FROM item_elementals WHERE itemId = ?");
  858. statement.setInt(1, getObjectId());
  859. statement.executeUpdate();
  860. statement.close();
  861. if (_elementals == null)
  862. return;
  863. statement = con.prepareStatement("INSERT INTO item_elementals VALUES(?,?,?)");
  864. for (Elementals elm : _elementals)
  865. {
  866. statement.setInt(1, getObjectId());
  867. statement.setByte(2, elm.getElement());
  868. statement.setInt(3, elm.getValue());
  869. statement.executeUpdate();
  870. statement.clearParameters();
  871. }
  872. statement.close();
  873. }
  874. catch (SQLException e)
  875. {
  876. _log.log(Level.SEVERE, "Could not update elementals for item: "+this+" from DB:", e);
  877. }
  878. finally
  879. {
  880. if (pooledCon == null)
  881. L2DatabaseFactory.close(con);
  882. }
  883. }
  884. public Elementals[] getElementals()
  885. {
  886. return _elementals;
  887. }
  888. public Elementals getElemental(byte attribute)
  889. {
  890. if (_elementals == null)
  891. return null;
  892. for (Elementals elm : _elementals)
  893. {
  894. if (elm.getElement() == attribute)
  895. return elm;
  896. }
  897. return null;
  898. }
  899. public byte getAttackElementType()
  900. {
  901. if (!isWeapon())
  902. return -2;
  903. else if (getItem().getElementals() != null)
  904. return getItem().getElementals()[0].getElement();
  905. else if (_elementals != null)
  906. return _elementals[0].getElement();
  907. return -2;
  908. }
  909. public int getAttackElementPower()
  910. {
  911. if (!isWeapon())
  912. return 0;
  913. else if (getItem().getElementals() != null)
  914. return getItem().getElementals()[0].getValue();
  915. else if (_elementals != null)
  916. return _elementals[0].getValue();
  917. return 0;
  918. }
  919. public int getElementDefAttr(byte element)
  920. {
  921. if (!isArmor())
  922. return 0;
  923. else if (getItem().getElementals() != null)
  924. {
  925. Elementals elm = getItem().getElemental(element);
  926. if (elm != null)
  927. return elm.getValue();
  928. }
  929. else if (_elementals != null)
  930. {
  931. Elementals elm = getElemental(element);
  932. if (elm != null )
  933. return elm.getValue();
  934. }
  935. return 0;
  936. }
  937. private void applyAttribute(byte element, int value)
  938. {
  939. if (_elementals == null)
  940. {
  941. _elementals = new Elementals[1];
  942. _elementals[0] = new Elementals(element, value);
  943. }
  944. else
  945. {
  946. Elementals elm = getElemental(element);
  947. if (elm != null)
  948. {
  949. elm.setValue(value);
  950. }
  951. else
  952. {
  953. elm = new Elementals(element, value);
  954. Elementals[] array = new Elementals[_elementals.length+1];
  955. System.arraycopy(_elementals, 0, array, 0, _elementals.length);
  956. array[_elementals.length] = elm;
  957. _elementals = array;
  958. }
  959. }
  960. }
  961. /**
  962. * Add elemental attribute to item and save to db
  963. * @param element
  964. * @param value
  965. */
  966. public void setElementAttr(byte element, int value)
  967. {
  968. applyAttribute(element, value);
  969. updateItemElements(null);
  970. }
  971. /**
  972. * Remove elemental from item
  973. * @param element byte element to remove, -1 for all elementals remove
  974. */
  975. public void clearElementAttr(byte element)
  976. {
  977. if (getElemental(element) == null && element != -1)
  978. {
  979. return;
  980. }
  981. Elementals[] array = null;
  982. if (element != -1 && _elementals != null && _elementals.length > 1)
  983. {
  984. array = new Elementals[_elementals.length - 1];
  985. int i = 0;
  986. for (Elementals elm : _elementals)
  987. {
  988. if (elm.getElement() != element)
  989. {
  990. array[i++] = elm;
  991. }
  992. }
  993. }
  994. _elementals = array;
  995. Connection con = null;
  996. try
  997. {
  998. con = L2DatabaseFactory.getInstance().getConnection();
  999. PreparedStatement statement = null;
  1000. if (element != -1)
  1001. {
  1002. //Item can have still others
  1003. statement = con.prepareStatement("DELETE FROM item_elementals WHERE itemId = ? AND elemType = ?");
  1004. statement.setInt(2, element);
  1005. }
  1006. else
  1007. {
  1008. // Remove the entries
  1009. statement = con.prepareStatement("DELETE FROM item_elementals WHERE itemId = ?");
  1010. }
  1011. statement.setInt(1, getObjectId());
  1012. statement.executeUpdate();
  1013. statement.close();
  1014. }
  1015. catch (Exception e)
  1016. {
  1017. _log.log(Level.SEVERE, "Could not remove elemental enchant for item: "+this+" from DB:", e);
  1018. }
  1019. finally
  1020. {
  1021. L2DatabaseFactory.close(con);
  1022. }
  1023. }
  1024. /**
  1025. * Used to decrease mana
  1026. * (mana means life time for shadow items)
  1027. */
  1028. public static class ScheduleConsumeManaTask implements Runnable
  1029. {
  1030. private final L2ItemInstance _shadowItem;
  1031. public ScheduleConsumeManaTask(L2ItemInstance item)
  1032. {
  1033. _shadowItem = item;
  1034. }
  1035. @Override
  1036. public void run()
  1037. {
  1038. try
  1039. {
  1040. // decrease mana
  1041. if (_shadowItem != null)
  1042. _shadowItem.decreaseMana(true);
  1043. }
  1044. catch (Exception e)
  1045. {
  1046. _log.log(Level.SEVERE, "", e);
  1047. }
  1048. }
  1049. }
  1050. /**
  1051. * Returns true if this item is a shadow item
  1052. * Shadow items have a limited life-time
  1053. * @return
  1054. */
  1055. public boolean isShadowItem()
  1056. {
  1057. return (_mana >= 0);
  1058. }
  1059. /**
  1060. * Returns the remaining mana of this shadow item
  1061. * @return lifeTime
  1062. */
  1063. public int getMana()
  1064. {
  1065. return _mana;
  1066. }
  1067. /**
  1068. * Decreases the mana of this shadow item,
  1069. * sends a inventory update
  1070. * schedules a new consumption task if non is running
  1071. * optionally one could force a new task
  1072. * @param resetConsumingMana if true forces a new consumption task if item is equipped
  1073. */
  1074. public void decreaseMana(boolean resetConsumingMana)
  1075. {
  1076. decreaseMana(resetConsumingMana, 1);
  1077. }
  1078. /**
  1079. * Decreases the mana of this shadow item,
  1080. * sends a inventory update
  1081. * schedules a new consumption task if non is running
  1082. * optionally one could force a new task
  1083. * @param resetConsumingMana if 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
  1231. * L2Item/L2Armor/L2Weapon, but may add additional
  1232. * functions, if this particular item instance is enhanched
  1233. * for a particular player.
  1234. * @param player : L2Character designating the player
  1235. * @return Func[]
  1236. */
  1237. public Func[] getStatFuncs(L2Character player)
  1238. {
  1239. return getItem().getStatFuncs(this, player);
  1240. }
  1241. /**
  1242. * Updates the database.<BR>
  1243. */
  1244. public void updateDatabase()
  1245. {
  1246. this.updateDatabase(false);
  1247. }
  1248. /**
  1249. * Updates the database.<BR>
  1250. *
  1251. * @param force if the update should necessarilly be done.
  1252. */
  1253. public void updateDatabase(boolean force)
  1254. {
  1255. _dbLock.lock();
  1256. try
  1257. {
  1258. if (_existsInDb)
  1259. {
  1260. if (_ownerId == 0
  1261. || _loc == ItemLocation.VOID
  1262. || _loc == ItemLocation.REFUND
  1263. || (getCount() == 0 && _loc != ItemLocation.LEASE))
  1264. {
  1265. removeFromDb();
  1266. }
  1267. else if (!Config.LAZY_ITEMS_UPDATE || force)
  1268. {
  1269. updateInDb();
  1270. }
  1271. }
  1272. else
  1273. {
  1274. if (_ownerId == 0
  1275. || _loc == ItemLocation.VOID
  1276. || _loc == ItemLocation.REFUND
  1277. || (getCount() == 0 && _loc != ItemLocation.LEASE))
  1278. {
  1279. return;
  1280. }
  1281. insertIntoDb();
  1282. }
  1283. }
  1284. finally
  1285. {
  1286. _dbLock.unlock();
  1287. }
  1288. }
  1289. /**
  1290. * Returns a L2ItemInstance stored in database from its objectID
  1291. * @param ownerId
  1292. * @param rs
  1293. * @return L2ItemInstance
  1294. */
  1295. public static L2ItemInstance restoreFromDb(int ownerId, ResultSet rs)
  1296. {
  1297. L2ItemInstance inst = null;
  1298. int objectId, item_id, loc_data, enchant_level, custom_type1, custom_type2, manaLeft;
  1299. long time, count;
  1300. ItemLocation loc;
  1301. try
  1302. {
  1303. objectId = rs.getInt(1);
  1304. item_id = rs.getInt("item_id");
  1305. count = rs.getLong("count");
  1306. loc = ItemLocation.valueOf(rs.getString("loc"));
  1307. loc_data = rs.getInt("loc_data");
  1308. enchant_level = rs.getInt("enchant_level");
  1309. custom_type1 = rs.getInt("custom_type1");
  1310. custom_type2 = rs.getInt("custom_type2");
  1311. manaLeft = rs.getInt("mana_left");
  1312. time = rs.getLong("time");
  1313. } catch (Exception e) {
  1314. _log.log(Level.SEVERE, "Could not restore an item owned by "+ownerId+" from DB:", e);
  1315. return null;
  1316. }
  1317. L2Item item = ItemTable.getInstance().getTemplate(item_id);
  1318. if (item == null) {
  1319. _log.severe("Item item_id="+item_id+" not known, object_id="+objectId);
  1320. return null;
  1321. }
  1322. inst = new L2ItemInstance(objectId, item);
  1323. inst._ownerId = ownerId;
  1324. inst.setCount(count);
  1325. inst._enchantLevel = enchant_level;
  1326. inst._type1 = custom_type1;
  1327. inst._type2 = custom_type2;
  1328. inst._loc = loc;
  1329. inst._locData = loc_data;
  1330. inst._existsInDb = true;
  1331. inst._storedInDb = true;
  1332. // Setup life time for shadow weapons
  1333. inst._mana = manaLeft;
  1334. inst._time = time;
  1335. //load augmentation and elemental enchant
  1336. if (inst.isEquipable())
  1337. inst.restoreAttributes();
  1338. return inst;
  1339. }
  1340. /**
  1341. * Init a dropped L2ItemInstance and add it in the world as a visible object.<BR><BR>
  1342. *
  1343. * <B><U> Actions</U> :</B><BR><BR>
  1344. * <li>Set the x,y,z position of the L2ItemInstance dropped and update its _worldregion </li>
  1345. * <li>Add the L2ItemInstance dropped to _visibleObjects of its L2WorldRegion</li>
  1346. * <li>Add the L2ItemInstance dropped in the world as a <B>visible</B> object</li><BR><BR>
  1347. *
  1348. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T ADD the object to _allObjects of L2World </B></FONT><BR><BR>
  1349. *
  1350. * <B><U> Assert </U> :</B><BR><BR>
  1351. * <li> _worldRegion == null <I>(L2Object is invisible at the beginning)</I></li><BR><BR>
  1352. *
  1353. * <B><U> Example of use </U> :</B><BR><BR>
  1354. * <li> Drop item</li>
  1355. * <li> Call Pet</li><BR>
  1356. *
  1357. */
  1358. public class ItemDropTask implements Runnable
  1359. {
  1360. private int _x,_y,_z;
  1361. private final L2Character _dropper;
  1362. private final L2ItemInstance _itm;
  1363. public ItemDropTask(L2ItemInstance item, L2Character dropper, int x, int y, int z)
  1364. {
  1365. _x = x;
  1366. _y = y;
  1367. _z = z;
  1368. _dropper = dropper;
  1369. _itm = item;
  1370. }
  1371. @Override
  1372. public final void run()
  1373. {
  1374. assert _itm.getPosition().getWorldRegion() == null;
  1375. if (Config.GEODATA > 0 && _dropper != null)
  1376. {
  1377. Location dropDest = GeoData.getInstance().moveCheck(_dropper.getX(), _dropper.getY(), _dropper.getZ(), _x, _y, _z, _dropper.getInstanceId());
  1378. _x = dropDest.getX();
  1379. _y = dropDest.getY();
  1380. _z = dropDest.getZ();
  1381. }
  1382. if(_dropper != null)
  1383. setInstanceId(_dropper.getInstanceId()); // Inherit instancezone when dropped in visible world
  1384. else
  1385. setInstanceId(0); // No dropper? Make it a global item...
  1386. synchronized (_itm)
  1387. {
  1388. // Set the x,y,z position of the L2ItemInstance dropped and update its _worldregion
  1389. _itm.setIsVisible(true);
  1390. _itm.getPosition().setWorldPosition(_x, _y ,_z);
  1391. _itm.getPosition().setWorldRegion(L2World.getInstance().getRegion(getPosition().getWorldPosition()));
  1392. // Add the L2ItemInstance dropped to _visibleObjects of its L2WorldRegion
  1393. }
  1394. _itm.getPosition().getWorldRegion().addVisibleObject(_itm);
  1395. _itm.setDropTime(System.currentTimeMillis());
  1396. _itm.setDropperObjectId(_dropper != null ? _dropper.getObjectId() : 0); //Set the dropper Id for the knownlist packets in sendInfo
  1397. // this can synchronize on others instancies, so it's out of
  1398. // synchronized, to avoid deadlocks
  1399. // Add the L2ItemInstance dropped in the world as a visible object
  1400. L2World.getInstance().addVisibleObject(_itm, _itm.getPosition().getWorldRegion());
  1401. if (Config.SAVE_DROPPED_ITEM)
  1402. ItemsOnGroundManager.getInstance().save(_itm);
  1403. _itm.setDropperObjectId(0); //Set the dropper Id back to 0 so it no longer shows the drop packet
  1404. }
  1405. }
  1406. public final void dropMe(L2Character dropper, int x, int y, int z)
  1407. {
  1408. ThreadPoolManager.getInstance().executeTask(new ItemDropTask(this, dropper, x, y, z));
  1409. }
  1410. /**
  1411. * Update the database with values of the item
  1412. */
  1413. private void updateInDb()
  1414. {
  1415. assert _existsInDb;
  1416. if (_wear)
  1417. return;
  1418. if (_storedInDb)
  1419. return;
  1420. Connection con = null;
  1421. PreparedStatement statement = null;
  1422. try
  1423. {
  1424. con = L2DatabaseFactory.getInstance().getConnection();
  1425. statement = con.prepareStatement(
  1426. "UPDATE items SET owner_id=?,count=?,loc=?,loc_data=?,enchant_level=?,custom_type1=?,custom_type2=?,mana_left=?,time=? " +
  1427. "WHERE object_id = ?");
  1428. statement.setInt(1, _ownerId);
  1429. statement.setLong(2, getCount());
  1430. statement.setString(3, _loc.name());
  1431. statement.setInt(4, _locData);
  1432. statement.setInt(5, getEnchantLevel());
  1433. statement.setInt(6, getCustomType1());
  1434. statement.setInt(7, getCustomType2());
  1435. statement.setInt(8, getMana());
  1436. statement.setLong(9, getTime());
  1437. statement.setInt(10, getObjectId());
  1438. statement.executeUpdate();
  1439. _existsInDb = true;
  1440. _storedInDb = true;
  1441. statement.close();
  1442. }
  1443. catch (Exception e)
  1444. {
  1445. _log.log(Level.SEVERE, "Could not update item "+this+" in DB: Reason: "+e.getMessage(), e);
  1446. }
  1447. finally
  1448. {
  1449. L2DatabaseFactory.close(con);
  1450. }
  1451. }
  1452. /**
  1453. * Insert the item in database
  1454. */
  1455. private void insertIntoDb()
  1456. {
  1457. assert !_existsInDb && getObjectId() != 0;
  1458. if (_wear)
  1459. return;
  1460. Connection con = null;
  1461. PreparedStatement statement = null;
  1462. try
  1463. {
  1464. con = L2DatabaseFactory.getInstance().getConnection();
  1465. statement = con.prepareStatement(
  1466. "INSERT INTO items (owner_id,item_id,count,loc,loc_data,enchant_level,object_id,custom_type1,custom_type2,mana_left,time) " +
  1467. "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
  1468. statement.setInt(1, _ownerId);
  1469. statement.setInt(2, _itemId);
  1470. statement.setLong(3, getCount());
  1471. statement.setString(4, _loc.name());
  1472. statement.setInt(5, _locData);
  1473. statement.setInt(6, getEnchantLevel());
  1474. statement.setInt(7, getObjectId());
  1475. statement.setInt(8, _type1);
  1476. statement.setInt(9, _type2);
  1477. statement.setInt(10, getMana());
  1478. statement.setLong(11, getTime());
  1479. statement.executeUpdate();
  1480. _existsInDb = true;
  1481. _storedInDb = true;
  1482. statement.close();
  1483. if (_augmentation != null)
  1484. updateItemAttributes(con);
  1485. if (_elementals != null)
  1486. updateItemElements(con);
  1487. }
  1488. catch (Exception e)
  1489. {
  1490. _log.log(Level.SEVERE, "Could not insert item " + this + " into DB: Reason: " + e.getMessage(), e);
  1491. }
  1492. finally
  1493. {
  1494. L2DatabaseFactory.close(con);
  1495. }
  1496. }
  1497. /**
  1498. * Delete item from database
  1499. */
  1500. private void removeFromDb()
  1501. {
  1502. assert _existsInDb;
  1503. if (_wear)
  1504. return;
  1505. Connection con = null;
  1506. PreparedStatement statement = null;
  1507. try
  1508. {
  1509. con = L2DatabaseFactory.getInstance().getConnection();
  1510. statement = con.prepareStatement("DELETE FROM items WHERE object_id=?");
  1511. statement.setInt(1, getObjectId());
  1512. statement.executeUpdate();
  1513. _existsInDb = false;
  1514. _storedInDb = false;
  1515. statement.close();
  1516. statement = con.prepareStatement("DELETE FROM item_attributes WHERE itemId = ?");
  1517. statement.setInt(1, getObjectId());
  1518. statement.executeUpdate();
  1519. statement.close();
  1520. }
  1521. catch (Exception e)
  1522. {
  1523. _log.log(Level.SEVERE, "Could not delete item "+this+" in DB: "+e.getMessage(), e);
  1524. }
  1525. finally
  1526. {
  1527. L2DatabaseFactory.close(con);
  1528. }
  1529. }
  1530. /**
  1531. * Returns the item in String format
  1532. * @return String
  1533. */
  1534. @Override
  1535. public String toString()
  1536. {
  1537. return _item+"["+getObjectId()+"]";
  1538. }
  1539. public void resetOwnerTimer()
  1540. {
  1541. if(itemLootShedule != null)
  1542. itemLootShedule.cancel(true);
  1543. itemLootShedule = null;
  1544. }
  1545. public void setItemLootShedule(ScheduledFuture<?> sf)
  1546. {
  1547. itemLootShedule = sf;
  1548. }
  1549. public ScheduledFuture<?> getItemLootShedule()
  1550. {
  1551. return itemLootShedule;
  1552. }
  1553. public void setProtected(boolean is_protected)
  1554. {
  1555. _protected = is_protected;
  1556. }
  1557. public boolean isProtected()
  1558. {
  1559. return _protected;
  1560. }
  1561. public boolean isNightLure()
  1562. {
  1563. return ((_itemId >= 8505 && _itemId <= 8513) || _itemId == 8485);
  1564. }
  1565. public void setCountDecrease(boolean decrease)
  1566. {
  1567. _decrease = decrease;
  1568. }
  1569. public boolean getCountDecrease()
  1570. {
  1571. return _decrease;
  1572. }
  1573. public void setInitCount(int InitCount)
  1574. {
  1575. _initCount = InitCount;
  1576. }
  1577. public long getInitCount()
  1578. {
  1579. return _initCount;
  1580. }
  1581. public void restoreInitCount()
  1582. {
  1583. if(_decrease)
  1584. setCount(_initCount);
  1585. }
  1586. public boolean isTimeLimitedItem()
  1587. {
  1588. return (_time > 0);
  1589. }
  1590. /**
  1591. * Returns (current system time + time) of this time limited item
  1592. * @return Time
  1593. */
  1594. public long getTime()
  1595. {
  1596. return _time;
  1597. }
  1598. public long getRemainingTime()
  1599. {
  1600. return _time - System.currentTimeMillis();
  1601. }
  1602. public void endOfLife()
  1603. {
  1604. L2PcInstance player = L2World.getInstance().getPlayer(getOwnerId());
  1605. if (player != null)
  1606. {
  1607. if (isEquipped())
  1608. {
  1609. L2ItemInstance[] unequiped = player.getInventory().unEquipItemInSlotAndRecord(getLocationSlot());
  1610. InventoryUpdate iu = new InventoryUpdate();
  1611. for (L2ItemInstance item: unequiped)
  1612. {
  1613. player.checkSShotsMatch(null, item);
  1614. iu.addModifiedItem(item);
  1615. }
  1616. player.sendPacket(iu);
  1617. player.broadcastUserInfo();
  1618. }
  1619. if (getLocation() != ItemLocation.WAREHOUSE)
  1620. {
  1621. // destroy
  1622. player.getInventory().destroyItem("L2ItemInstance", this, player, null);
  1623. // send update
  1624. InventoryUpdate iu = new InventoryUpdate();
  1625. iu.addRemovedItem(this);
  1626. player.sendPacket(iu);
  1627. StatusUpdate su = new StatusUpdate(player);
  1628. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  1629. player.sendPacket(su);
  1630. }
  1631. else
  1632. {
  1633. player.getWarehouse().destroyItem("L2ItemInstance", this, player, null);
  1634. }
  1635. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.TIME_LIMITED_ITEM_DELETED));
  1636. // delete from world
  1637. L2World.getInstance().removeObject(this);
  1638. }
  1639. }
  1640. public void scheduleLifeTimeTask()
  1641. {
  1642. if (!isTimeLimitedItem()) return;
  1643. if (getRemainingTime() <= 0)
  1644. endOfLife();
  1645. else
  1646. {
  1647. if (_lifeTimeTask != null)
  1648. _lifeTimeTask.cancel(false);
  1649. _lifeTimeTask = ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleLifeTimeTask(this), getRemainingTime());
  1650. }
  1651. }
  1652. public static class ScheduleLifeTimeTask implements Runnable
  1653. {
  1654. private final L2ItemInstance _limitedItem;
  1655. public ScheduleLifeTimeTask(L2ItemInstance item)
  1656. {
  1657. _limitedItem = item;
  1658. }
  1659. @Override
  1660. public void run()
  1661. {
  1662. try
  1663. {
  1664. if (_limitedItem != null)
  1665. _limitedItem.endOfLife();
  1666. }
  1667. catch (Exception e)
  1668. {
  1669. _log.log(Level.SEVERE, "", e);
  1670. }
  1671. }
  1672. }
  1673. public void updateElementAttrBonus(L2PcInstance player)
  1674. {
  1675. if (_elementals == null)
  1676. return;
  1677. for (Elementals elm : _elementals)
  1678. elm.updateBonus(player, isArmor());
  1679. }
  1680. public void removeElementAttrBonus(L2PcInstance player)
  1681. {
  1682. if (_elementals == null)
  1683. return;
  1684. for (Elementals elm : _elementals)
  1685. elm.removeBonus(player);
  1686. }
  1687. public void setDropperObjectId(int id)
  1688. {
  1689. _dropperObjectId = id;
  1690. }
  1691. @Override
  1692. public void sendInfo(L2PcInstance activeChar)
  1693. {
  1694. if (_dropperObjectId != 0)
  1695. activeChar.sendPacket(new DropItem(this, _dropperObjectId));
  1696. else
  1697. activeChar.sendPacket(new SpawnItem(this));
  1698. }
  1699. public final DropProtection getDropProtection()
  1700. {
  1701. return _dropProtection;
  1702. }
  1703. public boolean isPublished()
  1704. {
  1705. return _published;
  1706. }
  1707. public void publish()
  1708. {
  1709. _published = true;
  1710. }
  1711. /* (non-Javadoc)
  1712. * @see com.l2jserver.gameserver.model.L2Object#decayMe()
  1713. */
  1714. @Override
  1715. public void decayMe()
  1716. {
  1717. if (Config.SAVE_DROPPED_ITEM)
  1718. ItemsOnGroundManager.getInstance().removeObject(this);
  1719. super.decayMe();
  1720. }
  1721. public boolean isQuestItem()
  1722. {
  1723. return getItem().isQuestItem();
  1724. }
  1725. public boolean isFreightable()
  1726. {
  1727. return getItem().isFreightable();
  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. }