ItemTable.java 15 KB

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