Detect usernames out of array

Discussion in 'Plugin Development' started by Benny4298, Jun 13, 2012.

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

    Benny4298

    Dear Community,

    I'm trying to get it working, to load usernames out of an array and kick those, who are not on the 'List'. But i get an error in the with '*' marked part...

    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)
        {
            Player eventPlayer = event.getPlayer();
           
            String[] Username = {"Name1", "Name2"};
           
            *if (eventPlayer.getName() == Username)*
            {
                System.out.println(eventPlayer.getDisplayName() + " logged in.");
            }
           
            else
            {
                eventPlayer.kickPlayer("You're not on the Whitelist!");
            }
           
        }
    Can anyone please help me?

    Greetings
    -Benny4298
     
  2. Offline

    edocsyl

  3. Offline

    Benny4298

    Thanks for your fast answer! :)
     
  4. Offline

    ZachBora

    When comparing strings in java, always use .equals() or .equalsIgnoreCase()

    If I was you, instead of using String[] I'd use a List<String> and then you can just do variable.contains(name)
     
  5. do it inside an PlayerLoginEvent so there is not inside the game that the player has joined
     
  6. Offline

    desht

    If you have a non-trivial number of users in the list, it's better to use a Set. .contains() is much faster on a Set than it is on a List.
     
Thread Status:
Not open for further replies.

Share This Page