Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ***;
- import ninja.leaping.configurate.ConfigurationOptions;
- import ninja.leaping.configurate.commented.CommentedConfigurationNode;
- import ninja.leaping.configurate.loader.ConfigurationLoader;
- import org.slf4j.Logger;
- import org.spongepowered.api.Sponge;
- import org.spongepowered.api.entity.living.player.Player;
- import org.spongepowered.api.world.Chunk;
- import org.spongepowered.api.world.Location;
- import org.spongepowered.api.world.World;
- import java.io.IOException;
- import java.util.*;
- import java.util.concurrent.CompletableFuture;
- /**
- * Created by Prototype on 25.10.2017.
- */
- 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<>(3);
- 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.logger.info("p.setLocation(loc): " + p.setLocation(this.spawns.put(p.getUniqueId(), locs).get(0)));
- }
- public void toSpawn(Player p, int index) {
- this.logger.info("p: " + p.getName() + ", index: " + index);
- this.logger.info("p.setLocation(loc): " + p.setLocation(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);
- Optional<Chunk> chunk = world.loadChunk(x, 70, z, true);
- if (chunk.isPresent()) {
- Location<Chunk> location = chunk.get().getLocation(x, 70, z);
- Optional<Location<World>> sf = Sponge.getTeleportHelper().getSafeLocation(); //<- get safe location because y=70 it is not safe
- return sf.orElse(null);
- }
- return null;
- }
- public Map<UUID, List<Location<World>>> getSpawns() {
- return this.spawns;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement