ItemTable.java 14 KB

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