2
0

FishData.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.gameserver.engines.DocumentParser;
  27. import com.l2jserver.gameserver.model.StatsSet;
  28. import com.l2jserver.gameserver.model.fishing.L2Fish;
  29. /**
  30. * This class holds the Fish information.
  31. * @author nonom
  32. */
  33. public final class FishData extends DocumentParser
  34. {
  35. private static final Map<Integer, L2Fish> _fishNormal = new HashMap<>();
  36. private static final Map<Integer, L2Fish> _fishEasy = new HashMap<>();
  37. private static final Map<Integer, L2Fish> _fishHard = new HashMap<>();
  38. /**
  39. * Instantiates a new fish data.
  40. */
  41. protected FishData()
  42. {
  43. load();
  44. }
  45. @Override
  46. public void load()
  47. {
  48. _fishEasy.clear();
  49. _fishNormal.clear();
  50. _fishHard.clear();
  51. parseDatapackFile("data/stats/fishing/fishes.xml");
  52. _log.info(getClass().getSimpleName() + ": Loaded " + (_fishEasy.size() + _fishNormal.size() + _fishHard.size()) + " Fishes.");
  53. }
  54. @Override
  55. protected void parseDocument()
  56. {
  57. NamedNodeMap attrs;
  58. Node att;
  59. L2Fish fish;
  60. StatsSet set;
  61. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  62. {
  63. if ("list".equalsIgnoreCase(n.getNodeName()))
  64. {
  65. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  66. {
  67. if ("fish".equalsIgnoreCase(d.getNodeName()))
  68. {
  69. attrs = d.getAttributes();
  70. set = new StatsSet();
  71. for (int i = 0; i < attrs.getLength(); i++)
  72. {
  73. att = attrs.item(i);
  74. set.set(att.getNodeName(), att.getNodeValue());
  75. }
  76. fish = new L2Fish(set);
  77. switch (fish.getFishGrade())
  78. {
  79. case 0:
  80. {
  81. _fishEasy.put(fish.getFishId(), fish);
  82. break;
  83. }
  84. case 1:
  85. {
  86. _fishNormal.put(fish.getFishId(), fish);
  87. break;
  88. }
  89. case 2:
  90. {
  91. _fishHard.put(fish.getFishId(), fish);
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * Gets the fish.
  102. * @param level the fish Level
  103. * @param group the fish Group
  104. * @param grade the fish Grade
  105. * @return List of Fish that can be fished
  106. */
  107. public List<L2Fish> getFish(int level, int group, int grade)
  108. {
  109. final ArrayList<L2Fish> result = new ArrayList<>();
  110. Map<Integer, L2Fish> fish = null;
  111. switch (grade)
  112. {
  113. case 0:
  114. {
  115. fish = _fishEasy;
  116. break;
  117. }
  118. case 1:
  119. {
  120. fish = _fishNormal;
  121. break;
  122. }
  123. case 2:
  124. {
  125. fish = _fishHard;
  126. break;
  127. }
  128. default:
  129. {
  130. _log.warning(getClass().getSimpleName() + ": Unmanaged fish grade!");
  131. return result;
  132. }
  133. }
  134. for (L2Fish f : fish.values())
  135. {
  136. if ((f.getFishLevel() != level) || (f.getFishGroup() != group))
  137. {
  138. continue;
  139. }
  140. result.add(f);
  141. }
  142. if (result.isEmpty())
  143. {
  144. _log.warning(getClass().getSimpleName() + ": Cannot find any fish for level: " + level + " group: " + group + " and grade: " + grade + "!");
  145. }
  146. return result;
  147. }
  148. /**
  149. * Gets the single instance of FishData.
  150. * @return single instance of FishData
  151. */
  152. public static FishData getInstance()
  153. {
  154. return SingletonHolder._instance;
  155. }
  156. private static class SingletonHolder
  157. {
  158. protected static final FishData _instance = new FishData();
  159. }
  160. }