diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/SaveCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/SaveCommand.java index 6f288749..c10e0e5e 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/SaveCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/SaveCommand.java @@ -20,6 +20,7 @@ import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; 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.world.DEditWorld; import org.bukkit.command.CommandSender; @@ -46,6 +47,11 @@ public void onExecute(String[] args, CommandSender sender) { Player player = (Player) sender; DEditWorld editWorld = DEditWorld.getByWorld(player.getWorld()); 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(); MessageUtil.sendMessage(player, DMessages.CMD_SAVE_SUCCESS.getMessage()); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/config/MainConfig.java b/src/main/java/io/github/dre2n/dungeonsxl/config/MainConfig.java index d70e607b..bc22cf10 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/config/MainConfig.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/config/MainConfig.java @@ -17,6 +17,7 @@ package io.github.dre2n.dungeonsxl.config; import io.github.dre2n.commons.config.BRConfig; +import io.github.dre2n.commons.util.EnumUtil; import java.io.File; import java.util.ArrayList; import java.util.Arrays; @@ -30,7 +31,14 @@ */ 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 boolean enableEconomy = false; @@ -65,6 +73,7 @@ public class MainConfig extends BRConfig { private boolean openInventories = false; private boolean dropItems = false; private List editCommandWhitelist = new ArrayList<>(); + private BackupMode backupMode = BackupMode.ON_DISABLE_AND_SAVE; /* Permissions bridge */ private List editPermissions = new ArrayList<>(); @@ -305,6 +314,21 @@ public List getEditCommandWhitelist() { 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 */ @@ -386,6 +410,10 @@ public void initialize() { config.set("secureMode.editCommandWhitelist", editCommandWhitelist); } + if (!config.contains("backupMode")) { + config.set("backupMode", backupMode.toString()); + } + if (!config.contains("editPermissions")) { config.set("editPermissions", editPermissions); } @@ -465,6 +493,13 @@ public void load() { 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")) { editPermissions = config.getStringList("editPermissions"); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/task/BackupResourceTask.java b/src/main/java/io/github/dre2n/dungeonsxl/task/BackupResourceTask.java new file mode 100644 index 00000000..8ef9d825 --- /dev/null +++ b/src/main/java/io/github/dre2n/dungeonsxl/task/BackupResourceTask.java @@ -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 . + */ +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[]{}); + } + +} diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java index 0fb5a8f9..3c5d7c83 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java @@ -21,6 +21,7 @@ import io.github.dre2n.dungeonsxl.config.SignData; import io.github.dre2n.dungeonsxl.config.WorldConfig; import io.github.dre2n.dungeonsxl.player.DEditPlayer; +import io.github.dre2n.dungeonsxl.task.BackupResourceTask; import java.io.File; import java.io.IOException; import org.bukkit.Bukkit; @@ -28,6 +29,7 @@ import org.bukkit.World; import org.bukkit.WorldCreator; import org.bukkit.WorldType; +import org.bukkit.scheduler.BukkitRunnable; /** * This class represents unloaded worlds. @@ -165,6 +167,21 @@ public boolean isInvitedPlayer(OfflinePlayer player) { } /* 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 * whether the instance is a DGameWorld diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/DWorlds.java b/src/main/java/io/github/dre2n/dungeonsxl/world/DWorlds.java index 741a3a0e..6ae5a132 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/DWorlds.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/DWorlds.java @@ -19,6 +19,7 @@ import io.github.dre2n.commons.util.FileUtil; import io.github.dre2n.commons.util.NumberUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; +import io.github.dre2n.dungeonsxl.config.MainConfig.BackupMode; import java.io.File; import java.util.HashSet; import java.util.Set; @@ -29,6 +30,8 @@ */ public class DWorlds { + DungeonsXL plugin = DungeonsXL.getInstance(); + private Set resources = new HashSet<>(); private Set instances = new HashSet<>(); @@ -200,8 +203,13 @@ public void check() { * Clean up all instances. */ public void deleteAllInstances() { + BackupMode backupMode = plugin.getMainConfig().getBackupMode(); HashSet instances = new HashSet<>(this.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(); } }