Solved Chat Mention Help!

Discussion in 'Spigot Plugin Development' started by SneakyBruh, Sep 14, 2017.

Thread Status:
Not open for further replies.
  1. I am making a plugin with a feature of a ping sound when your name is written in chat. I also wanted this to be turned on and off via console but i keep getting this error in console!

    Error:
    Code (Text):
    at me.Sneaky.BroadCast.Chat.onMention(Chat.java:24) ~[?:?]
    Chat Code:
    Code (Text):

    package me.Sneaky.BroadCast;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Instrument;
    import org.bukkit.Note;
    import org.bukkit.Note.Tone;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    public class Chat implements Listener {
       
       
        private Main main;
        public Chat(Main main) {
        this.main = main;
        }
               
        @EventHandler
       
        public void onMention(AsyncPlayerChatEvent event) {
            Boolean isEnabled = main.getConfig().getBoolean("Sound-On-Mention");
           if(isEnabled);
          for (Player player : event.getPlayer().getWorld().getPlayers()) {
              if(player == event.getPlayer())continue;
              if(event.getMessage().contains(player.getName())){
            event.setMessage(event.getMessage().replaceAll(player.getName(), ChatColor.GREEN+"@"+player.getName()+ChatColor.RESET + ""));
            player.playNote(player.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A));
          }
              else {
                  String message = event.getMessage();
                Bukkit.getServer().broadcastMessage(message);
              }
    }
        }
    }
     
     
  2. you used a tutorial dintya,
    Code (Text):
           if(isEnabled);
    best if statement
    Code (Text):
         Bukkit.getServer().broadcastMessage(message);
    just..... why are you broadcasting a chat message (you should be getting 2 messages because of this unless you cancel the event)? also ur code is messy.
     
  3. Just dont worry about the other code :(. i just need this to work please
     
  4. If you're getting an error you paste the whole error not a single line, also use the primitive type of boolean ot the wrapper class, also this won't work if the player isn't on the same world hope you know that, you could instead simply use the method provided by the own event to get all the players that are going to see the message 'AsyncPlayerChatEvent#getRecipients()'. Also broadcasting the message for every player doesn't make any sense, you're not cancelling the event neither removing any recipient so the players still would see the message, also don't use the method 'String#replaceAll(String, String)' if you're not using regex, use instead 'String#replace(String, String)'. Also you're changing the format each time, so if the message contains the name of 2 players the format will only work for the last player.
     
  5. Code (Text):

    Could not pass event AsyncPlayerChatEvent

    org.bukkit.event.EventException

    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]

    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]

    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]

    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot.jar:git-Spigot-db6de12-18fbb24]

    at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.java:1084) [spigot.jar:git-Spigot-db6de12-18fbb24]

    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1022) [spigot.jar:git-Spigot-db6de12-18fbb24]

    at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot.jar:git-Spigot-db6de12-18fbb24]

    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_141]

    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_141]

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_141]

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_141]

    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_141]

    Caused by: java.lang.NullPointerException

    at me.Sneaky.BroadCast.Chat.onMention(Chat.java:24) ~[?:?]

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_141]

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_141]

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_141]

    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_141]

    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]

    ... 11 more
     
    Im fixing the other shit.... its just im kinda new to this. You are probably cringing at it
     
  6. Does your config actually contain Sound-On-Mention? (Based on copy&pasting to Notepad++, line 24 is):
    Code (Text):
    Boolean isEnabled = main.getConfig().getBoolean("Sound-On-Mention");
    So, either you don't have that in the config, or you don't have a config at all. Also, the if statement below it needs to be fixed.
     
    • Agree Agree x 1
  7. Everything in the config works except the boolean. and yes i fixed the if statement :p
    Code (Text):

    # Prefix used in the broadcast message.
     Prefix: '&8[&cAlert&8]'


    # Prefix used for staff chat.
     Staff-Prefix: '&8[&c&lStaff Alert&8]'


    # Message a player receives when attempting a command with incorrect permissions.
     No-Permission: '&c&lAlert > &7Sorry! You do not have the correct permissions!'



    # Message a player recieves when using the incorrect syntax.
     Usage: '&c&lAlert > &7Please use the correct syntax! /command [message]'



    # Message sent to notify player if the config reloaded properly.
     Reload: '&c&lAlert > &7Successfully reloaded config!'


     # Sound when name written in chat
     Sound-On-Mention: true
     
     
  8. But is the value actually in the config ? I mean is the config being properly generated ? Also print out the config keys/value to see if it's properly loaded.
     
  9. dw i got it working :)
     
Thread Status:
Not open for further replies.

Share This Page