InitialShortcutData.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. final int macroId = parseInteger(attrs, "macroId");
  134. final int icon = parseInteger(attrs, "icon");
  135. final String name = parseString(attrs, "name");
  136. final String description = parseString(attrs, "description");
  137. final String acronym = parseString(attrs, "acronym");
  138. final List<MacroCmd> commands = new ArrayList<>(1);
  139. int entry = 0;
  140. for (Node b = c.getFirstChild(); b != null; b = b.getNextSibling())
  141. {
  142. if ("command".equals(b.getNodeName()))
  143. {
  144. attrs = b.getAttributes();
  145. final MacroType type = parseEnum(attrs, MacroType.class, "type");
  146. int d1 = 0;
  147. int d2 = 0;
  148. final String cmd = b.getTextContent();
  149. switch (type)
  150. {
  151. case SKILL:
  152. {
  153. d1 = parseInteger(attrs, "skillId"); // Skill ID
  154. d2 = parseInteger(attrs, "skillLvl", 0); // Skill level
  155. break;
  156. }
  157. case ACTION:
  158. {
  159. // Not handled by client.
  160. d1 = parseInteger(attrs, "actionId");
  161. break;
  162. }
  163. case TEXT:
  164. {
  165. // Doesn't have numeric parameters.
  166. break;
  167. }
  168. case SHORTCUT:
  169. {
  170. d1 = parseInteger(attrs, "page"); // Page
  171. d2 = parseInteger(attrs, "slot", 0); // Slot
  172. break;
  173. }
  174. case ITEM:
  175. {
  176. // Not handled by client.
  177. d1 = parseInteger(attrs, "itemId");
  178. break;
  179. }
  180. case DELAY:
  181. {
  182. d1 = parseInteger(attrs, "delay"); // Delay in seconds
  183. break;
  184. }
  185. }
  186. commands.add(new MacroCmd(entry++, type, d1, d2, cmd));
  187. }
  188. }
  189. _macroPresets.put(macroId, new Macro(macroId, icon, name, description, acronym, commands));
  190. }
  191. }
  192. }
  193. /**
  194. * Parses a node an create a shortcut from it.
  195. * @param pageId the page ID
  196. * @param b the node to parse
  197. * @return the new shortcut
  198. */
  199. private Shortcut createShortcut(int pageId, Node b)
  200. {
  201. final NamedNodeMap attrs = b.getAttributes();
  202. final int slotId = parseInteger(attrs, "slotId");
  203. final ShortcutType shortcutType = parseEnum(attrs, ShortcutType.class, "shortcutType");
  204. final int shortcutId = parseInteger(attrs, "shortcutId");
  205. final int shortcutLevel = parseInteger(attrs, "shortcutLevel", 0);
  206. final int characterType = parseInteger(attrs, "characterType", 0);
  207. return new Shortcut(slotId, pageId, shortcutType, shortcutId, shortcutLevel, characterType);
  208. }
  209. /**
  210. * Gets the shortcut list.
  211. * @param cId the class ID for the shortcut list
  212. * @return the shortcut list for the give class ID
  213. */
  214. public List<Shortcut> getShortcutList(ClassId cId)
  215. {
  216. return _initialShortcutData.get(cId);
  217. }
  218. /**
  219. * Gets the shortcut list.
  220. * @param cId the class ID for the shortcut list
  221. * @return the shortcut list for the give class ID
  222. */
  223. public List<Shortcut> getShortcutList(int cId)
  224. {
  225. return _initialShortcutData.get(ClassId.getClassId(cId));
  226. }
  227. /**
  228. * Gets the global shortcut list.
  229. * @return the global shortcut list
  230. */
  231. public List<Shortcut> getGlobalMacroList()
  232. {
  233. return _initialGlobalShortcutList;
  234. }
  235. /**
  236. * Register all the available shortcuts for the given player.
  237. * @param player the player
  238. */
  239. public void registerAllShortcuts(L2PcInstance player)
  240. {
  241. if (player == null)
  242. {
  243. return;
  244. }
  245. // Register global shortcuts.
  246. for (Shortcut shortcut : _initialGlobalShortcutList)
  247. {
  248. int shortcutId = shortcut.getId();
  249. switch (shortcut.getType())
  250. {
  251. case ITEM:
  252. {
  253. final L2ItemInstance item = player.getInventory().getItemByItemId(shortcutId);
  254. if (item == null)
  255. {
  256. continue;
  257. }
  258. shortcutId = item.getObjectId();
  259. break;
  260. }
  261. case SKILL:
  262. {
  263. if (!player.getSkills().containsKey(shortcutId))
  264. {
  265. continue;
  266. }
  267. break;
  268. }
  269. case MACRO:
  270. {
  271. final Macro macro = _macroPresets.get(shortcutId);
  272. if (macro == null)
  273. {
  274. continue;
  275. }
  276. player.registerMacro(macro);
  277. break;
  278. }
  279. }
  280. // Register shortcut
  281. final Shortcut newShortcut = new Shortcut(shortcut.getSlot(), shortcut.getPage(), shortcut.getType(), shortcutId, shortcut.getLevel(), shortcut.getCharacterType());
  282. player.sendPacket(new ShortCutRegister(newShortcut));
  283. player.registerShortCut(newShortcut);
  284. }
  285. // Register class specific shortcuts.
  286. if (_initialShortcutData.containsKey(player.getClassId()))
  287. {
  288. for (Shortcut shortcut : _initialShortcutData.get(player.getClassId()))
  289. {
  290. int shortcutId = shortcut.getId();
  291. switch (shortcut.getType())
  292. {
  293. case ITEM:
  294. {
  295. final L2ItemInstance item = player.getInventory().getItemByItemId(shortcutId);
  296. if (item == null)
  297. {
  298. continue;
  299. }
  300. shortcutId = item.getObjectId();
  301. break;
  302. }
  303. case SKILL:
  304. {
  305. if (!player.getSkills().containsKey(shortcut.getId()))
  306. {
  307. continue;
  308. }
  309. break;
  310. }
  311. case MACRO:
  312. {
  313. final Macro macro = _macroPresets.get(shortcutId);
  314. if (macro == null)
  315. {
  316. continue;
  317. }
  318. player.registerMacro(macro);
  319. break;
  320. }
  321. }
  322. // Register shortcut
  323. final Shortcut newShortcut = new Shortcut(shortcut.getSlot(), shortcut.getPage(), shortcut.getType(), shortcutId, shortcut.getLevel(), shortcut.getCharacterType());
  324. player.sendPacket(new ShortCutRegister(newShortcut));
  325. player.registerShortCut(newShortcut);
  326. }
  327. }
  328. }
  329. /**
  330. * Gets the single instance of InitialEquipmentData.
  331. * @return single instance of InitialEquipmentData
  332. */
  333. public static InitialShortcutData getInstance()
  334. {
  335. return SingletonHolder._instance;
  336. }
  337. private static class SingletonHolder
  338. {
  339. protected static final InitialShortcutData _instance = new InitialShortcutData();
  340. }
  341. }