2
0

ItemTable.java 35 KB

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