PageRenderTime 4119ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/EpicManager/tags/EpicManager_0_1/src/main/java/com/bukkit/epicsaga/EpicManager/EpicManager.java

http://team-eso.googlecode.com/
Java | 293 lines | 158 code | 64 blank | 71 comment | 20 complexity | 2ff87b75564b61e6cf4571e2fc091531 MD5 | raw file
  1. /*
  2. This file is part of EpicManager
  3. Copyright (C) 2011 by Team ESO
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /**
  21. * @author sir.manic@gmail.com
  22. * @license MIT License
  23. */
  24. package com.bukkit.epicsaga.EpicManager;
  25. import java.util.List;
  26. import java.io.File;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import java.util.logging.Logger;
  31. import org.bukkit.Server;
  32. import org.bukkit.command.Command;
  33. import org.bukkit.command.CommandSender;
  34. import org.bukkit.entity.Player;
  35. import org.bukkit.plugin.Plugin;
  36. import org.bukkit.plugin.PluginDescriptionFile;
  37. import org.bukkit.plugin.PluginLoader;
  38. import org.bukkit.plugin.java.JavaPlugin;
  39. import com.nijikokun.bukkit.Permissions.Permissions;
  40. import com.nijiko.permissions.PermissionHandler;
  41. import com.bukkit.epicsaga.EpicManager.auth.AuthFeature;
  42. import com.bukkit.epicsaga.EpicManager.home.HomeFeature;
  43. import com.bukkit.epicsaga.EpicManager.misc.MiscFeature;
  44. /**
  45. * Core plugin handler.
  46. *
  47. * @author _sir_maniac
  48. */
  49. public class EpicManager extends JavaPlugin {
  50. private static final String CONFIG_FILE = "config.yml";
  51. private static List<PluginFeature> features = new ArrayList<PluginFeature>();
  52. public static final Logger log = Logger.getLogger("Minecraft");
  53. public static PermissionHandler permissions = null;
  54. public EMConfig config;
  55. private String pluginName = "EpicManager";
  56. private Map<String, CommandHandler> handlers =
  57. new HashMap<String, CommandHandler>();
  58. static {
  59. features.add(new MiscFeature());
  60. features.add(new AuthFeature());
  61. features.add(new HomeFeature());
  62. }
  63. public EpicManager(PluginLoader pluginLoader, Server instance,
  64. PluginDescriptionFile desc, File folder, File plugin,
  65. ClassLoader cLoader) {
  66. super(pluginLoader, instance, desc, folder, plugin, cLoader);
  67. pluginName = this.getDescription().getName();
  68. File file = new File(
  69. folder+File.separator+CONFIG_FILE);
  70. config = new EMConfig(file);
  71. // NOTE: Event registration should be done in onEnable not here as all events are unregistered when a plugin is disabled
  72. }
  73. public void onEnable() {
  74. try{
  75. checkConfigDir();
  76. config.load();
  77. setupPermissions();
  78. for(PluginFeature feature : features) {
  79. feature.onEnable(this);
  80. }
  81. PluginDescriptionFile pdfFile = this.getDescription();
  82. logInfo( " version " + pdfFile.getVersion() + " is enabled." );
  83. }
  84. catch(EnableError e) {
  85. logSevere("Error intilizing plugin, disabling." + e.toString());
  86. this.getServer().getPluginManager().disablePlugin(this);
  87. }
  88. }
  89. public void onDisable() {
  90. // NOTE: All registered events are automatically unregistered when a plugin is disabled
  91. for(PluginFeature subPlugin : features) {
  92. subPlugin.onDisable(this);
  93. }
  94. }
  95. public void registerCommand(String command, CommandHandler handler) {
  96. handlers.put(command.toLowerCase(), handler);
  97. }
  98. @Override
  99. public boolean onCommand(CommandSender sender, Command cmd,
  100. String commandLabel, String[] args) {
  101. CommandHandler handler = handlers.get(commandLabel.toLowerCase());
  102. if(handler == null)
  103. return true;
  104. return handler.onCommand(commandLabel, sender, args);
  105. }
  106. /**
  107. * Reload configuration information.
  108. *
  109. */
  110. public void reload() {
  111. config.load();
  112. }
  113. private void checkConfigDir() throws EnableError {
  114. File dir = this.getDataFolder();
  115. if(!dir.isDirectory() && !dir.mkdirs()) {
  116. throw new EnableError("Could not make configuration directory "+
  117. dir.getPath());
  118. }
  119. }
  120. private void setupPermissions() throws EnableError {
  121. Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
  122. if(test != null) {
  123. // make sure Permissions gets enabled first
  124. if(!test.isEnabled())
  125. getServer().getPluginManager().enablePlugin(test);
  126. permissions = ((Permissions)test).getHandler();
  127. }
  128. else {
  129. throw new EnableError("Permission plugin not available.");
  130. }
  131. }
  132. /**
  133. * @return val with all minecraft formatting removed
  134. *
  135. */
  136. private String stripFormatting(String val) {
  137. // remove any color codes
  138. return val.replaceAll("\u00A7.", "");
  139. }
  140. /**
  141. * Find closest matching player begining with searchName
  142. *
  143. * Compares the value of getName() for every player on the server,
  144. * if getName() doesn't match any player, it then tries the value of
  145. * getDisplayName() stripped of formatting characters.
  146. *
  147. * This method is necessary if the displayName(what the op sees in game)
  148. * is significantly different from name.
  149. *
  150. *
  151. * @param searchName
  152. * @return the player found, otherwise null
  153. */
  154. public Player getPlayerByDisplayName(String searchName) {
  155. searchName = searchName.toLowerCase();
  156. searchName = stripFormatting(searchName);
  157. Player[] players = getServer().getOnlinePlayers();
  158. Player matchingPlayer = null;
  159. String playerName;
  160. int diff, maxDiff = Integer.MAX_VALUE;
  161. for(Player player : players) {
  162. playerName = player.getName().toLowerCase();
  163. if(playerName.startsWith(searchName)) {
  164. diff = playerName.length() - searchName.length();
  165. if(diff == 0)
  166. return player;
  167. if(diff < maxDiff) {
  168. maxDiff = diff;
  169. matchingPlayer = player;
  170. }
  171. }
  172. }
  173. /*
  174. * Continue to try displayNames if no exact matches.
  175. * The name that matches the most of the two loops
  176. * will be returned.
  177. *
  178. * why two loops? I felt it would be faster if there wasn't two searches
  179. * for each name, unless absolutely neccessary.
  180. */
  181. for(Player player : players) {
  182. playerName = player.getDisplayName().toLowerCase();
  183. playerName = stripFormatting(playerName);
  184. if(playerName.startsWith(searchName)) {
  185. diff = playerName.length() - searchName.length();
  186. if(diff == 0)
  187. return player;
  188. if(diff < maxDiff) {
  189. maxDiff = diff;
  190. matchingPlayer = player;
  191. }
  192. }
  193. }
  194. return matchingPlayer;
  195. }
  196. public void logSevere(String message) {
  197. log.severe("[" + pluginName + "] " + message);
  198. }
  199. public void logWarning(String message) {
  200. log.warning("[" + pluginName + "] " + message);
  201. }
  202. public void logInfo(String message) {
  203. log.info("[" + pluginName + "] " + message);
  204. }
  205. public static class EnableError extends Exception {
  206. /**
  207. *
  208. */
  209. private static final long serialVersionUID = 6666252142075352300L;
  210. public EnableError() {
  211. super();
  212. }
  213. public EnableError(String message, Throwable cause) {
  214. super(message, cause);
  215. }
  216. public EnableError(String message) {
  217. super(message);
  218. }
  219. public EnableError(Throwable cause) {
  220. super(cause);
  221. }
  222. }
  223. }