Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class RandomSpawnManager {
  2.  
  3.     private static final List<BlockType> unsafeBody = ImmutableList.of(
  4.             BlockTypes.AIR,
  5.             BlockTypes.CACTUS,
  6.             BlockTypes.FIRE,
  7.             BlockTypes.LAVA,
  8.             BlockTypes.FLOWING_LAVA,
  9.             BlockTypes.WATER,
  10.             BlockTypes.FLOWING_WATER
  11.     );
  12.  
  13.     private final RandomSpawn plugin;
  14.     private final Logger logger;
  15.     private final Random random;
  16.     private final HashMap<String, List<Location<World>>> spawns;
  17.     private int maxArea;
  18.  
  19.     RandomSpawnManager(RandomSpawn plugin) {
  20.         this.plugin = plugin;
  21.         this.logger = plugin.getLogger();
  22.         this.random = new Random();
  23.         this.spawns = new HashMap<>();
  24.     }
  25.  
  26.     void init(ConfigurationLoader<CommentedConfigurationNode> localLoader) {
  27.         try {
  28.             ConfigurationOptions options = ConfigurationOptions.defaults()
  29.                     .setShouldCopyDefaults(true);
  30.             CommentedConfigurationNode node = localLoader.load(options);
  31.             this.maxArea = (int) node.getNode("spawnArea", "maxArea").getValue(2000);
  32.             localLoader.save(node);
  33.         } catch (IOException e) {
  34.             this.logger.warn("Error saving default config!", e);
  35.         }
  36.     }
  37.  
  38.     public void generationSpawnLocations(Player p) {
  39.         List<Location<World>> locs = new ArrayList<>();
  40.  
  41.         for (int i = 0; i < 3; i++) {
  42.             Location<World> loc;
  43.  
  44.             do {
  45.                 loc = getRandomLocation(p.getWorld());
  46.             } while (!isValidFloor(loc));
  47.  
  48.             locs.add(loc);
  49.             this.logger.info("final loc: " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
  50.         }
  51.  
  52.         spawns.put(p.getName(), locs);
  53.         this.toSpawn(p, 0);
  54.     }
  55.  
  56.     public void toSpawn(Player p, int index) {
  57.         Location<World> location = (this.spawns.get(p.getName())).get(index);
  58.         p.setLocation(location);
  59.     }
  60.  
  61.     private Location<World> getRandomLocation(World world) {
  62.  
  63.         int[] pos = {this.random.nextInt(this.maxArea + 1), this.random.nextInt(this.maxArea + 1)};
  64.  
  65.         for (int i = 0; i < 2; i++) {
  66.             if (this.random.nextBoolean()) {
  67.                 int r = this.random.nextInt(pos.length);
  68.                 if (this.random.nextBoolean()) {
  69.                     pos[r] = -pos[r];
  70.                 } else {
  71.                     pos[r] = Math.abs(pos[r]);
  72.                 }
  73.             }
  74.         }
  75.  
  76.         Location<World> loc = new Location<>(world, pos[0], 70, pos[1]);
  77.         world.loadChunk(loc.getPosition().toInt(), true);
  78.         Optional<Location<World>> safeLocation = Sponge.getGame().getTeleportHelper().getSafeLocation(loc, 20, 20);
  79.         if (safeLocation.isPresent()) {
  80.             loc = safeLocation.get();
  81.             this.logger.info("safeLocation get!");
  82.         }
  83.  
  84.         return loc;
  85.     }
  86.  
  87.     private boolean isValidFloor(Location loc) {
  88.         return (loc != null) && (!unsafeBody.contains(loc.getBlockType()));
  89.     }
  90.  
  91.     public HashMap<String, List<Location<World>>> getSpawns() {
  92.         return this.spawns;
  93.     }
  94. }