public class RandomSpawnManager {
private final RandomSpawn plugin;
private final Logger logger;
private final Random random;
private final HashMap<UUID, List<Location<World>>> spawns;
private int min, max;
RandomSpawnManager(RandomSpawn plugin) {
this.plugin = plugin;
this.logger = plugin.getLogger();
this.random = new Random();
this.spawns = new HashMap<>();
}
void init(ConfigurationLoader<CommentedConfigurationNode> localLoader) {
try {
ConfigurationOptions options = ConfigurationOptions.defaults()
.setShouldCopyDefaults(true);
CommentedConfigurationNode node = localLoader.load(options);
this.min = (int) node.getNode("spawnArea", "min").getValue(1500);
this.max = (int) node.getNode("spawnArea", "max").getValue(3000);
localLoader.save(node);
} catch (IOException e) {
this.logger.warn("Error saving default config!", e);
}
}
public void generationSpawnLocations(Player p) {
List<Location<World>> locs = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Location<World> loc = this.getRandomLocation(p.getWorld());
locs.add(loc);
this.logger.info("loc: " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
}
this.spawns.put(p.getUniqueId(), locs);
this.toSafeLocation(p, locs.get(0));
}
public void toSpawn(Player p, int index) {
this.logger.info("p: " + p.getName() + ", index: " + index);
this.toSafeLocation(p, this.spawns.get(p.getUniqueId()).get(index));
}
private Location<World> getRandomLocation(World world) {
int x = this.random.nextBoolean() ? this.random.nextInt(max + 1 - min) + min : -(this.random.nextInt(max + 1 - min) + min);
int z = this.random.nextBoolean() ? this.random.nextInt(max + 1 - min) + min : -(this.random.nextInt(max + 1 - min) + min);
return new Location<>(world, x, 90, z);
}
private void toSafeLocation(Player p, Location<World> location) {
Optional<Location<World>> sf = Sponge.getTeleportHelper().getSafeLocation(location);
sf.ifPresent(worldLocation -> this.logger.info("p.setLocation(loc): " + p.setLocation(worldLocation)));
}
public Map<UUID, List<Location<World>>> getSpawns() {
return this.spawns;
}
}