InitialShortcutData.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.gameserver.engines.DocumentParser;
  27. import com.l2jserver.gameserver.enums.MacroType;
  28. import com.l2jserver.gameserver.enums.ShortcutType;
  29. import com.l2jserver.gameserver.model.Macro;
  30. import com.l2jserver.gameserver.model.MacroCmd;
  31. import com.l2jserver.gameserver.model.Shortcut;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.base.ClassId;
  34. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  35. import com.l2jserver.gameserver.network.serverpackets.ShortCutRegister;
  36. /**
  37. * This class holds the Initial Shortcuts information.<br>
  38. * What shortcuts get each newly created character.
  39. * @author Zoey76
  40. */
  41. public final class InitialShortcutData extends DocumentParser
  42. {
  43. private final Map<ClassId, List<Shortcut>> _initialShortcutData = new HashMap<>();
  44. private final List<Shortcut> _initialGlobalShortcutList = new ArrayList<>();
  45. private final Map<Integer, Macro> _macroPresets = new HashMap<>();
  46. /**
  47. * Instantiates a new initial shortcuts data.
  48. */
  49. protected InitialShortcutData()
  50. {
  51. load();
  52. }
  53. @Override
  54. public void load()
  55. {
  56. _initialShortcutData.clear();
  57. _initialGlobalShortcutList.clear();
  58. parseDatapackFile("data/stats/initialShortcuts.xml");
  59. _log.info(getClass().getSimpleName() + ": Loaded " + _initialGlobalShortcutList.size() + " Initial Global Shortcuts data.");
  60. _log.info(getClass().getSimpleName() + ": Loaded " + _initialShortcutData.size() + " Initial Shortcuts data.");
  61. _log.info(getClass().getSimpleName() + ": Loaded " + _macroPresets.size() + " Macros presets.");
  62. }
  63. @Override
  64. protected void parseDocument()
  65. {
  66. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  67. {
  68. if ("list".equals(n.getNodeName()))
  69. {
  70. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  71. {
  72. switch (d.getNodeName())
  73. {
  74. case "shortcuts":
  75. {
  76. parseShortcuts(d);
  77. break;
  78. }
  79. case "macros":
  80. {
  81. parseMacros(d);
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. /**
  90. * Parses a shortcut.
  91. * @param d the node
  92. */
  93. private void parseShortcuts(Node d)
  94. {
  95. NamedNodeMap attrs = d.getAttributes();
  96. final Node classIdNode = attrs.getNamedItem("classId");
  97. final List<Shortcut> list = new ArrayList<>();
  98. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  99. {
  100. if ("page".equals(c.getNodeName()))
  101. {
  102. attrs = c.getAttributes();
  103. final int pageId = parseInteger(attrs, "pageId");
  104. for (Node b = c.getFirstChild(); b != null; b = b.getNextSibling())
  105. {
  106. if ("slot".equals(b.getNodeName()))
  107. {
  108. list.add(createShortcut(pageId, b));
  109. }
  110. }
  111. }
  112. }
  113. if (classIdNode != null)
  114. {
  115. _initialShortcutData.put(ClassId.getClassId(Integer.parseInt(classIdNode.getNodeValue())), list);
  116. }
  117. else
  118. {
  119. _initialGlobalShortcutList.addAll(list);
  120. }
  121. }
  122. /**
  123. * Parses a macro.
  124. * @param d the node
  125. */
  126. private void parseMacros(Node d)
  127. {
  128. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  129. {
  130. if ("macro".equals(c.getNodeName()))
  131. {
  132. NamedNodeMap attrs = c.getAttributes();
  133. if (!parseBoolean(attrs, "enabled", true))
  134. {
  135. continue;
  136. }
  137. final int macroId = parseInteger(attrs, "macroId");
  138. final int icon = parseInteger(attrs, "icon");
  139. final String name = parseString(attrs, "name");
  140. final String description = parseString(attrs, "description");
  141. final String acronym = parseString(attrs, "acronym");
  142. final List<MacroCmd> commands = new ArrayList<>(1);
  143. int entry = 0;
  144. for (Node b = c.getFirstChild(); b != null; b = b.getNextSibling())
  145. {
  146. if ("command".equals(b.getNodeName()))
  147. {
  148. attrs = b.getAttributes();
  149. final MacroType type = parseEnum(attrs, MacroType.class, "type");
  150. int d1 = 0;
  151. int d2 = 0;
  152. final String cmd = b.getTextContent();
  153. switch (type)
  154. {
  155. case SKILL:
  156. {
  157. d1 = parseInteger(attrs, "skillId"); // Skill ID
  158. d2 = parseInteger(attrs, "skillLvl", 0); // Skill level
  159. break;
  160. }
  161. case ACTION:
  162. {
  163. // Not handled by client.
  164. d1 = parseInteger(attrs, "actionId");
  165. break;
  166. }
  167. case TEXT:
  168. {
  169. // Doesn't have numeric parameters.
  170. break;
  171. }
  172. case SHORTCUT:
  173. {
  174. d1 = parseInteger(attrs, "page"); // Page
  175. d2 = parseInteger(attrs, "slot", 0); // Slot
  176. break;
  177. }
  178. case ITEM:
  179. {
  180. // Not handled by client.
  181. d1 = parseInteger(attrs, "itemId");
  182. break;
  183. }
  184. case DELAY:
  185. {
  186. d1 = parseInteger(attrs, "delay"); // Delay in seconds
  187. break;
  188. }
  189. }
  190. commands.add(new MacroCmd(entry++, type, d1, d2, cmd));
  191. }
  192. }
  193. _macroPresets.put(macroId, new Macro(macroId, icon, name, description, acronym, commands));
  194. }
  195. }
  196. }
  197. /**
  198. * Parses a node an create a shortcut from it.
  199. * @param pageId the page ID
  200. * @param b the node to parse
  201. * @return the new shortcut
  202. */
  203. private Shortcut createShortcut(int pageId, Node b)
  204. {
  205. final NamedNodeMap attrs = b.getAttributes();
  206. final int slotId = parseInteger(attrs, "slotId");
  207. final ShortcutType shortcutType = parseEnum(attrs, ShortcutType.class, "shortcutType");
  208. final int shortcutId = parseInteger(attrs, "shortcutId");
  209. final int shortcutLevel = parseInteger(attrs, "shortcutLevel", 0);
  210. final int characterType = parseInteger(attrs, "characterType", 0);
  211. return new Shortcut(slotId, pageId, shortcutType, shortcutId, shortcutLevel, characterType);
  212. }
  213. /**
  214. * Gets the shortcut list.
  215. * @param cId the class ID for the shortcut list
  216. * @return the shortcut list for the give class ID
  217. */
  218. public List<Shortcut> getShortcutList(ClassId cId)
  219. {
  220. return _initialShortcutData.get(cId);
  221. }
  222. /**
  223. * Gets the shortcut list.
  224. * @param cId the class ID for the shortcut list
  225. * @return the shortcut list for the give class ID
  226. */
  227. public List<Shortcut> getShortcutList(int cId)
  228. {
  229. return _initialShortcutData.get(ClassId.getClassId(cId));
  230. }
  231. /**
  232. * Gets the global shortcut list.
  233. * @return the global shortcut list
  234. */
  235. public List<Shortcut> getGlobalMacroList()
  236. {
  237. return _initialGlobalShortcutList;
  238. }
  239. /**
  240. * Register all the available shortcuts for the given player.
  241. * @param player the player
  242. */
  243. public void registerAllShortcuts(L2PcInstance player)
  244. {
  245. if (player == null)
  246. {
  247. return;
  248. }
  249. // Register global shortcuts.
  250. for (Shortcut shortcut : _initialGlobalShortcutList)
  251. {
  252. int shortcutId = shortcut.getId();
  253. switch (shortcut.getType())
  254. {
  255. case ITEM:
  256. {
  257. final L2ItemInstance item = player.getInventory().getItemByItemId(shortcutId);
  258. if (item == null)
  259. {
  260. continue;
  261. }
  262. shortcutId = item.getObjectId();
  263. break;
  264. }
  265. case SKILL:
  266. {
  267. if (!player.getSkills().containsKey(shortcutId))
  268. {
  269. continue;
  270. }
  271. break;
  272. }
  273. case MACRO:
  274. {
  275. final Macro macro = _macroPresets.get(shortcutId);
  276. if (macro == null)
  277. {
  278. continue;
  279. }
  280. player.registerMacro(macro);
  281. break;
  282. }
  283. }
  284. // Register shortcut
  285. final Shortcut newShortcut = new Shortcut(shortcut.getSlot(), shortcut.getPage(), shortcut.getType(), shortcutId, shortcut.getLevel(), shortcut.getCharacterType());
  286. player.sendPacket(new ShortCutRegister(newShortcut));
  287. player.registerShortCut(newShortcut);
  288. }
  289. // Register class specific shortcuts.
  290. if (_initialShortcutData.containsKey(player.getClassId()))
  291. {
  292. for (Shortcut shortcut : _initialShortcutData.get(player.getClassId()))
  293. {
  294. int shortcutId = shortcut.getId();
  295. switch (shortcut.getType())
  296. {
  297. case ITEM:
  298. {
  299. final L2ItemInstance item = player.getInventory().getItemByItemId(shortcutId);
  300. if (item == null)
  301. {
  302. continue;
  303. }
  304. shortcutId = item.getObjectId();
  305. break;
  306. }
  307. case SKILL:
  308. {
  309. if (!player.getSkills().containsKey(shortcut.getId()))
  310. {
  311. continue;
  312. }
  313. break;
  314. }
  315. case MACRO:
  316. {
  317. final Macro macro = _macroPresets.get(shortcutId);
  318. if (macro == null)
  319. {
  320. continue;
  321. }
  322. player.registerMacro(macro);
  323. break;
  324. }
  325. }
  326. // Register shortcut
  327. final Shortcut newShortcut = new Shortcut(shortcut.getSlot(), shortcut.getPage(), shortcut.getType(), shortcutId, shortcut.getLevel(), shortcut.getCharacterType());
  328. player.sendPacket(new ShortCutRegister(newShortcut));
  329. player.registerShortCut(newShortcut);
  330. }
  331. }
  332. }
  333. /**
  334. * Gets the single instance of InitialEquipmentData.
  335. * @return single instance of InitialEquipmentData
  336. */
  337. public static InitialShortcutData getInstance()
  338. {
  339. return SingletonHolder._instance;
  340. }
  341. private static class SingletonHolder
  342. {
  343. protected static final InitialShortcutData _instance = new InitialShortcutData();
  344. }
  345. }