How Can I get this listener to block the swear words included?

Discussion in 'Plugin Development' started by Alco_Rs11, Jan 25, 2016.

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

    Alco_Rs11

    [WARNING] The code provided contains explicit language. Please do not be offended by the words included in the code.
    Hi.
    I'm making a No-Swear plugin for my server, I deop myself (the plugin's designed to let OPs bypass the filter, and when I enter a banned word, the filter does not block the message and throw out a broadcasted warning message. I then check logs and I get an "AsyncPlayerChatEvent" or whatever its called error. How can I fix this error?

    The Code and Error message are inside the spoiler.
    Show Spoiler

    The Error:


    The Listener Code:
     
  2. Offline

    Zombie_Striker

    @Alco_Rs11
    It seems you one posted the imports. Please post your listener class again.
    Edit: Nvm, it just appeared for me.

    BTW: The rules do allow swearing, although it should not be excessive and it should not be used to insult others.
     
  3. Offline

    HenrySartori

    @Alco_Rs11 Holy Crap... Why so many if? Just put if(chat.contains("damn") || chat.contains("crap"))...
    And then continue.

    @Zombie_Striker What? I can see her Listener class, not just the imports... '-'
     
  4. Offline

    Zombie_Striker

    @HenrySartori
    What he should actually he (if you want efficiency) is put all the swearwords into a collection, and if the message contains the word then cancel the event.

    BTW what you currently have checks if it contains ALL of those swear words.
     
    WolfMage1 likes this.
  5. Offline

    WolfMage1

    @Alco_Rs11
    I think there's an event.getRecipients()

    then just use .clear()

    Also, instead of having 50 thousand if checks, just use regex or as @Zombie_Striker said put it into a Collection and then check if the message contains something that's in the Collection.
     
  6. Offline

    HenrySartori

    @Zombie_Striker Really? But || means OR... How am I checking if it contains all the swear words?
     
  7. Offline

    Zombie_Striker

    @WolfMage1
    Yes, but the recipients are almost always everyone on the server, so broadcasting still works.


    @HenrySartori
    I meant that for OP.

    @Alco_Rs11

    Caused by: java.lang.NullPointerException
    at com.efdevelopment.ConEngine.Listener.ConListener.onPlayerChat(ConListener.java:23) ~[?:?]

    This is your actual problem. "plugin" is never set, so it is null.
    [increasing size due to the size of the thread.]
     
    Last edited: Jan 26, 2016
  8. Offline

    HenrySartori

    @Zombie_Striker
    Ah, ok, sorry =)
     
  9. Offline

    WolfMage1

    I don't understand what you're saying.
     
  10. Offline

    Zombie_Striker

    @WolfMage1
    All the recipients are everyone who will receive the message. When you send a message, everyone on the server will receive the message, so everyone is apart of the recipients. When you broadcast a message, everyone is apart of the recipients (for the broadcast). They can be use interchangeably.

    What you said to do about clearing the recipients means that the event still triggers, but no one will receive the message. Bukkit will have to process all the information to not send it to anyone. Why not just cancel the event then? That way, it does not have to process any of the information for the message.
     
  11. Offline

    WolfMage1

    I suggested event.getRecipients().clear() because event.setCancelled() has never worked for me in the AsyncPlayerChatEvent but getRecipients().clear() has.
     
  12. Offline

    Zombie_Striker

    @WolfMage1
    True, it does work, but since he has not stated that event.setCanceled does not work for him, he should try to use the conventional way of canceling the event.
     
  13. Offline

    Redrield

    Alright, the first thing I notice in your plugin is you use a very messy, and inefficient way of checking the chat. A thing I would consider doing if I were you, to not only make it configurable, but make the code nicer to look at, is make the swear words come from a string list in the config, and then use a for each statement to check it. Or as others have said before me, put the swears in a collection and check if there is a word in the message that is in the collection.
     
  14. Offline

    WolfMage1

    I know, but if that doesn't work then at least he has another way :p
     
Thread Status:
Not open for further replies.

Share This Page