Please help me! I have an error and I don't know how to fix it!

Discussion in 'Spigot Plugin Development' started by ZevoGaems, May 4, 2017.

  1. In this line of code it says I need to add a cast to bukkit.getOnlinePlayers()
    Here is all of the code
    How would I fix that or do I need too?
    Code (Text):
                    if(cmd.getName().equalsIgnoreCase("ModeratorTeleport")) {
                        if(!sender.hasPermission("minegrid.moderatorteleport")) {
                            sender.sendMessage(ChatColor.RED + "Requested Permission Level /n TRAINEE");
                        } else if(sender.hasPermission("minegrid.moderateteleport")) {
                            Random rand = new Random();
                            int index = rand.nextInt(Bukkit.getOnlinePlayers().size());
                            Player target = Bukkit.getOnlinePlayers().get(index);
                            Player staff = (Player) sender;
                            staff.teleport(target.getLocation());
                            staff.sendMessage(ChatColor.RED + "Teleported to random player");
                        }
                    }
     
    #1 ZevoGaems, May 4, 2017
    Last edited: May 4, 2017
  2. Here's an excerpt from one of my plugins where I chose a random player, maybe this code will work better for you. :D

    Code (Text):

    List<Player> players = new ArrayList<>();
    for (Player online : Bukkit.getOnlinePlayers()) {
        if (!player.equals(online)) {
            players.add(online);
        }
    }
    if (!players.isEmpty()) {
        Player randomPlayer = players.get((int) (Math.random() * players.size()));
        player.teleport(randomPlayer.getLocation());
        player.setVelocity(player.getVelocity().setY(1.5));
    } else {
        player.sendMessage(ChatUtils.color("&cNo one to teleport to!"));
    }
     
     
  3. you can use getOnlinePlayers#toArray()[randomnumber] to retrieve a random player.
     
    #5 Theone1000, May 4, 2017
    Last edited: May 4, 2017
  4. Thats kinda long written and you made a couple of useless objects (ArrayList)
     
  5. Off Topic:

    Please use titles that actually describe your problem, "Please help me i have an error and i dont know how to fix it", Clearly, If your posting here you have a problem with something, http://prntscr.com/f4awu7 Use title which actually describe your problem, Your title could seriously mean anything, This could mean your eclipse doesn't work, Or your plugin.yml isn't made correctly, Or your Spigot jar isn't working, Use titles such as "Error with arraylist" or "Problem when saving config to a value" or something similar, As said above, Read the "Read Me For Everyone Asking Questions On Here" it has alot of stuff which can help you and the people trying to help you when making a post.

    Regards
    Jordeh (Weird dude who complains about anything xD)