Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hylandsmc.skyblock.island;
- import com.hylandsmc.skyblock.Skyblock;
- import com.hylandsmc.skyblock.files.FileManager;
- import com.hylandsmc.skyblock.files.IslandFile;
- import com.hylandsmc.skyblock.utils.ConsoleMessage;
- import com.sk89q.worldedit.EditSession;
- import com.sk89q.worldedit.WorldEdit;
- import com.sk89q.worldedit.WorldEditException;
- import com.sk89q.worldedit.bukkit.BukkitAdapter;
- import com.sk89q.worldedit.extent.clipboard.Clipboard;
- import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
- import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
- import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
- import com.sk89q.worldedit.function.operation.Operation;
- import com.sk89q.worldedit.function.operation.Operations;
- import com.sk89q.worldedit.math.BlockVector3;
- import com.sk89q.worldedit.session.ClipboardHolder;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.UUID;
- public class IslandManager {
- public static UUID getIslandUniqueId(IslandPlayer islandPlayer) {
- return IslandMap.playerIslandCache.get(islandPlayer.getUniqueId());
- }
- public static boolean playerHasIsland(IslandPlayer islandPlayer) {
- return IslandMap.playerIslandCache.containsKey(islandPlayer.getUniqueId());
- }
- public static Location getIslandLocation(IslandPlayer islandPlayer) {
- return new IslandFile(IslandFile.FileType.islandFileByPlayer, getIslandUniqueId(islandPlayer)).getLocation(IslandFile.IslandPath.islandLocation);
- }
- public static Location getIslandHomeLocation(IslandPlayer islandPlayer) {
- return new IslandFile(IslandFile.FileType.islandFileByPlayer, getIslandUniqueId(islandPlayer)).getLocation(IslandFile.IslandPath.islandHomeLocation);
- }
- public static Location getNextIslandLocation() {
- return new FileManager(FileManager.FileType.nextIslandLocation).loadFile().getLocation("Location");
- }
- public static void addPlayerToIslandCache(IslandPlayer islandPlayer, UUID islandUniqueId) {
- IslandMap.playerIslandCache.put(islandPlayer.getUniqueId(), islandUniqueId);
- }
- public static double getRadius(IslandPlayer islandPlayer) {
- double size = new IslandFile(IslandFile.FileType.islandFileByPlayer, getIslandUniqueId(islandPlayer)).getDouble(IslandFile.IslandPath.islandSize);
- return (size / 2) + 0.5;
- }
- public static void generateNewIsland(IslandPlayer islandPlayer) {
- Player player = islandPlayer.getPlayer();
- File file = new IslandFile(IslandFile.FileType.islandSchematic).getFile();
- com.sk89q.worldedit.world.World adaptedWorld = BukkitAdapter.adapt(Bukkit.getServer().getWorld("skyblock_world"));
- ClipboardFormat format = ClipboardFormats.findByFile(file);
- try (ClipboardReader reader = format.getReader(new FileInputStream(file))) {
- Clipboard clipboard = reader.read();
- try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(adaptedWorld,
- -1)) {
- IslandMap.playerIslandGenerationLocation.put(islandPlayer, getNextIslandLocation());
- updateNextIslandLocation();
- Location location = IslandMap.playerIslandGenerationLocation.get(islandPlayer);
- if (location.getX() > 0) {
- location.setX(location.getX() + 1);
- }
- Operation operation = new ClipboardHolder(clipboard).createPaste(editSession)
- .to(BlockVector3.at(location.getX(), location.getY(), location.getZ())).ignoreAirBlocks(true).build();
- //TODO remove logger?
- new ConsoleMessage("Created island at location: x: " + location.getX() + " y: " + location.getY() + " z: " + location.getZ()).lowPriority();
- try {
- Operations.complete(operation);
- editSession.flushSession();
- IslandFile.saveIslandFile(islandPlayer, location);
- Skyblock.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(Skyblock.getInstance(), () -> player.teleportAsync(new Location(Bukkit.getServer().getWorld("skyblock_world"), location.getX() + .5, location.getY(), location.getZ() + .5, location.getPitch(), location.getYaw())), 20L);
- IslandMap.playerIslandCache.remove(islandPlayer.getUniqueId());
- } catch (WorldEditException e) {
- player.sendMessage(ChatColor.RED + "OOPS! Something went wrong, please contact an administrator");
- e.printStackTrace();
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void updateNextIslandLocation() {
- FileConfiguration configuration = new FileManager(FileManager.FileType.nextIslandLocation).loadFile();
- Location location = getNextIslandLocation();
- if (location.getX() <= 10000) {
- location.setX(location.getX() + 300);
- configuration.set("Location", location);
- try {
- configuration.save(new FileManager(FileManager.FileType.nextIslandLocation).getFile());
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- location.setX(0);
- location.setZ(location.getZ() + 300);
- configuration.set("Location", location);
- try {
- configuration.save(new FileManager(FileManager.FileType.nextIslandLocation).getFile());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public File getIslandFile(UUID islandUUID) {
- return new File(Skyblock.getInstance().getDataFolder(), "/islands/" + islandUUID + ".yml");
- }
- public FileConfiguration loadIslandFile(UUID islandUUID) {
- return YamlConfiguration.loadConfiguration(getIslandFile(islandUUID));
- }
- public void loadIslandCache() {
- File[] files = new FileManager(FileManager.FileType.islandsFolder).getFile().listFiles();
- for (File file : files) {
- UUID uuid = UUID.fromString(file.getName().replace(".yml", ""));
- if (!loadIslandFile(uuid).getConfigurationSection(IslandFile.IslandPath.islandMembers.getPath()).getKeys(false).isEmpty()) {
- for (String playerUUID : loadIslandFile(uuid).getConfigurationSection(IslandFile.IslandPath.islandMembers.getPath()).getKeys(false)) {
- //islandUUIDS.add(uuid);
- IslandMap.playerIslandCache.put(UUID.fromString(playerUUID), uuid);
- }
- }
- }
- new ConsoleMessage("Player load cache complete - " + IslandMap.playerIslandCache.size() + " player islands have been loaded").lowPriority();
- //new ConsoleMessage("Island uuid load cache complete - " + islandUUIDS.size() + " island uuids have been loaded").lowPriority();
- }
- // public static void loadIslands() {
- // File[] files = new FileManager(FileManager.FileType.islandsFolder).getFile().listFiles();
- //
- // for (File file : files) {
- //
- // UUID uuid = UUID.fromString(file.getName().replace(".yml", ""));
- // IslandFile islandFile = new IslandFile(uuid);
- // if (!islandFile.loadIslandFile().getConfigurationSection(IslandFile.IslandPath.islandMembers.getPath()).getKeys(false).isEmpty()) {
- // for (String playerUUID : islandFile.loadIslandFile().getConfigurationSection(IslandFile.IslandPath.islandMembers.getPath()).getKeys(false)) {
- // //islandUUIDS.add(uuid);
- // IslandPlayer islandPlayer = new IslandPlayer(playerUUID);
- // new ConsoleMessage("Island player " + islandPlayer + " has been loaded").lowPriority();
- // IslandMap.playerIslandCache.put(islandPlayer.getUniqueId(), uuid);
- //
- // }
- // }
- // }
- // new ConsoleMessage("Player load cache complete - " + IslandMap.playerIslandCache.size() + " player islands have been loaded").lowPriority();
- // //new ConsoleMessage("Island uuid load cache complete - " + islandUUIDS.size() + " island uuids have been loaded").lowPriority();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement