2
0

FishData.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.datatables;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.w3c.dom.NamedNodeMap;
  21. import org.w3c.dom.Node;
  22. import com.l2jserver.gameserver.engines.DocumentParser;
  23. import com.l2jserver.gameserver.model.StatsSet;
  24. import com.l2jserver.gameserver.model.fishing.L2Fish;
  25. /**
  26. * This class holds the Fish information.
  27. * @author nonom
  28. */
  29. public final class FishData extends DocumentParser
  30. {
  31. private static final Map<Integer, L2Fish> _fishNormal = new HashMap<>();
  32. private static final Map<Integer, L2Fish> _fishEasy = new HashMap<>();
  33. private static final Map<Integer, L2Fish> _fishHard = new HashMap<>();
  34. /**
  35. * Instantiates a new fish data.
  36. */
  37. protected FishData()
  38. {
  39. load();
  40. }
  41. @Override
  42. public void load()
  43. {
  44. _fishEasy.clear();
  45. _fishNormal.clear();
  46. _fishHard.clear();
  47. parseDatapackFile("data/stats/fishing/fishes.xml");
  48. _log.info(getClass().getSimpleName() + ": Loaded " + (_fishEasy.size() + _fishNormal.size() + _fishHard.size()) + " Fishes.");
  49. }
  50. @Override
  51. protected void parseDocument()
  52. {
  53. NamedNodeMap attrs;
  54. Node att;
  55. L2Fish fish;
  56. StatsSet set;
  57. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  58. {
  59. if ("list".equalsIgnoreCase(n.getNodeName()))
  60. {
  61. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  62. {
  63. if ("fish".equalsIgnoreCase(d.getNodeName()))
  64. {
  65. attrs = d.getAttributes();
  66. set = new StatsSet();
  67. for (int i = 0; i < attrs.getLength(); i++)
  68. {
  69. att = attrs.item(i);
  70. set.set(att.getNodeName(), att.getNodeValue());
  71. }
  72. fish = new L2Fish(set);
  73. switch (fish.getFishGrade())
  74. {
  75. case 0:
  76. {
  77. _fishEasy.put(fish.getFishId(), fish);
  78. break;
  79. }
  80. case 1:
  81. {
  82. _fishNormal.put(fish.getFishId(), fish);
  83. break;
  84. }
  85. case 2:
  86. {
  87. _fishHard.put(fish.getFishId(), fish);
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * Gets the fish.
  98. * @param level the fish Level
  99. * @param group the fish Group
  100. * @param grade the fish Grade
  101. * @return List of Fish that can be fished
  102. */
  103. public List<L2Fish> getFish(int level, int group, int grade)
  104. {
  105. final ArrayList<L2Fish> result = new ArrayList<>();
  106. Map<Integer, L2Fish> fish = null;
  107. switch (grade)
  108. {
  109. case 0:
  110. {
  111. fish = _fishEasy;
  112. break;
  113. }
  114. case 1:
  115. {
  116. fish = _fishNormal;
  117. break;
  118. }
  119. case 2:
  120. {
  121. fish = _fishHard;
  122. break;
  123. }
  124. default:
  125. {
  126. _log.warning(getClass().getSimpleName() + ": Unmanaged fish grade!");
  127. return result;
  128. }
  129. }
  130. for (L2Fish f : fish.values())
  131. {
  132. if ((f.getFishLevel() != level) || (f.getFishGroup() != group))
  133. {
  134. continue;
  135. }
  136. result.add(f);
  137. }
  138. if (result.isEmpty())
  139. {
  140. _log.warning(getClass().getSimpleName() + ": Cannot find any fish for level: " + level + " group: " + group + " and grade: " + grade + "!");
  141. }
  142. return result;
  143. }
  144. /**
  145. * Gets the single instance of FishData.
  146. * @return single instance of FishData
  147. */
  148. public static FishData getInstance()
  149. {
  150. return SingletonHolder._instance;
  151. }
  152. private static class SingletonHolder
  153. {
  154. protected static final FishData _instance = new FishData();
  155. }
  156. }