UIData.java 4.7 KB

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