Advertisement
Exception_Prototype

Untitled

Feb 1st, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. package ***;
  2.  
  3. import ninja.leaping.configurate.ConfigurationOptions;
  4. import ninja.leaping.configurate.commented.CommentedConfigurationNode;
  5. import ninja.leaping.configurate.loader.ConfigurationLoader;
  6. import org.slf4j.Logger;
  7. import org.spongepowered.api.Sponge;
  8. import org.spongepowered.api.entity.living.player.Player;
  9. import org.spongepowered.api.world.Chunk;
  10. import org.spongepowered.api.world.Location;
  11. import org.spongepowered.api.world.World;
  12.  
  13. import java.io.IOException;
  14. import java.util.*;
  15. import java.util.concurrent.CompletableFuture;
  16.  
  17. /**
  18.  * Created by Prototype on 25.10.2017.
  19.  */
  20. public class RandomSpawnManager {
  21.  
  22.     private final RandomSpawn plugin;
  23.     private final Logger logger;
  24.     private final Random random;
  25.     private final HashMap<UUID, List<Location<World>>> spawns;
  26.     private int min, max;
  27.  
  28.     RandomSpawnManager(RandomSpawn plugin) {
  29.         this.plugin = plugin;
  30.         this.logger = plugin.getLogger();
  31.         this.random = new Random();
  32.         this.spawns = new HashMap<>();
  33.     }
  34.  
  35.     void init(ConfigurationLoader<CommentedConfigurationNode> localLoader) {
  36.         try {
  37.             ConfigurationOptions options = ConfigurationOptions.defaults()
  38.                     .setShouldCopyDefaults(true);
  39.             CommentedConfigurationNode node = localLoader.load(options);
  40.             this.min = (int) node.getNode("spawnArea", "min").getValue(1500);
  41.             this.max = (int) node.getNode("spawnArea", "max").getValue(3000);
  42.             localLoader.save(node);
  43.         } catch (IOException e) {
  44.             this.logger.warn("Error saving default config!", e);
  45.         }
  46.     }
  47.  
  48.     public void generationSpawnLocations(Player p) {
  49.         List<Location<World>> locs = new ArrayList<>(3);
  50.  
  51.         for (int i = 0; i < 3; i++) {
  52.             Location<World> loc = this.getRandomLocation(p.getWorld());
  53.             locs.add(loc);
  54.             this.logger.info("loc: " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
  55.         }
  56.  
  57.         this.logger.info("p.setLocation(loc): " + p.setLocation(this.spawns.put(p.getUniqueId(), locs).get(0)));
  58.     }
  59.  
  60.     public void toSpawn(Player p, int index) {
  61.         this.logger.info("p: " + p.getName() + ", index: " + index);
  62.         this.logger.info("p.setLocation(loc): " + p.setLocation(this.spawns.get(p.getUniqueId()).get(index)));
  63.     }
  64.  
  65.     private Location<World> getRandomLocation(World world) {
  66.         int x = this.random.nextBoolean() ? this.random.nextInt(max + 1 - min) + min : -(this.random.nextInt(max + 1 - min) + min);
  67.         int z = this.random.nextBoolean() ? this.random.nextInt(max + 1 - min) + min : -(this.random.nextInt(max + 1 - min) + min);
  68.  
  69.  
  70.         Optional<Chunk> chunk = world.loadChunk(x, 70, z, true);
  71.         if (chunk.isPresent()) {
  72.             Location<Chunk> location = chunk.get().getLocation(x, 70, z);
  73.             Optional<Location<World>> sf = Sponge.getTeleportHelper().getSafeLocation(); //<- get safe location because y=70 it is not safe
  74.             return sf.orElse(null);
  75.         }
  76.  
  77.         return null;
  78.     }
  79.  
  80.     public Map<UUID, List<Location<World>>> getSpawns() {
  81.         return this.spawns;
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement