ItemTable.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 static com.l2jserver.gameserver.model.itemcontainer.PcInventory.ADENA_ID;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import java.util.concurrent.ScheduledFuture;
  22. import java.util.logging.Level;
  23. import java.util.logging.LogRecord;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastList;
  26. import javolution.util.FastMap;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.engines.DocumentEngine;
  31. import com.l2jserver.gameserver.engines.items.Item;
  32. import com.l2jserver.gameserver.idfactory.IdFactory;
  33. import com.l2jserver.gameserver.model.L2Object;
  34. import com.l2jserver.gameserver.model.L2World;
  35. import com.l2jserver.gameserver.model.actor.L2Attackable;
  36. import com.l2jserver.gameserver.model.actor.instance.L2EventMonsterInstance;
  37. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  38. import com.l2jserver.gameserver.model.items.L2Armor;
  39. import com.l2jserver.gameserver.model.items.L2EtcItem;
  40. import com.l2jserver.gameserver.model.items.L2Item;
  41. import com.l2jserver.gameserver.model.items.L2Weapon;
  42. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  43. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance.ItemLocation;
  44. import com.l2jserver.gameserver.model.items.type.L2ArmorType;
  45. import com.l2jserver.gameserver.model.items.type.L2WeaponType;
  46. import com.l2jserver.gameserver.scripting.scriptengine.events.ItemCreateEvent;
  47. import com.l2jserver.gameserver.scripting.scriptengine.listeners.player.NewItemListener;
  48. import com.l2jserver.gameserver.util.GMAudit;
  49. /**
  50. * This class ...
  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 FastList<NewItemListener> newItemListeners = new FastList<NewItemListener>().shared();
  58. public static final Map<String, Integer> _materials = new FastMap<>();
  59. public static final Map<String, Integer> _crystalTypes = new FastMap<>();
  60. public static final Map<String, Integer> _slots = new FastMap<>();
  61. public static final Map<String, L2WeaponType> _weaponTypes = new FastMap<>();
  62. public static final Map<String, L2ArmorType> _armorTypes = new FastMap<>();
  63. private L2Item[] _allTemplates;
  64. private final Map<Integer, L2EtcItem> _etcItems;
  65. private final Map<Integer, L2Armor> _armors;
  66. private final Map<Integer, L2Weapon> _weapons;
  67. static
  68. {
  69. _materials.put("adamantaite", L2Item.MATERIAL_ADAMANTAITE);
  70. _materials.put("blood_steel", L2Item.MATERIAL_BLOOD_STEEL);
  71. _materials.put("bone", L2Item.MATERIAL_BONE);
  72. _materials.put("bronze", L2Item.MATERIAL_BRONZE);
  73. _materials.put("cloth", L2Item.MATERIAL_CLOTH);
  74. _materials.put("chrysolite", L2Item.MATERIAL_CHRYSOLITE);
  75. _materials.put("cobweb", L2Item.MATERIAL_COBWEB);
  76. _materials.put("cotton", L2Item.MATERIAL_FINE_STEEL);
  77. _materials.put("crystal", L2Item.MATERIAL_CRYSTAL);
  78. _materials.put("damascus", L2Item.MATERIAL_DAMASCUS);
  79. _materials.put("dyestuff", L2Item.MATERIAL_DYESTUFF);
  80. _materials.put("fine_steel", L2Item.MATERIAL_FINE_STEEL);
  81. _materials.put("fish", L2Item.MATERIAL_FISH);
  82. _materials.put("gold", L2Item.MATERIAL_GOLD);
  83. _materials.put("horn", L2Item.MATERIAL_HORN);
  84. _materials.put("leather", L2Item.MATERIAL_LEATHER);
  85. _materials.put("liquid", L2Item.MATERIAL_LIQUID);
  86. _materials.put("mithril", L2Item.MATERIAL_MITHRIL);
  87. _materials.put("oriharukon", L2Item.MATERIAL_ORIHARUKON);
  88. _materials.put("paper", L2Item.MATERIAL_PAPER);
  89. _materials.put("rune_xp", L2Item.MATERIAL_RUNE_XP);
  90. _materials.put("rune_sp", L2Item.MATERIAL_RUNE_SP);
  91. _materials.put("rune_remove_penalty", L2Item.MATERIAL_RUNE_PENALTY);
  92. _materials.put("scale_of_dragon", L2Item.MATERIAL_SCALE_OF_DRAGON);
  93. _materials.put("seed", L2Item.MATERIAL_SEED);
  94. _materials.put("silver", L2Item.MATERIAL_SILVER);
  95. _materials.put("steel", L2Item.MATERIAL_STEEL);
  96. _materials.put("wood", L2Item.MATERIAL_WOOD);
  97. _crystalTypes.put("s84", L2Item.CRYSTAL_S84);
  98. _crystalTypes.put("s80", L2Item.CRYSTAL_S80);
  99. _crystalTypes.put("s", L2Item.CRYSTAL_S);
  100. _crystalTypes.put("a", L2Item.CRYSTAL_A);
  101. _crystalTypes.put("b", L2Item.CRYSTAL_B);
  102. _crystalTypes.put("c", L2Item.CRYSTAL_C);
  103. _crystalTypes.put("d", L2Item.CRYSTAL_D);
  104. _crystalTypes.put("none", L2Item.CRYSTAL_NONE);
  105. // weapon types
  106. for (L2WeaponType type : L2WeaponType.values())
  107. {
  108. _weaponTypes.put(type.toString(), type);
  109. }
  110. // armor types
  111. for (L2ArmorType type : L2ArmorType.values())
  112. {
  113. _armorTypes.put(type.toString(), type);
  114. }
  115. _slots.put("shirt", L2Item.SLOT_UNDERWEAR);
  116. _slots.put("lbracelet", L2Item.SLOT_L_BRACELET);
  117. _slots.put("rbracelet", L2Item.SLOT_R_BRACELET);
  118. _slots.put("talisman", L2Item.SLOT_DECO);
  119. _slots.put("chest", L2Item.SLOT_CHEST);
  120. _slots.put("fullarmor", L2Item.SLOT_FULL_ARMOR);
  121. _slots.put("head", L2Item.SLOT_HEAD);
  122. _slots.put("hair", L2Item.SLOT_HAIR);
  123. _slots.put("hairall", L2Item.SLOT_HAIRALL);
  124. _slots.put("underwear", L2Item.SLOT_UNDERWEAR);
  125. _slots.put("back", L2Item.SLOT_BACK);
  126. _slots.put("neck", L2Item.SLOT_NECK);
  127. _slots.put("legs", L2Item.SLOT_LEGS);
  128. _slots.put("feet", L2Item.SLOT_FEET);
  129. _slots.put("gloves", L2Item.SLOT_GLOVES);
  130. _slots.put("chest,legs", L2Item.SLOT_CHEST | L2Item.SLOT_LEGS);
  131. _slots.put("belt", L2Item.SLOT_BELT);
  132. _slots.put("rhand", L2Item.SLOT_R_HAND);
  133. _slots.put("lhand", L2Item.SLOT_L_HAND);
  134. _slots.put("lrhand", L2Item.SLOT_LR_HAND);
  135. _slots.put("rear;lear", L2Item.SLOT_R_EAR | L2Item.SLOT_L_EAR);
  136. _slots.put("rfinger;lfinger", L2Item.SLOT_R_FINGER | L2Item.SLOT_L_FINGER);
  137. _slots.put("wolf", L2Item.SLOT_WOLF);
  138. _slots.put("greatwolf", L2Item.SLOT_GREATWOLF);
  139. _slots.put("hatchling", L2Item.SLOT_HATCHLING);
  140. _slots.put("strider", L2Item.SLOT_STRIDER);
  141. _slots.put("babypet", L2Item.SLOT_BABYPET);
  142. _slots.put("none", L2Item.SLOT_NONE);
  143. // retail compatibility
  144. _slots.put("onepiece", L2Item.SLOT_FULL_ARMOR);
  145. _slots.put("hair2", L2Item.SLOT_HAIR2);
  146. _slots.put("dhair", L2Item.SLOT_HAIRALL);
  147. _slots.put("alldress", L2Item.SLOT_ALLDRESS);
  148. _slots.put("deco1", L2Item.SLOT_DECO);
  149. _slots.put("waist", L2Item.SLOT_BELT);
  150. }
  151. /**
  152. * Returns instance of ItemTable
  153. * @return ItemTable
  154. */
  155. public static ItemTable getInstance()
  156. {
  157. return SingletonHolder._instance;
  158. }
  159. /**
  160. * @return a new object Item
  161. */
  162. public Item newItem()
  163. {
  164. return new Item();
  165. }
  166. /**
  167. * Constructor.
  168. */
  169. protected ItemTable()
  170. {
  171. _etcItems = new FastMap<>();
  172. _armors = new FastMap<>();
  173. _weapons = new FastMap<>();
  174. load();
  175. }
  176. private void load()
  177. {
  178. int highest = 0;
  179. _armors.clear();
  180. _etcItems.clear();
  181. _weapons.clear();
  182. for (L2Item item : DocumentEngine.getInstance().loadItems())
  183. {
  184. if (highest < item.getItemId())
  185. {
  186. highest = item.getItemId();
  187. }
  188. if (item instanceof L2EtcItem)
  189. {
  190. _etcItems.put(item.getItemId(), (L2EtcItem) item);
  191. }
  192. else if (item instanceof L2Armor)
  193. {
  194. _armors.put(item.getItemId(), (L2Armor) item);
  195. }
  196. else
  197. {
  198. _weapons.put(item.getItemId(), (L2Weapon) item);
  199. }
  200. }
  201. buildFastLookupTable(highest);
  202. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _etcItems.size() + " Etc Items");
  203. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _armors.size() + " Armor Items");
  204. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _weapons.size() + " Weapon Items");
  205. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + (_etcItems.size() + _armors.size() + _weapons.size()) + " Items in total.");
  206. }
  207. /**
  208. * Builds a variable in which all items are putting in in function of their ID.
  209. * @param size
  210. */
  211. private void buildFastLookupTable(int size)
  212. {
  213. // Create a FastLookUp Table called _allTemplates of size : value of the highest item ID
  214. _log.info(getClass().getSimpleName() + ": Highest item id used:" + size);
  215. _allTemplates = new L2Item[size + 1];
  216. // Insert armor item in Fast Look Up Table
  217. for (L2Armor item : _armors.values())
  218. {
  219. _allTemplates[item.getItemId()] = item;
  220. }
  221. // Insert weapon item in Fast Look Up Table
  222. for (L2Weapon item : _weapons.values())
  223. {
  224. _allTemplates[item.getItemId()] = item;
  225. }
  226. // Insert etcItem item in Fast Look Up Table
  227. for (L2EtcItem item : _etcItems.values())
  228. {
  229. _allTemplates[item.getItemId()] = item;
  230. }
  231. }
  232. /**
  233. * Returns the item corresponding to the item ID
  234. * @param id : int designating the item
  235. * @return L2Item
  236. */
  237. public L2Item getTemplate(int id)
  238. {
  239. if ((id >= _allTemplates.length) || (id < 0))
  240. {
  241. return null;
  242. }
  243. return _allTemplates[id];
  244. }
  245. /**
  246. * Create the L2ItemInstance corresponding to the Item Identifier and quantitiy add logs the activity. <B><U> Actions</U> :</B> <li>Create and Init the L2ItemInstance corresponding to the Item Identifier and quantity</li> <li>Add the L2ItemInstance object to _allObjects of L2world</li> <li>Logs
  247. * Item creation according to log settings</li>
  248. * @param process : String Identifier of process triggering this action
  249. * @param itemId : int Item Identifier of the item to be created
  250. * @param count : int Quantity of items to be created for stackable items
  251. * @param actor : L2PcInstance Player requesting the item creation
  252. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  253. * @return L2ItemInstance corresponding to the new item
  254. */
  255. public L2ItemInstance createItem(String process, int itemId, long count, L2PcInstance actor, Object reference)
  256. {
  257. if (!fireNewItemListeners(process, itemId, count, actor, reference))
  258. {
  259. return null;
  260. }
  261. // Create and Init the L2ItemInstance corresponding to the Item Identifier
  262. L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
  263. if (process.equalsIgnoreCase("loot"))
  264. {
  265. ScheduledFuture<?> itemLootShedule;
  266. if ((reference instanceof L2Attackable) && ((L2Attackable) reference).isRaid()) // loot privilege for raids
  267. {
  268. L2Attackable raid = (L2Attackable) reference;
  269. // if in CommandChannel and was killing a World/RaidBoss
  270. if ((raid.getFirstCommandChannelAttacked() != null) && !Config.AUTO_LOOT_RAIDS)
  271. {
  272. item.setOwnerId(raid.getFirstCommandChannelAttacked().getLeaderObjectId());
  273. itemLootShedule = ThreadPoolManager.getInstance().scheduleGeneral(new ResetOwner(item), Config.LOOT_RAIDS_PRIVILEGE_INTERVAL);
  274. item.setItemLootShedule(itemLootShedule);
  275. }
  276. }
  277. else if (!Config.AUTO_LOOT || ((reference instanceof L2EventMonsterInstance) && ((L2EventMonsterInstance) reference).eventDropOnGround()))
  278. {
  279. item.setOwnerId(actor.getObjectId());
  280. itemLootShedule = ThreadPoolManager.getInstance().scheduleGeneral(new ResetOwner(item), 15000);
  281. item.setItemLootShedule(itemLootShedule);
  282. }
  283. }
  284. if (Config.DEBUG)
  285. {
  286. _log.fine(getClass().getSimpleName() + ": Item created oid:" + item.getObjectId() + " itemid:" + itemId);
  287. }
  288. // Add the L2ItemInstance object to _allObjects of L2world
  289. L2World.getInstance().storeObject(item);
  290. // Set Item parameters
  291. if (item.isStackable() && (count > 1))
  292. {
  293. item.setCount(count);
  294. }
  295. if (Config.LOG_ITEMS && !process.equals("Reset"))
  296. {
  297. if (!Config.LOG_ITEMS_SMALL_LOG || (Config.LOG_ITEMS_SMALL_LOG && (item.isEquipable() || (item.getItemId() == ADENA_ID))))
  298. {
  299. LogRecord record = new LogRecord(Level.INFO, "CREATE:" + process);
  300. record.setLoggerName("item");
  301. record.setParameters(new Object[]
  302. {
  303. item,
  304. actor,
  305. reference
  306. });
  307. _logItems.log(record);
  308. }
  309. }
  310. if (actor != null)
  311. {
  312. if (actor.isGM())
  313. {
  314. String referenceName = "no-reference";
  315. if (reference instanceof L2Object)
  316. {
  317. referenceName = (((L2Object) reference).getName() != null ? ((L2Object) reference).getName() : "no-name");
  318. }
  319. else if (reference instanceof String)
  320. {
  321. referenceName = (String) reference;
  322. }
  323. String targetName = (actor.getTarget() != null ? actor.getTarget().getName() : "no-target");
  324. if (Config.GMAUDIT)
  325. {
  326. GMAudit.auditGMAction(actor.getName() + " [" + actor.getObjectId() + "]", process + "(id: " + itemId + " count: " + count + " name: " + item.getItemName() + " objId: " + item.getObjectId() + ")", targetName, "L2Object referencing this action is: " + referenceName);
  327. }
  328. }
  329. }
  330. return item;
  331. }
  332. public L2ItemInstance createItem(String process, int itemId, int count, L2PcInstance actor)
  333. {
  334. return createItem(process, itemId, count, actor, null);
  335. }
  336. /**
  337. * Returns a dummy item.<br>
  338. * <U><I>Concept :</I></U><BR>
  339. * Dummy item is created by setting the ID of the object in the world at null value
  340. * @param itemId : int designating the item
  341. * @return L2ItemInstance designating the dummy item created
  342. */
  343. public L2ItemInstance createDummyItem(int itemId)
  344. {
  345. L2Item item = getTemplate(itemId);
  346. if (item == null)
  347. {
  348. return null;
  349. }
  350. L2ItemInstance temp = new L2ItemInstance(0, item);
  351. return temp;
  352. }
  353. /**
  354. * Destroys the L2ItemInstance.<br>
  355. * <B><U> Actions</U> :</B>
  356. * <ul>
  357. * <li>Sets L2ItemInstance parameters to be unusable</li>
  358. * <li>Removes the L2ItemInstance object to _allObjects of L2world</li>
  359. * <li>Logs Item deletion according to log settings</li>
  360. * </ul>
  361. * @param process a string identifier of process triggering this action.
  362. * @param item the item instance to be destroyed.
  363. * @param actor the player requesting the item destroy.
  364. * @param reference the object referencing current action like NPC selling item or previous item in transformation.
  365. */
  366. public void destroyItem(String process, L2ItemInstance item, L2PcInstance actor, Object reference)
  367. {
  368. synchronized (item)
  369. {
  370. long old = item.getCount();
  371. item.setCount(0);
  372. item.setOwnerId(0);
  373. item.setLocation(ItemLocation.VOID);
  374. item.setLastChange(L2ItemInstance.REMOVED);
  375. L2World.getInstance().removeObject(item);
  376. IdFactory.getInstance().releaseId(item.getObjectId());
  377. if (Config.LOG_ITEMS)
  378. {
  379. if (!Config.LOG_ITEMS_SMALL_LOG || (Config.LOG_ITEMS_SMALL_LOG && (item.isEquipable() || (item.getItemId() == ADENA_ID))))
  380. {
  381. LogRecord record = new LogRecord(Level.INFO, "DELETE:" + process);
  382. record.setLoggerName("item");
  383. record.setParameters(new Object[]
  384. {
  385. item,
  386. "PrevCount(" + old + ")",
  387. actor,
  388. reference
  389. });
  390. _logItems.log(record);
  391. }
  392. }
  393. if (actor != null)
  394. {
  395. if (actor.isGM())
  396. {
  397. String referenceName = "no-reference";
  398. if (reference instanceof L2Object)
  399. {
  400. referenceName = (((L2Object) reference).getName() != null ? ((L2Object) reference).getName() : "no-name");
  401. }
  402. else if (reference instanceof String)
  403. {
  404. referenceName = (String) reference;
  405. }
  406. String targetName = (actor.getTarget() != null ? actor.getTarget().getName() : "no-target");
  407. if (Config.GMAUDIT)
  408. {
  409. GMAudit.auditGMAction(actor.getName() + " [" + actor.getObjectId() + "]", process + "(id: " + item.getItemId() + " count: " + item.getCount() + " itemObjId: " + item.getObjectId() + ")", targetName, "L2Object referencing this action is: " + referenceName);
  410. }
  411. }
  412. }
  413. // if it's a pet control item, delete the pet as well
  414. if (item.getItem().isPetItem())
  415. {
  416. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  417. PreparedStatement statement = con.prepareStatement("DELETE FROM pets WHERE item_obj_id=?"))
  418. {
  419. // Delete the pet in db
  420. statement.setInt(1, item.getObjectId());
  421. statement.execute();
  422. }
  423. catch (Exception e)
  424. {
  425. _log.log(Level.WARNING, "could not delete pet objectid:", e);
  426. }
  427. }
  428. }
  429. }
  430. public void reload()
  431. {
  432. load();
  433. EnchantHPBonusData.getInstance().load();
  434. }
  435. protected static class ResetOwner implements Runnable
  436. {
  437. L2ItemInstance _item;
  438. public ResetOwner(L2ItemInstance item)
  439. {
  440. _item = item;
  441. }
  442. @Override
  443. public void run()
  444. {
  445. _item.setOwnerId(0);
  446. _item.setItemLootShedule(null);
  447. }
  448. }
  449. public Set<Integer> getAllArmorsId()
  450. {
  451. return _armors.keySet();
  452. }
  453. public Set<Integer> getAllWeaponsId()
  454. {
  455. return _weapons.keySet();
  456. }
  457. public int getArraySize()
  458. {
  459. return _allTemplates.length;
  460. }
  461. private static class SingletonHolder
  462. {
  463. protected static final ItemTable _instance = new ItemTable();
  464. }
  465. // Listeners
  466. /**
  467. * Fires all the new item listeners, if any
  468. * @param process
  469. * @param itemId
  470. * @param count
  471. * @param actor
  472. * @param reference
  473. * @return
  474. */
  475. private boolean fireNewItemListeners(String process, int itemId, long count, L2PcInstance actor, Object reference)
  476. {
  477. if (!newItemListeners.isEmpty() && (actor != null))
  478. {
  479. ItemCreateEvent event = new ItemCreateEvent();
  480. event.setItemId(itemId);
  481. event.setPlayer(actor);
  482. event.setCount(count);
  483. event.setProcess(process);
  484. event.setReference(reference);
  485. for (NewItemListener listener : newItemListeners)
  486. {
  487. if (listener.containsItemId(itemId))
  488. {
  489. if (!listener.onCreate(event))
  490. {
  491. return false;
  492. }
  493. }
  494. }
  495. }
  496. return true;
  497. }
  498. /**
  499. * Adds a new item listener
  500. * @param listener
  501. */
  502. public static void addNewItemListener(NewItemListener listener)
  503. {
  504. if (!newItemListeners.contains(listener))
  505. {
  506. newItemListeners.add(listener);
  507. }
  508. }
  509. /**
  510. * Removes a new item listener
  511. * @param listener
  512. */
  513. public static void removeNewItemListener(NewItemListener listener)
  514. {
  515. newItemListeners.remove(listener);
  516. }
  517. }