HerbDropTable.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  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.L2DropCategory;
  25. import com.l2jserver.gameserver.model.L2DropData;
  26. /**
  27. * This class ...
  28. *
  29. * @version $Revision$ $Date$
  30. */
  31. public class HerbDropTable
  32. {
  33. private static Logger _log = Logger.getLogger(HerbDropTable.class.getName());
  34. private TIntObjectHashMap<FastList<L2DropCategory>> _herbGroups;
  35. public static HerbDropTable getInstance()
  36. {
  37. return SingletonHolder._instance;
  38. }
  39. private HerbDropTable()
  40. {
  41. _herbGroups = new TIntObjectHashMap<FastList<L2DropCategory>>();
  42. restoreData();
  43. }
  44. /**
  45. *
  46. */
  47. private void restoreData()
  48. {
  49. Connection con = null;
  50. try
  51. {
  52. con = L2DatabaseFactory.getInstance().getConnection();
  53. PreparedStatement statement = con.prepareStatement("SELECT "
  54. + L2DatabaseFactory.getInstance().safetyString(new String[] { "groupId", "itemId", "min", "max", "category", "chance" })
  55. + " FROM herb_droplist_groups ORDER BY groupId, chance DESC");
  56. ResultSet dropData = statement.executeQuery();
  57. L2DropData dropDat = null;
  58. while (dropData.next())
  59. {
  60. int groupId = dropData.getInt("groupId");
  61. FastList<L2DropCategory> category;
  62. if (_herbGroups.contains(groupId))
  63. category = _herbGroups.get(groupId);
  64. else
  65. {
  66. category = new FastList<L2DropCategory>();
  67. _herbGroups.put(groupId, category);
  68. }
  69. dropDat = new L2DropData();
  70. dropDat.setItemId(dropData.getInt("itemId"));
  71. dropDat.setMinDrop(dropData.getInt("min"));
  72. dropDat.setMaxDrop(dropData.getInt("max"));
  73. dropDat.setChance(dropData.getInt("chance"));
  74. int categoryType = dropData.getInt("category");
  75. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  76. {
  77. _log.warning("Herb Drop data for undefined item template! GroupId: " + groupId+" itemId: "+dropDat.getItemId());
  78. continue;
  79. }
  80. boolean catExists = false;
  81. for (L2DropCategory cat : category)
  82. // if the category exists, add the drop to this category.
  83. if (cat.getCategoryType() == categoryType)
  84. {
  85. cat.addDropData(dropDat, false);
  86. catExists = true;
  87. break;
  88. }
  89. // if the category doesn't exit, create it and add the drop
  90. if (!catExists)
  91. {
  92. L2DropCategory cat = new L2DropCategory(categoryType);
  93. cat.addDropData(dropDat, false);
  94. category.add(cat);
  95. }
  96. }
  97. dropData.close();
  98. statement.close();
  99. }
  100. catch (Exception e)
  101. {
  102. _log.log(Level.SEVERE, "HerbDroplistGroupsTable: Error reading Herb dropdata. ", e);
  103. }
  104. finally
  105. {
  106. L2DatabaseFactory.close(con);
  107. }
  108. }
  109. public FastList<L2DropCategory> getHerbDroplist(int groupId)
  110. {
  111. return _herbGroups.get(groupId);
  112. }
  113. @SuppressWarnings("synthetic-access")
  114. private static class SingletonHolder
  115. {
  116. protected static final HerbDropTable _instance = new HerbDropTable();
  117. }
  118. }