HerbDropTable.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.PreparedStatement;
  22. import java.sql.ResultSet;
  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. PreparedStatement statement = con.prepareStatement("SELECT " + L2DatabaseFactory.getInstance().safetyString(new String[]
  48. {
  49. "groupId",
  50. "itemId",
  51. "min",
  52. "max",
  53. "category",
  54. "chance"
  55. }) + " FROM herb_droplist_groups ORDER BY groupId, chance DESC");
  56. ResultSet dropData = statement.executeQuery())
  57. {
  58. L2DropData dropDat = null;
  59. while (dropData.next())
  60. {
  61. int groupId = dropData.getInt("groupId");
  62. List<L2DropCategory> category;
  63. if (_herbGroups.containsKey(groupId))
  64. {
  65. category = _herbGroups.get(groupId);
  66. }
  67. else
  68. {
  69. category = new ArrayList<>();
  70. _herbGroups.put(groupId, category);
  71. }
  72. dropDat = new L2DropData();
  73. dropDat.setItemId(dropData.getInt("itemId"));
  74. dropDat.setMinDrop(dropData.getInt("min"));
  75. dropDat.setMaxDrop(dropData.getInt("max"));
  76. dropDat.setChance(dropData.getInt("chance"));
  77. int categoryType = dropData.getInt("category");
  78. if (ItemTable.getInstance().getTemplate(dropDat.getItemId()) == null)
  79. {
  80. _log.warning(getClass().getSimpleName() + ": Data for undefined item template! GroupId: " + groupId + " itemId: " + dropDat.getItemId());
  81. continue;
  82. }
  83. boolean catExists = false;
  84. for (L2DropCategory cat : category)
  85. {
  86. // if the category exists, add the drop to this category.
  87. if (cat.getCategoryType() == categoryType)
  88. {
  89. cat.addDropData(dropDat, false);
  90. catExists = true;
  91. break;
  92. }
  93. }
  94. // if the category doesn't exit, create it and add the drop
  95. if (!catExists)
  96. {
  97. L2DropCategory cat = new L2DropCategory(categoryType);
  98. cat.addDropData(dropDat, false);
  99. category.add(cat);
  100. }
  101. }
  102. }
  103. catch (Exception e)
  104. {
  105. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error reading Herb dropdata. ", e);
  106. }
  107. }
  108. public List<L2DropCategory> getHerbDroplist(int groupId)
  109. {
  110. return _herbGroups.get(groupId);
  111. }
  112. public static HerbDropTable getInstance()
  113. {
  114. return SingletonHolder._instance;
  115. }
  116. private static class SingletonHolder
  117. {
  118. protected static final HerbDropTable _instance = new HerbDropTable();
  119. }
  120. }