UIData.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2004-2013 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 java.util.logging.Logger;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.gameserver.engines.DocumentParser;
  27. import com.l2jserver.gameserver.model.ActionKey;
  28. /**
  29. * UI Data parser.
  30. * @author Zoey76
  31. */
  32. public class UIData extends DocumentParser
  33. {
  34. private static final Logger _log = Logger.getLogger(UIData.class.getName());
  35. private final Map<Integer, List<ActionKey>> _storedKeys = new HashMap<>();
  36. private final Map<Integer, List<Integer>> _storedCategories = new HashMap<>();
  37. protected UIData()
  38. {
  39. load();
  40. }
  41. @Override
  42. public void load()
  43. {
  44. _storedKeys.clear();
  45. _storedCategories.clear();
  46. parseDatapackFile("data/ui/ui_en.xml");
  47. _log.info(getClass().getSimpleName() + ": Loaded " + _storedKeys.size() + " keys " + _storedCategories.size() + " categories.");
  48. }
  49. @Override
  50. protected void parseDocument()
  51. {
  52. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  53. {
  54. if ("list".equalsIgnoreCase(n.getNodeName()))
  55. {
  56. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  57. {
  58. if ("category".equalsIgnoreCase(d.getNodeName()))
  59. {
  60. parseCategory(d);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. private void parseCategory(Node n)
  67. {
  68. final int cat = parseInteger(n.getAttributes(), "id");
  69. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  70. {
  71. if ("commands".equalsIgnoreCase(d.getNodeName()))
  72. {
  73. parseCommands(cat, d);
  74. }
  75. else if ("keys".equalsIgnoreCase(d.getNodeName()))
  76. {
  77. parseKeys(cat, d);
  78. }
  79. }
  80. }
  81. private void parseCommands(int cat, Node d)
  82. {
  83. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  84. {
  85. if ("cmd".equalsIgnoreCase(c.getNodeName()))
  86. {
  87. addCategory(_storedCategories, cat, Integer.parseInt(c.getTextContent()));
  88. }
  89. }
  90. }
  91. private void parseKeys(int cat, Node d)
  92. {
  93. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  94. {
  95. if ("key".equalsIgnoreCase(c.getNodeName()))
  96. {
  97. final ActionKey akey = new ActionKey(cat);
  98. for (int i = 0; i < c.getAttributes().getLength(); i++)
  99. {
  100. final Node att = c.getAttributes().item(i);
  101. final int val = Integer.parseInt(att.getNodeValue());
  102. switch (att.getNodeName())
  103. {
  104. case "cmd":
  105. {
  106. akey.setCommandId(val);
  107. break;
  108. }
  109. case "key":
  110. {
  111. akey.setKeyId(val);
  112. break;
  113. }
  114. case "toggleKey1":
  115. {
  116. akey.setToogleKey1(val);
  117. break;
  118. }
  119. case "toggleKey2":
  120. {
  121. akey.setToogleKey2(val);
  122. break;
  123. }
  124. case "showType":
  125. {
  126. akey.setShowStatus(val);
  127. break;
  128. }
  129. }
  130. }
  131. addKey(_storedKeys, cat, akey);
  132. }
  133. }
  134. }
  135. /**
  136. * Add a category to the stored categories.
  137. * @param map the map to store the category
  138. * @param cat the category
  139. * @param cmd the command
  140. */
  141. public static void addCategory(Map<Integer, List<Integer>> map, int cat, int cmd)
  142. {
  143. if (!map.containsKey(cat))
  144. {
  145. map.put(cat, new ArrayList<Integer>());
  146. }
  147. map.get(cat).add(cmd);
  148. }
  149. /**
  150. * Create and insert an Action Key into the stored keys.
  151. * @param map the map to store the key
  152. * @param cat the category
  153. * @param akey the action key
  154. */
  155. public static void addKey(Map<Integer, List<ActionKey>> map, int cat, ActionKey akey)
  156. {
  157. if (!map.containsKey(cat))
  158. {
  159. map.put(cat, new ArrayList<ActionKey>());
  160. }
  161. map.get(cat).add(akey);
  162. }
  163. /**
  164. * @return the categories
  165. */
  166. public Map<Integer, List<Integer>> getCategories()
  167. {
  168. return _storedCategories;
  169. }
  170. /**
  171. * @return the keys
  172. */
  173. public Map<Integer, List<ActionKey>> getKeys()
  174. {
  175. return _storedKeys;
  176. }
  177. public static UIData getInstance()
  178. {
  179. return SingletonHolder._instance;
  180. }
  181. private static class SingletonHolder
  182. {
  183. protected static final UIData _instance = new UIData();
  184. }
  185. }