FishTable.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastList;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.model.FishData;
  25. /**
  26. * @author -Nemesiss-
  27. *
  28. */
  29. public class FishTable
  30. {
  31. private static Logger _log = Logger.getLogger(FishTable.class.getName());
  32. private static List<FishData> _fishsNormal;
  33. private static List<FishData> _fishsEasy;
  34. private static List<FishData> _fishsHard;
  35. public static FishTable getInstance()
  36. {
  37. return SingletonHolder._instance;
  38. }
  39. private FishTable()
  40. {
  41. //Create table that contains all fish datas
  42. int count = 0;
  43. Connection con = null;
  44. try
  45. {
  46. con = L2DatabaseFactory.getInstance().getConnection();
  47. _fishsEasy = new FastList<FishData>();
  48. _fishsNormal = new FastList<FishData>();
  49. _fishsHard = new FastList<FishData>();
  50. FishData fish;
  51. PreparedStatement statement = con.prepareStatement("SELECT id, level, name, hp, hpregen, fish_type, fish_group, fish_guts, guts_check_time, wait_time, combat_time FROM fish ORDER BY id");
  52. ResultSet fishes = statement.executeQuery();
  53. while (fishes.next())
  54. {
  55. int id = fishes.getInt("id");
  56. int lvl = fishes.getInt("level");
  57. String name = fishes.getString("name");
  58. int hp = fishes.getInt("hp");
  59. int hpreg = fishes.getInt("hpregen");
  60. int type = fishes.getInt("fish_type");
  61. int group = fishes.getInt("fish_group");
  62. int fish_guts = fishes.getInt("fish_guts");
  63. int guts_check_time = fishes.getInt("guts_check_time");
  64. int wait_time = fishes.getInt("wait_time");
  65. int combat_time = fishes.getInt("combat_time");
  66. fish = new FishData(id, lvl, name, hp, hpreg, type, group, fish_guts, guts_check_time, wait_time, combat_time);
  67. switch (fish.getGroup())
  68. {
  69. case 0:
  70. _fishsEasy.add(fish);
  71. break;
  72. case 1:
  73. _fishsNormal.add(fish);
  74. break;
  75. case 2:
  76. _fishsHard.add(fish);
  77. }
  78. }
  79. fishes.close();
  80. statement.close();
  81. count = _fishsEasy.size() + _fishsNormal.size() + _fishsHard.size();
  82. }
  83. catch (Exception e)
  84. {
  85. _log.log(Level.SEVERE, "Error while creating fish table" + e.getMessage(), e);
  86. }
  87. finally
  88. {
  89. L2DatabaseFactory.close(con);
  90. }
  91. _log.info("FishTable: Loaded " + count + " Fishes.");
  92. }
  93. /**
  94. * @param lvl
  95. * @param type
  96. * @param group
  97. * @return List of Fish that can be fished
  98. */
  99. public List<FishData> getfish(int lvl, int type, int group)
  100. {
  101. List<FishData> result = new FastList<FishData>();
  102. List<FishData> _Fishs = null;
  103. switch (group)
  104. {
  105. case 0:
  106. _Fishs = _fishsEasy;
  107. break;
  108. case 1:
  109. _Fishs = _fishsNormal;
  110. break;
  111. case 2:
  112. _Fishs = _fishsHard;
  113. }
  114. if (_Fishs == null)
  115. {
  116. // the fish list is empty
  117. _log.warning("Fish are not defined !");
  118. return null;
  119. }
  120. for (FishData f : _Fishs)
  121. {
  122. if (f.getLevel() != lvl)
  123. continue;
  124. if (f.getType() != type)
  125. continue;
  126. result.add(f);
  127. }
  128. if (result.isEmpty())
  129. _log.warning("Cant Find Any Fish!? - Lvl: " + lvl + " Type: " + type);
  130. return result;
  131. }
  132. @SuppressWarnings("synthetic-access")
  133. private static class SingletonHolder
  134. {
  135. protected static final FishTable _instance = new FishTable();
  136. }
  137. }