Newer
Older
ChallengeSystem / src / main / java / de / fanta / challenges / utils / SaveWorldUtils.java
@fantahund fantahund on 9 Sep 2020 2 KB Save World
package de.fanta.challenges.utils;

import de.fanta.challenges.Challenges;
import net.minecraft.server.v1_16_R2.WorldServer;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_16_R2.CraftWorld;
import org.bukkit.entity.Player;

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

public class SaveWorldUtils {

    public static void saveWorld(UUID uuid) {
        Challenges.getPlugin().getConfig().set("timertime", Challenges.getPlugin().getTimer().getTime());
        Challenges.getPlugin().saveConfig();
        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();
        CraftWorld craftWorld = (CraftWorld) world_world;
        WorldServer handle = craftWorld.getHandle();
        CraftWorld craftEnd = (CraftWorld) world_end;
        WorldServer handleend = craftEnd.getHandle();
        CraftWorld craftNether = (CraftWorld) world_nether;
        WorldServer handlenether = craftNether.getHandle();
        for (Player p : Bukkit.getOnlinePlayers()) {
            p.saveData();
        }
        try {
            handle.save(null, true, false);
            handleend.save(null, true, false);
            handlenether.save(null, true, false);
        } catch (Exception e) {
            Bukkit.getLogger().log(Level.SEVERE, "Exception while saving world", e);
        }
        File dir = new File(uuid.toString());
        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.toString() + "/world_the_end");
        File savenether = new File(uuid.toString() + "/world_nether");
        File saveworld = new File(uuid.toString() + "/world");
        File saveconfigs = new File(uuid.toString() + "/Challenges");
        File loaddir = new File("/home/minecraft/saves/" + uuid);
        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) {
            ex.printStackTrace();
        }

    }

}