2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.craftbukkit.CraftServer;
|
|
|
|
|
|
|
|
public class Backup implements Runnable {
|
|
|
|
private static final Logger logger = Logger.getLogger("Minecraft");
|
2011-06-01 10:40:12 +00:00
|
|
|
private final CraftServer server;
|
|
|
|
private final IEssentials ess;
|
2011-03-19 22:39:51 +00:00
|
|
|
private boolean running = false;
|
|
|
|
private int taskId = -1;
|
|
|
|
private boolean active = false;
|
|
|
|
|
2011-06-01 10:40:12 +00:00
|
|
|
public Backup(IEssentials ess) {
|
|
|
|
this.ess = ess;
|
|
|
|
server = (CraftServer)ess.getServer();
|
2011-03-19 22:39:51 +00:00
|
|
|
if (server.getOnlinePlayers().length > 0) {
|
|
|
|
startTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onPlayerJoin() {
|
|
|
|
startTask();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startTask() {
|
|
|
|
if (!running) {
|
2011-06-01 10:40:12 +00:00
|
|
|
long interval = ess.getSettings().getBackupInterval()*1200; // minutes -> ticks
|
2011-03-19 22:39:51 +00:00
|
|
|
if (interval < 1200) {
|
|
|
|
return;
|
|
|
|
}
|
2011-06-01 10:40:12 +00:00
|
|
|
taskId = ess.scheduleSyncRepeatingTask(this, interval, interval);
|
2011-03-19 22:39:51 +00:00
|
|
|
running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
if (active) return;
|
|
|
|
active = true;
|
2011-06-01 10:40:12 +00:00
|
|
|
final String command = ess.getSettings().getBackupCommand();
|
2011-03-19 22:39:51 +00:00
|
|
|
if (command == null || "".equals(command)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-05-09 20:46:25 +00:00
|
|
|
logger.log(Level.INFO, Util.i18n("backupStarted"));
|
2011-03-19 22:39:51 +00:00
|
|
|
final CommandSender cs = server.getServer().console;
|
|
|
|
server.dispatchCommand(cs, "save-all");
|
|
|
|
server.dispatchCommand(cs, "save-off");
|
|
|
|
|
2011-06-01 10:40:12 +00:00
|
|
|
ess.scheduleAsyncDelayedTask(
|
2011-03-19 22:39:51 +00:00
|
|
|
new Runnable() {
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Process child = Runtime.getRuntime().exec(command);
|
|
|
|
child.waitFor();
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
logger.log(Level.SEVERE, null, ex);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
logger.log(Level.SEVERE, null, ex);
|
|
|
|
} finally {
|
2011-06-01 10:40:12 +00:00
|
|
|
ess.scheduleSyncDelayedTask(
|
2011-03-19 22:39:51 +00:00
|
|
|
new Runnable() {
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
server.dispatchCommand(cs, "save-on");
|
|
|
|
if (server.getOnlinePlayers().length == 0) {
|
|
|
|
running = false;
|
|
|
|
if (taskId != -1) {
|
|
|
|
server.getScheduler().cancelTask(taskId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
active = false;
|
2011-05-09 20:46:25 +00:00
|
|
|
logger.log(Level.INFO, Util.i18n("backupFinished"));
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|