/src/main/java/de/logfilter/LogFilter.java

https://bitbucket.org/DaAllexx/logfilter · Java · 128 lines · 62 code · 36 blank · 30 comment · 2 complexity · dd364be1a2b320a13078fc77d93e726d MD5 · raw file

  1. package de.logfilter;
  2. /* Java related */
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.logging.Logger;
  7. import java.util.regex.Pattern;
  8. /* Bukkit related */
  9. import org.bukkit.configuration.file.YamlConfiguration;
  10. import org.bukkit.plugin.java.JavaPlugin;
  11. /* McStats related */
  12. import org.mcstats.Metrics;
  13. /* LogFilter related */
  14. import de.logfilter.commands.LogFilterCommands;
  15. import de.logfilter.listener.LogListener;
  16. import de.logfilter.stats.Statistics;
  17. public class LogFilter extends JavaPlugin {
  18. /* Version */
  19. public static final String VERSION = "0.2";
  20. /* Enabled */
  21. public static boolean enabled = true;
  22. /* Logger */
  23. public static Logger log = Logger.getLogger("Minecraft");
  24. /* Filter rules */
  25. public static final ArrayList<Pattern> rules = new ArrayList<Pattern>();
  26. /* LogInjector */
  27. private LogInjector injector;
  28. /* Configuration */
  29. public static YamlConfiguration config;
  30. /* Statistics for mcstats.org */
  31. public Statistics statistics;
  32. @Override
  33. public void onEnable() {
  34. /* Time */
  35. long time = System.currentTimeMillis();
  36. /* Load configuration */
  37. this.loadConfiguration();
  38. /* Injection formatter */
  39. log.info("[LogFilter] Injecting filters...");
  40. try {
  41. injector = new LogInjector(this.getServer().getPluginManager());
  42. injector.inject();
  43. } catch(Exception ex) {
  44. log.severe("[LogFilter] Error while injecting filters..!");
  45. return;
  46. }
  47. log.info("[LogFilter] Injected filters successfully!");
  48. /* Metrics */
  49. try {
  50. /* Define metrics object */
  51. Metrics metrics = new Metrics(this);
  52. /* Construct statistic object */
  53. statistics = new Statistics(metrics);
  54. /* Start metrics */
  55. metrics.start();
  56. } catch(Exception ex) {
  57. /* Ignore Exception */
  58. }
  59. /* Register listeners */
  60. this.getServer().getPluginManager().registerEvents(new LogListener(), this);
  61. /* Register commands */
  62. this.getCommand("logfilter").setExecutor(new LogFilterCommands());
  63. /* Estimated time */
  64. long estimated = System.currentTimeMillis() - time;
  65. log.info("[LogFilter] LogFilter version 0.1 enabled! (" + estimated + "ms)");
  66. }
  67. @Override
  68. public void onDisable() {
  69. /* Disable LogFilter */
  70. log.info("[LogFilter] LogFilter disabled!");
  71. /* Remove filters */
  72. log.info("[LogFilter] Remove filters..");
  73. injector.remove();
  74. log.info("[LogFilter] Filters removed!");
  75. }
  76. public void loadConfiguration() {
  77. /* Define file */
  78. File configuration_file = new File(this.getDataFolder(), "config.yml");
  79. /* Check if configuration exists */
  80. if(!configuration_file.exists())
  81. this.saveDefaultConfig();
  82. /* Load configuration */
  83. LogFilter.config = YamlConfiguration.loadConfiguration(configuration_file);
  84. /* Get list of rules from configuration */
  85. List<String> filter_rules = LogFilter.config.getStringList("filter-rules");
  86. /* Compile patterns for better efficiency */
  87. for(String rule : filter_rules) {
  88. /* Add rules to list and precompile it */
  89. rules.add(Pattern.compile(rule));
  90. }
  91. }
  92. }