Advertisement
Exception_Prototype

RandomSpawnManager

Sep 2nd, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. package com.prototype.randomspawn;
  2.  
  3. import com.google.common.collect.ImmutableList;
  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.block.BlockType;
  9. import org.spongepowered.api.block.BlockTypes;
  10. import org.spongepowered.api.entity.living.player.Player;
  11. import org.spongepowered.api.world.Location;
  12. import org.spongepowered.api.world.World;
  13.  
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.time.Instant;
  17. import java.time.temporal.ChronoUnit;
  18. import java.util.*;
  19.  
  20. /**
  21.  * Created by Prototype on 14.08.2017.
  22.  */
  23. public class RandomSpawnManager {
  24.  
  25.     private final RandomSpawn plugin;
  26.     private Logger logger;
  27.     private ConfigurationLoader<CommentedConfigurationNode> loader;
  28.     private CommentedConfigurationNode config;
  29.  
  30.     private static final List<BlockType> unsafeBody = ImmutableList.of(
  31.             BlockTypes.AIR,
  32.             BlockTypes.CACTUS,
  33.             BlockTypes.FIRE,
  34.             BlockTypes.LAVA,
  35.             BlockTypes.FLOWING_LAVA,
  36.             BlockTypes.WATER,
  37.             BlockTypes.FLOWING_WATER
  38.     );
  39.  
  40.     private final HashMap<String, List<Location<World>>> spawnList;
  41.     private int maxArea;
  42.     private final Random random;
  43.  
  44.     RandomSpawnManager(RandomSpawn plugin) {
  45.         this.plugin = plugin;
  46.         logger = plugin.getLogger();
  47.         spawnList = new HashMap<>();
  48.         random = new Random();
  49.     }
  50.  
  51.     void init(File configFile, ConfigurationLoader<CommentedConfigurationNode> loader) {
  52.         this.loader = loader;
  53.         if (configFile.exists()) {
  54.             loadConfig();
  55.             maxArea = config.getNode("spawnArea", "maxArea").getInt();
  56.         } else {
  57.             try {
  58.                 maxArea = 2000;
  59.                 configFile.createNewFile();
  60.                 loadConfig();
  61.                 config.getNode("spawnArea").setComment("The max spawn area");
  62.                 config.getNode("spawnArea", "maxArea").setValue(maxArea);
  63.                 saveConfig();
  64.             } catch (Exception ex) {
  65.                 logger.error("Error create default config.", ex);
  66.             }
  67.         }
  68.     }
  69.  
  70.     public void spawnGeneration(Player p) {
  71.         List<Location<World>> spawns = new ArrayList<>();
  72.         for (int i = 0; i < 3; i++) {
  73.             Location<World> loc;
  74.  
  75.             do {
  76.                 loc = getRandomLocation(p.getWorld());
  77.             } while (!isValidFloor(loc));
  78.  
  79.             spawns.add(loc);
  80.             logger.info("final loc: " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
  81.         }
  82.  
  83.         spawnList.put(p.getName(), spawns);
  84.         toSpawn(p, 0);
  85.     }
  86.  
  87.     public void toSpawn(Player p, int index) {
  88.         Location<World> location = (spawnList.get(p.getName())).get(index);
  89.         p.setLocation(location);
  90.     }
  91.  
  92.     private Location<World> getRandomLocation(World world) {
  93.  
  94.         int[] pos = {random.nextInt(maxArea + 1), random.nextInt(maxArea + 1)};
  95.  
  96.         for (int i = 0; i < 2; i++) {
  97.             if (random.nextBoolean()) {
  98.                 int r = random.nextInt(pos.length);
  99.                 if (random.nextBoolean()) {
  100.                     pos[r] = -pos[r];
  101.                 } else {
  102.                     pos[r] = Math.abs(pos[r]);
  103.                 }
  104.             }
  105.         }
  106.  
  107.         Location<World> loc = new Location<>(world, pos[0], 70, pos[1]);
  108.         world.loadChunk(loc.getPosition().toInt(), true);
  109.         Optional<Location<World>> safeLocation = Sponge.getGame().getTeleportHelper().getSafeLocation(loc, 20, 20);
  110.         if (safeLocation.isPresent()) {
  111.             loc = safeLocation.get();
  112.             logger.info("safeLocation get!");
  113.         }
  114.  
  115.         return loc;
  116.     }
  117.  
  118.     private boolean isValidFloor(Location loc) {
  119.         return (loc != null) && (!unsafeBody.contains(loc.getBlockType()));
  120.     }
  121.  
  122.  
  123.     public boolean _isFirstPlay(Player p) {
  124.         try {
  125.             Instant firstPlayed = p.firstPlayed().get();
  126.             Instant lastPlayed = p.lastPlayed().get();
  127.             plugin.getLogger().info(firstPlayed.toString());
  128.             plugin.getLogger().info(lastPlayed.toString());
  129.             plugin.getLogger().info(lastPlayed.minus(10, ChronoUnit.SECONDS).toString());
  130.             return firstPlayed.isAfter(lastPlayed.minus(10, ChronoUnit.SECONDS));
  131.         } catch (Exception e) {
  132.             plugin.getLogger().info("error isFirstPlay");
  133.             return false;
  134.         }
  135.     }
  136.  
  137.     private void saveConfig() {
  138.         try {
  139.             loader.save(config);
  140.         } catch (IOException ex) {
  141.             logger.error("Error save default config.", ex);
  142.         }
  143.     }
  144.  
  145.     private void loadConfig() {
  146.         try {
  147.             config = loader.load();
  148.         } catch (IOException ex) {
  149.             logger.error("Error load default config.", ex);
  150.         }
  151.     }
  152.  
  153.     public HashMap<String, List<Location<World>>> getSpawnList() {
  154.         return spawnList;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement