HerbDropTable.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. protected HerbDropTable()
  40. {
  41. _herbGroups = new TIntObjectHashMap<>();
  42. restoreData();
  43. }
  44. private void restoreData()
  45. {
  46. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  47. PreparedStatement statement = con.prepareStatement("SELECT "
  48. + L2DatabaseFactory.getInstance().safetyString(new String[] { "groupId", "itemId", "min", "max", "category", "chance" })
  49. + " FROM herb_droplist_groups ORDER BY groupId, chance DESC");
  50. ResultSet dropData = statement.executeQuery())
  51. {
  52. L2DropData dropDat = null;
  53. while (dropData.next())
  54. {
  55. int groupId = dropData.getInt("groupId");
  56. FastList<L2DropCategory> category;
  57. if (_herbGroups.contains(groupId))
  58. category = _herbGroups.get(groupId);
  59. else
  60. {
  61. category = new FastList<>();
  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("Herb Drop data for undefined item template! GroupId: " + groupId+" itemId: "+dropDat.getItemId());
  73. continue;
  74. }
  75. boolean catExists = false;
  76. for (L2DropCategory cat : category)
  77. // if the category exists, add the drop to this category.
  78. if (cat.getCategoryType() == categoryType)
  79. {
  80. cat.addDropData(dropDat, false);
  81. catExists = true;
  82. break;
  83. }
  84. // if the category doesn't exit, create it and add the drop
  85. if (!catExists)
  86. {
  87. L2DropCategory cat = new L2DropCategory(categoryType);
  88. cat.addDropData(dropDat, false);
  89. category.add(cat);
  90. }
  91. }
  92. }
  93. catch (Exception e)
  94. {
  95. _log.log(Level.SEVERE, "HerbDroplistGroupsTable: Error reading Herb dropdata. ", e);
  96. }
  97. }
  98. public FastList<L2DropCategory> getHerbDroplist(int groupId)
  99. {
  100. return _herbGroups.get(groupId);
  101. }
  102. private static class SingletonHolder
  103. {
  104. protected static final HerbDropTable _instance = new HerbDropTable();
  105. }
  106. }