AccessLevels.java 6.3 KB

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