Better error handling for bot yml issues.

This commit is contained in:
cnaude 2015-02-17 20:56:34 -07:00
parent 4b8c3ba945
commit f412a99de6
2 changed files with 242 additions and 216 deletions

View File

@ -76,6 +76,7 @@ public final class PurpleBot {
private PircBotX bot; private PircBotX bot;
protected boolean goodBot;
public final PurpleIRC plugin; public final PurpleIRC plugin;
private final File file; private final File file;
private YamlConfiguration config; private YamlConfiguration config;
@ -205,7 +206,8 @@ public final class PurpleBot {
this.reconnectCount = 0; this.reconnectCount = 0;
whoisSenders = new ArrayList<>(); whoisSenders = new ArrayList<>();
config = new YamlConfiguration(); config = new YamlConfiguration();
loadConfig(); goodBot = loadConfig();
if (goodBot) {
addListeners(); addListeners();
version = plugin.getDescription().getFullName() + ", " version = plugin.getDescription().getFullName() + ", "
+ plugin.getDescription().getDescription() + " - " + plugin.getDescription().getDescription() + " - "
@ -217,6 +219,9 @@ public final class PurpleBot {
buildBot(false); buildBot(false);
} }
}); });
} else {
plugin.logError("Error loading " + this.fileName);
}
messageQueue = new IRCMessageQueueWatcher(this, plugin); messageQueue = new IRCMessageQueueWatcher(this, plugin);
@ -363,8 +368,12 @@ public final class PurpleBot {
*/ */
public void reloadConfig(CommandSender sender) { public void reloadConfig(CommandSender sender) {
config = new YamlConfiguration(); config = new YamlConfiguration();
loadConfig(); goodBot = loadConfig();
if (goodBot) {
sender.sendMessage("[PurpleIRC] [" + botNick + "] IRC bot configuration reloaded."); sender.sendMessage("[PurpleIRC] [" + botNick + "] IRC bot configuration reloaded.");
} else {
sender.sendMessage("[PurpleIRC] [" + botNick + "] " + ChatColor.RED + "Error loading bot configuration!");
}
} }
/** /**
@ -505,6 +514,7 @@ public final class PurpleBot {
* @param sender * @param sender
*/ */
public void saveConfig(CommandSender sender) { public void saveConfig(CommandSender sender) {
if (goodBot) {
try { try {
config.save(file); config.save(file);
sender.sendMessage(plugin.LOG_HEADER_F sender.sendMessage(plugin.LOG_HEADER_F
@ -513,6 +523,10 @@ public final class PurpleBot {
plugin.logError(ex.getMessage()); plugin.logError(ex.getMessage());
sender.sendMessage(ex.getMessage()); sender.sendMessage(ex.getMessage());
} }
} else {
sender.sendMessage(plugin.LOG_HEADER_F
+ ChatColor.RED + " Not saving bot \"" + botNick + "\" to " + file.getName());
}
} }
/** /**
@ -613,7 +627,7 @@ public final class PurpleBot {
saveConfig(); saveConfig();
} }
private void loadConfig() { private boolean loadConfig() {
try { try {
config.load(file); config.load(file);
autoConnect = config.getBoolean("autoconnect", true); autoConnect = config.getBoolean("autoconnect", true);
@ -698,6 +712,10 @@ public final class PurpleBot {
plugin.logInfo(" No command-notify ignores defined."); plugin.logInfo(" No command-notify ignores defined.");
} }
if (config.getConfigurationSection("channels") == null) {
plugin.logError("No channels found!");
return false;
} else {
for (String enChannelName : config.getConfigurationSection("channels").getKeys(false)) { for (String enChannelName : config.getConfigurationSection("channels").getKeys(false)) {
String channelName = decodeChannel(enChannelName); String channelName = decodeChannel(enChannelName);
if (isValidChannel(channelName)) { if (isValidChannel(channelName)) {
@ -904,9 +922,12 @@ public final class PurpleBot {
+ "] [SSL: " + ssl + "]" + " [TrustAllCerts: " + "] [SSL: " + ssl + "]" + " [TrustAllCerts: "
+ trustAllCerts + "]"; + trustAllCerts + "]";
} }
}
} catch (IOException | InvalidConfigurationException ex) { } catch (IOException | InvalidConfigurationException ex) {
plugin.logError(ex.getMessage()); plugin.logError(ex.getMessage());
return false;
} }
return true;
} }
/** /**

View File

@ -717,8 +717,13 @@ public class PurpleIRC extends JavaPlugin {
for (final File file : botsFolder.listFiles()) { for (final File file : botsFolder.listFiles()) {
if (file.getName().toLowerCase().endsWith(".yml")) { if (file.getName().toLowerCase().endsWith(".yml")) {
logInfo("Loading bot file: " + file.getName()); logInfo("Loading bot file: " + file.getName());
ircBots.put(file.getName(), new PurpleBot(file, this)); PurpleBot ircBot = new PurpleBot(file, this);
logInfo("Loaded bot: " + file.getName() + "[" + ircBots.get(file.getName()).botNick + "]"); if (ircBot.goodBot) {
ircBots.put(file.getName(), ircBot);
logInfo("Loaded bot: " + file.getName() + " [" + ircBot.botNick + "]");
} else {
logError("Bot not loaded: " + file.getName());
}
} }
} }
} }