ItemTable.java 14 KB

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