ItemTable.java 35 KB

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