ItemTable.java 34 KB

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