AccessLevels.java 6.5 KB

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