Development Assistance Partial player name

Discussion in 'Plugin Help/Development/Requests' started by metmad22, Dec 24, 2014.

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

    metmad22

    Hello everyone,
    I am currently coding a plugin which highlights online player names in chat. My event only toggles when a full player name has been given. For example, if someone types "Hello, metmad22!" The event will toggle. But if they type "Hello, metmad!" or "Hello, met!", the event does not trigger. I was wondering if anyone can help me put this in the event.
    Currently, I check for
    Code:
    for(Player player : Bukkit.getOnlinePlayers())
    as a player. It is only working if I mention the whole player name.

    If someone could please tell me how I could do this in an AsyncPlayerChatEvent, I'll be very grateful.
    Also, Merry Christmas to you all! :)
     
  2. Offline

    pie_flavor

    Code:java
    1. String[] words = message.split(" ");
    2. for (String s : words) {
    3. if (player.getName().toLowerCase().contains(s.toLowerCase())) {
     
  3. Offline

    metmad22

    @pie_flavor It doesn't seem to be working. Here is my code.

    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onChat(AsyncPlayerChatEvent event)
        {
            Player player = event.getPlayer();
    
            String[] words = event.getMessage().split(" ");
    
            for(String string : words)
            {
                if(player.getName().toLowerCase().contains(string.toLowerCase()))
                {
                    if(players.contains(player.getName()))
                    {
                         String newMessage = event.getMessage().replaceAll(player.getName(), getConfig().getString("Player name color") + ChatColor.translateAlternateColorCodes('&', "") + player.getName() + "§f");
                          event.setMessage(newMessage);
    
                           player.playSound(player.getLocation(), Sound.LEVEL_UP, 50.0F, 5.0F);
                           player.playSound(player.getLocation(), Sound.FIZZ, 40.0F, 5.0F);
                    }
                    else
                    {
    
    
                    }
    
                }
            }
        }
     
  4. Offline

    lenis0012

    Put on async chat event:
    Code:java
    1. String message = event.getMessage();
    2. for(String word : message.split(" ")) {
    3. if(word.length() < 4) continue; // Ignore small words, i advice using this is you do contains check
    4. for(Player player : Bukkit.getOnlinePlayers()) {
    5. if(player.getName().equalsIgnoreCase(word) || player.getName().toLowercase().contains(world.toLowercase()) { // Use startswith for stricter checking
    6. message = message.replace(word, ChatColor.GREEN + word);
    7. break;
    8. }
    9. }
    10. }


    if a players name is yesyes
    you dont want all emssages with yes to be highlighted
    thats the problem you are running in to
     
  5. Offline

    pie_flavor

    actually the problem you are running into is that you have my code in the wrong spot. youre not supposed to be referencing string with e.gatPlayer(), you're supposed to be referencing it with the list of online players.
     
  6. Offline

    metmad22

    So far, I think i've fixed most issues possible. If I still run across one, I'll post it here. But thank you very much both of you! @pie_flavor @lenis0012
     
Thread Status:
Not open for further replies.

Share This Page