HerbDropTable.java 3.7 KB

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