summaryrefslogtreewikicommitdiff
path: root/src/main/java/cat/freya/khs/command/map/Save.java
blob: 8998bdd64106376b81ec6a42008e3981fc3589df (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
package cat.freya.khs.command.map;

import cat.freya.khs.Main;
import cat.freya.khs.command.util.ICommand;
import cat.freya.khs.configuration.Config;
import cat.freya.khs.configuration.Localization;
import cat.freya.khs.configuration.Map;
import cat.freya.khs.configuration.Maps;
import cat.freya.khs.game.util.Status;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.stream.Collectors;

public class Save implements ICommand {

	public static boolean runningBackup = false;

	public void execute(Player sender, String[] args) {
		if (!Config.mapSaveEnabled) {
			sender.sendMessage(Config.errorPrefix + Localization.message("MAPSAVE_DISABLED"));
			return;
		}
		if (Main.getInstance().getGame().getStatus() != Status.STANDBY) {
			sender.sendMessage(Config.errorPrefix + Localization.message("GAME_INPROGRESS"));
			return;
		}
		Map map = Maps.getMap(args[0]);
		if(map == null) {
			sender.sendMessage(Config.errorPrefix + Localization.message("INVALID_MAP"));
			return;
		}
		if (map.getSpawn().isNotSetup()) {
			sender.sendMessage(Config.errorPrefix + Localization.message("ERROR_GAME_SPAWN"));
			return;
		}
		if (map.isBoundsNotSetup()) {
			sender.sendMessage(Config.errorPrefix + Localization.message("ERROR_MAP_BOUNDS"));
			return;
		}
		sender.sendMessage(Config.messagePrefix + Localization.message("MAPSAVE_START"));
		sender.sendMessage(Config.warningPrefix + Localization.message("MAPSAVE_WARNING"));
		World world = map.getSpawn().load();
		if (world == null) {
			sender.sendMessage(Config.warningPrefix + Localization.message("MAPSAVE_FAIL_WORLD"));
			return;
		}
		world.save();
		BukkitRunnable runnable = new BukkitRunnable() {
			public void run() {
				sender.sendMessage(
						map.getWorldLoader().save()
						);
				runningBackup = false;
			}
		};
		runnable.runTaskAsynchronously(Main.getInstance());
		runningBackup = true;
	}

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

	public String getUsage() {
		return "<map>";
	}

	public String getDescription() {
		return "Saves the map to its own separate playable map";
	}

	public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) {
		if(parameter.equals("map")) {
			return Maps.getAllMaps().stream().map(Map::getName).collect(Collectors.toList());
		}
		return null;
	}

}