HennaTreeTable.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.model.L2HennaInstance;
  25. import com.l2jserver.gameserver.model.base.ClassId;
  26. import com.l2jserver.gameserver.templates.item.L2Henna;
  27. import javolution.util.FastList;
  28. import javolution.util.FastMap;
  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. while (classlist.next())
  58. {
  59. list = new FastList<L2HennaInstance>();
  60. classId = classlist.getInt("id");
  61. PreparedStatement statement2 = con.prepareStatement("SELECT class_id, symbol_id FROM henna_trees where class_id=? ORDER BY symbol_id");
  62. statement2.setInt(1, classId);
  63. ResultSet hennatree = statement2.executeQuery();
  64. while (hennatree.next())
  65. {
  66. int id = hennatree.getInt("symbol_id");
  67. //String name = hennatree.getString("name");
  68. L2Henna template = HennaTable.getInstance().getTemplate(id);
  69. if (template == null)
  70. {
  71. hennatree.close();
  72. statement2.close();
  73. classlist.close();
  74. statement.close();
  75. return;
  76. }
  77. L2HennaInstance temp = new L2HennaInstance(template);
  78. temp.setSymbolId(id);
  79. temp.setItemIdDye(template.getDyeId());
  80. temp.setAmountDyeRequire(template.getAmountDyeRequire());
  81. temp.setPrice(template.getPrice());
  82. temp.setStatINT(template.getStatINT());
  83. temp.setStatSTR(template.getStatSTR());
  84. temp.setStatCON(template.getStatCON());
  85. temp.setStatMEM(template.getStatMEM());
  86. temp.setStatDEX(template.getStatDEX());
  87. temp.setStatWIT(template.getStatWIT());
  88. list.add(temp);
  89. }
  90. _hennaTrees.put(ClassId.values()[classId], list);
  91. hennatree.close();
  92. statement2.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. _log.config("HennaTreeTable: Loaded " + count + " Henna Tree Templates.");
  99. }
  100. catch (Exception e)
  101. {
  102. _log.log(Level.SEVERE, "Error loading Henna Tree Table.", e);
  103. }
  104. finally
  105. {
  106. try
  107. {
  108. con.close();
  109. }
  110. catch (Exception e)
  111. {
  112. }
  113. }
  114. }
  115. public L2HennaInstance[] getAvailableHenna(ClassId classId)
  116. {
  117. List<L2HennaInstance> result = new FastList<L2HennaInstance>();
  118. List<L2HennaInstance> henna = _hennaTrees.get(classId);
  119. if (henna == null)
  120. {
  121. // the hennatree for this class is undefined, so we give an empty list
  122. _log.warning("Hennatree for class " + classId + " is not defined !");
  123. return new L2HennaInstance[0];
  124. }
  125. for (L2HennaInstance temp : henna)
  126. {
  127. result.add(temp);
  128. }
  129. return result.toArray(new L2HennaInstance[result.size()]);
  130. }
  131. public boolean isInitialized()
  132. {
  133. return _initialized;
  134. }
  135. @SuppressWarnings("synthetic-access")
  136. private static class SingletonHolder
  137. {
  138. protected static final HennaTreeTable _instance = new HennaTreeTable();
  139. }
  140. }