HerbDropTable.java 3.6 KB

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