HerbDropTable.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.L2DatabaseFactory;
  30. import com.l2jserver.gameserver.model.L2DropCategory;
  31. import com.l2jserver.gameserver.model.L2DropData;
  32. public class HerbDropTable
  33. {
  34. private static final 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. Statement statement = con.createStatement();
  44. ResultSet dropData = statement.executeQuery("SELECT groupId,itemId,min,max,category,chance FROM herb_droplist_groups ORDER BY groupId,chance DESC"))
  45. {
  46. L2DropData dropDat = null;
  47. while (dropData.next())
  48. {
  49. int groupId = dropData.getInt("groupId");
  50. List<L2DropCategory> category;
  51. if (_herbGroups.containsKey(groupId))
  52. {
  53. category = _herbGroups.get(groupId);
  54. }
  55. else
  56. {
  57. category = new ArrayList<>();
  58. _herbGroups.put(groupId, category);
  59. }
  60. dropDat = new L2DropData();
  61. dropDat.setItemId(dropData.getInt("itemId"));
  62. dropDat.setMinDrop(dropData.getInt("min"));
  63. dropDat.setMaxDrop(dropData.getInt("max"));
  64. dropDat.setChance(dropData.getInt("chance"));
  65. int categoryType = dropData.getInt("category");
  66. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  67. {
  68. _log.warning(getClass().getSimpleName() + ": Data for undefined item template! GroupId: " + groupId + " itemId: " + dropDat.getItemId());
  69. continue;
  70. }
  71. boolean catExists = false;
  72. for (L2DropCategory cat : category)
  73. {
  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. }
  82. // if the category doesn't exit, create it and add the drop
  83. if (!catExists)
  84. {
  85. L2DropCategory cat = new L2DropCategory(categoryType);
  86. cat.addDropData(dropDat, false);
  87. category.add(cat);
  88. }
  89. }
  90. }
  91. catch (Exception e)
  92. {
  93. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading Herb dropdata. ", e);
  94. }
  95. }
  96. public List<L2DropCategory> getHerbDroplist(int groupId)
  97. {
  98. return _herbGroups.get(groupId);
  99. }
  100. public static HerbDropTable getInstance()
  101. {
  102. return SingletonHolder._instance;
  103. }
  104. private static class SingletonHolder
  105. {
  106. protected static final HerbDropTable _instance = new HerbDropTable();
  107. }
  108. }