开发者

Java boolean issue

开发者 https://www.devze.com 2023-03-07 06:03 出处:网络
private static final Pattern namePattern = Pattern.compile(\"[a-zA-Z0-9_-]{3,12}\"); if (player.getName().length() < 3 ||
private static final Pattern namePattern = 
    Pattern.compile("[a-zA-Z0-9_-]{3,12}");

if (player.getName().length() < 3 || 
        player.getName().length() > 12 || 
        namePattern.matcher(player.getName()).matches()) {
    player.ban("[Autoban] Character-name PE", true);
    return;
}

The code is kinda obvious, I hope

If the players name is longer than 12 or shorter then 3, or have any symbols that shouldnt be i a name = BAM, ban!

but even thought my player name is like Chaos or test, it gets autobanned by this code. idk if the namepattern Pattern bl开发者_StackOverflowocks characters aswell, I just it to block symbols like "!#&%¤/&%(/)(/(=)$@£$@£{€@£ yeh... What am I doing wrong :(?


I think you meant (!namePattern.matcher(player.getName()).matches()).

Also, you could've use only the regex match: [a-zA-Z0-9_-]{3,12} since it checks for length.

Edit:

You can use such a function

public void banIfNeeded(Player player)
{
    Pattern namePattern = Pattern.compile("[a-zA-Z0-9_-]{3,12}");
    if(!namePattern.matcher(player.getName()).matches()) {
        System.out.print("Banned"); 
        // Ban somehow
    }
}

As @bkail commented (thanks!), there's no need for the ^ and \z so I removed them :)

0

精彩评论

暂无评论...
验证码 换一张
取 消