FishData.java 4.1 KB

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