Newer
Older
ChallengeSystem / src / main / java / de / fanta / challenges / utils / SaveWorldUtils.java
package de.fanta.challenges.utils;

import de.cubeside.nmsutils.NMSUtils;
import de.fanta.challenges.Challenges;
import de.fanta.challenges.challenges.AllItemsChallenge;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;

public class SaveWorldUtils {

    private static final Challenges plugin = Challenges.getPlugin();

    public static void saveWorld(String uuid) {
        Challenges.getPlugin().getConfig().set("timertime", Challenges.getPlugin().getTimer().getTime());
        Challenges.getPlugin().saveConfig();
        Challenges.getPlugin().getConfig().set("backpack_size", Challenges.getPlugin().getBackpack().getSize() / 9);
        Challenges.getPlugin().saveConfig();
        Challenges.getPlugin().getBackpack().saveInventoryToConfig();
        AllItemsChallenge.saveItems();
        World world_world = Bukkit.getWorlds().get(0);
        World world_nether = Bukkit.getWorlds().get(1);
        World world_end = Bukkit.getWorlds().get(2);
        world_world.save();
        world_nether.save();
        world_end.save();
        for (Player p : Bukkit.getOnlinePlayers()) {
            p.saveData();
        }
        NMSUtils nms = plugin.getNMSUtils();
        if (nms != null) {
            nms.getWorldUtils().saveWorldNow(world_world);
            nms.getWorldUtils().saveWorldNow(world_end);
            nms.getWorldUtils().saveWorldNow(world_nether);
        }
        File dir = new File(uuid);
        File configs = new File("plugins/Challenges");
        File end = new File("world_the_end");
        File nether = new File("world_nether");
        File world = new File("world");
        File saveend = new File(uuid + "/world_the_end");
        File savenether = new File(uuid + "/world_nether");
        File saveworld = new File(uuid + "/world");
        File saveconfigs = new File(uuid + "/Challenges");
        if (Bukkit.getMotd().equals("Challenge")) {
            File loaddir = new File("/home/minecraft/saves/" + uuid);
            if (loaddir.isDirectory()) {
                loaddir.delete();
            }
                try {
                    FileUtils.forceMkdir(dir);
                    FileUtils.copyDirectory(configs, saveconfigs);
                    FileUtils.copyDirectory(end, saveend);
                    FileUtils.copyDirectory(nether, savenether);
                    FileUtils.copyDirectory(world, saveworld);
                    if (loaddir.isDirectory()) {
                        FileUtils.deleteDirectory(loaddir);
                    }
                    FileUtils.moveDirectory(dir, loaddir);
                } catch (IOException ex) {
                    Bukkit.getLogger().log(Level.SEVERE, "Could not save world ", ex);
                }
        } else if (Bukkit.getMotd().equals("Adventure")) {
            File loaddir = new File("/home/minecraft/Adventure-saves/" + uuid);
            if (loaddir.isDirectory()) {
                loaddir.delete();
            }
            try {
                FileUtils.forceMkdir(dir);
                FileUtils.copyDirectory(configs, saveconfigs);
                FileUtils.copyDirectory(end, saveend);
                FileUtils.copyDirectory(nether, savenether);
                FileUtils.copyDirectory(world, saveworld);
                if (dir.isDirectory()) {
                    FileUtils.deleteDirectory(loaddir);
                }
                FileUtils.moveDirectory(dir, loaddir);
            } catch (IOException ex) {
                Bukkit.getLogger().log(Level.SEVERE, "Could not save world ", ex);
                ChatUtil.sendErrorMessage(Bukkit.getPlayer(uuid), "Map konnte nicht richtig gespeichert werden, bitte versuche es noch einmal");
            }
        } else {
            throw new RuntimeException("Motd stimmt nicht überein!");
        }
    }
}