Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.prototype.randomspawn;
- import com.google.common.collect.ImmutableList;
- 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.block.BlockType;
- import org.spongepowered.api.block.BlockTypes;
- import org.spongepowered.api.entity.living.player.Player;
- import org.spongepowered.api.world.Location;
- import org.spongepowered.api.world.World;
- import java.io.File;
- import java.io.IOException;
- import java.time.Instant;
- import java.time.temporal.ChronoUnit;
- import java.util.*;
- /**
- * Created by Prototype on 14.08.2017.
- */
- public class RandomSpawnManager {
- private final RandomSpawn plugin;
- private Logger logger;
- private ConfigurationLoader<CommentedConfigurationNode> loader;
- private CommentedConfigurationNode config;
- private static final List<BlockType> unsafeBody = ImmutableList.of(
- BlockTypes.AIR,
- BlockTypes.CACTUS,
- BlockTypes.FIRE,
- BlockTypes.LAVA,
- BlockTypes.FLOWING_LAVA,
- BlockTypes.WATER,
- BlockTypes.FLOWING_WATER
- );
- private final HashMap<String, List<Location<World>>> spawnList;
- private int maxArea;
- private final Random random;
- RandomSpawnManager(RandomSpawn plugin) {
- this.plugin = plugin;
- logger = plugin.getLogger();
- spawnList = new HashMap<>();
- random = new Random();
- }
- void init(File configFile, ConfigurationLoader<CommentedConfigurationNode> loader) {
- this.loader = loader;
- if (configFile.exists()) {
- loadConfig();
- maxArea = config.getNode("spawnArea", "maxArea").getInt();
- } else {
- try {
- maxArea = 2000;
- configFile.createNewFile();
- loadConfig();
- config.getNode("spawnArea").setComment("The max spawn area");
- config.getNode("spawnArea", "maxArea").setValue(maxArea);
- saveConfig();
- } catch (Exception ex) {
- logger.error("Error create default config.", ex);
- }
- }
- }
- public void spawnGeneration(Player p) {
- List<Location<World>> spawns = new ArrayList<>();
- for (int i = 0; i < 3; i++) {
- Location<World> loc;
- do {
- loc = getRandomLocation(p.getWorld());
- } while (!isValidFloor(loc));
- spawns.add(loc);
- logger.info("final loc: " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
- }
- spawnList.put(p.getName(), spawns);
- toSpawn(p, 0);
- }
- public void toSpawn(Player p, int index) {
- Location<World> location = (spawnList.get(p.getName())).get(index);
- p.setLocation(location);
- }
- private Location<World> getRandomLocation(World world) {
- int[] pos = {random.nextInt(maxArea + 1), random.nextInt(maxArea + 1)};
- for (int i = 0; i < 2; i++) {
- if (random.nextBoolean()) {
- int r = random.nextInt(pos.length);
- if (random.nextBoolean()) {
- pos[r] = -pos[r];
- } else {
- pos[r] = Math.abs(pos[r]);
- }
- }
- }
- Location<World> loc = new Location<>(world, pos[0], 70, pos[1]);
- world.loadChunk(loc.getPosition().toInt(), true);
- Optional<Location<World>> safeLocation = Sponge.getGame().getTeleportHelper().getSafeLocation(loc, 20, 20);
- if (safeLocation.isPresent()) {
- loc = safeLocation.get();
- logger.info("safeLocation get!");
- }
- return loc;
- }
- private boolean isValidFloor(Location loc) {
- return (loc != null) && (!unsafeBody.contains(loc.getBlockType()));
- }
- public boolean _isFirstPlay(Player p) {
- try {
- Instant firstPlayed = p.firstPlayed().get();
- Instant lastPlayed = p.lastPlayed().get();
- plugin.getLogger().info(firstPlayed.toString());
- plugin.getLogger().info(lastPlayed.toString());
- plugin.getLogger().info(lastPlayed.minus(10, ChronoUnit.SECONDS).toString());
- return firstPlayed.isAfter(lastPlayed.minus(10, ChronoUnit.SECONDS));
- } catch (Exception e) {
- plugin.getLogger().info("error isFirstPlay");
- return false;
- }
- }
- private void saveConfig() {
- try {
- loader.save(config);
- } catch (IOException ex) {
- logger.error("Error save default config.", ex);
- }
- }
- private void loadConfig() {
- try {
- config = loader.load();
- } catch (IOException ex) {
- logger.error("Error load default config.", ex);
- }
- }
- public HashMap<String, List<Location<World>>> getSpawnList() {
- return spawnList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement