PurpleIRC-spigot/src/main/java/com/cnaude/purpleirc/BotWatcher.java

65 lines
2.1 KiB
Java
Raw Normal View History

2015-01-01 20:38:43 +01:00
/*
* Copyright (C) 2014 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc;
import org.bukkit.scheduler.BukkitTask;
2015-03-21 18:45:04 +01:00
/**
* This thread checks each bot for connectivity and reconnects when appropriate.
2015-01-01 20:38:43 +01:00
*
* @author Chris Naude
* */
2015-01-01 20:38:43 +01:00
public class BotWatcher {
2015-05-17 20:20:34 +02:00
2015-01-01 20:38:43 +01:00
private final PurpleIRC plugin;
private final BukkitTask bt;
2015-05-17 20:20:34 +02:00
2015-01-01 20:38:43 +01:00
/**
* Run the BotWatcher thread asynchronously at configured interval.
*
* @param plugin the PurpleIRC plugin
2015-01-01 20:38:43 +01:00
*/
public BotWatcher(final PurpleIRC plugin) {
this.plugin = plugin;
bt = this.plugin.getServer().getScheduler().runTaskTimerAsynchronously(this.plugin, new Runnable() {
@Override
public void run() {
for (PurpleBot ircBot : plugin.ircBots.values()) {
if (ircBot.isConnectedBlocking()) {
ircBot.setConnected(true);
} else {
ircBot.setConnected(false);
if (ircBot.autoConnect) {
2015-02-07 18:45:37 +01:00
plugin.logDebug("[" + ircBot.botNick + "] Attempting reconnect...");
2015-01-01 20:38:43 +01:00
ircBot.reload();
}
}
}
}
}, plugin.ircConnCheckInterval, plugin.ircConnCheckInterval);
}
2015-05-17 20:20:34 +02:00
2015-01-01 20:38:43 +01:00
/**
* Cancel the scheduled BukkitTask. Call this when
* shutting down.
2015-01-01 20:38:43 +01:00
*/
public void cancel() {
bt.cancel();
}
2015-05-17 20:20:34 +02:00
}