Skip to content

Commit

Permalink
Added backup system; resolves #103
Browse files Browse the repository at this point in the history
  • Loading branch information
Sataniel98 committed Jul 6, 2016
1 parent 3b671a6 commit 3778009
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());

Expand Down
37 changes: 36 additions & 1 deletion src/main/java/io/github/dre2n/dungeonsxl/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -65,6 +73,7 @@ public class MainConfig extends BRConfig {
private boolean openInventories = false;
private boolean dropItems = false;
private List<String> editCommandWhitelist = new ArrayList<>();
private BackupMode backupMode = BackupMode.ON_DISABLE_AND_SAVE;

/* Permissions bridge */
private List<String> editPermissions = new ArrayList<>();
Expand Down Expand Up @@ -305,6 +314,21 @@ public List<String> 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
*/
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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[]{});
}

}
17 changes: 17 additions & 0 deletions src/main/java/io/github/dre2n/dungeonsxl/world/DResourceWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
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;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.scheduler.BukkitRunnable;

/**
* This class represents unloaded worlds.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/github/dre2n/dungeonsxl/world/DWorlds.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,8 @@
*/
public class DWorlds {

DungeonsXL plugin = DungeonsXL.getInstance();

private Set<DResourceWorld> resources = new HashSet<>();
private Set<DInstanceWorld> instances = new HashSet<>();

Expand Down Expand Up @@ -200,8 +203,13 @@ public void check() {
* Clean up all instances.
*/
public void deleteAllInstances() {
BackupMode backupMode = plugin.getMainConfig().getBackupMode();
HashSet<DInstanceWorld> 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();
}
}
Expand Down

0 comments on commit 3778009

Please sign in to comment.