Newer
Older
ChallengesJoinEntities / src / main / java / de / fanta / challengesjoinentities / Server.java
package de.fanta.challengesjoinentities;

import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Server {
    public static HashMap<Server, UUID> serverPlayers = new HashMap<>();

    private final ChallengesJoinEntities plugin;

    private final String name;

    private final String gPLocation;

    private final String dir;

    private final ServerType serverType;

    private final Location piglinLocation;

    private boolean online;

    public Server(String name, String gPLocation, String dir, ServerType serverType, ChallengesJoinEntities plugin, Location piglinLocation) {
        this.name = name;
        this.gPLocation = gPLocation;
        this.dir = dir;
        this.serverType = serverType;
        this.plugin = plugin;
        this.online = (plugin.getGlobalDataHelper().getServer(name) != null);
        this.piglinLocation = piglinLocation;
    }

    public static Server getServerfromPlayer(Player p) {
        Server server = null;
        if (serverPlayers.containsValue(p.getUniqueId()))
            for (Map.Entry<Server, UUID> entry : serverPlayers.entrySet()) {
                if (entry.getValue().equals(p.getUniqueId()))
                    server = entry.getKey();
            }
        return server;
    }

    public boolean isOnline() {
        return this.online;
    }

    public void setOnline(boolean online) {
        this.online = online;
    }

    public String getName() {
        return this.name;
    }

    public String getGPLocation() {
        return this.gPLocation;
    }

    public void loadSaved(Player p, Boolean isAutosave) {
        this.online = true;
        serverPlayers.put(this, p.getUniqueId());
        UUID uuid = p.getUniqueId();
        File dir;
        if (isAutosave) {
            dir = new File("/home/minecraft/" + this.serverType.getDir() + "/" + uuid + "/autosave");
        } else {
            dir = new File("/home/minecraft/" + this.serverType.getDir() + "/" + uuid + "/1");
        }
        if (!dir.isDirectory()) {
            ChatUtil.sendErrorMessage(p, "Du hast noch keine Map gespeichert!");
            this.online = false;
            serverPlayers.remove(this);
            return;
        }

        if (isAutosave) {
            load(p, this.serverType.getDir(), uuid + "/autosave", getMapVersion(new File(dir + "/Challenges/serverconfig.yml"))); //TODO LOAD VERSION FROM ADVENTURE CONFIG
        } else {
            load(p, this.serverType.getDir(), uuid + "/1", getMapVersion(new File(dir + "/Challenges/serverconfig.yml"))); //TODO LOAD VERSION FROM ADVENTURE CONFIG
        }
    }

    public void loadSaved(Player p, String mapname, Boolean isAutosave) {
        this.online = true;
        serverPlayers.put(this, p.getUniqueId());
        File dir;
        if (isAutosave) {
            dir = new File("/home/minecraft/" + this.serverType.getDir() + "/" + mapname + "/autosave");
            mapname = mapname + "/autosave";
        } else {
            dir = new File("/home/minecraft/" + this.serverType.getDir() + "/" + mapname + "/1");
            mapname = mapname + "/1";
        }

        if (!dir.isDirectory()) {
            ChatUtil.sendErrorMessage(p, "Map " + mapname + " nicht gefunden!");
            this.online = false;
            serverPlayers.remove(this);
            return;
        }
        load(p, this.serverType.getDir(), mapname, getMapVersion(new File(dir + "/Challenges/serverconfig.yml"))); //TODO LOAD VERSION FROM ADVENTURE CONFIG
    }

    public void loadNewAdventure(Player player, String category, String map) {
        this.online = true;
        serverPlayers.put(this, player.getUniqueId());
        File dir = new File("/home/minecraft/Adventure-Maps", category + "/" + map);
        if (!dir.isDirectory()) {
            ChatUtil.sendErrorMessage(player, "Map " + map + " nicht gefunden!");
            this.online = false;
            serverPlayers.remove(this);
            return;
        }
        load(player, "Adventure-Maps", category + "/" + map, getMapVersion(new File(dir + "/Challenges/serverconfig.yml"))); //TODO LOAD VERSION FROM ADVENTURE CONFIG
    }

    public void load(Player p, String serverTypeDir, String targetDir, String ServerVersion) {
        File configs = new File("/home/minecraft/" + this.dir + "/plugins/Challenges");
        File end = new File("/home/minecraft/" + this.dir + "/world_the_end");
        File nether = new File("/home/minecraft/" + this.dir + "/world_nether");
        File world = new File("/home/minecraft/" + this.dir + "/world");
        File mlgworld = new File("/home/minecraft/" + this.dir + "/mlg_challenge");
        File serverJar = new File("/home/minecraft/" + this.dir + "/paper.jar");
        File saveServerJar = new File("/home/minecraft/ServerJars/" + ServerVersion + "/paper.jar");
        File saveend = new File("/home/minecraft/" + serverTypeDir + "/" + targetDir + "/world_the_end");
        File savenether = new File("/home/minecraft/" + serverTypeDir + "/" + targetDir + "/world_nether");
        File saveworld = new File("/home/minecraft/" + serverTypeDir + "/" + targetDir + "/world");
        File saveconfigs = new File("/home/minecraft/" + serverTypeDir + "/" + targetDir + "/Challenges");
        ChatUtil.sendNormalMessage(p, "Versuche Map zu laden!");
        try {
            if (world.isDirectory()) {
                FileUtils.deleteDirectory(world);
                FileUtils.deleteDirectory(end);
                FileUtils.deleteDirectory(nether);
            }
            if (mlgworld.isDirectory()){
                FileUtils.deleteDirectory(mlgworld);
            }
            if (serverJar.exists()) {
                FileUtils.delete(serverJar);
            }
            FileUtils.copyFile(saveServerJar, serverJar);
            FileUtils.copyDirectory(saveconfigs, configs);
            FileUtils.copyDirectory(saveworld, world);
            FileUtils.copyDirectory(savenether, nether);
            FileUtils.copyDirectory(saveend, end);
        } catch (IOException e) {
            e.printStackTrace();
            ChatUtil.sendErrorMessage(p, "Map konnte nicht geladen werden!");
            this.online = false;
            serverPlayers.remove(this);
            return;
        }
        ChatUtil.sendNormalMessage(p, "Map wurde geladen! Server wird nun gestartet!");
        start(p);
    }

    public void start(Player player) {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("screen", "-AmdS", player.getName(), "./start.sh").directory(new File("/home/minecraft/" + this.dir));
        try {
            processBuilder.start();
            Bukkit.getLogger().info(this.serverType.toString().toLowerCase() + " Server: " + getServerfromPlayer(player).gPLocation + " wurde von " + player.getName() + " gestartet!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void spawnPiglin(Player player) {
        this.plugin.spawnPiglin(this.piglinLocation, this.name, this.gPLocation, player.getName(), false);
    }

    public void despawnPiglin() {
        this.plugin.despawnPiglin(this.name);
    }

    public void spawnNPC(Player player) {
        this.plugin.spawnNPC(this.piglinLocation, this.name, this.gPLocation, player.getName(), false);
    }

    public void despawnNPC() {
        this.plugin.despawnNPC(this.name);
    }

    public String getMapVersion(File config) {
        String serverVersion;
        YamlConfiguration serverConfiguration = new YamlConfiguration();
        if (!config.exists()) {
            serverVersion = "latest";
            return serverVersion;
        }
        try {
            serverConfiguration.load(config);
        } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) {
            e.printStackTrace();
        }
        String configServerVersion = serverConfiguration.getString("serverversion");
        File serverversionfolder = new File("/home/minecraft/ServerJars/" + configServerVersion);
        if (configServerVersion != null) {
            if (serverversionfolder.isDirectory()) {
                serverVersion = configServerVersion;
            } else {
                serverVersion = "latest";
            }
        } else {
            serverVersion = "latest";
        }
        System.out.println(serverVersion);
        return serverVersion;
    }
}