Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.41 KB | None | 0 0
  1. package de.evil.skywars.manager;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.UUID;
  8. import java.util.stream.Collectors;
  9.  
  10. import org.apache.commons.io.FilenameUtils;
  11. import org.apache.commons.lang3.exception.ExceptionUtils;
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.Location;
  14. import org.bukkit.entity.Player;
  15.  
  16. import de.evil.skywars.main.Main;
  17. import de.evil.skywars.manager.team.Team;
  18. import de.evil.skywars.status.GameStatusEnum;
  19. import de.sprax2013.advanced_dev_utils.math_and_numbers.RandomNumberUtils;
  20. import de.sprax2013.advanced_dev_utils.misc.UUIDUtils;
  21. import de.sprax2013.advanced_dev_utils.mojang.MojangAPI;
  22. import de.sprax2013.advanced_dev_utils.mojang.MojangProfile;
  23. import de.sprax2013.advanced_dev_utils.spigot.files.yaml.YAMLFile;
  24. import de.sprax2013.advanced_dev_utils.spigot.files.yaml.YAMLFileManager;
  25. import de.sprax2013.advanced_dev_utils.spigot.utils.location.LocationUtils;
  26. import net.lingala.zip4j.core.ZipFile;
  27. import net.lingala.zip4j.exception.ZipException;
  28. import net.lingala.zip4j.model.FileHeader;
  29. import net.md_5.bungee.api.ChatColor;
  30.  
  31. public class MapManager {
  32. static final File mapDir = new File(Main.getInstance().getDataFolder(), "maps");
  33. private static Location LobbySpawn, LobbyStats;
  34.  
  35. private static List<SkyWarsMap> maps = new ArrayList<>();
  36. private static SkyWarsMap currMap;
  37.  
  38. public static void init() {
  39. reload();
  40. }
  41.  
  42. public static void deInit() {
  43. changeMap(null);
  44. }
  45.  
  46. public static boolean reload() {
  47. boolean result = true;
  48.  
  49. LobbySpawn = LocationUtils.toLocation(Main.getCfg().getCfg().getString("LobbySpawn"));
  50. LobbyStats = LocationUtils.toLocation(Main.getCfg().getCfg().getString("LobbyStats"));
  51.  
  52. YAMLFileManager.removeFileFromCache(Main.getInstance(), Main.getCfg());
  53.  
  54. changeMap(null);
  55. reloadMapInfo();
  56.  
  57. return result;
  58. }
  59.  
  60. public static void reloadMapInfo() {
  61. mapDir.mkdirs();
  62.  
  63. final String prefix = ChatColor.stripColor(Main.prefix);
  64.  
  65. final File[] mapDirFiles = mapDir.listFiles();
  66. HashMap<File, File> mapStorage = new HashMap<>();
  67. if (mapDirFiles != null && mapDirFiles.length > 0) {
  68. for (File f : mapDirFiles) {
  69. if (f.isFile()) {
  70. String name = f.getName().toLowerCase();
  71.  
  72. if ((name.endsWith(".yml") || name.endsWith(".yaml")) && isValidSkyWarsYAML(f)) {
  73. File archive = null;
  74.  
  75. String base = FilenameUtils.getBaseName(name);
  76. for (File f2 : mapDirFiles) {
  77. if (f2.getName().equalsIgnoreCase(base + ".zip")) {
  78. archive = f2;
  79. break;
  80.  
  81. }
  82. }
  83.  
  84. if (archive != null && isValidSkyWarsArchive(archive)) {
  85. mapStorage.put(f, archive);
  86. } else {
  87. System.err.println(
  88. prefix + "Es wurde kein gültiges Map-Archiv für '" + f.getName() + "' gefunden!");
  89. }
  90. } else if (!name.endsWith(".zip")) {
  91. System.err.println(prefix + "Es wurde eine ungültige Map-YAML gefunden: " + f.getName());
  92. }
  93. } else {
  94. System.err.println(prefix + "Es wurde ein unbekannter Ordner gefunden: " + f.getAbsolutePath());
  95. }
  96. }
  97. }
  98.  
  99. if (mapStorage.isEmpty()) {
  100. GameStatusEnum.setCurrentStatus(GameStatusEnum.MAINTENANCE);
  101.  
  102. System.err.println(prefix + "Es wurden keine Maps gefunden!");
  103.  
  104. for (Player online : Bukkit.getOnlinePlayers()) {
  105. online.kickPlayer("§cDu wurdest automatisch vom Server getrennt");
  106. }
  107.  
  108. for (SkyWarsMap map : maps) {
  109. map.unload();
  110. }
  111.  
  112. maps.clear();
  113. } else {
  114. List<SkyWarsMap> newMaps = new ArrayList<>();
  115.  
  116. for (File mapCfg : mapStorage.keySet()) {
  117. YAMLFile yml = YAMLFileManager.getFile(Main.getInstance(), mapCfg);
  118.  
  119. newMaps.add(getSkyWarsMap(yml));
  120.  
  121. YAMLFileManager.removeFileFromCache(Main.getInstance(), yml);
  122. }
  123.  
  124. changeMap(newMaps.get(RandomNumberUtils.randomInteger(0, newMaps.size() - 1)));
  125.  
  126. maps = newMaps;
  127. }
  128. }
  129.  
  130. public static int getMapCount() {
  131. return maps.size();
  132. }
  133.  
  134. public static void changeMap(SkyWarsMap map) {
  135. if (map == null || !map.equals(getCurrentMap())) {
  136. if (map != null) {
  137. map.load();
  138. }
  139.  
  140. if (currMap != null && currMap.getWorld() != null) {
  141. for (Player inWorld : currMap.getWorld().getPlayers()) {
  142. if (map != null) {
  143. if (GameStatusEnum.getCurrentStatus() == GameStatusEnum.LOBBY) {
  144. inWorld.teleport(getLobbySpawn());
  145. } else {
  146. inWorld.teleport(map.getSpectatorSpawn());
  147. }
  148. } else {
  149. inWorld.kickPlayer("§cDu wurdest automatisch vom Server getrennt");
  150. }
  151. }
  152.  
  153. currMap.unload();
  154. }
  155.  
  156. currMap = map;
  157. if (currMap != null) {
  158. for (Player inWorld : currMap.getWorld().getPlayers()) {
  159. HologramManager.spawnPlayerHologram(inWorld);
  160. HologramManager.spawnTopHologram(inWorld);
  161. }
  162. }
  163.  
  164. ScoreBoardManager.updateAllPlayers();
  165. }
  166. }
  167.  
  168. public static SkyWarsMap getMapByName(String name) {
  169. for (SkyWarsMap map : maps) {
  170. if (map.getName().equalsIgnoreCase(name)) {
  171. return map;
  172. }
  173. }
  174. return null;
  175. }
  176.  
  177. public static List<String> getMapNames() {
  178. return maps.stream().map(map -> {
  179. return map.getName();
  180. }).collect(Collectors.toList());
  181. }
  182.  
  183. public static List<SkyWarsMap> getMaps() {
  184. return maps;
  185. }
  186.  
  187. public static SkyWarsMap getCurrentMap() {
  188. return currMap;
  189. }
  190.  
  191. public static SkyWarsMap getRandomMap(boolean excludeCurrMap) {
  192. if (getMapCount() > 1) {
  193. List<SkyWarsMap> possMaps = new ArrayList<>(maps);
  194. if (excludeCurrMap && getCurrentMap() != null) {
  195. possMaps.remove(getCurrentMap());
  196. }
  197.  
  198. return possMaps.get(RandomNumberUtils.randomInteger(0, possMaps.size() - 1));
  199. }
  200.  
  201. return maps.get(0);
  202. }
  203.  
  204. private static SkyWarsMap getSkyWarsMap(YAMLFile yaml) {
  205. final String errSuffix = Main.prefix + "§cBei der Map §6" + yaml.getFile().getName()
  206. + " §cist ein Fehler aufgetreten§7: §r";
  207. try {
  208. int minPlayersToStart = yaml.getCfg().getInt("Players.MinPlayersToStart"),
  209. teamSize = yaml.getCfg().getInt("Players.TeamSize");
  210.  
  211. if (minPlayersToStart >= 2) {
  212. /* [Start] Meta */
  213. String mapName = yaml.getCfg().getString("Meta.Name");
  214. List<String> builders = new ArrayList<>();
  215.  
  216. List<String> newBuilders = new ArrayList<>();
  217. boolean updatedBuilders = false;
  218. for (String s : yaml.getCfg().getStringList("Meta.Builders")) {
  219. if (s.contains(":")) {
  220. String[] args = s.split(":");
  221.  
  222. if (args[0].equalsIgnoreCase("NULL")
  223. || (Long.parseLong(args[2]) + (1_000 * 60 * 60 * 24 * 30)) /* +30 days */ < System
  224. .currentTimeMillis()) {
  225. MojangProfile p = MojangAPI.getProfile(UUID.fromString(UUIDUtils.addDashesToUUID(args[0])));
  226.  
  227. builders.add(p.getUsername());
  228. newBuilders.add(
  229. p.getUUID().toString() + ":" + p.getUsername() + ":" + System.currentTimeMillis());
  230. } else {
  231. builders.add(args[1]);
  232. newBuilders.add(s);
  233. }
  234. } else {
  235. try {
  236. UUID uuid = UUID.fromString(UUIDUtils.addDashesToUUID(s));
  237. MojangProfile p = MojangAPI.getProfile(uuid);
  238.  
  239. builders.add(p.getUsername());
  240. newBuilders.add(
  241. p.getUUID().toString() + ":" + p.getUsername() + ":" + System.currentTimeMillis());
  242. updatedBuilders = true;
  243. } catch (IllegalArgumentException ignore) {
  244. MojangProfile p = MojangAPI.getProfile(s);
  245.  
  246. if (p != null) {
  247. builders.add(p.getUsername());
  248. newBuilders.add(p.getUUID() + ":" + p.getUsername() + ":" + System.currentTimeMillis());
  249. updatedBuilders = true;
  250. } else {
  251. builders.add(s);
  252. newBuilders.add("NULL:" + s + ":" + System.currentTimeMillis());
  253. updatedBuilders = true;
  254. }
  255. }
  256. }
  257. }
  258. if (updatedBuilders) {
  259. yaml.getCfg().set("Meta.Builders", newBuilders);
  260. yaml.saveNonBoolean();
  261. }
  262. newBuilders = null;
  263.  
  264. HashMap<Team, Location> teamSpawn = new HashMap<>();
  265. /* [Start] Teams */
  266. for (String teamStr : yaml.getCfg().getConfigurationSection("Teams").getKeys(false)) {
  267. Team t = Team.getMatching(teamStr);
  268. if (t != null) {
  269. Location tSpawn = LocationUtils
  270. .toLocation(yaml.getCfg().getString("Teams." + teamStr + ".Spawn"));
  271. if (tSpawn == null) {
  272. Bukkit.getConsoleSender().sendMessage(
  273. errSuffix + "Ungültige Spawnkoordinaten für das Team '" + teamStr + "'");
  274. return null;
  275. }
  276. teamSpawn.put(t, tSpawn);
  277. }
  278. }
  279. /* [Start] Map */
  280. Location specLoc = LocationUtils.toLocation(yaml.getCfg().getString("Map.SpectatorSpawn"));
  281.  
  282. if (specLoc == null) {
  283. Bukkit.getConsoleSender().sendMessage(errSuffix + "Ungültige Zuschauerspawn-Koordinaten");
  284. return null;
  285. }
  286. return new SkyWarsMap(new File(mapDir, FilenameUtils.getBaseName(yaml.getFile().getName()) + ".zip"), mapName, builders, minPlayersToStart, teamSize, teamSpawn, specLoc);
  287. }
  288. Bukkit.getConsoleSender().sendMessage(errSuffix + "Ungültiger Wert für 'Players.MinPlayersToStart'");
  289. return null;
  290. } catch (Throwable th) {
  291. Bukkit.getConsoleSender().sendMessage(errSuffix + ExceptionUtils.getStackTrace(th));
  292. return null;
  293. }
  294. }
  295.  
  296. private static boolean isValidSkyWarsYAML(File f) {
  297. SkyWarsMap map = null;
  298. String fName = f.getName().toLowerCase();
  299. if (f.exists() && f.isFile() && (fName.endsWith(".yml") || fName.endsWith(".yaml"))) {
  300. YAMLFile yaml = YAMLFileManager.getFile(Main.getInstance(), f);
  301.  
  302. map = getSkyWarsMap(yaml);
  303.  
  304. YAMLFileManager.removeFileFromCache(Main.getInstance(), yaml);
  305. }
  306.  
  307. return map != null;
  308. }
  309.  
  310. private static boolean isValidSkyWarsArchive(File f) {
  311. if (f.exists() && f.isFile() && f.getName().toLowerCase().endsWith(".zip")) {
  312. try {
  313. ZipFile zip = new ZipFile(f);
  314.  
  315. if (zip.isValidZipFile()) {
  316. boolean validWorldArchive = false;
  317.  
  318. for (FileHeader fH : zip.getFileHeaders()) {
  319. if (fH.getFileName().equalsIgnoreCase("level.dat")) {
  320. validWorldArchive = true;
  321. break;
  322. }
  323. }
  324.  
  325. return validWorldArchive;
  326. }
  327. } catch (ZipException ex) {
  328. ex.printStackTrace();
  329. }
  330. }
  331. return false;
  332. }
  333. public static Location getLobbySpawn() {
  334. if(LobbySpawn.getWorld() == null) {
  335. LobbySpawn.setWorld(Bukkit.getWorlds().get(0));
  336. }
  337. return LobbySpawn;
  338. }
  339. public static Location getLobbyStats() {
  340. if(LobbyStats.getWorld() == null) {
  341. LobbyStats.setWorld(Bukkit.getWorlds().get(0));
  342. }
  343. return LobbyStats;
  344. }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement