From aa362de03bf6c4e3a06a31c24680ac24a22e46c0 Mon Sep 17 00:00:00 2001 From: xishift Date: Wed, 15 Jul 2020 14:21:02 +0200 Subject: [PATCH] Code improvements. Former-commit-id: 33618ffffc309924ae7f67391af4f9f994a288ed --- .../epicguard/bukkit/BukkitMethods.java | 42 +++++++++++++++++ .../epicguard/bukkit/BukkitNotificator.java | 15 ------ .../epicguard/bukkit/EpicGuardBukkit.java | 12 +---- .../bukkit/command/EpicGuardCommand.java | 4 +- .../epicguard/bungee/BungeeMethods.java | 46 +++++++++++++++++++ .../epicguard/bungee/BungeeNotificator.java | 16 ------- .../epicguard/bungee/EpicGuardBungee.java | 13 +----- .../bungee/command/EpicGuardCommand.java | 4 +- .../me/ishift/epicguard/core/EpicGuard.java | 35 +++++++++----- .../epicguard/core/check/impl/ProxyCheck.java | 8 +++- .../core/manager/NotificationManager.java | 8 ++-- .../core/task/UpdateCheckerTask.java | 3 +- .../epicguard/core/util/MethodInterface.java | 16 +++++++ .../epicguard/core/util/Notificator.java | 7 --- .../epicguard/core/util/UpdateChecker.java | 2 +- 15 files changed, 147 insertions(+), 84 deletions(-) create mode 100644 src/main/java/me/ishift/epicguard/bukkit/BukkitMethods.java delete mode 100644 src/main/java/me/ishift/epicguard/bukkit/BukkitNotificator.java create mode 100644 src/main/java/me/ishift/epicguard/bungee/BungeeMethods.java delete mode 100644 src/main/java/me/ishift/epicguard/bungee/BungeeNotificator.java create mode 100644 src/main/java/me/ishift/epicguard/core/util/MethodInterface.java delete mode 100644 src/main/java/me/ishift/epicguard/core/util/Notificator.java diff --git a/src/main/java/me/ishift/epicguard/bukkit/BukkitMethods.java b/src/main/java/me/ishift/epicguard/bukkit/BukkitMethods.java new file mode 100644 index 00000000..9e24e661 --- /dev/null +++ b/src/main/java/me/ishift/epicguard/bukkit/BukkitMethods.java @@ -0,0 +1,42 @@ +package me.ishift.epicguard.bukkit; + +import me.ishift.epicguard.bukkit.util.Reflections; +import me.ishift.epicguard.core.util.MethodInterface; +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.UUID; +import java.util.logging.Logger; + +public class BukkitMethods implements MethodInterface { + private final JavaPlugin plugin; + + public BukkitMethods(JavaPlugin plugin) { + this.plugin = plugin; + } + + @Override + public void sendActionBar(String message, UUID target) { + Reflections.sendActionBar(Bukkit.getPlayer(target), message + " &8░ &7TPS: &6" + Reflections.getTPS()); + } + + @Override + public Logger getLogger() { + return this.plugin.getLogger(); + } + + @Override + public String getVersion() { + return this.plugin.getDescription().getVersion(); + } + + @Override + public void scheduleSyncTask(Runnable task, long seconds) { + Bukkit.getScheduler().runTaskTimer(this.plugin, task, 20L, seconds * 20L); + } + + @Override + public void scheduleAsyncTask(Runnable task, long seconds) { + Bukkit.getScheduler().runTaskTimerAsynchronously(this.plugin, task, 20L, seconds * 20L); + } +} diff --git a/src/main/java/me/ishift/epicguard/bukkit/BukkitNotificator.java b/src/main/java/me/ishift/epicguard/bukkit/BukkitNotificator.java deleted file mode 100644 index a4e3ed69..00000000 --- a/src/main/java/me/ishift/epicguard/bukkit/BukkitNotificator.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.ishift.epicguard.bukkit; - -import me.ishift.epicguard.bukkit.util.Reflections; -import me.ishift.epicguard.core.util.Notificator; -import org.bukkit.Bukkit; - -import java.util.UUID; - -public class BukkitNotificator implements Notificator { - - @Override - public void sendActionBar(String message, UUID target) { - Reflections.sendActionBar(Bukkit.getPlayer(target), message + " &8░ &7TPS: &6" + Reflections.getTPS()); - } -} diff --git a/src/main/java/me/ishift/epicguard/bukkit/EpicGuardBukkit.java b/src/main/java/me/ishift/epicguard/bukkit/EpicGuardBukkit.java index 32ecd630..b9571c25 100644 --- a/src/main/java/me/ishift/epicguard/bukkit/EpicGuardBukkit.java +++ b/src/main/java/me/ishift/epicguard/bukkit/EpicGuardBukkit.java @@ -9,13 +9,9 @@ import me.ishift.epicguard.bukkit.module.ModuleTask; import me.ishift.epicguard.bukkit.util.Metrics; import me.ishift.epicguard.core.EpicGuard; -import me.ishift.epicguard.core.task.AttackResetTask; -import me.ishift.epicguard.core.task.CounterTask; -import me.ishift.epicguard.core.task.UpdateCheckerTask; import org.bukkit.Bukkit; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitScheduler; public class EpicGuardBukkit extends JavaPlugin { private EpicGuard epicGuard; @@ -23,7 +19,7 @@ public class EpicGuardBukkit extends JavaPlugin { @Override public void onEnable() { - this.epicGuard = new EpicGuard(this.getLogger(), new BukkitNotificator()); + this.epicGuard = new EpicGuard(new BukkitMethods(this)); this.moduleManager = new ModuleManager(this.epicGuard); PluginManager pm = Bukkit.getPluginManager(); @@ -32,11 +28,7 @@ public void onEnable() { pm.registerEvents(new PlayerJoinListener(this, epicGuard), this); pm.registerEvents(new CommandListener(this), this); - BukkitScheduler scheduler = Bukkit.getScheduler(); - scheduler.runTaskTimerAsynchronously(this, new CounterTask(this.epicGuard), 20L, 20L); - scheduler.runTaskTimerAsynchronously(this, new AttackResetTask(this.epicGuard), 20L, 20L * 20L); - scheduler.runTaskTimerAsynchronously(this, new UpdateCheckerTask(this.epicGuard), 20L, 1800L * 20L); // 30 minutes - scheduler.runTaskTimer(this, new ModuleTask(this), 20L * 5L, 20L); + Bukkit.getScheduler().runTaskTimer(this, new ModuleTask(this), 20L * 5L, 20L); this.getCommand("epicguard").setExecutor(new EpicGuardCommand(this.epicGuard)); diff --git a/src/main/java/me/ishift/epicguard/bukkit/command/EpicGuardCommand.java b/src/main/java/me/ishift/epicguard/bukkit/command/EpicGuardCommand.java index 610c5288..a8a284a2 100644 --- a/src/main/java/me/ishift/epicguard/bukkit/command/EpicGuardCommand.java +++ b/src/main/java/me/ishift/epicguard/bukkit/command/EpicGuardCommand.java @@ -25,7 +25,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command if (args.length < 1) { send(sender, "&8&m---------------------------------------------------"); send(sender, " &6&lEpicGuard &8(Spigot version)"); - send(sender, " &7Version: &f" + this.epicGuard.getVersion()); + send(sender, " &7Version: &f" + this.epicGuard.getMethodInterface().getVersion()); if (UpdateChecker.isAvailable()) { send(sender, ""); send(sender, " &7New version is available: &c&n" + UpdateChecker.getRemoteVersion()); @@ -50,7 +50,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command case "stats": send(sender, "&8&m---------------------------------------------------"); send(sender, " &6&lEpicGuard &7(Statistics)"); - send(sender, " &7Version: &f" + this.epicGuard.getVersion()); + send(sender, " &7Version: &f" + this.epicGuard.getMethodInterface().getVersion()); if (UpdateChecker.isAvailable()) { send(sender, ""); send(sender, " &7New version is available: &c&n" + UpdateChecker.getRemoteVersion()); diff --git a/src/main/java/me/ishift/epicguard/bungee/BungeeMethods.java b/src/main/java/me/ishift/epicguard/bungee/BungeeMethods.java new file mode 100644 index 00000000..ce4808c1 --- /dev/null +++ b/src/main/java/me/ishift/epicguard/bungee/BungeeMethods.java @@ -0,0 +1,46 @@ +package me.ishift.epicguard.bungee; + +import me.ishift.epicguard.core.util.ChatUtils; +import me.ishift.epicguard.core.util.MethodInterface; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.plugin.Plugin; + +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; + +public class BungeeMethods implements MethodInterface { + private final Plugin plugin; + + public BungeeMethods(Plugin plugin) { + this.plugin = plugin; + } + + @Override + public void sendActionBar(String message, UUID target) { + ProxyServer.getInstance().getPlayer(target).sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatUtils.colored(message))); + } + + @Override + public Logger getLogger() { + return this.plugin.getLogger(); + } + + @Override + public String getVersion() { + return this.plugin.getDescription().getVersion(); + } + + @Override + public void scheduleSyncTask(Runnable task, long seconds) { + this.plugin.getProxy().getScheduler().schedule(this.plugin, task, seconds, TimeUnit.SECONDS); + } + + @Override + public void scheduleAsyncTask(Runnable task, long seconds) { + // There are no async repeating tasks in BungeeCord + this.scheduleSyncTask(task, seconds); + } +} diff --git a/src/main/java/me/ishift/epicguard/bungee/BungeeNotificator.java b/src/main/java/me/ishift/epicguard/bungee/BungeeNotificator.java deleted file mode 100644 index f851fbc4..00000000 --- a/src/main/java/me/ishift/epicguard/bungee/BungeeNotificator.java +++ /dev/null @@ -1,16 +0,0 @@ -package me.ishift.epicguard.bungee; - -import me.ishift.epicguard.core.util.ChatUtils; -import me.ishift.epicguard.core.util.Notificator; -import net.md_5.bungee.api.ChatMessageType; -import net.md_5.bungee.api.ProxyServer; -import net.md_5.bungee.api.chat.TextComponent; - -import java.util.UUID; - -public class BungeeNotificator implements Notificator { - @Override - public void sendActionBar(String message, UUID target) { - ProxyServer.getInstance().getPlayer(target).sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(ChatUtils.colored(message))); - } -} diff --git a/src/main/java/me/ishift/epicguard/bungee/EpicGuardBungee.java b/src/main/java/me/ishift/epicguard/bungee/EpicGuardBungee.java index 59407e9d..40393313 100644 --- a/src/main/java/me/ishift/epicguard/bungee/EpicGuardBungee.java +++ b/src/main/java/me/ishift/epicguard/bungee/EpicGuardBungee.java @@ -6,26 +6,15 @@ import me.ishift.epicguard.bungee.listener.PreLoginListener; import me.ishift.epicguard.bungee.util.Metrics; import me.ishift.epicguard.core.EpicGuard; -import me.ishift.epicguard.core.task.AttackResetTask; -import me.ishift.epicguard.core.task.CounterTask; -import me.ishift.epicguard.core.task.UpdateCheckerTask; import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.PluginManager; -import net.md_5.bungee.api.scheduler.TaskScheduler; - -import java.util.concurrent.TimeUnit; public class EpicGuardBungee extends Plugin { private EpicGuard epicGuard; @Override public void onEnable() { - this.epicGuard = new EpicGuard(this.getLogger(), new BungeeNotificator()); - - TaskScheduler scheduler = this.getProxy().getScheduler(); - scheduler.schedule(this, new CounterTask(this.epicGuard), 1L, 1L, TimeUnit.SECONDS); - scheduler.schedule(this, new AttackResetTask(this.epicGuard), 1L, 20L, TimeUnit.SECONDS); - scheduler.schedule(this, new UpdateCheckerTask(this.epicGuard), 1L, 1800L, TimeUnit.SECONDS); // 30 minutes + this.epicGuard = new EpicGuard(new BungeeMethods(this)); PluginManager pm = this.getProxy().getPluginManager(); pm.registerListener(this, new PreLoginListener(this.epicGuard)); diff --git a/src/main/java/me/ishift/epicguard/bungee/command/EpicGuardCommand.java b/src/main/java/me/ishift/epicguard/bungee/command/EpicGuardCommand.java index b55bca10..7704090f 100644 --- a/src/main/java/me/ishift/epicguard/bungee/command/EpicGuardCommand.java +++ b/src/main/java/me/ishift/epicguard/bungee/command/EpicGuardCommand.java @@ -25,7 +25,7 @@ public void execute(CommandSender sender, String[] args) { if (args.length < 1) { send(sender, "&8&m---------------------------------------------------"); send(sender, " &6&lEpicGuard &8(BungeeCord version)"); - send(sender, " &7Version: &f" + this.epicGuard.getVersion()); + send(sender, " &7Version: &f" + this.epicGuard.getMethodInterface().getVersion()); if (UpdateChecker.isAvailable()) { send(sender, ""); send(sender, " &7New version is available: &c&n" + UpdateChecker.getRemoteVersion()); @@ -45,7 +45,7 @@ public void execute(CommandSender sender, String[] args) { case "stats": send(sender, "&8&m---------------------------------------------------"); send(sender, " &6&lEpicGuard &7(Statistics)"); - send(sender, " &7Version: &f" + this.epicGuard.getVersion()); + send(sender, " &7Version: &f" + this.epicGuard.getMethodInterface().getVersion()); if (UpdateChecker.isAvailable()) { send(sender, ""); send(sender, " &7New version is available: &c&n" + UpdateChecker.getRemoteVersion()); diff --git a/src/main/java/me/ishift/epicguard/core/EpicGuard.java b/src/main/java/me/ishift/epicguard/core/EpicGuard.java index a65f5fd3..07717a52 100644 --- a/src/main/java/me/ishift/epicguard/core/EpicGuard.java +++ b/src/main/java/me/ishift/epicguard/core/EpicGuard.java @@ -5,12 +5,16 @@ import me.ishift.epicguard.core.manager.GeoManager; import me.ishift.epicguard.core.manager.NotificationManager; import me.ishift.epicguard.core.manager.StorageManager; +import me.ishift.epicguard.core.task.AttackResetTask; +import me.ishift.epicguard.core.task.CounterTask; +import me.ishift.epicguard.core.task.UpdateCheckerTask; import me.ishift.epicguard.core.user.UserManager; import me.ishift.epicguard.core.util.ConfigHelper; import me.ishift.epicguard.core.util.LogFilter; -import me.ishift.epicguard.core.util.Notificator; +import me.ishift.epicguard.core.util.MethodInterface; import java.io.File; +import java.util.concurrent.TimeUnit; import java.util.logging.Logger; public class EpicGuard { @@ -19,6 +23,7 @@ public class EpicGuard { private final GeoManager geoManager; private final UserManager userManager; private final NotificationManager notificationManager; + private final MethodInterface methodInterface; private PluginConfiguration config; private MessagesConfiguration messages; @@ -26,8 +31,10 @@ public class EpicGuard { private boolean attack; private int connectionPerSecond; - public EpicGuard(Logger logger, Notificator notificator) { - this.logger = logger; + public EpicGuard(MethodInterface methodInterface) { + this.methodInterface = methodInterface; + this.logger = methodInterface.getLogger(); + logger.info("███████╗██████╗ ██╗ ██████╗ ██████╗ ██╗ ██╗ █████╗ ██████╗ ██████╗"); logger.info("██╔════╝██╔══██╗██║██╔════╝██╔════╝ ██║ ██║██╔══██╗██╔══██╗██╔══██╗"); logger.info("█████╗ ██████╔╝██║██║ ██║ ███╗██║ ██║███████║██████╔╝██║ ██║"); @@ -39,7 +46,7 @@ public EpicGuard(Logger logger, Notificator notificator) { this.reloadConfig(); this.storageManager = new StorageManager(); this.userManager = new UserManager(); - this.notificationManager = new NotificationManager(this, notificator); + this.notificationManager = new NotificationManager(this); this.geoManager = new GeoManager(this); try { @@ -48,15 +55,11 @@ public EpicGuard(Logger logger, Notificator notificator) { logger.warning("LogFilter can't be enabled, because log4j is not found. If you are running on BungeeCord, consider a switch to Waterfall."); } - logger.info("EpicGuard v5 finished startup successfully."); - } - - public void shutdown() { - this.storageManager.save(); - } + this.methodInterface.scheduleAsyncTask(new CounterTask(this), 1L); + this.methodInterface.scheduleAsyncTask(new AttackResetTask(this), 30L); + this.methodInterface.scheduleAsyncTask(new UpdateCheckerTask(this), 1800L); - public String getVersion() { - return "5.0.2"; + logger.info("EpicGuard v5 finished startup successfully."); } public void reloadConfig() { @@ -71,6 +74,14 @@ public void reloadConfig() { this.messages = ConfigHelper.loadConfig(messagesFile, MessagesConfiguration.class); } + public void shutdown() { + this.storageManager.save(); + } + + public MethodInterface getMethodInterface() { + return this.methodInterface; + } + public Logger getLogger() { return this.logger; } diff --git a/src/main/java/me/ishift/epicguard/core/check/impl/ProxyCheck.java b/src/main/java/me/ishift/epicguard/core/check/impl/ProxyCheck.java index c6791540..58132275 100644 --- a/src/main/java/me/ishift/epicguard/core/check/impl/ProxyCheck.java +++ b/src/main/java/me/ishift/epicguard/core/check/impl/ProxyCheck.java @@ -7,11 +7,12 @@ import java.util.List; -public class ProxyCheck extends Check { +public class ProxyCheck extends Check implements Runnable { private int requests; public ProxyCheck(EpicGuard epicGuard) { super(epicGuard); + epicGuard.getMethodInterface().scheduleAsyncTask(this, 86400L); //24 hours } @Override @@ -45,6 +46,11 @@ private boolean proxyCheck(String address) { return URLUtils.readString(url).contains("yes"); } + @Override + public void run() { + this.requests = 0; + } + @Override public List getKickMessage() { return this.getEpicGuard().getMessages().kickMessageProxy; diff --git a/src/main/java/me/ishift/epicguard/core/manager/NotificationManager.java b/src/main/java/me/ishift/epicguard/core/manager/NotificationManager.java index 3a386799..5677902b 100644 --- a/src/main/java/me/ishift/epicguard/core/manager/NotificationManager.java +++ b/src/main/java/me/ishift/epicguard/core/manager/NotificationManager.java @@ -2,21 +2,19 @@ import me.ishift.epicguard.core.EpicGuard; import me.ishift.epicguard.core.user.User; -import me.ishift.epicguard.core.util.Notificator; +import me.ishift.epicguard.core.util.MethodInterface; public class NotificationManager { private final EpicGuard epicGuard; - private final Notificator notificator; - public NotificationManager(EpicGuard epicGuard, Notificator notificator) { + public NotificationManager(EpicGuard epicGuard) { this.epicGuard = epicGuard; - this.notificator = notificator; } public void notify(String message) { for (User user : this.epicGuard.getUserManager().getUsers()) { if (user.isNotifications()) { - this.notificator.sendActionBar(message, user.getUniqueId()); + this.epicGuard.getMethodInterface().sendActionBar(message, user.getUniqueId()); } } } diff --git a/src/main/java/me/ishift/epicguard/core/task/UpdateCheckerTask.java b/src/main/java/me/ishift/epicguard/core/task/UpdateCheckerTask.java index 593dc988..e1fea784 100644 --- a/src/main/java/me/ishift/epicguard/core/task/UpdateCheckerTask.java +++ b/src/main/java/me/ishift/epicguard/core/task/UpdateCheckerTask.java @@ -15,7 +15,8 @@ public void run() { UpdateChecker.checkForUpdates(this.epicGuard); if (UpdateChecker.isAvailable()) { - this.epicGuard.getLogger().info("New update is available"); + this.epicGuard.getLogger().info("New update is available: " + UpdateChecker.getRemoteVersion()); + this.epicGuard.getLogger().info("Download it here: https://www.spigotmc.org/resources/72369/"); } } } diff --git a/src/main/java/me/ishift/epicguard/core/util/MethodInterface.java b/src/main/java/me/ishift/epicguard/core/util/MethodInterface.java new file mode 100644 index 00000000..9f256d86 --- /dev/null +++ b/src/main/java/me/ishift/epicguard/core/util/MethodInterface.java @@ -0,0 +1,16 @@ +package me.ishift.epicguard.core.util; + +import java.util.UUID; +import java.util.logging.Logger; + +public interface MethodInterface { + void sendActionBar(String message, UUID target); + + Logger getLogger(); + + String getVersion(); + + void scheduleSyncTask(Runnable task, long seconds); + + void scheduleAsyncTask(Runnable task, long seconds); +} diff --git a/src/main/java/me/ishift/epicguard/core/util/Notificator.java b/src/main/java/me/ishift/epicguard/core/util/Notificator.java deleted file mode 100644 index 9b38f296..00000000 --- a/src/main/java/me/ishift/epicguard/core/util/Notificator.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.ishift.epicguard.core.util; - -import java.util.UUID; - -public interface Notificator { - void sendActionBar(String message, UUID target); -} diff --git a/src/main/java/me/ishift/epicguard/core/util/UpdateChecker.java b/src/main/java/me/ishift/epicguard/core/util/UpdateChecker.java index 0d9b9ed6..2a694724 100644 --- a/src/main/java/me/ishift/epicguard/core/util/UpdateChecker.java +++ b/src/main/java/me/ishift/epicguard/core/util/UpdateChecker.java @@ -10,7 +10,7 @@ public final class UpdateChecker { public static void checkForUpdates(EpicGuard epicGuard) { remoteVersion = URLUtils.readString(CHECK_URL); int latest = Integer.parseInt(remoteVersion.replace(".", "")); - int current = Integer.parseInt(epicGuard.getVersion().replace(".", "")); + int current = Integer.parseInt(epicGuard.getMethodInterface().getVersion().replace(".", "")); available = latest > current; }