FishData.java 4.0 KB

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