AccessLevels.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.TIntObjectHashMap;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.model.L2AccessLevel;
  26. /**
  27. * @author FBIagent<br>
  28. */
  29. public class AccessLevels
  30. {
  31. /** The logger<br> */
  32. private static Logger _log = Logger.getLogger(AccessLevels.class.getName());
  33. /** Reserved master access level<br> */
  34. public static final int _masterAccessLevelNum = Config.MASTERACCESS_LEVEL;
  35. /** The master access level which can use everything<br> */
  36. public static L2AccessLevel _masterAccessLevel = new L2AccessLevel(_masterAccessLevelNum, "Master Access", Config.MASTERACCESS_NAME_COLOR, Config.MASTERACCESS_TITLE_COLOR, null, true, true, true, true, true, true, true, true);
  37. /** Reserved user access level<br> */
  38. public static final int _userAccessLevelNum = 0;
  39. /** The user access level which can do no administrative tasks<br> */
  40. public static L2AccessLevel _userAccessLevel = new L2AccessLevel(_userAccessLevelNum, "User", -1, -1, null, false, false, false, true, false, true, true, true);
  41. /** FastMap of access levels defined in database<br> */
  42. private TIntObjectHashMap<L2AccessLevel> _accessLevels;
  43. /**
  44. * Returns the one and only instance of this class<br><br>
  45. *
  46. * @return AccessLevels: the one and only instance of this class<br>
  47. */
  48. public static AccessLevels getInstance()
  49. {
  50. return SingletonHolder._instance;
  51. }
  52. private AccessLevels()
  53. {
  54. loadAccessLevels();
  55. }
  56. /**
  57. * Loads the access levels from database<br>
  58. */
  59. private void loadAccessLevels()
  60. {
  61. _accessLevels = new TIntObjectHashMap<L2AccessLevel>();
  62. Connection con = null;
  63. try
  64. {
  65. con = L2DatabaseFactory.getInstance().getConnection();
  66. PreparedStatement stmt = con.prepareStatement("SELECT * FROM `access_levels` ORDER BY `accessLevel` DESC");
  67. ResultSet rset = stmt.executeQuery();
  68. int accessLevel = 0;
  69. String name = null;
  70. int nameColor = 0;
  71. int titleColor = 0;
  72. String childs = null;
  73. boolean isGm = false;
  74. boolean allowPeaceAttack = false;
  75. boolean allowFixedRes = false;
  76. boolean allowTransaction = false;
  77. boolean allowAltG = false;
  78. boolean giveDamage = false;
  79. boolean takeAggro = false;
  80. boolean gainExp = false;
  81. while (rset.next())
  82. {
  83. accessLevel = rset.getInt("accessLevel");
  84. name = rset.getString("name");
  85. if (accessLevel == _userAccessLevelNum)
  86. {
  87. _log.warning("AccessLevels: Access level with name " + name + " is using reserved user access level " + _userAccessLevelNum + ". Ignoring it!");
  88. continue;
  89. }
  90. else if (accessLevel == _masterAccessLevelNum)
  91. {
  92. _log.warning("AccessLevels: Access level with name " + name + " is using reserved master access level " + _masterAccessLevelNum + ". Ignoring it!");
  93. continue;
  94. }
  95. else if (accessLevel < 0)
  96. {
  97. _log.warning("AccessLevels: Access level with name " + name + " is using banned access level state(below 0). Ignoring it!");
  98. continue;
  99. }
  100. try
  101. {
  102. nameColor = Integer.decode("0x" + rset.getString("nameColor"));
  103. }
  104. catch (NumberFormatException nfe)
  105. {
  106. try
  107. {
  108. nameColor = Integer.decode("0xFFFFFF");
  109. }
  110. catch (NumberFormatException nfe2)
  111. {
  112. }
  113. }
  114. try
  115. {
  116. titleColor = Integer.decode("0x" + rset.getString("titleColor"));
  117. }
  118. catch (NumberFormatException nfe)
  119. {
  120. try
  121. {
  122. titleColor = Integer.decode("0xFFFF77");
  123. }
  124. catch (NumberFormatException nfe2)
  125. {
  126. }
  127. }
  128. childs = rset.getString("childAccess");
  129. isGm = rset.getBoolean("isGm");
  130. allowPeaceAttack = rset.getBoolean("allowPeaceAttack");
  131. allowFixedRes = rset.getBoolean("allowFixedRes");
  132. allowTransaction = rset.getBoolean("allowTransaction");
  133. allowAltG = rset.getBoolean("allowAltg");
  134. giveDamage = rset.getBoolean("giveDamage");
  135. takeAggro = rset.getBoolean("takeAggro");
  136. gainExp = rset.getBoolean("gainExp");
  137. _accessLevels.put(accessLevel, new L2AccessLevel(accessLevel, name, nameColor, titleColor, childs.isEmpty() ? null : childs, isGm, allowPeaceAttack, allowFixedRes, allowTransaction, allowAltG, giveDamage, takeAggro, gainExp));
  138. }
  139. rset.close();
  140. stmt.close();
  141. }
  142. catch (SQLException e)
  143. {
  144. _log.log(Level.WARNING, "AccessLevels: Error loading from database:" + e.getMessage(), e);
  145. }
  146. finally
  147. {
  148. L2DatabaseFactory.close(con);
  149. }
  150. _log.info("AccessLevels: Loaded " + _accessLevels.size() + " from database.");
  151. }
  152. /**
  153. * Returns the access level by characterAccessLevel<br><br>
  154. *
  155. * @param accessLevelNum as int<br><br>
  156. *
  157. * @return AccessLevel: AccessLevel instance by char access level<br>
  158. */
  159. public L2AccessLevel getAccessLevel(int accessLevelNum)
  160. {
  161. L2AccessLevel accessLevel = null;
  162. synchronized (_accessLevels)
  163. {
  164. accessLevel = _accessLevels.get(accessLevelNum);
  165. }
  166. return accessLevel;
  167. }
  168. public void addBanAccessLevel(int accessLevel)
  169. {
  170. synchronized (_accessLevels)
  171. {
  172. if (accessLevel > -1)
  173. {
  174. return;
  175. }
  176. _accessLevels.put(accessLevel, new L2AccessLevel(accessLevel, "Banned", -1, -1, null, false, false, false, false, false, false, false, false));
  177. }
  178. }
  179. public void reloadAccessLevels()
  180. {
  181. loadAccessLevels();
  182. }
  183. @SuppressWarnings("synthetic-access")
  184. private static class SingletonHolder
  185. {
  186. protected static final AccessLevels _instance = new AccessLevels();
  187. }
  188. }