2
0

ItemTable.java 16 KB

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