ItemTable.java 35 KB

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