HennaTreeTable.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.List;
  20. import java.util.Map;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastList;
  24. import javolution.util.FastMap;
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.model.base.ClassId;
  27. import com.l2jserver.gameserver.model.items.L2Henna;
  28. import com.l2jserver.gameserver.model.items.instance.L2HennaInstance;
  29. /**
  30. * This class ...
  31. *
  32. * @version $Revision$ $Date$
  33. */
  34. public class HennaTreeTable
  35. {
  36. private static Logger _log = Logger.getLogger(HennaTreeTable.class.getName());
  37. private Map<ClassId, List<L2HennaInstance>> _hennaTrees;
  38. private boolean _initialized = true;
  39. public static HennaTreeTable getInstance()
  40. {
  41. return SingletonHolder._instance;
  42. }
  43. private HennaTreeTable()
  44. {
  45. _hennaTrees = new FastMap<ClassId, List<L2HennaInstance>>();
  46. int classId = 0;
  47. int count = 0;
  48. Connection con = null;
  49. try
  50. {
  51. con = L2DatabaseFactory.getInstance().getConnection();
  52. PreparedStatement statement = con.prepareStatement("SELECT class_name, id, parent_id FROM class_list ORDER BY id");
  53. ResultSet classlist = statement.executeQuery();
  54. List<L2HennaInstance> list;
  55. //int parentClassId;
  56. //L2Henna henna;
  57. PreparedStatement statement2 = con.prepareStatement("SELECT class_id, symbol_id FROM henna_trees where class_id=? ORDER BY symbol_id");
  58. while (classlist.next())
  59. {
  60. list = new FastList<L2HennaInstance>();
  61. classId = classlist.getInt("id");
  62. statement2.setInt(1, classId);
  63. ResultSet hennatree = statement2.executeQuery();
  64. statement2.clearParameters();
  65. while (hennatree.next())
  66. {
  67. int id = hennatree.getInt("symbol_id");
  68. //String name = hennatree.getString("name");
  69. L2Henna template = HennaTable.getInstance().getTemplate(id);
  70. if (template == null)
  71. {
  72. hennatree.close();
  73. statement2.close();
  74. classlist.close();
  75. statement.close();
  76. return;
  77. }
  78. L2HennaInstance temp = new L2HennaInstance(template);
  79. temp.setSymbolId(id);
  80. temp.setItemIdDye(template.getDyeId());
  81. temp.setAmountDyeRequire(template.getAmountDyeRequire());
  82. temp.setPrice(template.getPrice());
  83. temp.setStatINT(template.getStatINT());
  84. temp.setStatSTR(template.getStatSTR());
  85. temp.setStatCON(template.getStatCON());
  86. temp.setStatMEM(template.getStatMEM());
  87. temp.setStatDEX(template.getStatDEX());
  88. temp.setStatWIT(template.getStatWIT());
  89. list.add(temp);
  90. }
  91. _hennaTrees.put(ClassId.values()[classId], list);
  92. hennatree.close();
  93. count += list.size();
  94. _log.fine("Henna Tree for Class: " + classId + " has " + list.size() + " Henna Templates.");
  95. }
  96. classlist.close();
  97. statement.close();
  98. statement2.close();
  99. _log.info("HennaTreeTable: Loaded " + count + " Henna Tree Templates.");
  100. }
  101. catch (Exception e)
  102. {
  103. _log.log(Level.SEVERE, "Error loading Henna Tree Table.", e);
  104. }
  105. finally
  106. {
  107. L2DatabaseFactory.close(con);
  108. }
  109. }
  110. public L2HennaInstance[] getAvailableHenna(ClassId classId)
  111. {
  112. List<L2HennaInstance> result = new FastList<L2HennaInstance>();
  113. List<L2HennaInstance> henna = _hennaTrees.get(classId);
  114. if (henna == null)
  115. {
  116. // the hennatree for this class is undefined, so we give an empty list
  117. _log.warning("Hennatree for class " + classId + " is not defined !");
  118. return new L2HennaInstance[0];
  119. }
  120. for (L2HennaInstance temp : henna)
  121. {
  122. result.add(temp);
  123. }
  124. return result.toArray(new L2HennaInstance[result.size()]);
  125. }
  126. public boolean isInitialized()
  127. {
  128. return _initialized;
  129. }
  130. @SuppressWarnings("synthetic-access")
  131. private static class SingletonHolder
  132. {
  133. protected static final HennaTreeTable _instance = new HennaTreeTable();
  134. }
  135. }