UIData.java 4.6 KB

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