FishData.java 4.0 KB

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