Coding your own AntiSwear

Discussion in 'Resources' started by Xyplo, May 31, 2014.

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

    Xyplo

    NOTE: There are many ways that you can do this, the way i'm going to do this is the way that I personally think is the best and most customisable way of doing this. In this tutorial we WILL be using a config as our way of getting the 'banned words'.
    This is the way that I do this and it works perfectly fine, in the way I want it to work.
    PLUS: Do not forget to register the ChatListener.

    This tutorial we will NOT be doing it from the beginning, we will be going as if we have our ChatListener class created already.

    To begin with, make a 'config.yml' and put the string 'banned-words:' just list this...
    Code:
    banned-words:
        - like
        - this
    I'm not going to do anything linked to the MainClass as you should already know how to do that, so in out ChatListener, we're going to add the following to the class so that we can refer the MainClass as plugin and to complete our registeration of the ChatListener. THIS IS THE WAY I DO IT. IF YOU DO IT DIFFERENTLY GO AHEAD AND DO IT YOUR WAY!
    Code:java
    1. MainClass plugin;
    2. public ChatListener(MainClass instance) {
    3. plugin = instance;
    4. }


    So now that we've done that, we just need to add the following:
    Code:java
    1. @EventHandler
    2. public void messageSent(AsyncPlayerChatEvent e) {
    3. //This is where our code will be.
    4. }

    Don't forget to import AsyncPlayerChatEvent as well as EventHandler.
    Now we need to go ahead and add the following inside of the AsyncPlayerChatEvent:
    Code:java
    1. String[] msg = e.getMessage().split(" ");
    2. //Getting the StringList
    3. List<String> censored = plugin.getConfig().getStringList("banned-words");
    4.  
    5. for(String word : msg) {
    6. for(String cword : censored) {
    7.  
    8. if(cword.contains(cword)) {
    9. //We're going to replace the banned word with *'s in this tutorial.
    10. e.getMessage().toLowerCase().replace(cword, "****" );
    11. //We're going to make sure that the message is set to lowercase so that they cannot bypass it
    12. String afterModified = e.getMessage().toLowerCase().replace(cword, "****" );
    13. e.setMessage(afterModified);
    14. }
    15. }

    AND BOOM! You've got a fully functional Anti-Swear plugin that you just coded with help! The code that you should get in the AsyncPlayerChatEvent should look something like this...
    Code:java
    1. @EventHandler
    2. public void messageSent(AsyncPlayerChatEvent e) {
    3. String[] msg = e.getMessage().split(" ");
    4. //Getting the StringList
    5. List<String> censored = plugin.getConfig().getStringList("banned-words");
    6.  
    7. for(String word : msg) {
    8. for(String cword : censored) {
    9.  
    10. if(cword.contains(cword)) {
    11. //We're going to replace the banned word with *'s in this tutorial.
    12. e.getMessage().toLowerCase().replace(cword, "****" );
    13. //We're going to make sure that the message is set to lowercase so that they cannot bypass it
    14. String afterModified = e.getMessage().toLowerCase().replace(cword, "****" );
    15. e.setMessage(afterModified);
    16. }
    17. }
    18. }
    19. }


    If you've got this then you should be all set. Just setup the plugin.yml and make sure that you've CORRECTLY setup the config.yml and you should be sorted.

    If you couldn't follow this little tutorial, please tell me and i'll improve. Got a better way of doing this? O'well, this is my tutorial. This seems to work fine, if you don't mind the message being forced into lowercase. (You can make it go back to normal though). I'm aware that if(cword.contains(cword)) is going to itself, any other way and it didn't work. So yeah.
    Also, no this doesn't have Color Code support. Nor does it have regex and space in word support for e.g. "That was so f ucking cool!"

    FAQ's
    Coming soon, I guess.
     
    TeamJesus likes this.
  2. Offline

    bigteddy98

    Correct my if I am wrong, but I thought working with a config on multiple threads will give problems, right?
     
  3. Offline

    Necrodoom

    if(cword.contains(cword))

    What is the point of this check? Of course a word contains it self.
    You also try to replace the word twice, first time discarding the result, second time actually using it.

    More flaws:
    Sets message to lowercase for no reason
    No space in word support
    No regex support
    No color code support

    While I can understand asking on how to make your plugin on plugin development thread, do not assume that it automatically makes you able to code quality plugins to make good resources. For one to try to teach other people, he must understand the subject first.

    Also, clarify, when I said to check against a lowercased message, I did not mean set the message to lowercase and then check against it, I mean save a lowercased copy, check against it, and then replace word in the real message.
     
  4. Offline

    theguynextdoor

    Oh how I could not agree more. If only more people shared and understood this view.
     
    Plo124 likes this.
  5. Offline

    Xyplo

    I've NEVER had a problem with that.
     
  6. Offline

    bigteddy98

    Maybe Comphenix or ferrybig know more about this?
     
  7. Offline

    Xyplo

    I've been doing this for months, I've had no problem at all, just my experience
     
  8. Offline

    xTrollxDudex

  9. Honestly, anyone with basic knowledge of how configs and the AsyncPlayerChatEvent work could do this. Just saying ;)

    Necrodoom
    I couldn't agree more :)
     
  10. Offline

    creepers84

  11. Offline

    desht

    You're not wrong. The Bukkit config API should not be considered thread-safe. The safe way to do this is to cache the config values in a thread-safe data structure (e.g something discussed in http://stackoverflow.com/questions/6720396/different-types-of-thread-safe-sets-in-java) in the main thread. The event listener can then refer to that.

    Along with the fact that a List is a terrible choice of data structure for this task (a Set is what is needed here) and that this implementation suffers from the Scunthorpe problem, I would not recommend that anyone use it.
     
Thread Status:
Not open for further replies.

Share This Page