Advertisement
MrPixxima

IslandManager.java

Oct 22nd, 2020
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.69 KB | None | 0 0
  1. package com.hylandsmc.skyblock.island;
  2.  
  3. import com.hylandsmc.skyblock.Skyblock;
  4. import com.hylandsmc.skyblock.files.FileManager;
  5. import com.hylandsmc.skyblock.files.IslandFile;
  6. import com.hylandsmc.skyblock.utils.ConsoleMessage;
  7. import com.sk89q.worldedit.EditSession;
  8. import com.sk89q.worldedit.WorldEdit;
  9. import com.sk89q.worldedit.WorldEditException;
  10. import com.sk89q.worldedit.bukkit.BukkitAdapter;
  11. import com.sk89q.worldedit.extent.clipboard.Clipboard;
  12. import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
  13. import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
  14. import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
  15. import com.sk89q.worldedit.function.operation.Operation;
  16. import com.sk89q.worldedit.function.operation.Operations;
  17. import com.sk89q.worldedit.math.BlockVector3;
  18. import com.sk89q.worldedit.session.ClipboardHolder;
  19. import org.bukkit.Bukkit;
  20. import org.bukkit.ChatColor;
  21. import org.bukkit.Location;
  22. import org.bukkit.configuration.file.FileConfiguration;
  23. import org.bukkit.configuration.file.YamlConfiguration;
  24. import org.bukkit.entity.Player;
  25.  
  26. import java.io.File;
  27. import java.io.FileInputStream;
  28. import java.io.FileNotFoundException;
  29. import java.io.IOException;
  30. import java.util.UUID;
  31.  
  32. public class IslandManager {
  33.  
  34.     public static UUID getIslandUniqueId(IslandPlayer islandPlayer) {
  35.         return IslandMap.playerIslandCache.get(islandPlayer.getUniqueId());
  36.     }
  37.  
  38.     public static boolean playerHasIsland(IslandPlayer islandPlayer) {
  39.         return IslandMap.playerIslandCache.containsKey(islandPlayer.getUniqueId());
  40.     }
  41.  
  42.     public static Location getIslandLocation(IslandPlayer islandPlayer) {
  43.         return new IslandFile(IslandFile.FileType.islandFileByPlayer, getIslandUniqueId(islandPlayer)).getLocation(IslandFile.IslandPath.islandLocation);
  44.     }
  45.  
  46.     public static Location getIslandHomeLocation(IslandPlayer islandPlayer) {
  47.         return new IslandFile(IslandFile.FileType.islandFileByPlayer, getIslandUniqueId(islandPlayer)).getLocation(IslandFile.IslandPath.islandHomeLocation);
  48.     }
  49.  
  50.     public static Location getNextIslandLocation() {
  51.         return new FileManager(FileManager.FileType.nextIslandLocation).loadFile().getLocation("Location");
  52.     }
  53.  
  54.     public static void addPlayerToIslandCache(IslandPlayer islandPlayer, UUID islandUniqueId) {
  55.         IslandMap.playerIslandCache.put(islandPlayer.getUniqueId(), islandUniqueId);
  56.     }
  57.  
  58.     public static double getRadius(IslandPlayer islandPlayer) {
  59.         double size = new IslandFile(IslandFile.FileType.islandFileByPlayer, getIslandUniqueId(islandPlayer)).getDouble(IslandFile.IslandPath.islandSize);
  60.         return (size / 2) + 0.5;
  61.     }
  62.  
  63.     public static void generateNewIsland(IslandPlayer islandPlayer) {
  64.  
  65.         Player player = islandPlayer.getPlayer();
  66.  
  67.         File file = new IslandFile(IslandFile.FileType.islandSchematic).getFile();
  68.  
  69.         com.sk89q.worldedit.world.World adaptedWorld = BukkitAdapter.adapt(Bukkit.getServer().getWorld("skyblock_world"));
  70.  
  71.         ClipboardFormat format = ClipboardFormats.findByFile(file);
  72.  
  73.         try (ClipboardReader reader = format.getReader(new FileInputStream(file))) {
  74.  
  75.             Clipboard clipboard = reader.read();
  76.  
  77.             try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(adaptedWorld,
  78.                     -1)) {
  79.  
  80.                 IslandMap.playerIslandGenerationLocation.put(islandPlayer, getNextIslandLocation());
  81.                 updateNextIslandLocation();
  82.                 Location location = IslandMap.playerIslandGenerationLocation.get(islandPlayer);
  83.  
  84.                 if (location.getX() > 0) {
  85.                     location.setX(location.getX() + 1);
  86.                 }
  87.  
  88.                 Operation operation = new ClipboardHolder(clipboard).createPaste(editSession)
  89.                         .to(BlockVector3.at(location.getX(), location.getY(), location.getZ())).ignoreAirBlocks(true).build();
  90.                 //TODO remove logger?
  91.                 new ConsoleMessage("Created island at location: x: " + location.getX() + " y: " + location.getY() + " z: " + location.getZ()).lowPriority();
  92.  
  93.                 try {
  94.                     Operations.complete(operation);
  95.                     editSession.flushSession();
  96.                     IslandFile.saveIslandFile(islandPlayer, location);
  97.                     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);
  98.                     IslandMap.playerIslandCache.remove(islandPlayer.getUniqueId());
  99.                 } catch (WorldEditException e) {
  100.                     player.sendMessage(ChatColor.RED + "OOPS! Something went wrong, please contact an administrator");
  101.                     e.printStackTrace();
  102.                 }
  103.             }
  104.  
  105.         } catch (FileNotFoundException e) {
  106.             e.printStackTrace();
  107.         } catch (IOException e) {
  108.             e.printStackTrace();
  109.         }
  110.  
  111.     }
  112.  
  113.     public static void updateNextIslandLocation() {
  114.         FileConfiguration configuration = new FileManager(FileManager.FileType.nextIslandLocation).loadFile();
  115.  
  116.         Location location = getNextIslandLocation();
  117.         if (location.getX() <= 10000) {
  118.             location.setX(location.getX() + 300);
  119.  
  120.             configuration.set("Location", location);
  121.  
  122.             try {
  123.                 configuration.save(new FileManager(FileManager.FileType.nextIslandLocation).getFile());
  124.             } catch (IOException e) {
  125.                 e.printStackTrace();
  126.             }
  127.  
  128.         } else {
  129.             location.setX(0);
  130.             location.setZ(location.getZ() + 300);
  131.  
  132.             configuration.set("Location", location);
  133.  
  134.             try {
  135.                 configuration.save(new FileManager(FileManager.FileType.nextIslandLocation).getFile());
  136.             } catch (IOException e) {
  137.                 e.printStackTrace();
  138.             }
  139.  
  140.         }
  141.  
  142.     }
  143.  
  144.     public File getIslandFile(UUID islandUUID) {
  145.         return new File(Skyblock.getInstance().getDataFolder(), "/islands/" + islandUUID + ".yml");
  146.     }
  147.     public FileConfiguration loadIslandFile(UUID islandUUID) {
  148.         return YamlConfiguration.loadConfiguration(getIslandFile(islandUUID));
  149.     }
  150.  
  151.  
  152.     public void loadIslandCache() {
  153.         File[] files = new FileManager(FileManager.FileType.islandsFolder).getFile().listFiles();
  154.  
  155.         for (File file : files) {
  156.  
  157.             UUID uuid = UUID.fromString(file.getName().replace(".yml", ""));
  158.  
  159.             if (!loadIslandFile(uuid).getConfigurationSection(IslandFile.IslandPath.islandMembers.getPath()).getKeys(false).isEmpty()) {
  160.                 for (String playerUUID : loadIslandFile(uuid).getConfigurationSection(IslandFile.IslandPath.islandMembers.getPath()).getKeys(false)) {
  161.                     //islandUUIDS.add(uuid);
  162.                     IslandMap.playerIslandCache.put(UUID.fromString(playerUUID), uuid);
  163.                 }
  164.             }
  165.         }
  166.         new ConsoleMessage("Player load cache complete - " + IslandMap.playerIslandCache.size() + " player islands have been loaded").lowPriority();
  167.         //new ConsoleMessage("Island uuid load cache complete - " + islandUUIDS.size() + " island uuids have been loaded").lowPriority();
  168.     }
  169.  
  170. //    public static void loadIslands() {
  171. //        File[] files = new FileManager(FileManager.FileType.islandsFolder).getFile().listFiles();
  172. //
  173. //        for (File file : files) {
  174. //
  175. //            UUID uuid = UUID.fromString(file.getName().replace(".yml", ""));
  176. //            IslandFile islandFile = new IslandFile(uuid);
  177. //            if (!islandFile.loadIslandFile().getConfigurationSection(IslandFile.IslandPath.islandMembers.getPath()).getKeys(false).isEmpty()) {
  178. //                for (String playerUUID : islandFile.loadIslandFile().getConfigurationSection(IslandFile.IslandPath.islandMembers.getPath()).getKeys(false)) {
  179. //                    //islandUUIDS.add(uuid);
  180. //                    IslandPlayer islandPlayer = new IslandPlayer(playerUUID);
  181. //                    new ConsoleMessage("Island player " + islandPlayer + " has been loaded").lowPriority();
  182. //                    IslandMap.playerIslandCache.put(islandPlayer.getUniqueId(), uuid);
  183. //
  184. //                }
  185. //            }
  186. //        }
  187. //        new ConsoleMessage("Player load cache complete - " + IslandMap.playerIslandCache.size() + " player islands have been loaded").lowPriority();
  188. //        //new ConsoleMessage("Island uuid load cache complete - " + islandUUIDS.size() + " island uuids have been loaded").lowPriority();
  189. //    }
  190.  
  191.  
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement