I need some example code.

Discussion in 'Plugin Development' started by Daniel Heppner, Mar 4, 2011.

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

    Daniel Heppner

    As some of you know, I'm making a plugin that will kick someone when they say a certain word. I'm still learning Java but I really want to make this plugin and it should be simple, so could I please have a small example of how to use the onPlayerChat?

    I really need to learn more Java... But this it important for me. :)
    My first plugin; I hope I'm not bothersome with all these requests.

    Daniel.
     
  2. Offline

    retsrif

    In your onEnable method:
    Code:
    PluginManager pm = getServer().getPluginManager();
    
    //I assume you have a PL named playerListener
    pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.Normal, this);
    
    In your PlayerListener:
    Code:
    public void onPlayerChat(PlayerChatEvent event) {
      //Gives you the message the player tries to send
      String msg = event.getMessage();
    }
    Ask if you want more details. :)
     
  3. Offline

    Daniel Heppner

    Great,
    Thank you! Now, how do I go about searching a string for one of the values in an array? Rather, how do I search the person's message for a string from an array of strings. I need it to check each of the strings.

    I've got the kicking and stuff working, all I need is for it to check the user's message for one of the "bad words."
     
  4. Offline

    AchillePomeroy

    Are you saying you have an array of bad words, and want to compare the string to this array? I'm not sure on how you implement it with this set up...
    I'd do it more along the lines of making the message into an array of words, and iterate over that array, comparing it to each bad word.

    Assuming you've got the message using the bit of code in a couple posts above:
    Code:
    String[] str = msg.split(" ");
    for (i = 0, i < str.size(), i++) {
        String s = str[i];
        if (s.equals("badWord1" || s.equals("badWord2") || s.equals("badWord3")) {
            //kick user
        }
    }
    For each bad word just add another OR statement. Hope this helps. :)
     
  5. Offline

    Daniel Heppner

    Thank you, but there is one problem with that code that I can see and that's that if I want to add a config file where one could easily add new words, this wouldn't work.

    Also, I don't really understand how your code works; how is it checking str to see if it contains one of the bad words? Your code says "s.equals("badWord")" wouldn't that just check s and not str.
    --- merged: Mar 5, 2011 5:05 PM ---
    Okay, I get it now. How come you put all the other s.equals inside the first one?

    This is irrelevant to the topic of the conversation. Could an admin please delete it?
    --- merged: Mar 5, 2011 5:28 PM ---
    Okay, I'm getting a million error messages. It doesn't like your for statement.
    Here's my code:
    https://github.com/danielhep/Librar...listener/player/ChatCensorPlayerListener.java

    Try throwing it into an IDE. You'll see errors. Unless I forgot to close something beforehand...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 11, 2016
  6. Offline

    mindless728

    there is a missing parenthesis after "badWorld1"

    sould be
    Code:
    String[] str = msg.split(" ");
    for (i = 0, i < str.size(), i++) {
        String s = str[i];
        if (s.equals("badWord1") || s.equals("badWord2") || s.equals("badWord3")) {
            //kick user
        }
    }
    coult be shortened to
    Code:
    for(String s : msg.split(" ")) {
        if (s.equals("badWord1") || s.equals("badWord2") || s.equals("badWord3")) {
            //kick user
        }
    }
     
  7. Offline

    Daniel Heppner

    I
    I'm still getting an error around the (" ")). It doesn't like that last parenthesis. And it still doesn't solve my problem of getting a config file in there. Thanks a lot though. I must say I've learned quite a bit from these forums. :)
     
  8. Offline

    retsrif

    The parentheses should be there. Give us the errors you have. I'll tell you what's wrong.
     
  9. Offline

    AchillePomeroy

    Yes, I apologize for the confusion, I forgot a parenthesis. That's what I get for writing code at 2 in the morning.

    And you're right, that sort of code wouldn't work at all for a config file. I am actually just starting looking at programming plugins myself and don't know how to do that yet. Sorry I can't be of any more help.
     
  10. Offline

    Lodran

    @Daniel,

    You should look at using java.util.HashSet to contain your list of bad words, then you can use badwords.contains(testWord) to determine whether or not a word is in the list.

    i.e. something like this:
    Code:
    public class shutyomouf extends JavaPlugin
    {
      private final HashSet<String> badwords = new HashSet<String>();
      ...
      public boolean wordIsBad(String inTestWord)
      {
        return badwords.contains(inTestWord);
      }
    }
    Of course, you'll have to add code to fill badwords with your set of bad words.
     
  11. Offline

    Daniel Heppner

    What do you mean "You'll have to add code too fill badwords with your set of bad words."?
    Aside, this is great. What is the inTestWord var supposed to be? I'll experiment. Thanks. :D
    --- merged: Mar 6, 2011 7:10 PM ---
    I just got it. :p
    --- merged: Mar 6, 2011 7:21 PM ---
    Nevermind, I don't get it at all. I just finished breakfast so now that I can really look at it, I realize that I don't get it at all. I don't understand HashSet.
    --- merged: Mar 6, 2011 7:33 PM ---
    How do I make a config file?
    Also, I'm sorry to be posting so much, I'm just running into problems along the way that I need help with. Expect a billion more merges/updates.
     
  12. Offline

    Lodran

    You'll need to read up on java collections - it's pretty essential to successful coding.

    As for reading a config file, that's not something I've put into my plugin. If I ever need to, I'll probably download the source to some other plugin that uses a config, and read through its code.
     
  13. Offline

    Daniel Heppner

    Okay, I got the code working but it only detects when you say just the badword.
    Example:
    Here's the code: http://pastebin.com/e8EUWQTR
    And here's the correct branch on GitHub: https://github.com/danielhep/Library-Builder/tree/HashSet
    On GitHub I just updated it to use an array, but that doesn't work at all. You can use the history to go back one commit to see what it looked like in PasteBin.
     
  14. Offline

    Lodran

    Your next step will be to use the String.split method to break the incoming message up into words, so you can match each word against the bad words list - google "Java String split example whitespace" for examples.
     
  15. Offline

    ddj

    look at my wip plugin
    it does what you want and more (source included in jar)
     
  16. Offline

    Dragonowner

    This is better and easier
    Code:
    String[] badWords = {"badword1", "bw2", "bw3", "bt4", "bw5", "etc...."}; // More here...
    
    String[] str = msg.split(" ");
                for (String f : badWords) {
                    if (str.toLowerCase().contains(f)) {
                        //add kick code here, or call the method to kick
    
                    }
                }
     
  17. Offline

    Lodran

    The advantage to this approach is that it'll find badwords that aren't surrounded by spaces. The disadvantage is that it'll find badwords that aren't surrounded by spaces, and kick players for saying things like "I like grape soda".

    The other disadvantage is that performing a linear search on an array is *way* inefficient when compared to binary search performed by a HashSet.

    One of the basic tenets of good plugin design is "First, do no harm." - the efficiency of your plugin should always be foremost in your design.
     
  18. Offline

    Dragonowner

    Whenever I used this on my RSPS it would find just those words.

    Code:
    String[] curseWords = {""};//removed the words just so I don't get in trouble on the forums
            for (String c : curseWords) {
                if (p.chatText.toLowerCase().contains(c)) {
                    String replace = p.chatText.replace(c, "****");//simple filter
                    p.chatText = replace;
                }
            }
    Also I wish to become a future Plugin dev. I might have to bug you a couple times ;)
     
  19. Offline

    Daniel Heppner

    Great, so what should I do? How should I do this? You said that it's both an advantage and disadvantage that it'll find badwords surrounded by spaces. So what would you recommend I do to make this? Some kind of for each array item type of thing, or should I use a hashmap? I really have no idea how to use one or what it is, so I could use some help with that too. :)
     
  20. Offline

    Lodran

    Honestly, the performance difference between the two approaches won't really show until you've got hundreds of words in the array/hashmap, so go with whichever approach you're more comfortable with.
     
Thread Status:
Not open for further replies.

Share This Page