Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Code improvements.
Browse files Browse the repository at this point in the history
Former-commit-id: 33618ff
  • Loading branch information
xishift committed Jul 15, 2020
1 parent 59c6078 commit aa362de
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 84 deletions.
42 changes: 42 additions & 0 deletions src/main/java/me/ishift/epicguard/bukkit/BukkitMethods.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 0 additions & 15 deletions src/main/java/me/ishift/epicguard/bukkit/BukkitNotificator.java

This file was deleted.

12 changes: 2 additions & 10 deletions src/main/java/me/ishift/epicguard/bukkit/EpicGuardBukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@
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;
private ModuleManager moduleManager;

@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();
Expand All @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/me/ishift/epicguard/bungee/BungeeMethods.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 0 additions & 16 deletions src/main/java/me/ishift/epicguard/bungee/BungeeNotificator.java

This file was deleted.

13 changes: 1 addition & 12 deletions src/main/java/me/ishift/epicguard/bungee/EpicGuardBungee.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
35 changes: 23 additions & 12 deletions src/main/java/me/ishift/epicguard/core/EpicGuard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,15 +23,18 @@ 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;

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("█████╗ ██████╔╝██║██║ ██║ ███╗██║ ██║███████║██████╔╝██║ ██║");
Expand All @@ -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 {
Expand All @@ -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() {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String> getKickMessage() {
return this.getEpicGuard().getMessages().kickMessageProxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/");
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/me/ishift/epicguard/core/util/MethodInterface.java
Original file line number Diff line number Diff line change
@@ -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);
}
7 changes: 0 additions & 7 deletions src/main/java/me/ishift/epicguard/core/util/Notificator.java

This file was deleted.

Loading

0 comments on commit aa362de

Please sign in to comment.