Solved Teleporting to random player

Discussion in 'Plugin Development' started by AeroLiquid, Oct 5, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    AeroLiquid

    Hey fellas,
    I can't figure out how to transfer an integer to a player name, allowing me to teleport to a random player. This is the code I have so far:
    PHP:
        @EventHandler
        
    public void onItemRightClick(PlayerInteractEvent e) {
            
    Player p e.getPlayer();
            
    Random r = new Random();
            
    int target r.nextInt(Bukkit.getServer().getOnlinePlayers().size());
            if (
    p.getItemInHand().getType() == (Material.EYE_OF_ENDER)
                    && 
    sm.getStaffmode().contains(p)) {
                if (
    e.getAction() == Action.RIGHT_CLICK_AIR) {
                    
    p.teleport(target);
                    
    p.sendMessage(ChatColor.GOLD "You got teleported to "
                            
    target.getName() + "!");
                } else if (
    p.getItemInHand().getType() != (Material.EYE_OF_ENDER)) {
                    return;
                }
            }
        }
     
  2. Offline

    jusjus112

    @AeroLiquid
    You cant transform a player to an Integer, soo

    Here is an example from my plugin, it is a good example:
    Code:
    Random r = new Random();
              ArrayList<Player> players = new ArrayList<Player>();
              for(Player online : Bukkit.getServer().getOnlinePlayers()) {
              if(online == p) {
              }
              else
              players.add(online);
              }
              int index = r.nextInt(players.size());
              Player loc = (Player) players.get(index);
              p.teleport(loc);
              p.sendMessage("§eTelporting you to " + "§6(name)" + "§e...");
             }
     
  3. Offline

    PDKnight

    @jusjus112 made very good example, I'm just going to make it simpler, faster and shorter:
    Code:java
    1. ArrayList<Player> players = new ArrayList<Player>();
    2. for (Player e : Bukkit.getOnlinePlayers()) players.add(e);
    3. Player randomPlayer = players.get(new Random().nextInt(players.size()));
    4. player.teleport(randomPlayer.getLocation());
     
    Last edited: Oct 5, 2015
  4. Offline

    Gonmarte

    Zombie_Striker likes this.
  5. Offline

    jusjus112

    @PDKnight
    Haha, thanks for that. But i want it to paste the exact same code, and i see that i didnt work with 1.8+
    Maybe it is just a bug for older versions of 1.8 but to let you know ;)
     
  6. Offline

    PDKnight

    @jusjus112 Thank you for your reply, I've repaired my code. The error appears because since 1.8 method getOnlinePlayers() only returns Collection<Player>, not Player[] :)

    @Gonmarte Wow, and what now?
     
  7. Offline

    Protophite

    @jusjus112
    1. if(online == p) {
    2. }
    no point in having this block if you have nothing in it
     
  8. Offline

    AeroLiquid

  9. Offline

    PDKnight

    No problem at all! :)
     
  10. Offline

    Scimiguy

    Don't forget to mark as solved if Solved
     
  11. Offline

    Gonmarte

    @PDKnight and What now? kkk do you thing i care about that? the problem is of the jusjust12 that thinks the his learning but the only thing is doing is copy and paste.... grow up kid, if you cant explain to him how to do it dont sponefeed =)
     
  12. Offline

    Scimiguy

    Cut it out, Gon.

    Knight only made previous code more efficient, which is not "spoonfeeding"

    He already had the code by that point, Knight just made sure that if he was going to use it, he use the best version of it he could think of
     
  13. Offline

    Gonmarte

    In this forum we dont sponefeed, he needs to learn by him self. ITS the same as learning with YT u just say that u understood because the gay is writing all the code....
     
Thread Status:
Not open for further replies.

Share This Page