0

Made Main file more generic + disabled joining on severe load error

This commit is contained in:
David Panić 2019-04-07 20:44:13 +02:00
parent df36c6d3d4
commit 0418d12414
No known key found for this signature in database
GPG Key ID: 0FDC88FEC4D8A110
3 changed files with 65 additions and 41 deletions

View File

@ -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<PlayerBan> playerBanManager = new BanManager<>(bansFile, PlayerBan.class);
private BanManager<PlayerBan> 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;
}
}
}

View File

@ -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<T extends Ban> implements Listener {
private BanManager<T> bm;
BanJoinListener(BanManager<T> bm) {
public BanJoinListener(BanManager<T> bm) {
this.bm = bm;
}

View File

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