From 0418d124148fce1fc352a92806eedc2414b8a005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Pani=C4=87?= Date: Sun, 7 Apr 2019 20:44:13 +0200 Subject: [PATCH] Made Main file more generic + disabled joining on severe load error --- .../java/com/redstoner/bungeeBans/Main.java | 80 ++++++++++--------- .../{ => listeners}/BanJoinListener.java | 6 +- .../listeners/DisableJoinListener.java | 20 +++++ 3 files changed, 65 insertions(+), 41 deletions(-) rename src/main/java/com/redstoner/bungeeBans/{ => listeners}/BanJoinListener.java (90%) create mode 100644 src/main/java/com/redstoner/bungeeBans/listeners/DisableJoinListener.java diff --git a/src/main/java/com/redstoner/bungeeBans/Main.java b/src/main/java/com/redstoner/bungeeBans/Main.java index 3b847e4..5df9486 100644 --- a/src/main/java/com/redstoner/bungeeBans/Main.java +++ b/src/main/java/com/redstoner/bungeeBans/Main.java @@ -4,6 +4,8 @@ import com.redstoner.bungeeBans.commands.BanCommand; import com.redstoner.bungeeBans.commands.GetBanCommand; import com.redstoner.bungeeBans.commands.UnbanCommand; import com.redstoner.bungeeBans.json.PlayerBan; +import com.redstoner.bungeeBans.listeners.BanJoinListener; +import com.redstoner.bungeeBans.listeners.DisableJoinListener; import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.PluginManager; @@ -12,44 +14,17 @@ import java.io.FileNotFoundException; import java.io.IOException; public class Main extends Plugin { - private File bansFile = new File("banned-players.json"); + private File playerBanFile = new File("banned-players.json"); - private BanManager playerBanManager = new BanManager<>(bansFile, PlayerBan.class); + private BanManager playerBanManager = new BanManager<>(playerBanFile, PlayerBan.class); private boolean shouldSave = true; @Override public void onEnable() { - try { - getLogger().info("Loading bans..."); - - playerBanManager.loadBans(); - - getLogger().info("Loaded bans!"); - } catch (FileNotFoundException e) { - getLogger().warning("Bans file does not exist! Creating!"); - - try { - if (bansFile.createNewFile()) { - getLogger().info("File created! Retrying load..."); - onEnable(); - return; - } else { - getLogger().severe("File could not be created! Disabling!"); - } - } catch (IOException e2) { - getLogger().severe("File could not be created! Disabling!"); - getLogger().severe(e2.getMessage()); - e2.printStackTrace(); - } - - disable(); - return; - } catch (Exception e) { - getLogger().severe("Failed to load bans: " + e.getMessage()); - e.printStackTrace(); - - disable(); + if ( + loadBans("player", playerBanManager, playerBanFile) + ) { return; } @@ -76,16 +51,43 @@ public class Main extends Plugin { } private void disable() { - getLogger().severe("Disabling plugin!"); + getLogger().severe("Players will not be able to join because of a severe error!!! Check the log output above!"); shouldSave = false; - this.onDisable(); - - PluginManager pm = getProxy().getPluginManager(); - - pm.unregisterListeners(this); - pm.unregisterCommands(this); + getProxy().getPluginManager().registerListener(this, new DisableJoinListener()); } + private boolean loadBans(String name, BanManager banManager, File banFile) { + try { + getLogger().info("Loading " + name + " bans..."); + banManager.loadBans(); + getLogger().info("Loaded " + name + " bans!"); + return false; + } catch (FileNotFoundException e) { + getLogger().warning("Ban file (" + name + ") does not exist! Creating!"); + + try { + if (banFile.createNewFile()) { + getLogger().info("Ban file (" + name + ") created! Retrying load..."); + onEnable(); + return false; + } + } catch (IOException e2) { + getLogger().severe(e2.getMessage()); + e2.printStackTrace(); + } + + getLogger().severe("Ban file (" + name + ") could not be created!"); + + disable(); + return true; + } catch (Exception e) { + getLogger().severe("Failed to load " + name + " bans: " + e.getMessage()); + e.printStackTrace(); + + disable(); + return true; + } + } } diff --git a/src/main/java/com/redstoner/bungeeBans/BanJoinListener.java b/src/main/java/com/redstoner/bungeeBans/listeners/BanJoinListener.java similarity index 90% rename from src/main/java/com/redstoner/bungeeBans/BanJoinListener.java rename to src/main/java/com/redstoner/bungeeBans/listeners/BanJoinListener.java index c868c4b..22dd212 100644 --- a/src/main/java/com/redstoner/bungeeBans/BanJoinListener.java +++ b/src/main/java/com/redstoner/bungeeBans/listeners/BanJoinListener.java @@ -1,6 +1,8 @@ -package com.redstoner.bungeeBans; +package com.redstoner.bungeeBans.listeners; import com.mojang.api.profiles.Profile; +import com.redstoner.bungeeBans.BanManager; +import com.redstoner.bungeeBans.Util; import com.redstoner.bungeeBans.json.Ban; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.ComponentBuilder; @@ -13,7 +15,7 @@ import net.md_5.bungee.event.EventPriority; public class BanJoinListener implements Listener { private BanManager bm; - BanJoinListener(BanManager bm) { + public BanJoinListener(BanManager bm) { this.bm = bm; } diff --git a/src/main/java/com/redstoner/bungeeBans/listeners/DisableJoinListener.java b/src/main/java/com/redstoner/bungeeBans/listeners/DisableJoinListener.java new file mode 100644 index 0000000..df4a325 --- /dev/null +++ b/src/main/java/com/redstoner/bungeeBans/listeners/DisableJoinListener.java @@ -0,0 +1,20 @@ +package com.redstoner.bungeeBans.listeners; + +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.event.PreLoginEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.event.EventHandler; +import net.md_5.bungee.event.EventPriority; + +public class DisableJoinListener implements Listener { + @EventHandler (priority = EventPriority.HIGHEST) + public void onJoin(PreLoginEvent event) { + event.setCancelled(true); + + event.setCancelReason( + new ComponentBuilder(ChatColor.RED + "Joining is disabled because of a bungee error! Please notify an admin ASAP!") + .create() + ); + } +}