Added backup system; resolves #103

This commit is contained in:
Daniel Saukel 2016-07-06 16:13:43 +02:00
parent 3b671a6005
commit 3778009785
5 changed files with 109 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import io.github.dre2n.commons.command.BRCommand;
import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessages; import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.config.MainConfig.BackupMode;
import io.github.dre2n.dungeonsxl.player.DPermissions; import io.github.dre2n.dungeonsxl.player.DPermissions;
import io.github.dre2n.dungeonsxl.world.DEditWorld; import io.github.dre2n.dungeonsxl.world.DEditWorld;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -46,6 +47,11 @@ public class SaveCommand extends BRCommand {
Player player = (Player) sender; Player player = (Player) sender;
DEditWorld editWorld = DEditWorld.getByWorld(player.getWorld()); DEditWorld editWorld = DEditWorld.getByWorld(player.getWorld());
if (editWorld != null) { if (editWorld != null) {
BackupMode backupMode = plugin.getMainConfig().getBackupMode();
if (backupMode == BackupMode.ON_SAVE || backupMode == BackupMode.ON_DISABLE_AND_SAVE) {
editWorld.getResource().backup(false);
}
editWorld.save(); editWorld.save();
MessageUtil.sendMessage(player, DMessages.CMD_SAVE_SUCCESS.getMessage()); MessageUtil.sendMessage(player, DMessages.CMD_SAVE_SUCCESS.getMessage());

View File

@ -17,6 +17,7 @@
package io.github.dre2n.dungeonsxl.config; package io.github.dre2n.dungeonsxl.config;
import io.github.dre2n.commons.config.BRConfig; import io.github.dre2n.commons.config.BRConfig;
import io.github.dre2n.commons.util.EnumUtil;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -30,7 +31,14 @@ import org.bukkit.configuration.ConfigurationSection;
*/ */
public class MainConfig extends BRConfig { public class MainConfig extends BRConfig {
public static final int CONFIG_VERSION = 9; public enum BackupMode {
ON_DISABLE,
ON_DISABLE_AND_SAVE,
ON_SAVE,
NEVER
}
public static final int CONFIG_VERSION = 10;
private String language = "english"; private String language = "english";
private boolean enableEconomy = false; private boolean enableEconomy = false;
@ -65,6 +73,7 @@ public class MainConfig extends BRConfig {
private boolean openInventories = false; private boolean openInventories = false;
private boolean dropItems = false; private boolean dropItems = false;
private List<String> editCommandWhitelist = new ArrayList<>(); private List<String> editCommandWhitelist = new ArrayList<>();
private BackupMode backupMode = BackupMode.ON_DISABLE_AND_SAVE;
/* Permissions bridge */ /* Permissions bridge */
private List<String> editPermissions = new ArrayList<>(); private List<String> editPermissions = new ArrayList<>();
@ -305,6 +314,21 @@ public class MainConfig extends BRConfig {
return editCommandWhitelist; return editCommandWhitelist;
} }
/**
* @return the backup mode
*/
public BackupMode getBackupMode() {
return backupMode;
}
/**
* @param mode
* the BackupMode to set
*/
public void setBackupMode(BackupMode mode) {
backupMode = mode;
}
/** /**
* @return the edit mode permissions * @return the edit mode permissions
*/ */
@ -386,6 +410,10 @@ public class MainConfig extends BRConfig {
config.set("secureMode.editCommandWhitelist", editCommandWhitelist); config.set("secureMode.editCommandWhitelist", editCommandWhitelist);
} }
if (!config.contains("backupMode")) {
config.set("backupMode", backupMode.toString());
}
if (!config.contains("editPermissions")) { if (!config.contains("editPermissions")) {
config.set("editPermissions", editPermissions); config.set("editPermissions", editPermissions);
} }
@ -465,6 +493,13 @@ public class MainConfig extends BRConfig {
editCommandWhitelist = config.getStringList("secureMode.editCommandWhitelist"); editCommandWhitelist = config.getStringList("secureMode.editCommandWhitelist");
} }
if (config.contains("backupMode")) {
String mode = config.getString("backupMode");
if (EnumUtil.isValidEnum(BackupMode.class, mode)) {
backupMode = BackupMode.valueOf(mode);
}
}
if (config.contains("editPermissions")) { if (config.contains("editPermissions")) {
editPermissions = config.getStringList("editPermissions"); editPermissions = config.getStringList("editPermissions");
} }

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2012-2016 Frank Baumann
*
* 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 io.github.dre2n.dungeonsxl.task;
import io.github.dre2n.commons.util.FileUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.world.DResourceWorld;
import java.io.File;
import org.bukkit.scheduler.BukkitRunnable;
/**
* @author Daniel Saukel
*/
public class BackupResourceTask extends BukkitRunnable {
private DResourceWorld resource;
public BackupResourceTask(DResourceWorld resource) {
this.resource = resource;
}
@Override
public void run() {
File target = new File(DungeonsXL.BACKUPS, resource.getName() + "-" + System.currentTimeMillis());
FileUtil.copyDirectory(resource.getFolder(), target, new String[]{});
}
}

View File

@ -21,6 +21,7 @@ import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.SignData; import io.github.dre2n.dungeonsxl.config.SignData;
import io.github.dre2n.dungeonsxl.config.WorldConfig; import io.github.dre2n.dungeonsxl.config.WorldConfig;
import io.github.dre2n.dungeonsxl.player.DEditPlayer; import io.github.dre2n.dungeonsxl.player.DEditPlayer;
import io.github.dre2n.dungeonsxl.task.BackupResourceTask;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -28,6 +29,7 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldCreator; import org.bukkit.WorldCreator;
import org.bukkit.WorldType; import org.bukkit.WorldType;
import org.bukkit.scheduler.BukkitRunnable;
/** /**
* This class represents unloaded worlds. * This class represents unloaded worlds.
@ -165,6 +167,21 @@ public class DResourceWorld {
} }
/* Actions */ /* Actions */
/**
* Creates a backup of the resource
*
* @param async
* whether the task shall be performed asyncronously
*/
public void backup(boolean async) {
BackupResourceTask task = new BackupResourceTask(this);
if (async) {
task.runTaskAsynchronously(plugin);
} else {
task.run();
}
}
/** /**
* @param game * @param game
* whether the instance is a DGameWorld * whether the instance is a DGameWorld

View File

@ -19,6 +19,7 @@ package io.github.dre2n.dungeonsxl.world;
import io.github.dre2n.commons.util.FileUtil; import io.github.dre2n.commons.util.FileUtil;
import io.github.dre2n.commons.util.NumberUtil; import io.github.dre2n.commons.util.NumberUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.MainConfig.BackupMode;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -29,6 +30,8 @@ import org.bukkit.Bukkit;
*/ */
public class DWorlds { public class DWorlds {
DungeonsXL plugin = DungeonsXL.getInstance();
private Set<DResourceWorld> resources = new HashSet<>(); private Set<DResourceWorld> resources = new HashSet<>();
private Set<DInstanceWorld> instances = new HashSet<>(); private Set<DInstanceWorld> instances = new HashSet<>();
@ -200,8 +203,13 @@ public class DWorlds {
* Clean up all instances. * Clean up all instances.
*/ */
public void deleteAllInstances() { public void deleteAllInstances() {
BackupMode backupMode = plugin.getMainConfig().getBackupMode();
HashSet<DInstanceWorld> instances = new HashSet<>(this.instances); HashSet<DInstanceWorld> instances = new HashSet<>(this.instances);
for (DInstanceWorld instance : instances) { for (DInstanceWorld instance : instances) {
if (backupMode == BackupMode.ON_DISABLE | backupMode == BackupMode.ON_DISABLE_AND_SAVE && instance instanceof DEditWorld) {
instance.getResource().backup(false);
}
instance.delete(); instance.delete();
} }
} }