Random tp within worldborder?

Discussion in 'Spigot Plugin Development' started by JacksonHogan, Jun 5, 2016.

  1. Does anyone have an idea about how i would teleport someone within the world border randomly?
     
  2. // figure out the maximum and minimum possible values for x and z based on your world border position
    // pick a random value within those min-max ranges for each of x and z
    // set 256 as your y value. Increment that value downward until you hit solid ground.
    // spawn the player at your final x, y, and z coordinates
     
    • Informative Informative x 1
  3. This is what i got to work:

    Code (Text):
    public class RandomTp {
     
        static Long time = (long) 3600;
     
        static int max = 5000;
        static int min = -5000;
        static int y = 256;
     
        public static int getRandomNum(){
            Random rand = new Random();
         
            int randomNum = rand.nextInt((max - min) + min);
         
            return randomNum;
        }
     
        public static void randomTeleport(Player player){
         
            int x = getRandomNum(), y = 0, z = getRandomNum();
            Block bLoc = player.getWorld().getHighestBlockAt(new Location(player.getWorld(), x, y, z));
             
                player.teleport(bLoc.getLocation());
                player.sendMessage(FantasyCore.prefix
                        + ChatColor.GRAY + "You have been teleported to "
                        + ChatColor.RED + "(" + bLoc.getLocation().getX() + ")"
                        + ChatColor.GRAY + "," + ChatColor.RED
                        + " (" + bLoc.getLocation().getZ());
        }
    }
    But now i can't get my cooldown to work:
    Timer class:
    Code (Text):
    public class Timer {

        static HashMap<String, Long> cooldowns = new HashMap<String, Long>();

        static int seconds = 60;

        public static boolean hasCooldown(Player player) {
            if (cooldowns.get(player.getName()) < (System.currentTimeMillis() - seconds * 1000))
                return false;
            else
                return true;
        }

        public static void activateCooldown(Player player) {
            cooldowns.put(player.getName(), System.currentTimeMillis());
        }
    }
    RandomTp class:

    Code (Text):
    public class RandomTp {
     
        static Long time = (long) 3600;
     
        static int max = 5000;
        static int min = -5000;
        static int y = 256;
     
        public static int getRandomNum(){
            Random rand = new Random();
         
            int randomNum = rand.nextInt((max - min) + min);
         
            return randomNum;
        }
     
        public static void randomTeleport(Player player){
         
            int x = getRandomNum(), y = 0, z = getRandomNum();
            Block bLoc = player.getWorld().getHighestBlockAt(new Location(player.getWorld(), x, y, z));
         
            if(Timer.hasCooldown(player)){
                player.sendMessage(FantasyCore.prefix
                        + ChatColor.GRAY + "You must wait "
                        + ChatColor.GOLD
                        + Timer.cooldowns.get(player.getName())
                        + ChatColor.GRAY + "second(s)");
            }else{
                Timer.activateCooldown(player);
             
                player.teleport(bLoc.getLocation());
                player.sendMessage(FantasyCore.prefix
                        + ChatColor.GRAY + "You have been teleported to "
                        + ChatColor.RED + "(" + bLoc.getLocation().getX() + ")"
                        + ChatColor.GRAY + "," + ChatColor.RED
                        + " (" + bLoc.getLocation().getZ());
            }
        }
    }
     
    EDIT:
    I get an NPE on this line:
    Code (Text):
    if (cooldowns.get(player.getName()) < (System.currentTimeMillis() - seconds * 1000))
     
  4. Just saying, you really do not need a class for something that simple - it is useless bloat and complexity. Use a method, or just put the code inside your teleport method
    (there is zero reason to make a method or class for something unless it is going to be re-used in different places or ways, or for organizational purposes in much bigger classes)

    HashMap.get returns null is the player is not in the map, what did you expect it to do, return owls?

    Use cooldowns.contains, or check if the value is null (i am fairly sure Long objects (a wrapper for long) can be null, but long primitives cannot be null)