Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class RandomSpawnManager {
  2.  
  3.     private final RandomSpawn plugin;
  4.     private final Logger logger;
  5.     private final Random random;
  6.     private final HashMap<UUID, List<Location<World>>> spawns;
  7.     private int min, max;
  8.  
  9.     RandomSpawnManager(RandomSpawn plugin) {
  10.         this.plugin = plugin;
  11.         this.logger = plugin.getLogger();
  12.         this.random = new Random();
  13.         this.spawns = new HashMap<>();
  14.     }
  15.  
  16.     void init(ConfigurationLoader<CommentedConfigurationNode> localLoader) {
  17.         try {
  18.             ConfigurationOptions options = ConfigurationOptions.defaults()
  19.                     .setShouldCopyDefaults(true);
  20.             CommentedConfigurationNode node = localLoader.load(options);
  21.             this.min = (int) node.getNode("spawnArea", "min").getValue(1500);
  22.             this.max = (int) node.getNode("spawnArea", "max").getValue(3000);
  23.             localLoader.save(node);
  24.         } catch (IOException e) {
  25.             this.logger.warn("Error saving default config!", e);
  26.         }
  27.     }
  28.  
  29.     public void generationSpawnLocations(Player p) {
  30.         List<Location<World>> locs = new ArrayList<>();
  31.  
  32.         for (int i = 0; i < 3; i++) {
  33.             Location<World> loc = this.getRandomLocation(p.getWorld());
  34.             locs.add(loc);
  35.             this.logger.info("loc: " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
  36.         }
  37.  
  38.         this.logger.info("p.setLocation(loc): " + p.setLocation(this.spawns.put(p.getUniqueId(), locs).get(0)));
  39.     }
  40.  
  41.     public void toSpawn(Player p, int index) {
  42.         this.logger.info("p: " + p.getName() + ", index: " + index);
  43.         this.logger.info("p.setLocation(loc): " + p.setLocation(this.spawns.get(p.getUniqueId()).get(index)));
  44.     }
  45.  
  46.     private Location<World> getRandomLocation(World world) {
  47.         int x = this.random.nextBoolean() ? this.random.nextInt(max + 1 - min) + min : -(this.random.nextInt(max + 1 - min) + min);
  48.         int z = this.random.nextBoolean() ? this.random.nextInt(max + 1 - min) + min : -(this.random.nextInt(max + 1 - min) + min);
  49.         Optional<Location<World>> sf = Sponge.getTeleportHelper().getSafeLocation(new Location<>(world, x, 90, z));
  50.         return sf.orElse(null);
  51.     }
  52.  
  53.     public Map<UUID, List<Location<World>>> getSpawns() {
  54.         return this.spawns;
  55.     }
  56. }