Solved Censor Plugin not working

Discussion in 'Plugin Development' started by MrWallah, May 13, 2017.

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

    MrWallah

    Hey Guys!

    Im trying to create a Chat Filter and it won't work!

    Pleas help me
    Code:
    package ch.mrwallah.listener;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class EVENTonChat extends JavaPlugin implements Listener{
    
         List<String> words = new ArrayList<String>();
      
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            String msg = e.getMessage();
            for (String words : this.getConfig().getStringList("Woerter")) {
                if (msg.contains(words)) {
                     e.setCancelled(true);
                     p.sendMessage("§cBitte achte auf deine Wortwahl!");
                }
            }
                  
        }
      
    }
    
    Code:
    package ch.mrwallah.main;
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import ch.mrwallah.listener.EVENTonChat;
    
    
    public class Main extends JavaPlugin {
      
      
      
    
        public void onEnable(){
            System.out.println(ChatColor.AQUA + "ChatFilter - GermanEliteGaming wurde geladen.");
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(new EVENTonChat(), this);
         
            List<String> words = new ArrayList<String>();
          
            words.add("d");
            words.add("or");
           
            this.getConfig().addDefault("blacklist", words);
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
    
        }
      
      
      
      
      
    }
    
     
  2. Offline

    drillzy

    @MrWallah

    I saw a common mistake with starters and u seem to have it. In the listener class, it's not supposed to extend java, just implement listners. Only the main class is supposed to have extends JavaPlugin. Also try to set the priority to HIGHEST for your listener class too.
     
  3. Offline

    MrWallah

    @drillzy

    Then how can i solve this?

    Code:
    for (String words : this.getConfig().getStringList("Woerter")) {
                if (msg.contains(words)) {
                     e.setCancelled(true);
                     p.sendMessage("§cBitte achte auf deine Wortwahl!");
                }
     
    Last edited: May 13, 2017
  4. Offline

    yPedx

    @MrWallah
    I don't see anything wrong with that code.
     
  5. Offline

    MrWallah

  6. Offline

    yPedx

    @MrWallah
    Hover over it, what does it say?
     
  7. Offline

    ipodtouch0218

    @MrWallah
    That's because getConfig is a JavaPlugin method, and when you extend JavaPlugin your class inherits all methods of the JavaPlugin, including getConfig.

    To get the config, pass an instance of the main class to the event class using Constructors
     
  8. Offline

    martian3333

    @MrWallah
    You need to pass an instance of your plugin to your listener and use plugin.getConfig()
     
  9. Offline

    yPedx

    @MrWallah
    Instead of using addDefault, and adding the words everytime the plugin enables;
    Use saveDefaultConfig();
    Remove addDefault and make the list in the config.yml (make one if you don't have it, as a file, of course)
     
  10. Offline

    drillzy

    @MrWallah

    What they all said. you should've just said the problem in the first place that it was with getting the config in the listener class. If your problem is solved, mark this thread as solved in the thread tools section
     
  11. Offline

    MrWallah

    It finally worked. Thanks
     
    Last edited: May 13, 2017
Thread Status:
Not open for further replies.

Share This Page