ItemTable.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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," +
  161. " material, crystal_type, avoid_modify, duration, p_def, m_def, mp_bonus," +
  162. " price, crystal_count, sellable, dropable, destroyable, tradeable, skill FROM armor",
  163. "SELECT item_id, name, bodypart, crystallizable, weight, soulshots, spiritshots," +
  164. " material, crystal_type, p_dam, rnd_dam, weaponType, critical, hit_modify, avoid_modify," +
  165. " shield_def, shield_def_rate, atk_speed, mp_consume, m_dam, duration, price, crystal_count," +
  166. " sellable, dropable, destroyable, tradeable, skill,enchant4_skill_id,enchant4_skill_lvl, onCast_skill_id, onCast_skill_lvl," +
  167. " onCast_skill_chance, onCrit_skill_id, onCrit_skill_lvl, onCrit_skill_chance, change_weaponId FROM weapon"
  168. };
  169. /** List of etcItem */
  170. private static final Map<Integer, Item> itemData = new FastMap<Integer, Item>();
  171. /** List of weapons */
  172. private static final Map<Integer, Item> weaponData = new FastMap<Integer, Item>();
  173. /** List of armor */
  174. private static final Map<Integer, Item> armorData = new FastMap<Integer, Item>();
  175. /**
  176. * Returns instance of ItemTable
  177. * @return ItemTable
  178. */
  179. public static ItemTable getInstance()
  180. {
  181. if (_instance == null)
  182. {
  183. _instance = new ItemTable();
  184. }
  185. return _instance;
  186. }
  187. /**
  188. * Returns a new object Item
  189. * @return
  190. */
  191. public Item newItem()
  192. {
  193. return new Item();
  194. }
  195. /**
  196. * Constructor.
  197. */
  198. public ItemTable()
  199. {
  200. _etcItems = new FastMap<Integer, L2EtcItem>();
  201. _armors = new FastMap<Integer, L2Armor>();
  202. _weapons = new FastMap<Integer, L2Weapon>();
  203. java.sql.Connection con = null;
  204. try
  205. {
  206. con = L2DatabaseFactory.getInstance().getConnection();
  207. for (String selectQuery : SQL_ITEM_SELECTS)
  208. {
  209. // Don't load custom tables if disabled.
  210. if (selectQuery.endsWith("_etcitem") && !Config.CUSTOM_ETCITEM_TABLE)
  211. continue;
  212. else if (selectQuery.endsWith("_armor") && !Config.CUSTOM_ARMOR_TABLE)
  213. continue;
  214. else if (selectQuery.endsWith("_weapon") && !Config.CUSTOM_WEAPON_TABLE)
  215. continue;
  216. PreparedStatement statement = con.prepareStatement(selectQuery);
  217. ResultSet rset = statement.executeQuery();
  218. // Add item in correct FastMap
  219. while(rset.next())
  220. {
  221. if (selectQuery.endsWith("etcitem"))
  222. {
  223. Item newItem = readItem(rset);
  224. itemData.put(newItem.id, newItem);
  225. }
  226. else if (selectQuery.endsWith("armor"))
  227. {
  228. Item newItem = readArmor(rset);
  229. armorData.put(newItem.id, newItem);
  230. }
  231. else if (selectQuery.endsWith("weapon"))
  232. {
  233. Item newItem = readWeapon(rset);
  234. weaponData.put(newItem.id, newItem);
  235. }
  236. }
  237. rset.close();
  238. statement.close();
  239. }
  240. }
  241. catch (Exception e)
  242. {
  243. _log.log(Level.WARNING, "data error on item: ", e);
  244. }
  245. finally { try { con.close(); } catch (Exception e) {} }
  246. for (L2Armor armor : SkillsEngine.getInstance().loadArmors(armorData))
  247. {
  248. _armors.put(armor.getItemId(), armor);
  249. }
  250. _log.config("ItemTable: Loaded " + _armors.size() + " Armors.");
  251. for (L2EtcItem item : SkillsEngine.getInstance().loadItems(itemData))
  252. {
  253. _etcItems.put(item.getItemId(), item);
  254. }
  255. _log.config("ItemTable: Loaded " + _etcItems.size() + " Items.");
  256. for (L2Weapon weapon : SkillsEngine.getInstance().loadWeapons(weaponData))
  257. {
  258. _weapons.put(weapon.getItemId(), weapon);
  259. }
  260. _log.config("ItemTable: Loaded " + _weapons.size() + " Weapons.");
  261. //fillEtcItemsTable();
  262. //fillArmorsTable();
  263. //FillWeaponsTable();
  264. buildFastLookupTable();
  265. }
  266. /**
  267. * Returns object Item from the record of the database
  268. * @param rset : ResultSet designating a record of the [weapon] table of database
  269. * @return Item : object created from the database record
  270. * @throws SQLException
  271. */
  272. private Item readWeapon(ResultSet rset) throws SQLException
  273. {
  274. Item item = new Item();
  275. item.set = new StatsSet();
  276. item.type = _weaponTypes.get(rset.getString("weaponType"));
  277. item.id = rset.getInt("item_id");
  278. item.name = rset.getString("name");
  279. item.set.set("item_id", item.id);
  280. item.set.set("name", item.name);
  281. // lets see if this is a shield
  282. if (item.type == L2WeaponType.NONE)
  283. {
  284. item.set.set("type1", L2Item.TYPE1_SHIELD_ARMOR);
  285. item.set.set("type2", L2Item.TYPE2_SHIELD_ARMOR);
  286. }
  287. else
  288. {
  289. item.set.set("type1", L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE);
  290. item.set.set("type2", L2Item.TYPE2_WEAPON);
  291. }
  292. item.set.set("bodypart", _slots.get(rset.getString("bodypart")));
  293. item.set.set("material", _materials.get(rset.getString("material")));
  294. item.set.set("crystal_type", _crystalTypes.get(rset.getString("crystal_type")));
  295. item.set.set("crystallizable", Boolean.valueOf(rset.getString("crystallizable")).booleanValue());
  296. item.set.set("weight", rset.getInt("weight"));
  297. item.set.set("soulshots", rset.getInt("soulshots"));
  298. item.set.set("spiritshots", rset.getInt("spiritshots"));
  299. item.set.set("p_dam", rset.getInt("p_dam"));
  300. item.set.set("rnd_dam", rset.getInt("rnd_dam"));
  301. item.set.set("critical", rset.getInt("critical"));
  302. item.set.set("hit_modify", rset.getDouble("hit_modify"));
  303. item.set.set("avoid_modify", rset.getInt("avoid_modify"));
  304. item.set.set("shield_def", rset.getInt("shield_def"));
  305. item.set.set("shield_def_rate",rset.getInt("shield_def_rate"));
  306. item.set.set("atk_speed", rset.getInt("atk_speed"));
  307. item.set.set("mp_consume", rset.getInt("mp_consume"));
  308. item.set.set("m_dam", rset.getInt("m_dam"));
  309. item.set.set("duration", rset.getInt("duration"));
  310. item.set.set("price", rset.getInt("price"));
  311. item.set.set("crystal_count", rset.getInt("crystal_count"));
  312. item.set.set("sellable", Boolean.valueOf(rset.getString("sellable")));
  313. item.set.set("dropable", Boolean.valueOf(rset.getString("dropable")));
  314. item.set.set("destroyable", Boolean.valueOf(rset.getString("destroyable")));
  315. item.set.set("tradeable", Boolean.valueOf(rset.getString("tradeable")));
  316. item.set.set("skill", rset.getString("skill"));
  317. item.set.set("enchant4_skill_id", rset.getInt("enchant4_skill_id"));
  318. item.set.set("enchant4_skill_lvl", rset.getInt("enchant4_skill_lvl"));
  319. item.set.set("onCast_skill_id", rset.getInt("onCast_skill_id"));
  320. item.set.set("onCast_skill_lvl", rset.getInt("onCast_skill_lvl"));
  321. item.set.set("onCast_skill_chance", rset.getInt("onCast_skill_chance"));
  322. item.set.set("onCrit_skill_id", rset.getInt("onCrit_skill_id"));
  323. item.set.set("onCrit_skill_lvl", rset.getInt("onCrit_skill_lvl"));
  324. item.set.set("onCrit_skill_chance", rset.getInt("onCrit_skill_chance"));
  325. item.set.set("change_weaponId", rset.getInt("change_weaponId"));
  326. if (item.type == L2WeaponType.PET)
  327. {
  328. item.set.set("type1", L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE);
  329. if (item.set.getInteger("bodypart") == L2Item.SLOT_WOLF)
  330. item.set.set("type2", L2Item.TYPE2_PET_WOLF);
  331. else if (item.set.getInteger("bodypart") == L2Item.SLOT_GREATWOLF)
  332. item.set.set("type2", L2Item.TYPE2_PET_GREATWOLF);
  333. else if (item.set.getInteger("bodypart") == L2Item.SLOT_HATCHLING)
  334. item.set.set("type2", L2Item.TYPE2_PET_HATCHLING);
  335. else if (item.set.getInteger("bodypart") == L2Item.SLOT_BABYPET)
  336. item.set.set("type2", L2Item.TYPE2_PET_BABY);
  337. else
  338. item.set.set("type2", L2Item.TYPE2_PET_STRIDER);
  339. item.set.set("bodypart", L2Item.SLOT_R_HAND);
  340. }
  341. return item;
  342. }
  343. /**
  344. * Returns object Item from the record of the database
  345. * @param rset : ResultSet designating a record of the [armor] table of database
  346. * @return Item : object created from the database record
  347. * @throws SQLException
  348. */
  349. private Item readArmor(ResultSet rset) throws SQLException
  350. {
  351. Item item = new Item();
  352. item.set = new StatsSet();
  353. item.type = _armorTypes.get(rset.getString("armor_type"));
  354. item.id = rset.getInt("item_id");
  355. item.name = rset.getString("name");
  356. item.set.set("item_id", item.id);
  357. item.set.set("name", item.name);
  358. int bodypart = _slots.get(rset.getString("bodypart"));
  359. item.set.set("bodypart", bodypart);
  360. item.set.set("crystallizable", Boolean.valueOf(rset.getString("crystallizable")));
  361. item.set.set("crystal_count", rset.getInt("crystal_count"));
  362. item.set.set("sellable", Boolean.valueOf(rset.getString("sellable")));
  363. item.set.set("dropable", Boolean.valueOf(rset.getString("dropable")));
  364. item.set.set("destroyable", Boolean.valueOf(rset.getString("destroyable")));
  365. item.set.set("tradeable", Boolean.valueOf(rset.getString("tradeable")));
  366. item.set.set("skill", rset.getString("skill"));
  367. if (bodypart == L2Item.SLOT_NECK ||
  368. bodypart == L2Item.SLOT_HAIR ||
  369. bodypart == L2Item.SLOT_HAIR2 ||
  370. bodypart == L2Item.SLOT_HAIRALL ||
  371. (bodypart & L2Item.SLOT_L_EAR) != 0 ||
  372. (bodypart & L2Item.SLOT_L_FINGER) != 0)
  373. {
  374. item.set.set("type1", L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE);
  375. item.set.set("type2", L2Item.TYPE2_ACCESSORY);
  376. }
  377. else
  378. {
  379. item.set.set("type1", L2Item.TYPE1_SHIELD_ARMOR);
  380. item.set.set("type2", L2Item.TYPE2_SHIELD_ARMOR);
  381. }
  382. item.set.set("weight", rset.getInt("weight"));
  383. item.set.set("material", _materials.get(rset.getString("material")));
  384. item.set.set("crystal_type", _crystalTypes.get(rset.getString("crystal_type")));
  385. item.set.set("avoid_modify", rset.getInt("avoid_modify"));
  386. item.set.set("duration", rset.getInt("duration"));
  387. item.set.set("p_def", rset.getInt("p_def"));
  388. item.set.set("m_def", rset.getInt("m_def"));
  389. item.set.set("mp_bonus", rset.getInt("mp_bonus"));
  390. item.set.set("price", rset.getInt("price"));
  391. if (item.type == L2ArmorType.PET)
  392. {
  393. item.set.set("type1", L2Item.TYPE1_SHIELD_ARMOR);
  394. if (item.set.getInteger("bodypart") == L2Item.SLOT_WOLF)
  395. item.set.set("type2", L2Item.TYPE2_PET_WOLF);
  396. else if (item.set.getInteger("bodypart") == L2Item.SLOT_GREATWOLF)
  397. item.set.set("type2", L2Item.TYPE2_PET_GREATWOLF);
  398. else if (item.set.getInteger("bodypart") == L2Item.SLOT_HATCHLING)
  399. item.set.set("type2", L2Item.TYPE2_PET_HATCHLING);
  400. else if (item.set.getInteger("bodypart") == L2Item.SLOT_BABYPET)
  401. item.set.set("type2", L2Item.TYPE2_PET_BABY);
  402. else
  403. item.set.set("type2", L2Item.TYPE2_PET_STRIDER);
  404. item.set.set("bodypart", L2Item.SLOT_CHEST);
  405. }
  406. return item;
  407. }
  408. /**
  409. * Returns object Item from the record of the database
  410. * @param rset : ResultSet designating a record of the [etcitem] table of database
  411. * @return Item : object created from the database record
  412. * @throws SQLException
  413. */
  414. private Item readItem(ResultSet rset) throws SQLException
  415. {
  416. Item item = new Item();
  417. item.set = new StatsSet();
  418. item.id = rset.getInt("item_id");
  419. item.set.set("item_id", item.id);
  420. item.set.set("crystallizable", Boolean.valueOf(rset.getString("crystallizable")));
  421. item.set.set("type1", L2Item.TYPE1_ITEM_QUESTITEM_ADENA);
  422. item.set.set("type2", L2Item.TYPE2_OTHER);
  423. item.set.set("bodypart", 0);
  424. item.set.set("crystal_count", rset.getInt("crystal_count"));
  425. item.set.set("sellable", Boolean.valueOf(rset.getString("sellable")));
  426. item.set.set("dropable", Boolean.valueOf(rset.getString("dropable")));
  427. item.set.set("destroyable", Boolean.valueOf(rset.getString("destroyable")));
  428. item.set.set("tradeable", Boolean.valueOf(rset.getString("tradeable")));
  429. String itemType = rset.getString("item_type");
  430. if (itemType.equals("none")) item.type = L2EtcItemType.OTHER; // only for default
  431. else if (itemType.equals("castle_guard")) item.type = L2EtcItemType.SCROLL; // dummy
  432. else if (itemType.equals("material")) item.type = L2EtcItemType.MATERIAL;
  433. else if (itemType.equals("pet_collar")) item.type = L2EtcItemType.PET_COLLAR;
  434. else if (itemType.equals("potion")) item.type = L2EtcItemType.POTION;
  435. else if (itemType.equals("recipe")) item.type = L2EtcItemType.RECEIPE;
  436. else if (itemType.equals("scroll")) item.type = L2EtcItemType.SCROLL;
  437. else if (itemType.equals("seed")) item.type = L2EtcItemType.SEED;
  438. else if (itemType.equals("shot")) item.type = L2EtcItemType.SHOT;
  439. else if (itemType.equals("spellbook")) item.type = L2EtcItemType.SPELLBOOK; // Spellbook, Amulet, Blueprint
  440. else if (itemType.equals("herb")) item.type = L2EtcItemType.HERB;
  441. else if (itemType.equals("arrow"))
  442. {
  443. item.type = L2EtcItemType.ARROW;
  444. item.set.set("bodypart", L2Item.SLOT_L_HAND);
  445. }
  446. else if(itemType.equals("bolt"))
  447. {
  448. item.type = L2EtcItemType.BOLT;
  449. item.set.set("bodypart", L2Item.SLOT_L_HAND);
  450. }
  451. else if (itemType.equals("quest"))
  452. {
  453. item.type = L2EtcItemType.QUEST;
  454. item.set.set("type2", L2Item.TYPE2_QUEST);
  455. }
  456. else if (itemType.equals("lure"))
  457. {
  458. item.type = L2EtcItemType.OTHER;
  459. item.set.set("bodypart", L2Item.SLOT_L_HAND);
  460. }
  461. else
  462. {
  463. _log.fine("unknown etcitem type:" + itemType);
  464. item.type = L2EtcItemType.OTHER;
  465. }
  466. String consume = rset.getString("consume_type");
  467. if (consume.equals("asset"))
  468. {
  469. item.type = L2EtcItemType.MONEY;
  470. item.set.set("stackable", true);
  471. item.set.set("type2", L2Item.TYPE2_MONEY);
  472. }
  473. else if (consume.equals("stackable"))
  474. {
  475. item.set.set("stackable", true);
  476. }
  477. else
  478. {
  479. item.set.set("stackable", false);
  480. }
  481. int material = _materials.get(rset.getString("material"));
  482. item.set.set("material", material);
  483. int crystal = _crystalTypes.get(rset.getString("crystal_type"));
  484. item.set.set("crystal_type", crystal);
  485. int weight = rset.getInt("weight");
  486. item.set.set("weight", weight);
  487. item.name = rset.getString("name");
  488. item.set.set("name", item.name);
  489. item.set.set("duration", rset.getInt("duration"));
  490. item.set.set("price", rset.getInt("price"));
  491. return item;
  492. }
  493. /**
  494. * Returns if ItemTable initialized
  495. * @return boolean
  496. */
  497. public boolean isInitialized()
  498. {
  499. return _initialized;
  500. }
  501. /*
  502. private void fillEtcItemsTable()
  503. {
  504. for (Item itemInfo : itemData.values())
  505. {
  506. L2EtcItem item = SkillsEngine.getInstance().loadEtcItem(itemInfo.id, itemInfo.type, itemInfo.name, itemInfo.set);
  507. if (item == null)
  508. {
  509. item = new L2EtcItem((L2EtcItemType)itemInfo.type, itemInfo.set);
  510. }
  511. _etcItems.put(item.getItemId(), item);
  512. }
  513. }
  514. private void fillArmorsTable()
  515. {
  516. List<L2Armor> armorList = SkillsEngine.getInstance().loadArmors(armorData);
  517. /*for (Item itemInfo : armorData.values())
  518. {
  519. L2Armor armor = SkillsEngine.getInstance().loadArmor(itemInfo.id, itemInfo.type, itemInfo.name, itemInfo.set);
  520. if (armor == null)
  521. armor = new L2Armor((L2ArmorType)itemInfo.type, itemInfo.set);
  522. _armors.put(armor.getItemId(), armor);
  523. }*
  524. }
  525. private void FillWeaponsTable()
  526. {
  527. for (Item itemInfo : weaponData.values())
  528. {
  529. L2Weapon weapon = SkillsEngine.getInstance().loadWeapon(itemInfo.id, itemInfo.type, itemInfo.name, itemInfo.set);
  530. if (weapon == null)
  531. weapon = new L2Weapon((L2WeaponType)itemInfo.type, itemInfo.set);
  532. _weapons.put(weapon.getItemId(), weapon);
  533. }
  534. }*/
  535. /**
  536. * Builds a variable in which all items are putting in in function of their ID.
  537. */
  538. private void buildFastLookupTable()
  539. {
  540. int highestId = 0;
  541. // Get highest ID of item in armor FastMap, then in weapon FastMap, and finally in etcitem FastMap
  542. for (L2Armor item : _armors.values())
  543. {
  544. if (item.getItemId() > highestId)
  545. {
  546. highestId = item.getItemId();
  547. }
  548. }
  549. for (L2Weapon item : _weapons.values())
  550. {
  551. if (item.getItemId() > highestId)
  552. {
  553. highestId = item.getItemId();
  554. }
  555. }
  556. for (L2EtcItem item : _etcItems.values())
  557. {
  558. if (item.getItemId() > highestId)
  559. {
  560. highestId = item.getItemId();
  561. }
  562. }
  563. // Create a FastLookUp Table called _allTemplates of size : value of the highest item ID
  564. if (true) _log.severe("highest item id used:" + highestId);
  565. _allTemplates = new L2Item[highestId +1];
  566. // Insert armor item in Fast Look Up Table
  567. for (L2Armor item : _armors.values())
  568. {
  569. assert _allTemplates[item.getItemId()] == null;
  570. _allTemplates[item.getItemId()] = item;
  571. }
  572. // Insert weapon item in Fast Look Up Table
  573. for (L2Weapon item : _weapons.values())
  574. {
  575. assert _allTemplates[item.getItemId()] == null;
  576. _allTemplates[item.getItemId()] = item;
  577. }
  578. // Insert etcItem item in Fast Look Up Table
  579. for (L2EtcItem item : _etcItems.values())
  580. {
  581. assert _allTemplates[item.getItemId()] == null;
  582. _allTemplates[item.getItemId()] = item;
  583. }
  584. }
  585. /**
  586. * Returns the item corresponding to the item ID
  587. * @param id : int designating the item
  588. * @return L2Item
  589. */
  590. public L2Item getTemplate(int id)
  591. {
  592. if (id > _allTemplates.length)
  593. return null;
  594. else
  595. return _allTemplates[id];
  596. }
  597. /**
  598. * Create the L2ItemInstance corresponding to the Item Identifier and quantitiy add logs the activity.<BR><BR>
  599. *
  600. * <B><U> Actions</U> :</B><BR><BR>
  601. * <li>Create and Init the L2ItemInstance corresponding to the Item Identifier and quantity </li>
  602. * <li>Add the L2ItemInstance object to _allObjects of L2world </li>
  603. * <li>Logs Item creation according to log settings</li><BR><BR>
  604. *
  605. * @param process : String Identifier of process triggering this action
  606. * @param itemId : int Item Identifier of the item to be created
  607. * @param count : int Quantity of items to be created for stackable items
  608. * @param actor : L2PcInstance Player requesting the item creation
  609. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  610. * @return L2ItemInstance corresponding to the new item
  611. */
  612. public L2ItemInstance createItem(String process, int itemId, int count, L2PcInstance actor, L2Object reference)
  613. {
  614. // Create and Init the L2ItemInstance corresponding to the Item Identifier
  615. L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
  616. if (process.equalsIgnoreCase("loot") && !Config.AUTO_LOOT)
  617. {
  618. ScheduledFuture<?> itemLootShedule;
  619. long delay = 0;
  620. // if in CommandChannel and was killing a World/RaidBoss
  621. if (reference instanceof L2GrandBossInstance || reference instanceof L2RaidBossInstance)
  622. {
  623. if(((L2Attackable)reference).getFirstCommandChannelAttacked() != null
  624. && ((L2Attackable)reference).getFirstCommandChannelAttacked().meetRaidWarCondition(reference))
  625. {
  626. item.setOwnerId(((L2Attackable)reference).getFirstCommandChannelAttacked().getChannelLeader().getObjectId());
  627. delay = 300000;
  628. }
  629. else
  630. {
  631. delay = 15000;
  632. item.setOwnerId(actor.getObjectId());
  633. }
  634. }
  635. else
  636. {
  637. item.setOwnerId(actor.getObjectId());
  638. delay = 15000;
  639. }
  640. itemLootShedule = ThreadPoolManager.getInstance().scheduleGeneral(new resetOwner(item), delay);
  641. item.setItemLootShedule(itemLootShedule);
  642. }
  643. if (Config.DEBUG) _log.fine("ItemTable: Item created oid:" + item.getObjectId()+ " itemid:" + itemId);
  644. // Add the L2ItemInstance object to _allObjects of L2world
  645. L2World.getInstance().storeObject(item);
  646. // Set Item parameters
  647. if (item.isStackable() && count > 1) item.setCount(count);
  648. if (Config.LOG_ITEMS)
  649. {
  650. LogRecord record = new LogRecord(Level.INFO, "CREATE:" + process);
  651. record.setLoggerName("item");
  652. record.setParameters(new Object[]{item, actor, reference});
  653. _logItems.log(record);
  654. }
  655. if (actor != null)
  656. {
  657. if (actor.isGM())
  658. {
  659. String referenceName = "no-reference";
  660. if (reference != null)
  661. {
  662. referenceName = (reference.getName() != null?reference.getName():"no-name");
  663. }
  664. String targetName = (actor.getTarget() != null?actor.getTarget().getName():"no-target");
  665. GMAudit.auditGMAction(actor.getName(), process+"(id: "+itemId+" count: "+count+" name: "+item.getItemName()+" objId: "+item.getObjectId()+")", targetName, "L2Object referencing this action is: " + referenceName);
  666. }
  667. }
  668. return item;
  669. }
  670. public L2ItemInstance createItem(String process, int itemId, int count, L2PcInstance actor)
  671. {
  672. return createItem(process, itemId, count, actor, null);
  673. }
  674. /**
  675. * Returns a dummy (fr = factice) item.<BR><BR>
  676. * <U><I>Concept :</I></U><BR>
  677. * Dummy item is created by setting the ID of the object in the world at null value
  678. * @param itemId : int designating the item
  679. * @return L2ItemInstance designating the dummy item created
  680. */
  681. public L2ItemInstance createDummyItem(int itemId)
  682. {
  683. L2Item item = getTemplate(itemId);
  684. if (item == null)
  685. return null;
  686. L2ItemInstance temp = new L2ItemInstance(0, item);
  687. try
  688. {
  689. temp = new L2ItemInstance(0, itemId);
  690. }
  691. catch (ArrayIndexOutOfBoundsException e)
  692. {
  693. // this can happen if the item templates were not initialized
  694. }
  695. if (temp.getItem() == null)
  696. {
  697. _log.warning("ItemTable: Item Template missing for Id: "+ itemId);
  698. }
  699. return temp;
  700. }
  701. /**
  702. * Destroys the L2ItemInstance.<BR><BR>
  703. *
  704. * <B><U> Actions</U> :</B><BR><BR>
  705. * <li>Sets L2ItemInstance parameters to be unusable </li>
  706. * <li>Removes the L2ItemInstance object to _allObjects of L2world </li>
  707. * <li>Logs Item delettion 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 actor : L2PcInstance Player requesting the item destroy
  712. * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  713. */
  714. public void destroyItem(String process, L2ItemInstance item, L2PcInstance actor, L2Object reference)
  715. {
  716. synchronized (item)
  717. {
  718. item.setCount(0);
  719. item.setOwnerId(0);
  720. item.setLocation(ItemLocation.VOID);
  721. item.setLastChange(L2ItemInstance.REMOVED);
  722. L2World.getInstance().removeObject(item);
  723. IdFactory.getInstance().releaseId(item.getObjectId());
  724. if (Config.LOG_ITEMS)
  725. {
  726. LogRecord record = new LogRecord(Level.INFO, "DELETE:" + process);
  727. record.setLoggerName("item");
  728. record.setParameters(new Object[]{item, actor, reference});
  729. _logItems.log(record);
  730. }
  731. if (actor != null)
  732. {
  733. if (actor.isGM())
  734. {
  735. String referenceName = "no-reference";
  736. if (reference != null)
  737. {
  738. referenceName = (reference.getName() != null?reference.getName():"no-name");
  739. }
  740. String targetName = (actor.getTarget() != null?actor.getTarget().getName():"no-target");
  741. GMAudit.auditGMAction(actor.getName(), process + "(id: " + item.getItemId() + " count: "+item.getCount()+" itemObjId: " +item.getObjectId()+")", targetName, "L2Object referencing this action is: " + referenceName);
  742. }
  743. }
  744. // if it's a pet control item, delete the pet as well
  745. if (L2PetDataTable.isPetItem(item.getItemId()))
  746. {
  747. java.sql.Connection con = null;
  748. try
  749. {
  750. // Delete the pet in db
  751. con = L2DatabaseFactory.getInstance().getConnection();
  752. PreparedStatement statement = con.prepareStatement("DELETE FROM pets WHERE item_obj_id=?");
  753. statement.setInt(1, item.getObjectId());
  754. statement.execute();
  755. statement.close();
  756. }
  757. catch (Exception e)
  758. {
  759. _log.log(Level.WARNING, "could not delete pet objectid:", e);
  760. }
  761. finally
  762. {
  763. try { con.close(); } catch (Exception e) {}
  764. }
  765. }
  766. }
  767. }
  768. public void reload()
  769. {
  770. synchronized(_instance)
  771. {
  772. _instance = null;
  773. _instance = new ItemTable();
  774. }
  775. }
  776. protected class resetOwner implements Runnable
  777. {
  778. L2ItemInstance _item;
  779. public resetOwner(L2ItemInstance item)
  780. {
  781. _item = item;
  782. }
  783. public void run()
  784. {
  785. _item.setOwnerId(0);
  786. _item.setItemLootShedule(null);
  787. }
  788. }
  789. }