ItemTable.java 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 net.sf.l2j.gameserver.datatables;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.Collection;
  21. import java.util.Map;
  22. import java.util.concurrent.ScheduledFuture;
  23. import java.util.logging.Level;
  24. import java.util.logging.LogRecord;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastMap;
  27. import net.sf.l2j.Config;
  28. import net.sf.l2j.L2DatabaseFactory;
  29. import net.sf.l2j.gameserver.Item;
  30. import net.sf.l2j.gameserver.ThreadPoolManager;
  31. import net.sf.l2j.gameserver.idfactory.IdFactory;
  32. import net.sf.l2j.gameserver.model.L2ItemInstance;
  33. import net.sf.l2j.gameserver.model.L2Object;
  34. import net.sf.l2j.gameserver.model.L2PetDataTable;
  35. import net.sf.l2j.gameserver.model.L2World;
  36. import net.sf.l2j.gameserver.model.L2ItemInstance.ItemLocation;
  37. import net.sf.l2j.gameserver.model.actor.L2Attackable;
  38. import net.sf.l2j.gameserver.model.actor.instance.L2GrandBossInstance;
  39. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  40. import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
  41. import net.sf.l2j.gameserver.skills.SkillsEngine;
  42. import net.sf.l2j.gameserver.templates.StatsSet;
  43. import net.sf.l2j.gameserver.templates.item.L2Armor;
  44. import net.sf.l2j.gameserver.templates.item.L2ArmorType;
  45. import net.sf.l2j.gameserver.templates.item.L2EtcItem;
  46. import net.sf.l2j.gameserver.templates.item.L2EtcItemType;
  47. import net.sf.l2j.gameserver.templates.item.L2Item;
  48. import net.sf.l2j.gameserver.templates.item.L2Weapon;
  49. import net.sf.l2j.gameserver.templates.item.L2WeaponType;
  50. import net.sf.l2j.gameserver.util.GMAudit;
  51. /**
  52. * This class ...
  53. *
  54. * @version $Revision: 1.9.2.6.2.9 $ $Date: 2005/04/02 15:57:34 $
  55. */
  56. public class ItemTable
  57. {
  58. private static Logger _log = Logger.getLogger(ItemTable.class.getName());
  59. private static Logger _logItems = Logger.getLogger("item");
  60. private static final Map<String, Integer> _materials = new FastMap<String, Integer>();
  61. private static final Map<String, Integer> _crystalTypes = new FastMap<String, Integer>();
  62. private static final Map<String, L2WeaponType> _weaponTypes = new FastMap<String, L2WeaponType>();
  63. private static final Map<String, L2ArmorType> _armorTypes = new FastMap<String, L2ArmorType>();
  64. private static final Map<String, Integer> _slots = new FastMap<String, Integer>();
  65. private L2Item[] _allTemplates;
  66. private Map<Integer, L2EtcItem> _etcItems;
  67. private Map<Integer, L2Armor> _armors;
  68. private Map<Integer, L2Weapon> _weapons;
  69. private boolean _initialized = true;
  70. static
  71. {
  72. _materials.put("paper", L2Item.MATERIAL_PAPER);
  73. _materials.put("wood", L2Item.MATERIAL_WOOD);
  74. _materials.put("liquid", L2Item.MATERIAL_LIQUID);
  75. _materials.put("cloth", L2Item.MATERIAL_CLOTH);
  76. _materials.put("leather", L2Item.MATERIAL_LEATHER);
  77. _materials.put("horn", L2Item.MATERIAL_HORN);
  78. _materials.put("bone", L2Item.MATERIAL_BONE);
  79. _materials.put("bronze", L2Item.MATERIAL_BRONZE);
  80. _materials.put("fine_steel", L2Item.MATERIAL_FINE_STEEL);
  81. _materials.put("cotton", L2Item.MATERIAL_FINE_STEEL);
  82. _materials.put("mithril", L2Item.MATERIAL_MITHRIL);
  83. _materials.put("silver", L2Item.MATERIAL_SILVER);
  84. _materials.put("gold", L2Item.MATERIAL_GOLD);
  85. _materials.put("adamantaite", L2Item.MATERIAL_ADAMANTAITE);
  86. _materials.put("steel", L2Item.MATERIAL_STEEL);
  87. _materials.put("oriharukon", L2Item.MATERIAL_ORIHARUKON);
  88. _materials.put("blood_steel", L2Item.MATERIAL_BLOOD_STEEL);
  89. _materials.put("crystal", L2Item.MATERIAL_CRYSTAL);
  90. _materials.put("damascus", L2Item.MATERIAL_DAMASCUS);
  91. _materials.put("chrysolite", L2Item.MATERIAL_CHRYSOLITE);
  92. _materials.put("scale_of_dragon", L2Item.MATERIAL_SCALE_OF_DRAGON);
  93. _materials.put("dyestuff", L2Item.MATERIAL_DYESTUFF);
  94. _materials.put("cobweb", L2Item.MATERIAL_COBWEB);
  95. _materials.put("seed", L2Item.MATERIAL_SEED);
  96. _crystalTypes.put("s84", L2Item.CRYSTAL_S84);
  97. _crystalTypes.put("s80", L2Item.CRYSTAL_S80);
  98. _crystalTypes.put("s", L2Item.CRYSTAL_S);
  99. _crystalTypes.put("a", L2Item.CRYSTAL_A);
  100. _crystalTypes.put("b", L2Item.CRYSTAL_B);
  101. _crystalTypes.put("c", L2Item.CRYSTAL_C);
  102. _crystalTypes.put("d", L2Item.CRYSTAL_D);
  103. _crystalTypes.put("none", L2Item.CRYSTAL_NONE);
  104. _weaponTypes.put("blunt", L2WeaponType.BLUNT);
  105. _weaponTypes.put("bow", L2WeaponType.BOW);
  106. _weaponTypes.put("dagger", L2WeaponType.DAGGER);
  107. _weaponTypes.put("dual", L2WeaponType.DUAL);
  108. _weaponTypes.put("dualfist", L2WeaponType.DUALFIST);
  109. _weaponTypes.put("etc", L2WeaponType.ETC);
  110. _weaponTypes.put("fist", L2WeaponType.FIST);
  111. _weaponTypes.put("none", L2WeaponType.NONE); // these are shields !
  112. _weaponTypes.put("pole", L2WeaponType.POLE);
  113. _weaponTypes.put("sword", L2WeaponType.SWORD);
  114. _weaponTypes.put("bigsword", L2WeaponType.BIGSWORD); //Two-Handed Swords
  115. _weaponTypes.put("pet", L2WeaponType.PET); //Pet Weapon
  116. _weaponTypes.put("rod", L2WeaponType.ROD); //Fishing Rods
  117. _weaponTypes.put("bigblunt", L2WeaponType.BIGBLUNT); //Two handed blunt
  118. _weaponTypes.put("crossbow", L2WeaponType.CROSSBOW);
  119. _weaponTypes.put("rapier", L2WeaponType.RAPIER);
  120. _weaponTypes.put("ancient", L2WeaponType.ANCIENT_SWORD);
  121. _weaponTypes.put("dualdagger", L2WeaponType.DUAL_DAGGER);
  122. _armorTypes.put("none", L2ArmorType.NONE);
  123. _armorTypes.put("light", L2ArmorType.LIGHT);
  124. _armorTypes.put("heavy", L2ArmorType.HEAVY);
  125. _armorTypes.put("magic", L2ArmorType.MAGIC);
  126. _armorTypes.put("pet", L2ArmorType.PET);
  127. _armorTypes.put("sigil", L2ArmorType.SIGIL);
  128. _slots.put("shirt", L2Item.SLOT_UNDERWEAR);
  129. _slots.put("lbracelet", L2Item.SLOT_L_BRACELET);
  130. _slots.put("rbracelet", L2Item.SLOT_R_BRACELET);
  131. _slots.put("talisman", L2Item.SLOT_DECO);
  132. _slots.put("chest", L2Item.SLOT_CHEST);
  133. _slots.put("fullarmor", L2Item.SLOT_FULL_ARMOR);
  134. _slots.put("head", L2Item.SLOT_HEAD);
  135. _slots.put("hair", L2Item.SLOT_HAIR);
  136. _slots.put("face", L2Item.SLOT_HAIR2);
  137. _slots.put("hair2", L2Item.SLOT_HAIR2);
  138. _slots.put("dhair", L2Item.SLOT_HAIRALL);
  139. _slots.put("hairall", L2Item.SLOT_HAIRALL);
  140. _slots.put("underwear", L2Item.SLOT_UNDERWEAR);
  141. _slots.put("back", L2Item.SLOT_BACK);
  142. _slots.put("neck", L2Item.SLOT_NECK);
  143. _slots.put("legs", L2Item.SLOT_LEGS);
  144. _slots.put("feet", L2Item.SLOT_FEET);
  145. _slots.put("gloves", L2Item.SLOT_GLOVES);
  146. _slots.put("chest,legs", L2Item.SLOT_CHEST | L2Item.SLOT_LEGS);
  147. _slots.put("belt", L2Item.SLOT_BELT);
  148. _slots.put("rhand", L2Item.SLOT_R_HAND);
  149. _slots.put("lhand", L2Item.SLOT_L_HAND);
  150. _slots.put("lrhand", L2Item.SLOT_LR_HAND);
  151. _slots.put("rear,lear", L2Item.SLOT_R_EAR | L2Item.SLOT_L_EAR);
  152. _slots.put("rfinger,lfinger", L2Item.SLOT_R_FINGER | L2Item.SLOT_L_FINGER);
  153. _slots.put("wolf", L2Item.SLOT_WOLF);
  154. _slots.put("greatwolf", L2Item.SLOT_GREATWOLF);
  155. _slots.put("hatchling", L2Item.SLOT_HATCHLING);
  156. _slots.put("strider", L2Item.SLOT_STRIDER);
  157. _slots.put("babypet", L2Item.SLOT_BABYPET);
  158. _slots.put("none", L2Item.SLOT_NONE);
  159. }
  160. /** Table of SQL request in order to obtain items from tables [etcitem], [armor], [weapon] */
  161. private static final String[] SQL_ITEM_SELECTS = {
  162. "SELECT item_id, name, crystallizable, item_type, weight, consume_type, material,"
  163. + " crystal_type, duration, time, price, crystal_count, sellable, dropable, destroyable, tradeable, depositable, handler, skill FROM etcitem",
  164. "SELECT item_id, name, bodypart, crystallizable, armor_type, weight,"
  165. + " material, crystal_type, avoid_modify, duration, time, p_def, m_def, mp_bonus,"
  166. + " price, crystal_count, sellable, dropable, destroyable, tradeable, depositable, enchant4_skill, skill FROM armor",
  167. "SELECT item_id, name, bodypart, crystallizable, weight, soulshots, spiritshots,"
  168. + " material, crystal_type, p_dam, rnd_dam, weaponType, critical, hit_modify, avoid_modify,"
  169. + " shield_def, shield_def_rate, atk_speed, mp_consume, m_dam, duration, time, price, crystal_count,"
  170. + " sellable, dropable, destroyable, tradeable, depositable, skill,enchant4_skill_id,enchant4_skill_lvl, onCast_skill_id, onCast_skill_lvl,"
  171. + " onCast_skill_chance, onCrit_skill_id, onCrit_skill_lvl, onCrit_skill_chance, change_weaponId FROM weapon" };
  172. private static final String[] SQL_CUSTOM_ITEM_SELECTS = {
  173. "SELECT item_id, name, crystallizable, item_type, weight, consume_type, material,"
  174. + " crystal_type, duration, time, price, crystal_count, sellable, dropable, destroyable, tradeable, depositable, handler, skill FROM custom_etcitem",
  175. "SELECT item_id, name, bodypart, crystallizable, armor_type, weight,"
  176. + " material, crystal_type, avoid_modify, duration, time, p_def, m_def, mp_bonus,"
  177. + " price, crystal_count, sellable, dropable, destroyable, tradeable, depositable, enchant4_skill, skill FROM custom_armor",
  178. "SELECT item_id, name, bodypart, crystallizable, weight, soulshots, spiritshots,"
  179. + " material, crystal_type, p_dam, rnd_dam, weaponType, critical, hit_modify, avoid_modify,"
  180. + " shield_def, shield_def_rate, atk_speed, mp_consume, m_dam, duration, time, price, crystal_count,"
  181. + " sellable, dropable, destroyable, tradeable, depositable, skill,enchant4_skill_id,enchant4_skill_lvl, onCast_skill_id, onCast_skill_lvl,"
  182. + " onCast_skill_chance, onCrit_skill_id, onCrit_skill_lvl, onCrit_skill_chance, change_weaponId FROM custom_weapon" };
  183. /**
  184. * Returns instance of ItemTable
  185. * @return ItemTable
  186. */
  187. public static ItemTable getInstance()
  188. {
  189. return SingletonHolder._instance;
  190. }
  191. /**
  192. * Returns a new object Item
  193. * @return
  194. */
  195. public Item newItem()
  196. {
  197. return new Item();
  198. }
  199. /**
  200. * Constructor.
  201. */
  202. private ItemTable()
  203. {
  204. _etcItems = new FastMap<Integer, L2EtcItem>();
  205. _armors = new FastMap<Integer, L2Armor>();
  206. _weapons = new FastMap<Integer, L2Weapon>();
  207. load();
  208. }
  209. private void load()
  210. {
  211. /** List of etcItem */
  212. final Map<Integer, Item> itemData = new FastMap<Integer, Item>();
  213. /** List of weapons */
  214. final Map<Integer, Item> weaponData = new FastMap<Integer, Item>();
  215. /** List of armor */
  216. final Map<Integer, Item> armorData = new FastMap<Integer, Item>();
  217. Connection con = null;
  218. try
  219. {
  220. con = L2DatabaseFactory.getInstance().getConnection();
  221. for (String selectQuery : SQL_ITEM_SELECTS)
  222. {
  223. PreparedStatement statement = con.prepareStatement(selectQuery);
  224. ResultSet rset = statement.executeQuery();
  225. // Add item in correct FastMap
  226. while (rset.next())
  227. {
  228. if (selectQuery.endsWith("etcitem"))
  229. {
  230. Item newItem = readItem(rset);
  231. itemData.put(newItem.id, newItem);
  232. }
  233. else if (selectQuery.endsWith("armor"))
  234. {
  235. Item newItem = readArmor(rset);
  236. armorData.put(newItem.id, newItem);
  237. }
  238. else if (selectQuery.endsWith("weapon"))
  239. {
  240. Item newItem = readWeapon(rset);
  241. weaponData.put(newItem.id, newItem);
  242. }
  243. }
  244. rset.close();
  245. statement.close();
  246. }
  247. }
  248. catch (Exception e)
  249. {
  250. _log.log(Level.WARNING, "data error on item: ", e);
  251. }
  252. finally
  253. {
  254. try
  255. {
  256. con.close();
  257. }
  258. catch (Exception e)
  259. {
  260. }
  261. }
  262. if (Config.CUSTOM_ITEM_TABLES)
  263. {
  264. try
  265. {
  266. con = L2DatabaseFactory.getInstance().getConnection();
  267. for (String selectQuery : SQL_CUSTOM_ITEM_SELECTS)
  268. {
  269. PreparedStatement statement = con.prepareStatement(selectQuery);
  270. ResultSet rset = statement.executeQuery();
  271. // Add item in correct FastMap
  272. while (rset.next())
  273. {
  274. if (selectQuery.endsWith("etcitem"))
  275. {
  276. Item newItem = readItem(rset);
  277. if (itemData.containsKey(newItem.id))
  278. itemData.remove(newItem.id);
  279. itemData.put(newItem.id, newItem);
  280. }
  281. else if (selectQuery.endsWith("armor"))
  282. {
  283. Item newItem = readArmor(rset);
  284. if (armorData.containsKey(newItem.id))
  285. armorData.remove(newItem.id);
  286. armorData.put(newItem.id, newItem);
  287. }
  288. else if (selectQuery.endsWith("weapon"))
  289. {
  290. Item newItem = readWeapon(rset);
  291. if (weaponData.containsKey(newItem.id))
  292. weaponData.remove(newItem.id);
  293. weaponData.put(newItem.id, newItem);
  294. }
  295. }
  296. rset.close();
  297. statement.close();
  298. }
  299. }
  300. catch (Exception e)
  301. {
  302. _log.log(Level.WARNING, "data error on custom_item: ", e);
  303. }
  304. finally
  305. {
  306. try
  307. {
  308. con.close();
  309. }
  310. catch (Exception e)
  311. {
  312. }
  313. }
  314. }
  315. _armors.clear();
  316. for (L2Armor armor : SkillsEngine.getInstance().loadArmors(armorData))
  317. {
  318. _armors.put(armor.getItemId(), armor);
  319. }
  320. _log.config("ItemTable: Loaded " + _armors.size() + " Armors.");
  321. _etcItems.clear();
  322. for (L2EtcItem item : SkillsEngine.getInstance().loadItems(itemData))
  323. {
  324. _etcItems.put(item.getItemId(), item);
  325. }
  326. _log.config("ItemTable: Loaded " + _etcItems.size() + " Items.");
  327. _weapons.clear();
  328. for (L2Weapon weapon : SkillsEngine.getInstance().loadWeapons(weaponData))
  329. {
  330. _weapons.put(weapon.getItemId(), weapon);
  331. }
  332. _log.config("ItemTable: Loaded " + _weapons.size() + " Weapons.");
  333. buildFastLookupTable();
  334. }
  335. /**
  336. * Returns object Item from the record of the database
  337. *
  338. * @param rset : ResultSet designating a record of the [weapon] table of database
  339. * @return Item : object created from the database record
  340. * @throws SQLException
  341. */
  342. private Item readWeapon(ResultSet rset) throws SQLException
  343. {
  344. Item item = new Item();
  345. item.set = new StatsSet();
  346. item.type = _weaponTypes.get(rset.getString("weaponType"));
  347. item.id = rset.getInt("item_id");
  348. item.name = rset.getString("name");
  349. item.set.set("item_id", item.id);
  350. item.set.set("name", item.name);
  351. // lets see if this is a shield
  352. if (item.type == L2WeaponType.NONE)
  353. {
  354. item.set.set("type1", L2Item.TYPE1_SHIELD_ARMOR);
  355. item.set.set("type2", L2Item.TYPE2_SHIELD_ARMOR);
  356. }
  357. else
  358. {
  359. item.set.set("type1", L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE);
  360. item.set.set("type2", L2Item.TYPE2_WEAPON);
  361. }
  362. item.set.set("bodypart", _slots.get(rset.getString("bodypart")));
  363. item.set.set("material", _materials.get(rset.getString("material")));
  364. item.set.set("crystal_type", _crystalTypes.get(rset.getString("crystal_type")));
  365. item.set.set("crystallizable", Boolean.valueOf(rset.getString("crystallizable")).booleanValue());
  366. item.set.set("weight", rset.getInt("weight"));
  367. item.set.set("soulshots", rset.getInt("soulshots"));
  368. item.set.set("spiritshots", rset.getInt("spiritshots"));
  369. item.set.set("p_dam", rset.getInt("p_dam"));
  370. item.set.set("rnd_dam", rset.getInt("rnd_dam"));
  371. item.set.set("critical", rset.getInt("critical"));
  372. item.set.set("hit_modify", rset.getDouble("hit_modify"));
  373. item.set.set("avoid_modify", rset.getInt("avoid_modify"));
  374. item.set.set("shield_def", rset.getInt("shield_def"));
  375. item.set.set("shield_def_rate", rset.getInt("shield_def_rate"));
  376. item.set.set("atk_speed", rset.getInt("atk_speed"));
  377. item.set.set("mp_consume", rset.getInt("mp_consume"));
  378. item.set.set("m_dam", rset.getInt("m_dam"));
  379. item.set.set("duration", rset.getInt("duration"));
  380. item.set.set("time", rset.getInt("time"));
  381. item.set.set("price", rset.getInt("price"));
  382. item.set.set("crystal_count", rset.getInt("crystal_count"));
  383. item.set.set("sellable", Boolean.valueOf(rset.getString("sellable")));
  384. item.set.set("dropable", Boolean.valueOf(rset.getString("dropable")));
  385. item.set.set("destroyable", Boolean.valueOf(rset.getString("destroyable")));
  386. item.set.set("tradeable", Boolean.valueOf(rset.getString("tradeable")));
  387. item.set.set("depositable", Boolean.valueOf(rset.getString("depositable")));
  388. item.set.set("skill", rset.getString("skill"));
  389. item.set.set("enchant4_skill_id", rset.getInt("enchant4_skill_id"));
  390. item.set.set("enchant4_skill_lvl", rset.getInt("enchant4_skill_lvl"));
  391. item.set.set("onCast_skill_id", rset.getInt("onCast_skill_id"));
  392. item.set.set("onCast_skill_lvl", rset.getInt("onCast_skill_lvl"));
  393. item.set.set("onCast_skill_chance", rset.getInt("onCast_skill_chance"));
  394. item.set.set("onCrit_skill_id", rset.getInt("onCrit_skill_id"));
  395. item.set.set("onCrit_skill_lvl", rset.getInt("onCrit_skill_lvl"));
  396. item.set.set("onCrit_skill_chance", rset.getInt("onCrit_skill_chance"));
  397. item.set.set("change_weaponId", rset.getInt("change_weaponId"));
  398. if (item.type == L2WeaponType.PET)
  399. {
  400. item.set.set("type1", L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE);
  401. if (item.set.getInteger("bodypart") == L2Item.SLOT_WOLF)
  402. item.set.set("type2", L2Item.TYPE2_PET_WOLF);
  403. else if (item.set.getInteger("bodypart") == L2Item.SLOT_GREATWOLF)
  404. item.set.set("type2", L2Item.TYPE2_PET_EVOLVEDWOLF);
  405. else if (item.set.getInteger("bodypart") == L2Item.SLOT_HATCHLING)
  406. item.set.set("type2", L2Item.TYPE2_PET_HATCHLING);
  407. else if (item.set.getInteger("bodypart") == L2Item.SLOT_BABYPET)
  408. item.set.set("type2", L2Item.TYPE2_PET_BABY);
  409. else
  410. item.set.set("type2", L2Item.TYPE2_PET_STRIDER);
  411. item.set.set("bodypart", L2Item.SLOT_R_HAND);
  412. }
  413. return item;
  414. }
  415. /**
  416. * Returns object Item from the record of the database
  417. * @param rset : ResultSet designating a record of the [armor] table of database
  418. * @return Item : object created from the database record
  419. * @throws SQLException
  420. */
  421. private Item readArmor(ResultSet rset) throws SQLException
  422. {
  423. Item item = new Item();
  424. item.set = new StatsSet();
  425. item.type = _armorTypes.get(rset.getString("armor_type"));
  426. item.id = rset.getInt("item_id");
  427. item.name = rset.getString("name");
  428. item.set.set("item_id", item.id);
  429. item.set.set("name", item.name);
  430. int bodypart = _slots.get(rset.getString("bodypart"));
  431. item.set.set("bodypart", bodypart);
  432. item.set.set("crystallizable", Boolean.valueOf(rset.getString("crystallizable")));
  433. item.set.set("crystal_count", rset.getInt("crystal_count"));
  434. item.set.set("sellable", Boolean.valueOf(rset.getString("sellable")));
  435. item.set.set("dropable", Boolean.valueOf(rset.getString("dropable")));
  436. item.set.set("destroyable", Boolean.valueOf(rset.getString("destroyable")));
  437. item.set.set("tradeable", Boolean.valueOf(rset.getString("tradeable")));
  438. item.set.set("depositable", Boolean.valueOf(rset.getString("depositable")));
  439. item.set.set("enchant4_skill", rset.getString("enchant4_skill"));
  440. item.set.set("skill", rset.getString("skill"));
  441. if (bodypart == L2Item.SLOT_NECK || bodypart == L2Item.SLOT_HAIR || bodypart == L2Item.SLOT_HAIR2
  442. || bodypart == L2Item.SLOT_HAIRALL || (bodypart & L2Item.SLOT_L_EAR) != 0 || (bodypart & L2Item.SLOT_L_FINGER) != 0
  443. || (bodypart & L2Item.SLOT_R_BRACELET) != 0 || (bodypart & L2Item.SLOT_L_BRACELET) != 0 )
  444. {
  445. item.set.set("type1", L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE);
  446. item.set.set("type2", L2Item.TYPE2_ACCESSORY);
  447. }
  448. else
  449. {
  450. item.set.set("type1", L2Item.TYPE1_SHIELD_ARMOR);
  451. item.set.set("type2", L2Item.TYPE2_SHIELD_ARMOR);
  452. }
  453. item.set.set("weight", rset.getInt("weight"));
  454. item.set.set("material", _materials.get(rset.getString("material")));
  455. item.set.set("crystal_type", _crystalTypes.get(rset.getString("crystal_type")));
  456. item.set.set("avoid_modify", rset.getInt("avoid_modify"));
  457. item.set.set("duration", rset.getInt("duration"));
  458. item.set.set("time", rset.getInt("time"));
  459. item.set.set("p_def", rset.getInt("p_def"));
  460. item.set.set("m_def", rset.getInt("m_def"));
  461. item.set.set("mp_bonus", rset.getInt("mp_bonus"));
  462. item.set.set("price", rset.getInt("price"));
  463. if (item.type == L2ArmorType.PET)
  464. {
  465. if (bodypart == L2Item.SLOT_NECK)
  466. {
  467. item.set.set("type1", L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE);
  468. item.set.set("type2", L2Item.TYPE2_ACCESSORY);
  469. item.set.set("bodypart", L2Item.SLOT_NECK);
  470. }
  471. else
  472. {
  473. item.set.set("type1", L2Item.TYPE1_SHIELD_ARMOR);
  474. switch (item.set.getInteger("bodypart"))
  475. {
  476. case L2Item.SLOT_WOLF:
  477. item.set.set("type2", L2Item.TYPE2_PET_WOLF);
  478. break;
  479. case L2Item.SLOT_GREATWOLF:
  480. item.set.set("type2", L2Item.TYPE2_PET_EVOLVEDWOLF);
  481. break;
  482. case L2Item.SLOT_HATCHLING:
  483. item.set.set("type2", L2Item.TYPE2_PET_HATCHLING);
  484. break;
  485. case L2Item.SLOT_BABYPET:
  486. item.set.set("type2", L2Item.TYPE2_PET_BABY);
  487. break;
  488. default:
  489. item.set.set("type2", L2Item.TYPE2_PET_STRIDER);
  490. break;
  491. }
  492. item.set.set("bodypart", L2Item.SLOT_CHEST);
  493. }
  494. }
  495. return item;
  496. }
  497. /**
  498. * Returns object Item from the record of the database
  499. * @param rset : ResultSet designating a record of the [etcitem] table of database
  500. * @return Item : object created from the database record
  501. * @throws SQLException
  502. */
  503. private Item readItem(ResultSet rset) throws SQLException
  504. {
  505. Item item = new Item();
  506. item.set = new StatsSet();
  507. item.id = rset.getInt("item_id");
  508. item.set.set("item_id", item.id);
  509. item.set.set("crystallizable", Boolean.valueOf(rset.getString("crystallizable")));
  510. item.set.set("type1", L2Item.TYPE1_ITEM_QUESTITEM_ADENA);
  511. item.set.set("type2", L2Item.TYPE2_OTHER);
  512. item.set.set("bodypart", 0);
  513. item.set.set("crystal_count", rset.getInt("crystal_count"));
  514. item.set.set("sellable", Boolean.valueOf(rset.getString("sellable")));
  515. item.set.set("dropable", Boolean.valueOf(rset.getString("dropable")));
  516. item.set.set("destroyable", Boolean.valueOf(rset.getString("destroyable")));
  517. item.set.set("tradeable", Boolean.valueOf(rset.getString("tradeable")));
  518. item.set.set("depositable", Boolean.valueOf(rset.getString("depositable")));
  519. item.set.set("handler", rset.getString("handler"));
  520. item.set.set("skill", rset.getString("skill"));
  521. String itemType = rset.getString("item_type");
  522. if (itemType.equals("none"))
  523. item.type = L2EtcItemType.OTHER; // only for default
  524. else if (itemType.equals("castle_guard"))
  525. item.type = L2EtcItemType.SCROLL; // dummy
  526. else if (itemType.equals("material"))
  527. item.type = L2EtcItemType.MATERIAL;
  528. else if (itemType.equals("pet_collar"))
  529. item.type = L2EtcItemType.PET_COLLAR;
  530. else if (itemType.equals("potion"))
  531. item.type = L2EtcItemType.POTION;
  532. else if (itemType.equals("recipe"))
  533. item.type = L2EtcItemType.RECEIPE;
  534. else if (itemType.equals("scroll"))
  535. item.type = L2EtcItemType.SCROLL;
  536. else if (itemType.equals("seed"))
  537. item.type = L2EtcItemType.SEED;
  538. else if (itemType.equals("shot"))
  539. item.type = L2EtcItemType.SHOT;
  540. else if (itemType.equals("spellbook"))
  541. item.type = L2EtcItemType.SPELLBOOK; // Spellbook, Amulet, Blueprint
  542. else if (itemType.equals("herb"))
  543. item.type = L2EtcItemType.HERB;
  544. else if (itemType.equals("arrow"))
  545. {
  546. item.type = L2EtcItemType.ARROW;
  547. item.set.set("bodypart", L2Item.SLOT_L_HAND);
  548. }
  549. else if (itemType.equals("bolt"))
  550. {
  551. item.type = L2EtcItemType.BOLT;
  552. item.set.set("bodypart", L2Item.SLOT_L_HAND);
  553. }
  554. else if (itemType.equals("quest"))
  555. {
  556. item.type = L2EtcItemType.QUEST;
  557. item.set.set("type2", L2Item.TYPE2_QUEST);
  558. }
  559. else if (itemType.equals("lure"))
  560. {
  561. item.type = L2EtcItemType.OTHER;
  562. item.set.set("bodypart", L2Item.SLOT_L_HAND);
  563. }
  564. else
  565. {
  566. _log.fine("unknown etcitem type:" + itemType);
  567. item.type = L2EtcItemType.OTHER;
  568. }
  569. String consume = rset.getString("consume_type");
  570. if (consume.equals("asset"))
  571. {
  572. item.type = L2EtcItemType.MONEY;
  573. item.set.set("stackable", true);
  574. item.set.set("type2", L2Item.TYPE2_MONEY);
  575. }
  576. else if (consume.equals("stackable"))
  577. {
  578. item.set.set("stackable", true);
  579. }
  580. else
  581. {
  582. item.set.set("stackable", false);
  583. }
  584. int material = _materials.get(rset.getString("material"));
  585. item.set.set("material", material);
  586. int crystal = _crystalTypes.get(rset.getString("crystal_type"));
  587. item.set.set("crystal_type", crystal);
  588. int weight = rset.getInt("weight");
  589. item.set.set("weight", weight);
  590. item.name = rset.getString("name");
  591. item.set.set("name", item.name);
  592. item.set.set("duration", rset.getInt("duration"));
  593. item.set.set("time", rset.getInt("time"));
  594. item.set.set("price", rset.getInt("price"));
  595. return item;
  596. }
  597. /**
  598. * Returns if ItemTable initialized
  599. * @return boolean
  600. */
  601. public boolean isInitialized()
  602. {
  603. return _initialized;
  604. }
  605. /*
  606. private void fillEtcItemsTable()
  607. {
  608. for (Item itemInfo : itemData.values())
  609. {
  610. L2EtcItem item = SkillsEngine.getInstance().loadEtcItem(itemInfo.id, itemInfo.type, itemInfo.name, itemInfo.set);
  611. if (item == null)
  612. {
  613. item = new L2EtcItem((L2EtcItemType)itemInfo.type, itemInfo.set);
  614. }
  615. _etcItems.put(item.getItemId(), item);
  616. }
  617. }
  618. private void fillArmorsTable()
  619. {
  620. List<L2Armor> armorList = SkillsEngine.getInstance().loadArmors(armorData);
  621. /*for (Item itemInfo : armorData.values())
  622. {
  623. L2Armor armor = SkillsEngine.getInstance().loadArmor(itemInfo.id, itemInfo.type, itemInfo.name, itemInfo.set);
  624. if (armor == null)
  625. armor = new L2Armor((L2ArmorType)itemInfo.type, itemInfo.set);
  626. _armors.put(armor.getItemId(), armor);
  627. }*
  628. }
  629. private void FillWeaponsTable()
  630. {
  631. for (Item itemInfo : weaponData.values())
  632. {
  633. L2Weapon weapon = SkillsEngine.getInstance().loadWeapon(itemInfo.id, itemInfo.type, itemInfo.name, itemInfo.set);
  634. if (weapon == null)
  635. weapon = new L2Weapon((L2WeaponType)itemInfo.type, itemInfo.set);
  636. _weapons.put(weapon.getItemId(), weapon);
  637. }
  638. }*/
  639. /**
  640. * Builds a variable in which all items are putting in in function of their ID.
  641. */
  642. private void buildFastLookupTable()
  643. {
  644. int highestId = 0;
  645. // Get highest ID of item in armor FastMap, then in weapon FastMap, and finally in etcitem FastMap
  646. for (L2Armor item : _armors.values())
  647. {
  648. if (item.getItemId() > highestId)
  649. {
  650. highestId = item.getItemId();
  651. }
  652. }
  653. for (L2Weapon item : _weapons.values())
  654. {
  655. if (item.getItemId() > highestId)
  656. {
  657. highestId = item.getItemId();
  658. }
  659. }
  660. for (L2EtcItem item : _etcItems.values())
  661. {
  662. if (item.getItemId() > highestId)
  663. {
  664. highestId = item.getItemId();
  665. }
  666. }
  667. // Create a FastLookUp Table called _allTemplates of size : value of the highest item ID
  668. _log.info("Highest item id used:" + highestId);
  669. _allTemplates = new L2Item[highestId + 1];
  670. // Insert armor item in Fast Look Up Table
  671. for (L2Armor item : _armors.values())
  672. {
  673. assert _allTemplates[item.getItemId()] == null;
  674. _allTemplates[item.getItemId()] = item;
  675. }
  676. // Insert weapon item in Fast Look Up Table
  677. for (L2Weapon item : _weapons.values())
  678. {
  679. assert _allTemplates[item.getItemId()] == null;
  680. _allTemplates[item.getItemId()] = item;
  681. }
  682. // Insert etcItem item in Fast Look Up Table
  683. for (L2EtcItem item : _etcItems.values())
  684. {
  685. assert _allTemplates[item.getItemId()] == null;
  686. _allTemplates[item.getItemId()] = item;
  687. }
  688. }
  689. /**
  690. * Returns the item corresponding to the item ID
  691. * @param id : int designating the item
  692. * @return L2Item
  693. */
  694. public L2Item getTemplate(int id)
  695. {
  696. if (id >= _allTemplates.length)
  697. return null;
  698. else
  699. return _allTemplates[id];
  700. }
  701. /**
  702. * Create the L2ItemInstance corresponding to the Item Identifier and quantitiy add logs the activity.<BR><BR>
  703. *
  704. * <B><U> Actions</U> :</B><BR><BR>
  705. * <li>Create and Init the L2ItemInstance corresponding to the Item Identifier and quantity </li>
  706. * <li>Add the L2ItemInstance object to _allObjects of L2world </li>
  707. * <li>Logs Item creation according to log settings</li><BR><BR>
  708. *
  709. * @param process : String Identifier of process triggering this action
  710. * @param itemId : int Item Identifier of the item to be created
  711. * @param count : int Quantity of items to be created for stackable items
  712. * @param actor : L2PcInstance Player requesting the item creation
  713. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  714. * @return L2ItemInstance corresponding to the new item
  715. */
  716. public L2ItemInstance createItem(String process, int itemId, long count, L2PcInstance actor, L2Object reference)
  717. {
  718. // Create and Init the L2ItemInstance corresponding to the Item Identifier
  719. L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
  720. if (process.equalsIgnoreCase("loot") && !Config.AUTO_LOOT)
  721. {
  722. ScheduledFuture<?> itemLootShedule;
  723. long delay = 0;
  724. // if in CommandChannel and was killing a World/RaidBoss
  725. if (reference instanceof L2GrandBossInstance || reference instanceof L2RaidBossInstance)
  726. {
  727. if (((L2Attackable) reference).getFirstCommandChannelAttacked() != null
  728. && ((L2Attackable) reference).getFirstCommandChannelAttacked().meetRaidWarCondition(reference))
  729. {
  730. item.setOwnerId(((L2Attackable) reference).getFirstCommandChannelAttacked().getChannelLeader().getObjectId());
  731. delay = 300000;
  732. }
  733. else
  734. {
  735. delay = 15000;
  736. item.setOwnerId(actor.getObjectId());
  737. }
  738. }
  739. else
  740. {
  741. item.setOwnerId(actor.getObjectId());
  742. delay = 15000;
  743. }
  744. itemLootShedule = ThreadPoolManager.getInstance().scheduleGeneral(new resetOwner(item), delay);
  745. item.setItemLootShedule(itemLootShedule);
  746. }
  747. if (Config.DEBUG)
  748. _log.fine("ItemTable: Item created oid:" + item.getObjectId() + " itemid:" + itemId);
  749. // Add the L2ItemInstance object to _allObjects of L2world
  750. L2World.getInstance().storeObject(item);
  751. // Set Item parameters
  752. if (item.isStackable() && count > 1)
  753. item.setCount(count);
  754. if (Config.LOG_ITEMS && !process.equals("Reset"))
  755. {
  756. LogRecord record = new LogRecord(Level.INFO, "CREATE:" + process);
  757. record.setLoggerName("item");
  758. record.setParameters(new Object[] { item, actor, reference });
  759. _logItems.log(record);
  760. }
  761. if (actor != null)
  762. {
  763. if (actor.isGM())
  764. {
  765. String referenceName = "no-reference";
  766. if (reference != null)
  767. {
  768. referenceName = (reference.getName() != null ? reference.getName() : "no-name");
  769. }
  770. String targetName = (actor.getTarget() != null ? actor.getTarget().getName() : "no-target");
  771. if (Config.GMAUDIT)
  772. GMAudit.auditGMAction(actor.getName(), process + "(id: " + itemId + " count: " + count + " name: " + item.getItemName()
  773. + " objId: " + item.getObjectId() + ")", targetName, "L2Object referencing this action is: " + referenceName);
  774. }
  775. }
  776. return item;
  777. }
  778. public L2ItemInstance createItem(String process, int itemId, int count, L2PcInstance actor)
  779. {
  780. return createItem(process, itemId, count, actor, null);
  781. }
  782. /**
  783. * Returns a dummy (fr = factice) item.<BR><BR>
  784. * <U><I>Concept :</I></U><BR>
  785. * Dummy item is created by setting the ID of the object in the world at null value
  786. * @param itemId : int designating the item
  787. * @return L2ItemInstance designating the dummy item created
  788. */
  789. public L2ItemInstance createDummyItem(int itemId)
  790. {
  791. L2Item item = getTemplate(itemId);
  792. if (item == null)
  793. return null;
  794. L2ItemInstance temp = new L2ItemInstance(0, item);
  795. try
  796. {
  797. temp = new L2ItemInstance(0, itemId);
  798. }
  799. catch (ArrayIndexOutOfBoundsException e)
  800. {
  801. // this can happen if the item templates were not initialized
  802. }
  803. if (temp.getItem() == null)
  804. {
  805. _log.warning("ItemTable: Item Template missing for Id: " + itemId);
  806. }
  807. return temp;
  808. }
  809. /**
  810. * Destroys the L2ItemInstance.<BR><BR>
  811. *
  812. * <B><U> Actions</U> :</B><BR><BR>
  813. * <li>Sets L2ItemInstance parameters to be unusable </li>
  814. * <li>Removes the L2ItemInstance object to _allObjects of L2world </li>
  815. * <li>Logs Item delettion according to log settings</li><BR><BR>
  816. *
  817. * @param process : String Identifier of process triggering this action
  818. * @param itemId : int Item Identifier of the item to be created
  819. * @param actor : L2PcInstance Player requesting the item destroy
  820. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  821. */
  822. public void destroyItem(String process, L2ItemInstance item, L2PcInstance actor, L2Object reference)
  823. {
  824. synchronized (item)
  825. {
  826. item.setCount(0);
  827. item.setOwnerId(0);
  828. item.setLocation(ItemLocation.VOID);
  829. item.setLastChange(L2ItemInstance.REMOVED);
  830. L2World.getInstance().removeObject(item);
  831. IdFactory.getInstance().releaseId(item.getObjectId());
  832. if (Config.LOG_ITEMS)
  833. {
  834. LogRecord record = new LogRecord(Level.INFO, "DELETE:" + process);
  835. record.setLoggerName("item");
  836. record.setParameters(new Object[] { item, actor, reference });
  837. _logItems.log(record);
  838. }
  839. if (actor != null)
  840. {
  841. if (actor.isGM())
  842. {
  843. String referenceName = "no-reference";
  844. if (reference != null)
  845. {
  846. referenceName = (reference.getName() != null ? reference.getName() : "no-name");
  847. }
  848. String targetName = (actor.getTarget() != null ? actor.getTarget().getName() : "no-target");
  849. if (Config.GMAUDIT)
  850. GMAudit.auditGMAction(actor.getName(), process + "(id: " + item.getItemId() + " count: " + item.getCount()
  851. + " itemObjId: " + item.getObjectId() + ")", targetName, "L2Object referencing this action is: "
  852. + referenceName);
  853. }
  854. }
  855. // if it's a pet control item, delete the pet as well
  856. if (L2PetDataTable.isPetItem(item.getItemId()))
  857. {
  858. Connection con = null;
  859. try
  860. {
  861. // Delete the pet in db
  862. con = L2DatabaseFactory.getInstance().getConnection();
  863. PreparedStatement statement = con.prepareStatement("DELETE FROM pets WHERE item_obj_id=?");
  864. statement.setInt(1, item.getObjectId());
  865. statement.execute();
  866. statement.close();
  867. }
  868. catch (Exception e)
  869. {
  870. _log.log(Level.WARNING, "could not delete pet objectid:", e);
  871. }
  872. finally
  873. {
  874. try
  875. {
  876. con.close();
  877. }
  878. catch (Exception e)
  879. {
  880. }
  881. }
  882. }
  883. }
  884. }
  885. public void reload()
  886. {
  887. load();
  888. }
  889. protected class resetOwner implements Runnable
  890. {
  891. L2ItemInstance _item;
  892. public resetOwner(L2ItemInstance item)
  893. {
  894. _item = item;
  895. }
  896. public void run()
  897. {
  898. _item.setOwnerId(0);
  899. _item.setItemLootShedule(null);
  900. }
  901. }
  902. public Collection <Integer> getAllArmorsId()
  903. {
  904. return _armors.keySet();
  905. }
  906. public Collection <Integer> getAllWeaponsId()
  907. {
  908. return _weapons.keySet();
  909. }
  910. @SuppressWarnings("synthetic-access")
  911. private static class SingletonHolder
  912. {
  913. protected static final ItemTable _instance = new ItemTable();
  914. }
  915. }