Getting all strings in one key

Discussion in 'Plugin Development' started by dingus007, Oct 6, 2013.

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

    dingus007

    I used another method for getting all values in one key, so I edited it but it doesn't work. Either the method I use doesn't work or I'm using the wrong function to find the string in the message. Here's my all the code I have.

    Code:java
    1. package net.minedcore.spamcheck;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.configuration.ConfigurationSection;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.EventPriority;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.AsyncPlayerChatEvent;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class SpamCheck extends JavaPlugin implements Listener {
    14. public String _format(String msg){
    15. return ChatColor.BLUE + "[" + ChatColor.DARK_GREEN + "Minedcore" + ChatColor.BLUE + "] " + ChatColor.GOLD + msg;
    16. }
    17. @Override
    18. public void onEnable() {
    19. getLogger().info("Enabling SpamCheck...");
    20. this.saveDefaultConfig();
    21. PluginManager pm = getServer().getPluginManager();
    22. pm.registerEvents(this, this);
    23. }
    24. @Override
    25. public void onDisable() {
    26. getLogger().info("Disabling SpamCheck");
    27. }
    28. @EventHandler(priority=EventPriority.LOW)
    29. public void PlayerChatted(AsyncPlayerChatEvent event) {
    30. Player player = event.getPlayer();
    31. String msg = event.getMessage();
    32. ConfigurationSection cs = getConfig().getConfigurationSection("blockedphrases"); //here's where it gets the config section (key)
    33. for(String s : cs.getKeys(false)) { //it loops through everything in it
    34. if (msg.contains(s) /*&& !(player.isOp())*/) {
    35. event.setCancelled(true);
    36. player.sendMessage(_format(ChatColor.RED + "Your message is not allowed."));
    37. }
    38. }
    39. }
    40. }


    here is my config.yml

    HTML:
    #Minedcore SpamChecker
    blockedphrases:
      - 'lag'
      - 'server sucks'
      - 'i hate this server'
      - 'hate this server'
      - 'hate'
      - 'noob'
      - 'can i have admin'
      - 'give me admin'
      - 'i want admin'
      - 'admin me'
      - 'can i have op'
      - 'give me op'
      - 'i want op'
      - 'op me'
      - 'do you need help'
      - 'can i be staff'
      - 'give me items'
      - 'make me staff'
      - 'ddos'
      - 'dos'
      - 'hack'
      - 'nigger'
      - 'need staff'
      - 'want staff'
     
  2. Offline

    chasechocolate

    It's not a configuration section, it's a string list:
    Code:java
    1. for(String blocked : config.getStringList("blockedphrases")){
    2. if(msg.contains(blocked)){
    3. //It's blocked, send message, cancel event, etc.
    4. break; //Prevent multiple messages being sent if they say more than one blocked string
    5. }
    6. }
     
  3. Offline

    dingus007

    Thanks, I'll try that. Also, why did you put a break statement in? I don't understand why I would use that other than stopping the loop.
     
  4. Offline

    chasechocolate

    dingus007 I guess you could either use break or return. It's just if someone, for example said "make me staff or I will ddos you" it won't send a message saying you can't say that twice.
     
  5. Offline

    dingus007

    Well all it says is "Your message was not allowed"
     
  6. Offline

    chasechocolate

Thread Status:
Not open for further replies.

Share This Page