PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/me/Todkommt/ThumbsApply/ThumbsApply.java

https://github.com/Todkommt/ThumbsApply
Java | 286 lines | 263 code | 23 blank | 0 comment | 34 complexity | 13bd800730f6b701f7aea6a4d1edb753 MD5 | raw file
  1. package me.Todkommt.ThumbsApply;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map.Entry;
  11. import java.util.Set;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import me.Todkommt.ThumbsApply.listeners.ThumbsApplyPlayerListener;
  15. import me.Todkommt.ThumbsApply.modules.ModulePassword;
  16. import me.Todkommt.ThumbsApply.permissions.GroupManagerHandler;
  17. import me.Todkommt.ThumbsApply.permissions.PEX;
  18. import me.Todkommt.ThumbsApply.permissions.Permissions3;
  19. import me.Todkommt.ThumbsApply.permissions.PermissionsBukkit;
  20. import me.Todkommt.ThumbsApply.permissions.PermissionsHandler;
  21. import me.Todkommt.ThumbsApply.permissions.bPermissions;
  22. import me.Todkommt.ThumbsApply.utils.TAClassHandler;
  23. import me.Todkommt.ThumbsApply.utils.ThumbsApplyModule;
  24. import org.bukkit.command.Command;
  25. import org.bukkit.command.CommandSender;
  26. import org.bukkit.configuration.file.FileConfiguration;
  27. import org.bukkit.configuration.file.YamlConfiguration;
  28. import org.bukkit.plugin.Plugin;
  29. import org.bukkit.plugin.java.JavaPlugin;
  30. public class ThumbsApply extends JavaPlugin {
  31. public static ThumbsApply instance;
  32. public Logger log;
  33. public FileConfiguration localizationConfig;
  34. public PermissionsHandler permissionsHandler;
  35. public File mainDir = new File("plugins/ThumbsApply");
  36. public File localizationDir = new File(mainDir + "/localization");
  37. public File localizationFile = new File(localizationDir + "/local.yml");
  38. public File moduleDir = new File(mainDir + "/modules");
  39. public File logFile = new File(mainDir + "/log.txt");
  40. public HashMap<String, String> externalLocals = new HashMap<String, String>();
  41. public List<ThumbsApplyGroup> groups = new ArrayList<ThumbsApplyGroup>();
  42. public List<Class<?>> modules = new ArrayList<Class<?>>();
  43. public ThumbsApplyPlayerListener playerListener;
  44. public void onDisable() {
  45. }
  46. public void appendToLog(String key)
  47. {
  48. try {
  49. BufferedWriter writer = new BufferedWriter(new FileWriter(logFile, true));
  50. writer.write(key);
  51. writer.newLine();
  52. writer.close();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. public void onEnable() {
  58. mainDir.mkdir();
  59. moduleDir.mkdir();
  60. localizationDir.mkdir();
  61. instance = this;
  62. log = Logger.getLogger("Minecraft");
  63. setupPermissions();
  64. setupDefaults();
  65. playerListener = new ThumbsApplyPlayerListener(this);
  66. getServer().getPluginManager().registerEvents(playerListener, this);
  67. try {
  68. modules = TAClassHandler.loadClasses(moduleDir);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. loadGroups();
  73. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  74. public void run() {
  75. update();
  76. }
  77. }, getConfig().getInt("options.tickDelay")/50, getConfig().getInt("options.tickDelay")/50);
  78. reloadLocalizationConfig();
  79. log.info("[ThumbsApply] Loaded Localization.");
  80. log.info("[ThumbsApply] Started tick thread with tick delay " + getConfig().getInt("options.tickDelay") + ".");
  81. log.info("[ThumbsApply] Loading process completed.");
  82. }
  83. private void loadGroups()
  84. {
  85. Set<String> keys = null;
  86. try
  87. {
  88. keys = getConfig().getConfigurationSection("groups").getKeys(false);
  89. }
  90. catch(NullPointerException e)
  91. {
  92. log.info("[ThumbsApply] No groups present. Generating default groups. Please configure them correctly.");
  93. getConfig().set("groups.world-User", "default");
  94. getConfig().set("groups.world-Mod", "%1000");
  95. saveConfig();
  96. reloadConfig();
  97. keys = getConfig().getConfigurationSection("groups").getKeys(false);
  98. }
  99. log.info("[ThumbsApply] Groups Loaded:");
  100. for(String key : keys)
  101. {
  102. String value = getConfig().getString("groups." + key);
  103. String world = "";
  104. String group = key;
  105. ThumbsApplyModule module = null;
  106. if(key.contains("-"))
  107. {
  108. world = key.split("-")[0];
  109. group = key.split("-")[1];
  110. }
  111. char prefix = value.charAt(0);
  112. String newvalue = value.substring(1);
  113. List<ThumbsApplyModule> mods = new ArrayList<ThumbsApplyModule>();
  114. for(Class<?> cls : modules)
  115. {
  116. try {
  117. mods.add((ThumbsApplyModule)cls.newInstance());
  118. } catch (InstantiationException e) {
  119. e.printStackTrace();
  120. } catch (IllegalAccessException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. for(ThumbsApplyModule mod : mods)
  125. {
  126. if(mod.prefix == prefix)
  127. {
  128. module = mod;
  129. break;
  130. }
  131. }
  132. if(module == null)
  133. {
  134. module = new ModulePassword();
  135. module.group = group;
  136. module.world = world;
  137. module.onLoad(value);
  138. }
  139. else
  140. {
  141. module.group = group;
  142. module.world = world;
  143. module.onLoad(newvalue);
  144. }
  145. ThumbsApplyGroup tagroup = new ThumbsApplyGroup(module, group, world);
  146. groups.add(tagroup);
  147. log.info("- " + tagroup.group + " in world " + tagroup.world + " with method " + tagroup.method.getClass().getSimpleName() + " and value " + tagroup.method.value);
  148. }
  149. }
  150. private void setupPermissions(){
  151. Plugin permissions = getServer().getPluginManager().getPlugin("Permissions");
  152. Plugin PEX = getServer().getPluginManager().getPlugin("PermissionsEx");
  153. Plugin GroupManager = getServer().getPluginManager().getPlugin("GroupManager");
  154. Plugin bPermissions = getServer().getPluginManager().getPlugin("bPermissions");
  155. if(PEX != null)
  156. {
  157. permissionsHandler = new PEX(this);
  158. log.info("[ThumbsApply] PEX System activated.");
  159. }
  160. else if(GroupManager != null)
  161. {
  162. permissionsHandler = new GroupManagerHandler(this);
  163. log.info("[ThumbsApply] GroupManager System activated.");
  164. }
  165. else if(permissions != null && permissions.getDescription().getVersion().startsWith("3"))
  166. {
  167. permissionsHandler = new Permissions3(permissions, this);
  168. log.info("[ThumbsApply] Permissions3 System activated.");
  169. }
  170. else if(bPermissions != null)
  171. {
  172. permissionsHandler = new bPermissions(this);
  173. log.info("[ThumbsApply] bPermissions System activated.");
  174. }
  175. else
  176. {
  177. permissionsHandler = new PermissionsBukkit(this);
  178. log.info("[ThumbsApply] PermissionsBukkit System activated.");
  179. }
  180. }
  181. public void setupDefaults()
  182. {
  183. String main = "options.";
  184. getConfig().addDefault(main + "chatBlockEnabled", false);
  185. getConfig().addDefault(main + "joinMessageEnabled", true);
  186. getConfig().addDefault(main + "tickDelay", 60000);
  187. getConfig().options().copyDefaults(true);
  188. saveConfig();
  189. }
  190. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
  191. {
  192. if(command.getName().equalsIgnoreCase("apply"))
  193. {
  194. for(ThumbsApplyGroup group : groups)
  195. {
  196. boolean res = group.method.onCommand(sender, args);
  197. if(res)
  198. {
  199. break;
  200. }
  201. }
  202. Messaging.sendMsgBuffer();
  203. return true;
  204. }
  205. else return false;
  206. }
  207. public void reloadLocalizationConfig() {
  208. if (localizationFile == null) {
  209. localizationFile = new File(localizationDir + File.separator + "localization.yml");
  210. }
  211. localizationConfig = YamlConfiguration.loadConfiguration(localizationFile);
  212. String main = "messages.";
  213. Iterator<Entry<String, String>> iterate = externalLocals.entrySet().iterator();
  214. while(iterate.hasNext())
  215. {
  216. Entry<String, String> entry = iterate.next();
  217. localizationConfig.addDefault(main + entry.getKey(), entry.getValue());
  218. }
  219. localizationConfig.addDefault(main + "SUCCESS", "You were promoted to {group} successfully.");
  220. localizationConfig.addDefault(main + "GUEST_CHAT", "You can't chat as a guest.");
  221. localizationConfig.addDefault(main + "WRONG_PASSWORD", "You entered the wrong password!");
  222. localizationConfig.addDefault(main + "USAGE", "Usage: /apply password");
  223. localizationConfig.addDefault(main + "ALREADY_PROMOTED", "You are already promoted!");
  224. localizationConfig.addDefault(main + "THIS_IS_NOT_A_CONSOLE_COMMAND", "You must be a player to use that command.");
  225. localizationConfig.addDefault(main + "NULL_COMMAND", "This command is not available.");
  226. localizationConfig.addDefault(main + "UNKNOWN_ERROR", "An unknown error occured.");
  227. localizationConfig.options().copyDefaults(true);
  228. try {
  229. localizationConfig.save(localizationFile);
  230. } catch (IOException e) {
  231. e.printStackTrace();
  232. }
  233. for(Entry<String, String> entry : externalLocals.entrySet())
  234. {
  235. String value = localizationConfig.getString(main + entry.getKey());
  236. externalLocals.put(entry.getKey(), value);
  237. }
  238. }
  239. public FileConfiguration getLocalizationConfig() {
  240. if (localizationConfig == null) {
  241. reloadLocalizationConfig();
  242. }
  243. return localizationConfig;
  244. }
  245. public void saveLocalizationConfig() {
  246. if (localizationConfig == null || localizationFile == null) {
  247. return;
  248. }
  249. try {
  250. localizationConfig.save(localizationFile);
  251. } catch (IOException ex) {
  252. Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save config to " + localizationFile, ex);
  253. }
  254. }
  255. public void update()
  256. {
  257. for(ThumbsApplyGroup group : groups)
  258. {
  259. group.method.onUpdate(getConfig().getInt("options.tickDelay"));
  260. }
  261. Messaging.sendMsgBuffer();
  262. }
  263. }