ItemTable.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import static com.l2jserver.gameserver.model.itemcontainer.Inventory.ADENA_ID;
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.concurrent.ScheduledFuture;
  26. import java.util.logging.Level;
  27. import java.util.logging.LogRecord;
  28. import java.util.logging.Logger;
  29. import javolution.util.FastList;
  30. import javolution.util.FastMap;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.L2DatabaseFactory;
  33. import com.l2jserver.gameserver.ThreadPoolManager;
  34. import com.l2jserver.gameserver.engines.DocumentEngine;
  35. import com.l2jserver.gameserver.enums.ItemLocation;
  36. import com.l2jserver.gameserver.idfactory.IdFactory;
  37. import com.l2jserver.gameserver.model.L2Object;
  38. import com.l2jserver.gameserver.model.L2World;
  39. import com.l2jserver.gameserver.model.actor.L2Attackable;
  40. import com.l2jserver.gameserver.model.actor.instance.L2EventMonsterInstance;
  41. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  42. import com.l2jserver.gameserver.model.items.L2Armor;
  43. import com.l2jserver.gameserver.model.items.L2EtcItem;
  44. import com.l2jserver.gameserver.model.items.L2Item;
  45. import com.l2jserver.gameserver.model.items.L2Weapon;
  46. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  47. import com.l2jserver.gameserver.model.items.type.ArmorType;
  48. import com.l2jserver.gameserver.model.items.type.WeaponType;
  49. import com.l2jserver.gameserver.scripting.scriptengine.events.ItemCreateEvent;
  50. import com.l2jserver.gameserver.scripting.scriptengine.listeners.player.NewItemListener;
  51. import com.l2jserver.gameserver.util.GMAudit;
  52. /**
  53. * This class serves as a container for all item templates in the game.
  54. */
  55. public class ItemTable
  56. {
  57. private static Logger _log = Logger.getLogger(ItemTable.class.getName());
  58. private static Logger _logItems = Logger.getLogger("item");
  59. private static FastList<NewItemListener> newItemListeners = new FastList<NewItemListener>().shared();
  60. public static final Map<String, Integer> _slots = new FastMap<>();
  61. public static final Map<String, WeaponType> _weaponTypes = new FastMap<>();
  62. public static final Map<String, ArmorType> _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. // weapon types
  70. for (WeaponType type : WeaponType.values())
  71. {
  72. _weaponTypes.put(type.getName(), type);
  73. }
  74. // armor types
  75. for (ArmorType type : ArmorType.values())
  76. {
  77. _armorTypes.put(type.getName(), type);
  78. }
  79. _slots.put("shirt", L2Item.SLOT_UNDERWEAR);
  80. _slots.put("lbracelet", L2Item.SLOT_L_BRACELET);
  81. _slots.put("rbracelet", L2Item.SLOT_R_BRACELET);
  82. _slots.put("talisman", L2Item.SLOT_DECO);
  83. _slots.put("chest", L2Item.SLOT_CHEST);
  84. _slots.put("fullarmor", L2Item.SLOT_FULL_ARMOR);
  85. _slots.put("head", L2Item.SLOT_HEAD);
  86. _slots.put("hair", L2Item.SLOT_HAIR);
  87. _slots.put("hairall", L2Item.SLOT_HAIRALL);
  88. _slots.put("underwear", L2Item.SLOT_UNDERWEAR);
  89. _slots.put("back", L2Item.SLOT_BACK);
  90. _slots.put("neck", L2Item.SLOT_NECK);
  91. _slots.put("legs", L2Item.SLOT_LEGS);
  92. _slots.put("feet", L2Item.SLOT_FEET);
  93. _slots.put("gloves", L2Item.SLOT_GLOVES);
  94. _slots.put("chest,legs", L2Item.SLOT_CHEST | L2Item.SLOT_LEGS);
  95. _slots.put("belt", L2Item.SLOT_BELT);
  96. _slots.put("rhand", L2Item.SLOT_R_HAND);
  97. _slots.put("lhand", L2Item.SLOT_L_HAND);
  98. _slots.put("lrhand", L2Item.SLOT_LR_HAND);
  99. _slots.put("rear;lear", L2Item.SLOT_R_EAR | L2Item.SLOT_L_EAR);
  100. _slots.put("rfinger;lfinger", L2Item.SLOT_R_FINGER | L2Item.SLOT_L_FINGER);
  101. _slots.put("wolf", L2Item.SLOT_WOLF);
  102. _slots.put("greatwolf", L2Item.SLOT_GREATWOLF);
  103. _slots.put("hatchling", L2Item.SLOT_HATCHLING);
  104. _slots.put("strider", L2Item.SLOT_STRIDER);
  105. _slots.put("babypet", L2Item.SLOT_BABYPET);
  106. _slots.put("none", L2Item.SLOT_NONE);
  107. // retail compatibility
  108. _slots.put("onepiece", L2Item.SLOT_FULL_ARMOR);
  109. _slots.put("hair2", L2Item.SLOT_HAIR2);
  110. _slots.put("dhair", L2Item.SLOT_HAIRALL);
  111. _slots.put("alldress", L2Item.SLOT_ALLDRESS);
  112. _slots.put("deco1", L2Item.SLOT_DECO);
  113. _slots.put("waist", L2Item.SLOT_BELT);
  114. }
  115. /**
  116. * @return a reference to this ItemTable object
  117. */
  118. public static ItemTable getInstance()
  119. {
  120. return SingletonHolder._instance;
  121. }
  122. protected ItemTable()
  123. {
  124. _etcItems = new FastMap<>();
  125. _armors = new FastMap<>();
  126. _weapons = new FastMap<>();
  127. load();
  128. }
  129. private void load()
  130. {
  131. int highest = 0;
  132. _armors.clear();
  133. _etcItems.clear();
  134. _weapons.clear();
  135. for (L2Item item : DocumentEngine.getInstance().loadItems())
  136. {
  137. if (highest < item.getId())
  138. {
  139. highest = item.getId();
  140. }
  141. if (item instanceof L2EtcItem)
  142. {
  143. _etcItems.put(item.getId(), (L2EtcItem) item);
  144. }
  145. else if (item instanceof L2Armor)
  146. {
  147. _armors.put(item.getId(), (L2Armor) item);
  148. }
  149. else
  150. {
  151. _weapons.put(item.getId(), (L2Weapon) item);
  152. }
  153. }
  154. buildFastLookupTable(highest);
  155. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _etcItems.size() + " Etc Items");
  156. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _armors.size() + " Armor Items");
  157. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _weapons.size() + " Weapon Items");
  158. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + (_etcItems.size() + _armors.size() + _weapons.size()) + " Items in total.");
  159. }
  160. /**
  161. * Builds a variable in which all items are putting in in function of their ID.
  162. * @param size
  163. */
  164. private void buildFastLookupTable(int size)
  165. {
  166. // Create a FastLookUp Table called _allTemplates of size : value of the highest item ID
  167. _log.info(getClass().getSimpleName() + ": Highest item id used:" + size);
  168. _allTemplates = new L2Item[size + 1];
  169. // Insert armor item in Fast Look Up Table
  170. for (L2Armor item : _armors.values())
  171. {
  172. _allTemplates[item.getId()] = item;
  173. }
  174. // Insert weapon item in Fast Look Up Table
  175. for (L2Weapon item : _weapons.values())
  176. {
  177. _allTemplates[item.getId()] = item;
  178. }
  179. // Insert etcItem item in Fast Look Up Table
  180. for (L2EtcItem item : _etcItems.values())
  181. {
  182. _allTemplates[item.getId()] = item;
  183. }
  184. }
  185. /**
  186. * Returns the item corresponding to the item ID
  187. * @param id : int designating the item
  188. * @return L2Item
  189. */
  190. public L2Item getTemplate(int id)
  191. {
  192. if ((id >= _allTemplates.length) || (id < 0))
  193. {
  194. return null;
  195. }
  196. return _allTemplates[id];
  197. }
  198. /**
  199. * 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
  200. * Item creation according to log settings</li>
  201. * @param process : String Identifier of process triggering this action
  202. * @param itemId : int Item Identifier of the item to be created
  203. * @param count : int Quantity of items to be created for stackable items
  204. * @param actor : L2PcInstance Player requesting the item creation
  205. * @param reference : Object Object referencing current action like NPC selling item or previous item in transformation
  206. * @return L2ItemInstance corresponding to the new item
  207. */
  208. public L2ItemInstance createItem(String process, int itemId, long count, L2PcInstance actor, Object reference)
  209. {
  210. if (!fireNewItemListeners(process, itemId, count, actor, reference))
  211. {
  212. return null;
  213. }
  214. // Create and Init the L2ItemInstance corresponding to the Item Identifier
  215. L2ItemInstance item = new L2ItemInstance(IdFactory.getInstance().getNextId(), itemId);
  216. if (process.equalsIgnoreCase("loot"))
  217. {
  218. ScheduledFuture<?> itemLootShedule;
  219. if ((reference instanceof L2Attackable) && ((L2Attackable) reference).isRaid()) // loot privilege for raids
  220. {
  221. L2Attackable raid = (L2Attackable) reference;
  222. // if in CommandChannel and was killing a World/RaidBoss
  223. if ((raid.getFirstCommandChannelAttacked() != null) && !Config.AUTO_LOOT_RAIDS)
  224. {
  225. item.setOwnerId(raid.getFirstCommandChannelAttacked().getLeaderObjectId());
  226. itemLootShedule = ThreadPoolManager.getInstance().scheduleGeneral(new ResetOwner(item), Config.LOOT_RAIDS_PRIVILEGE_INTERVAL);
  227. item.setItemLootShedule(itemLootShedule);
  228. }
  229. }
  230. else if (!Config.AUTO_LOOT || ((reference instanceof L2EventMonsterInstance) && ((L2EventMonsterInstance) reference).eventDropOnGround()))
  231. {
  232. item.setOwnerId(actor.getObjectId());
  233. itemLootShedule = ThreadPoolManager.getInstance().scheduleGeneral(new ResetOwner(item), 15000);
  234. item.setItemLootShedule(itemLootShedule);
  235. }
  236. }
  237. if (Config.DEBUG)
  238. {
  239. _log.fine(getClass().getSimpleName() + ": Item created oid:" + item.getObjectId() + " itemid:" + itemId);
  240. }
  241. // Add the L2ItemInstance object to _allObjects of L2world
  242. L2World.getInstance().storeObject(item);
  243. // Set Item parameters
  244. if (item.isStackable() && (count > 1))
  245. {
  246. item.setCount(count);
  247. }
  248. if (Config.LOG_ITEMS && !process.equals("Reset"))
  249. {
  250. if (!Config.LOG_ITEMS_SMALL_LOG || (Config.LOG_ITEMS_SMALL_LOG && (item.isEquipable() || (item.getId() == ADENA_ID))))
  251. {
  252. LogRecord record = new LogRecord(Level.INFO, "CREATE:" + process);
  253. record.setLoggerName("item");
  254. record.setParameters(new Object[]
  255. {
  256. item,
  257. actor,
  258. reference
  259. });
  260. _logItems.log(record);
  261. }
  262. }
  263. if (actor != null)
  264. {
  265. if (actor.isGM())
  266. {
  267. String referenceName = "no-reference";
  268. if (reference instanceof L2Object)
  269. {
  270. referenceName = (((L2Object) reference).getName() != null ? ((L2Object) reference).getName() : "no-name");
  271. }
  272. else if (reference instanceof String)
  273. {
  274. referenceName = (String) reference;
  275. }
  276. String targetName = (actor.getTarget() != null ? actor.getTarget().getName() : "no-target");
  277. if (Config.GMAUDIT)
  278. {
  279. GMAudit.auditGMAction(actor.getName() + " [" + actor.getObjectId() + "]", process + "(id: " + itemId + " count: " + count + " name: " + item.getItemName() + " objId: " + item.getObjectId() + ")", targetName, "L2Object referencing this action is: " + referenceName);
  280. }
  281. }
  282. }
  283. return item;
  284. }
  285. public L2ItemInstance createItem(String process, int itemId, int count, L2PcInstance actor)
  286. {
  287. return createItem(process, itemId, count, actor, null);
  288. }
  289. /**
  290. * Returns a dummy item.<br>
  291. * <U><I>Concept :</I></U><BR>
  292. * Dummy item is created by setting the ID of the object in the world at null value
  293. * @param itemId : int designating the item
  294. * @return L2ItemInstance designating the dummy item created
  295. */
  296. public L2ItemInstance createDummyItem(int itemId)
  297. {
  298. L2Item item = getTemplate(itemId);
  299. if (item == null)
  300. {
  301. return null;
  302. }
  303. L2ItemInstance temp = new L2ItemInstance(0, item);
  304. return temp;
  305. }
  306. /**
  307. * Destroys the L2ItemInstance.<br>
  308. * <B><U> Actions</U> :</B>
  309. * <ul>
  310. * <li>Sets L2ItemInstance parameters to be unusable</li>
  311. * <li>Removes the L2ItemInstance object to _allObjects of L2world</li>
  312. * <li>Logs Item deletion according to log settings</li>
  313. * </ul>
  314. * @param process a string identifier of process triggering this action.
  315. * @param item the item instance to be destroyed.
  316. * @param actor the player requesting the item destroy.
  317. * @param reference the object referencing current action like NPC selling item or previous item in transformation.
  318. */
  319. public void destroyItem(String process, L2ItemInstance item, L2PcInstance actor, Object reference)
  320. {
  321. synchronized (item)
  322. {
  323. long old = item.getCount();
  324. item.setCount(0);
  325. item.setOwnerId(0);
  326. item.setItemLocation(ItemLocation.VOID);
  327. item.setLastChange(L2ItemInstance.REMOVED);
  328. L2World.getInstance().removeObject(item);
  329. IdFactory.getInstance().releaseId(item.getObjectId());
  330. if (Config.LOG_ITEMS)
  331. {
  332. if (!Config.LOG_ITEMS_SMALL_LOG || (Config.LOG_ITEMS_SMALL_LOG && (item.isEquipable() || (item.getId() == ADENA_ID))))
  333. {
  334. LogRecord record = new LogRecord(Level.INFO, "DELETE:" + process);
  335. record.setLoggerName("item");
  336. record.setParameters(new Object[]
  337. {
  338. item,
  339. "PrevCount(" + old + ")",
  340. actor,
  341. reference
  342. });
  343. _logItems.log(record);
  344. }
  345. }
  346. if (actor != null)
  347. {
  348. if (actor.isGM())
  349. {
  350. String referenceName = "no-reference";
  351. if (reference instanceof L2Object)
  352. {
  353. referenceName = (((L2Object) reference).getName() != null ? ((L2Object) reference).getName() : "no-name");
  354. }
  355. else if (reference instanceof String)
  356. {
  357. referenceName = (String) reference;
  358. }
  359. String targetName = (actor.getTarget() != null ? actor.getTarget().getName() : "no-target");
  360. if (Config.GMAUDIT)
  361. {
  362. GMAudit.auditGMAction(actor.getName() + " [" + actor.getObjectId() + "]", process + "(id: " + item.getId() + " count: " + item.getCount() + " itemObjId: " + item.getObjectId() + ")", targetName, "L2Object referencing this action is: " + referenceName);
  363. }
  364. }
  365. }
  366. // if it's a pet control item, delete the pet as well
  367. if (item.getItem().isPetItem())
  368. {
  369. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  370. PreparedStatement statement = con.prepareStatement("DELETE FROM pets WHERE item_obj_id=?"))
  371. {
  372. // Delete the pet in db
  373. statement.setInt(1, item.getObjectId());
  374. statement.execute();
  375. }
  376. catch (Exception e)
  377. {
  378. _log.log(Level.WARNING, "could not delete pet objectid:", e);
  379. }
  380. }
  381. }
  382. }
  383. public void reload()
  384. {
  385. load();
  386. EnchantItemHPBonusData.getInstance().load();
  387. }
  388. protected static class ResetOwner implements Runnable
  389. {
  390. L2ItemInstance _item;
  391. public ResetOwner(L2ItemInstance item)
  392. {
  393. _item = item;
  394. }
  395. @Override
  396. public void run()
  397. {
  398. _item.setOwnerId(0);
  399. _item.setItemLootShedule(null);
  400. }
  401. }
  402. public Set<Integer> getAllArmorsId()
  403. {
  404. return _armors.keySet();
  405. }
  406. public Set<Integer> getAllWeaponsId()
  407. {
  408. return _weapons.keySet();
  409. }
  410. public int getArraySize()
  411. {
  412. return _allTemplates.length;
  413. }
  414. private static class SingletonHolder
  415. {
  416. protected static final ItemTable _instance = new ItemTable();
  417. }
  418. // Listeners
  419. /**
  420. * Fires all the new item listeners, if any
  421. * @param process
  422. * @param itemId
  423. * @param count
  424. * @param actor
  425. * @param reference
  426. * @return
  427. */
  428. private boolean fireNewItemListeners(String process, int itemId, long count, L2PcInstance actor, Object reference)
  429. {
  430. if (!newItemListeners.isEmpty() && (actor != null))
  431. {
  432. ItemCreateEvent event = new ItemCreateEvent();
  433. event.setItemId(itemId);
  434. event.setPlayer(actor);
  435. event.setCount(count);
  436. event.setProcess(process);
  437. event.setReference(reference);
  438. for (NewItemListener listener : newItemListeners)
  439. {
  440. if (listener.containsItemId(itemId))
  441. {
  442. if (!listener.onCreate(event))
  443. {
  444. return false;
  445. }
  446. }
  447. }
  448. }
  449. return true;
  450. }
  451. /**
  452. * Adds a new item listener
  453. * @param listener
  454. */
  455. public static void addNewItemListener(NewItemListener listener)
  456. {
  457. if (!newItemListeners.contains(listener))
  458. {
  459. newItemListeners.add(listener);
  460. }
  461. }
  462. /**
  463. * Removes a new item listener
  464. * @param listener
  465. */
  466. public static void removeNewItemListener(NewItemListener listener)
  467. {
  468. newItemListeners.remove(listener);
  469. }
  470. }