HerbDropTable.java 3.7 KB

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