summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/command/SaveMap.java
blob: f6fe228e6fc01085ca936bfee647120e127cfa55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net.tylermurphy.hideAndSeek.command;

import static net.tylermurphy.hideAndSeek.Config.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable;

import net.tylermurphy.hideAndSeek.Main;

public class SaveMap implements ICommand {

	public static boolean runningBackup = false;
	
	public void execute(CommandSender sender, String[] args) {
		if(!Main.plugin.status.equals("Standby")) {
			sender.sendMessage(errorPrefix + "Game is currently in session");
			return;
		}
		if(spawnPosition.getBlockX() == 0 && spawnPosition.getBlockY() == 0 && spawnPosition.getBlockZ() == 0) {
			sender.sendMessage(errorPrefix + "Please set spawn location first");
			return;
		}
		sender.sendMessage(messagePrefix + "Starting map save");
		sender.sendMessage(warningPrefix + "All commands will be disabled when the save is in progress. Do not turn off the server.");
		Bukkit.getServer().getWorld(spawnWorld).save();
		BukkitRunnable runnable = new BukkitRunnable() {
			public void run() {
				File current = new File(Main.root+File.separator+spawnWorld);
				if(current.exists()) {
					File temp_destenation = new File(Main.root+File.separator+"temp_hideandseek_"+spawnWorld);
					File destenation = new File(Main.root+File.separator+"hideandseek_"+spawnWorld);
					copyFileStructure(current, temp_destenation);
					if(destenation.exists()) {
						deleteDirectory(destenation);
						destenation.mkdir();
					}
					temp_destenation.renameTo(destenation);
					sender.sendMessage(messagePrefix + "Map save complete");
					runningBackup = false;
				} else {
					sender.sendMessage(errorPrefix + "Coudnt find current map");
				}
			}
		};
		runnable.runTaskAsynchronously(Main.plugin);
		runningBackup = true;
	}
	
	private static void copyFileStructure(File source, File target){
	    try {
	        ArrayList<String> ignore = new ArrayList<>(Arrays.asList("uid.dat", "session.lock"));
	        if(!ignore.contains(source.getName())) {
	            if(source.isDirectory()) {
	                if(!target.exists())
	                    if (!target.mkdirs())
	                        throw new IOException("Couldn't create world directory!");
	                String files[] = source.list();
	                for (String file : files) {
	                    File srcFile = new File(source, file);
	                    File destFile = new File(target, file);
	                    copyFileStructure(srcFile, destFile);
	                }
	            } else {
	                InputStream in = new FileInputStream(source);
	                OutputStream out = new FileOutputStream(target);
	                byte[] buffer = new byte[1024];
	                int length;
	                while ((length = in.read(buffer)) > 0)
	                    out.write(buffer, 0, length);
	                in.close();
	                out.close();
	            }
	        }
	    } catch (IOException e) {
	        throw new RuntimeException(e);
	    }
	}
	
	private boolean deleteDirectory(File directoryToBeDeleted) {
	    File[] allContents = directoryToBeDeleted.listFiles();
	    if (allContents != null) {
	        for (File file : allContents) {
	            deleteDirectory(file);
	        }
	    }
	    return directoryToBeDeleted.delete();
	}

	public String getLabel() {
		return "saveMap";
	}

	public String getUsage() {
		return "";
	}

	public String getDescription() {
		return "Saves current map for the game. May lag server.";
	}
	
}