Solved Weird Error and no Errors in eclipse?

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

  1. I was trying to make a moderator tool so they can teleport randomly to a player and take a look at them for a little like the hcf servers. But when you run the command it keeps saying the player is offline. Please help me.
    Here is the code.
    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")) {
                            ArrayList<List> players = new ArrayList<>();
                            for (Player online : Bukkit.getOnlinePlayers()) {
                                if (players.equals(online)) {
                                    players.add((List) online);
                                }
                            }
                            if (!players.isEmpty()) {
                                Player player = (Player) sender;
                                Player randomPlayer = (Player) players.get((int) (Math.random() * players.size()));
                                player.teleport(randomPlayer.getLocation());
                            } else {
                                Player player = (Player) sender;
                                player.sendMessage(ChatColor.RED + "Error, no one to teleport to");
                            }
                        }
                    }
     
  2. Why are you checking to see if(players.equals(online));?
    Obviously they won't equal because you haven't added anything to the list.
     
  3. Someone told me to try this and they told me to copy and paste it and setup for my way. Is that causing the problem though?
     
  4. Try removing the if-statement and see if that works. Keep the part where you add the players to the list, however.
     
  5. Nope it just now keeps saying internal error
     
  6. The first time I've ever spoon fed somebody.

    Code (Text):

    // Make sure the sender is a Player
    if(!(sender instanceof Player))
        return false;

    // Get the player that ran the command
    Player player = (Player) sender;

    // List containing all players
    List<Player> players = new ArrayList<>();
    players.addAll(Bukkit.getOnlinePlayers());

    // Teleport to a random player    
    Player randomPlayer = players.get((int) (Math.random() * players.size()));
    player.teleport(randomPlayer.getLocation());
                           
    Why did you have an ArrayList containing List objects, and casting Player to List, and then back to Player?
     
    #6 SpiroMarshes, May 5, 2017
    Last edited: May 5, 2017
  7. That still wouldn't work. A List of Players will never equal an individual player.
     
  8. Actually what he did works :D
     
    • Agree Agree x 1
  9. Just edited my post to be a bit cleaner. I recommend using it over the snippet you used.
     
  10. So you have a list of online players and checking if the players are online? what?
     
  11. He tried to remove the command sender from list. The solution from SpiroMarshes may teleport the player to himself.

    something like:
    Code (Text):

    List<Player> players = Bukkit.getOnlinePlayers().stream().filter(sender::equals).collect(Collectors.toList());
    Optional<Player> target = players.stream().skip((int) (players.size() * Math.random())).findFirst();
    if (target.isPresent())
    {
        ((Player) sender.teleport(target.get().getLocation());
    }
    else
    {
        ((Player) sender).sendMessage(ChatColor.RED + "Error, no one to teleport to");
    }