Illuminatiiiiii
3/3/2019 - 7:50 PM

RandomTP Plugin

package me.illuminatiproductions.youtubeplugin;

import me.illuminatiproductions.youtubeplugin.commands.RandomTPCommand;
import org.bukkit.plugin.java.JavaPlugin;

public class YoutubePlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        // Plugin startup logic

        TeleportUtils yeet = new TeleportUtils(this);

        getCommand("rtp").setExecutor(new RandomTPCommand());

        //Setup Config
        getConfig().options().copyDefaults();
        saveDefaultConfig();
    }

}
package me.illuminatiproductions.youtubeplugin.commands;

import me.illuminatiproductions.youtubeplugin.TeleportUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class RandomTPCommand implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        if (sender instanceof Player){
            Player player = (Player) sender;
            if (args.length == 0){

                //Safe Location that has been generated
                Location randomLocation = TeleportUtils.findSafeLocation(player);

                //Teleport player
                player.teleport(randomLocation);

                player.sendMessage(ChatColor.RED + "Teleported to Random Location!!!");
                player.sendMessage(ChatColor.AQUA + "New Coordinates: " + ChatColor.LIGHT_PURPLE + randomLocation.getX() + " " + randomLocation.getY() + " " + randomLocation.getZ());

            }else if(args.length == 1){ //Specify a player to teleport
                if (player.hasPermission("rtp.others")){
                    //Get the player to teleport
                    Player target = Bukkit.getPlayer(args[0]);

                    //Safe Location that has been generated
                    Location randomLocation = TeleportUtils.findSafeLocation(target);

                    //Teleport player
                    target.teleport(randomLocation);

                    target.sendMessage(ChatColor.GREEN + player.getDisplayName() + ChatColor.GOLD + " just Random Teleported you!");
                    target.sendMessage(ChatColor.AQUA + "New Coordinates: " + ChatColor.LIGHT_PURPLE + randomLocation.getX() + " " + randomLocation.getY() + " " + randomLocation.getZ());

                    player.sendMessage(ChatColor.RED + "Player successfully teleported to: " + ChatColor.LIGHT_PURPLE + randomLocation.getX() + " " + randomLocation.getY() + " " + randomLocation.getZ());
                }
            }
        }else {
            System.out.println("You need to be a player to execute this command.");
        }

        return true;
    }
}
package me.illuminatiproductions.youtubeplugin;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

import java.util.HashSet;
import java.util.Random;

public class TeleportUtils {

    //Get an instance of the main class so we can access config
    static YoutubePlugin plugin;

    public TeleportUtils(YoutubePlugin plugin) {
        this.plugin = plugin;
    }

    //List of all the unsafe blocks
    public static HashSet<Material> bad_blocks = new HashSet<>();

    static{
        bad_blocks.add(Material.LAVA);
        bad_blocks.add(Material.FIRE);
        bad_blocks.add(Material.CACTUS);
    }

    public static Location generateLocation(Player player){
        //Generate Random Location
        Random random = new Random();

        int x = 0;
        int z = 0;
        int y = 0;

            if(plugin.getConfig().getBoolean("world-border")){ //If they want to limit the distance
                x = random.nextInt(plugin.getConfig().getInt("border"));
                z = random.nextInt(plugin.getConfig().getInt("border"));
                y = 150;
            }else if(!plugin.getConfig().getBoolean("world-border")){ //If they dont
                x = random.nextInt(25000); //25000 is default
                z = random.nextInt(25000);
                y = 150;
            }

        Location randomLocation = new Location(player.getWorld(), x, y, z);
        y = randomLocation.getWorld().getHighestBlockYAt(randomLocation);
        randomLocation.setY(y);

        return randomLocation;
    }

    public static Location findSafeLocation(Player player){

        Location randomLocation = generateLocation(player);

        while (!isLocationSafe(randomLocation)){
            //Keep looking for a safe location
            randomLocation = generateLocation(player);
        }
        return randomLocation;
    }

    public static boolean isLocationSafe(Location location){

        int x = location.getBlockX();
        int y = location.getBlockY();
        int z = location.getBlockZ();
        //Get instances of the blocks around where the player would spawn
        Block block = location.getWorld().getBlockAt(x, y, z);
        Block below = location.getWorld().getBlockAt(x, y - 1, z);
        Block above = location.getWorld().getBlockAt(x, y + 1, z);

        //Check to see if the surroundings are safe or not
        return !(bad_blocks.contains(below.getType())) || (block.getType().isSolid()) || (above.getType().isSolid());
    }

}
#True if you want to limit how far it teleports players
world-border: true
#How many blocks away they CAN spawn(not will)
border: 200 #If no world border, limit is automatically 25,000 blocks