AccessLevels.java 6.2 KB

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