Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.evil.skywars.manager;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.UUID;
- import java.util.stream.Collectors;
- import org.apache.commons.io.FilenameUtils;
- import org.apache.commons.lang3.exception.ExceptionUtils;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.entity.Player;
- import de.evil.skywars.main.Main;
- import de.evil.skywars.manager.team.Team;
- import de.evil.skywars.status.GameStatusEnum;
- import de.sprax2013.advanced_dev_utils.math_and_numbers.RandomNumberUtils;
- import de.sprax2013.advanced_dev_utils.misc.UUIDUtils;
- import de.sprax2013.advanced_dev_utils.mojang.MojangAPI;
- import de.sprax2013.advanced_dev_utils.mojang.MojangProfile;
- import de.sprax2013.advanced_dev_utils.spigot.files.yaml.YAMLFile;
- import de.sprax2013.advanced_dev_utils.spigot.files.yaml.YAMLFileManager;
- import de.sprax2013.advanced_dev_utils.spigot.utils.location.LocationUtils;
- import net.lingala.zip4j.core.ZipFile;
- import net.lingala.zip4j.exception.ZipException;
- import net.lingala.zip4j.model.FileHeader;
- import net.md_5.bungee.api.ChatColor;
- public class MapManager {
- static final File mapDir = new File(Main.getInstance().getDataFolder(), "maps");
- private static Location LobbySpawn, LobbyStats;
- private static List<SkyWarsMap> maps = new ArrayList<>();
- private static SkyWarsMap currMap;
- public static void init() {
- reload();
- }
- public static void deInit() {
- changeMap(null);
- }
- public static boolean reload() {
- boolean result = true;
- LobbySpawn = LocationUtils.toLocation(Main.getCfg().getCfg().getString("LobbySpawn"));
- LobbyStats = LocationUtils.toLocation(Main.getCfg().getCfg().getString("LobbyStats"));
- YAMLFileManager.removeFileFromCache(Main.getInstance(), Main.getCfg());
- changeMap(null);
- reloadMapInfo();
- return result;
- }
- public static void reloadMapInfo() {
- mapDir.mkdirs();
- final String prefix = ChatColor.stripColor(Main.prefix);
- final File[] mapDirFiles = mapDir.listFiles();
- HashMap<File, File> mapStorage = new HashMap<>();
- if (mapDirFiles != null && mapDirFiles.length > 0) {
- for (File f : mapDirFiles) {
- if (f.isFile()) {
- String name = f.getName().toLowerCase();
- if ((name.endsWith(".yml") || name.endsWith(".yaml")) && isValidSkyWarsYAML(f)) {
- File archive = null;
- String base = FilenameUtils.getBaseName(name);
- for (File f2 : mapDirFiles) {
- if (f2.getName().equalsIgnoreCase(base + ".zip")) {
- archive = f2;
- break;
- }
- }
- if (archive != null && isValidSkyWarsArchive(archive)) {
- mapStorage.put(f, archive);
- } else {
- System.err.println(
- prefix + "Es wurde kein gültiges Map-Archiv für '" + f.getName() + "' gefunden!");
- }
- } else if (!name.endsWith(".zip")) {
- System.err.println(prefix + "Es wurde eine ungültige Map-YAML gefunden: " + f.getName());
- }
- } else {
- System.err.println(prefix + "Es wurde ein unbekannter Ordner gefunden: " + f.getAbsolutePath());
- }
- }
- }
- if (mapStorage.isEmpty()) {
- GameStatusEnum.setCurrentStatus(GameStatusEnum.MAINTENANCE);
- System.err.println(prefix + "Es wurden keine Maps gefunden!");
- for (Player online : Bukkit.getOnlinePlayers()) {
- online.kickPlayer("§cDu wurdest automatisch vom Server getrennt");
- }
- for (SkyWarsMap map : maps) {
- map.unload();
- }
- maps.clear();
- } else {
- List<SkyWarsMap> newMaps = new ArrayList<>();
- for (File mapCfg : mapStorage.keySet()) {
- YAMLFile yml = YAMLFileManager.getFile(Main.getInstance(), mapCfg);
- newMaps.add(getSkyWarsMap(yml));
- YAMLFileManager.removeFileFromCache(Main.getInstance(), yml);
- }
- changeMap(newMaps.get(RandomNumberUtils.randomInteger(0, newMaps.size() - 1)));
- maps = newMaps;
- }
- }
- public static int getMapCount() {
- return maps.size();
- }
- public static void changeMap(SkyWarsMap map) {
- if (map == null || !map.equals(getCurrentMap())) {
- if (map != null) {
- map.load();
- }
- if (currMap != null && currMap.getWorld() != null) {
- for (Player inWorld : currMap.getWorld().getPlayers()) {
- if (map != null) {
- if (GameStatusEnum.getCurrentStatus() == GameStatusEnum.LOBBY) {
- inWorld.teleport(getLobbySpawn());
- } else {
- inWorld.teleport(map.getSpectatorSpawn());
- }
- } else {
- inWorld.kickPlayer("§cDu wurdest automatisch vom Server getrennt");
- }
- }
- currMap.unload();
- }
- currMap = map;
- if (currMap != null) {
- for (Player inWorld : currMap.getWorld().getPlayers()) {
- HologramManager.spawnPlayerHologram(inWorld);
- HologramManager.spawnTopHologram(inWorld);
- }
- }
- ScoreBoardManager.updateAllPlayers();
- }
- }
- public static SkyWarsMap getMapByName(String name) {
- for (SkyWarsMap map : maps) {
- if (map.getName().equalsIgnoreCase(name)) {
- return map;
- }
- }
- return null;
- }
- public static List<String> getMapNames() {
- return maps.stream().map(map -> {
- return map.getName();
- }).collect(Collectors.toList());
- }
- public static List<SkyWarsMap> getMaps() {
- return maps;
- }
- public static SkyWarsMap getCurrentMap() {
- return currMap;
- }
- public static SkyWarsMap getRandomMap(boolean excludeCurrMap) {
- if (getMapCount() > 1) {
- List<SkyWarsMap> possMaps = new ArrayList<>(maps);
- if (excludeCurrMap && getCurrentMap() != null) {
- possMaps.remove(getCurrentMap());
- }
- return possMaps.get(RandomNumberUtils.randomInteger(0, possMaps.size() - 1));
- }
- return maps.get(0);
- }
- private static SkyWarsMap getSkyWarsMap(YAMLFile yaml) {
- final String errSuffix = Main.prefix + "§cBei der Map §6" + yaml.getFile().getName()
- + " §cist ein Fehler aufgetreten§7: §r";
- try {
- int minPlayersToStart = yaml.getCfg().getInt("Players.MinPlayersToStart"),
- teamSize = yaml.getCfg().getInt("Players.TeamSize");
- if (minPlayersToStart >= 2) {
- /* [Start] Meta */
- String mapName = yaml.getCfg().getString("Meta.Name");
- List<String> builders = new ArrayList<>();
- List<String> newBuilders = new ArrayList<>();
- boolean updatedBuilders = false;
- for (String s : yaml.getCfg().getStringList("Meta.Builders")) {
- if (s.contains(":")) {
- String[] args = s.split(":");
- if (args[0].equalsIgnoreCase("NULL")
- || (Long.parseLong(args[2]) + (1_000 * 60 * 60 * 24 * 30)) /* +30 days */ < System
- .currentTimeMillis()) {
- MojangProfile p = MojangAPI.getProfile(UUID.fromString(UUIDUtils.addDashesToUUID(args[0])));
- builders.add(p.getUsername());
- newBuilders.add(
- p.getUUID().toString() + ":" + p.getUsername() + ":" + System.currentTimeMillis());
- } else {
- builders.add(args[1]);
- newBuilders.add(s);
- }
- } else {
- try {
- UUID uuid = UUID.fromString(UUIDUtils.addDashesToUUID(s));
- MojangProfile p = MojangAPI.getProfile(uuid);
- builders.add(p.getUsername());
- newBuilders.add(
- p.getUUID().toString() + ":" + p.getUsername() + ":" + System.currentTimeMillis());
- updatedBuilders = true;
- } catch (IllegalArgumentException ignore) {
- MojangProfile p = MojangAPI.getProfile(s);
- if (p != null) {
- builders.add(p.getUsername());
- newBuilders.add(p.getUUID() + ":" + p.getUsername() + ":" + System.currentTimeMillis());
- updatedBuilders = true;
- } else {
- builders.add(s);
- newBuilders.add("NULL:" + s + ":" + System.currentTimeMillis());
- updatedBuilders = true;
- }
- }
- }
- }
- if (updatedBuilders) {
- yaml.getCfg().set("Meta.Builders", newBuilders);
- yaml.saveNonBoolean();
- }
- newBuilders = null;
- HashMap<Team, Location> teamSpawn = new HashMap<>();
- /* [Start] Teams */
- for (String teamStr : yaml.getCfg().getConfigurationSection("Teams").getKeys(false)) {
- Team t = Team.getMatching(teamStr);
- if (t != null) {
- Location tSpawn = LocationUtils
- .toLocation(yaml.getCfg().getString("Teams." + teamStr + ".Spawn"));
- if (tSpawn == null) {
- Bukkit.getConsoleSender().sendMessage(
- errSuffix + "Ungültige Spawnkoordinaten für das Team '" + teamStr + "'");
- return null;
- }
- teamSpawn.put(t, tSpawn);
- }
- }
- /* [Start] Map */
- Location specLoc = LocationUtils.toLocation(yaml.getCfg().getString("Map.SpectatorSpawn"));
- if (specLoc == null) {
- Bukkit.getConsoleSender().sendMessage(errSuffix + "Ungültige Zuschauerspawn-Koordinaten");
- return null;
- }
- return new SkyWarsMap(new File(mapDir, FilenameUtils.getBaseName(yaml.getFile().getName()) + ".zip"), mapName, builders, minPlayersToStart, teamSize, teamSpawn, specLoc);
- }
- Bukkit.getConsoleSender().sendMessage(errSuffix + "Ungültiger Wert für 'Players.MinPlayersToStart'");
- return null;
- } catch (Throwable th) {
- Bukkit.getConsoleSender().sendMessage(errSuffix + ExceptionUtils.getStackTrace(th));
- return null;
- }
- }
- private static boolean isValidSkyWarsYAML(File f) {
- SkyWarsMap map = null;
- String fName = f.getName().toLowerCase();
- if (f.exists() && f.isFile() && (fName.endsWith(".yml") || fName.endsWith(".yaml"))) {
- YAMLFile yaml = YAMLFileManager.getFile(Main.getInstance(), f);
- map = getSkyWarsMap(yaml);
- YAMLFileManager.removeFileFromCache(Main.getInstance(), yaml);
- }
- return map != null;
- }
- private static boolean isValidSkyWarsArchive(File f) {
- if (f.exists() && f.isFile() && f.getName().toLowerCase().endsWith(".zip")) {
- try {
- ZipFile zip = new ZipFile(f);
- if (zip.isValidZipFile()) {
- boolean validWorldArchive = false;
- for (FileHeader fH : zip.getFileHeaders()) {
- if (fH.getFileName().equalsIgnoreCase("level.dat")) {
- validWorldArchive = true;
- break;
- }
- }
- return validWorldArchive;
- }
- } catch (ZipException ex) {
- ex.printStackTrace();
- }
- }
- return false;
- }
- public static Location getLobbySpawn() {
- if(LobbySpawn.getWorld() == null) {
- LobbySpawn.setWorld(Bukkit.getWorlds().get(0));
- }
- return LobbySpawn;
- }
- public static Location getLobbyStats() {
- if(LobbyStats.getWorld() == null) {
- LobbyStats.setWorld(Bukkit.getWorlds().get(0));
- }
- return LobbyStats;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement