Solved Code not working no idea

Discussion in 'Plugin Development' started by jkjames6, Feb 13, 2014.

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

    jkjames6

    Can someone please read my code , it doesn't work, I don't know why...........

    Please don't say illiterate your events bla I don't under stand it :(

    http://dev.bukkit.org/paste/9343/ or
    @EventHandler
    public void AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent event) {
    Player player = event.getPlayer();
    String message = event.getMessage();
    List<String> words = Arrays.asList("fuck", "shit", "cunt", "bitch", "whore", "wanker", "boner", "gay", "prick", "", "");
    }else if(message.contains((words.toString()))) {
    player.kickPlayer(ChatColor.RED + "Kicked by Firework. Reason: Swearing");
    Bukkit.broadcastMessage(ChatColor.RED + player.getName() + " Got Kicked by Firework Reason: Swearing" );
    event.setCancelled(true);
     
  2. jkjames6
    Illiterate your events? That doesn't make any sense.

    Jokes aside, you have to register your events, otherwise they will simply not work. If you don't understand it, then a simple Google search can help you.

    As for your code, use
    Code:
    for(String word : words) {
    if(message.contains(word)) {
    // cancel event and kick them
    }
    }
    to check if the message contains any of the words in the list.
     
  3. Offline

    xTigerRebornx

    jkjames6 Well, your else if statement is outside the entire method....and you'd have to loop through words, not use words.toString()
     
  4. Offline

    jkjames6

    Assist I have changed it to this:
    }else if(message.contains(word)) {

    And the contains underlines with a red line so I changed it to this: And it still doesen't work :(
    }else if(message.contains((CharSequence) word)) {
     
  5. Offline

    Derpiee

    Code:java
    1.  
    2. List<String> words = Arrays.asList("bad","words","in","this","list");
    3. for(String s : words) {
    4. if(message.contains(s)) {
    5. player.kickPlayer(ChatColor.RED + "Kicked by Firework. Reason: Swearing");
    6. Bukkit.broadcastMessage(ChatColor.RED + player.getName() + " Got Kicked by Firework Reason: Swearing" );
    7. event.setCancelled(true);
    8. }
    9. }
    10.  


    The
    Code:java
    1.  
    2. for(String s : words)
    3.  

    loops (iterates) through every string that's inside of the list words.
    Also, you don't need that else before the if statement.

    If you really want to censor words, you should look into Regex. Regex is a good way to block cuss words because your method may block the word but people can still find ways to type it.

    For example:
    Your method blocks the word cat. I would still be able to type caat or c a t, etc..

    http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
     
    Assist likes this.
  6. jkjames6
    Hmm.. I was thinking of something else when writing that, and now I can't think of anything easy :confused:

    Maybe xTigerRebornx knows how? Currently the only thing I can think of is to split the message with 'word' and checking if the array length is more than 0.
    Code:
    for(String word : words) {
        if(message.split(word).length > 0) {
            // someone's been naughty
        }
    }
    Edit: ignore me, I'm tired after several hour coding session...
     
  7. Offline

    jkjames6

    Derpiee Thanks and I do need the else if because I have other things going on before it that I didn't show ;)
     
  8. Offline

    xTigerRebornx

Thread Status:
Not open for further replies.

Share This Page