EventHandler failing.

Discussion in 'Plugin Development' started by Veyzor, Feb 19, 2016.

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

    Veyzor

    Hello Bukkit community,

    I am currently creating a plugin which has an "ecwadd" command. This is what it says:

    Code:
    else if (cmd.getName().equalsIgnoreCase("ecwadd")){
                    if(p.hasPermission("easyadmin.cw.add")){
                        if(args.length == 0){
                            p.sendMessage(prefix + " Usage: /ecwadd <word>");
                            return true;
                        }
                      
                        if(!censoredWords.contains(args[0])){
                            censoredWords.add(args[0]);
                            p.sendMessage(prefix + " " + args[0] + " has been added to the list!");
                            return true;
                        }else{
                            p.sendMessage(prefix + " This is word is already in the censored words list!\n" + prefix + " Try: /ecwlist");
                            return true;
                        }
                      
                    }
                }
    I have added an ArrayList saver on my onEnable() and onDisable():

    Code:
    public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            this.mutechat = false;
          
            try{
    
                if(configFile.exists() && configFile.isFile()){
    
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(configFile));
    
                 Object o = ois.readObject();
    
                 ois.close();
    
                 this.censoredWords = (ArrayList<String>) o;
    
                 }else{//file not found
    
                    if(getDataFolder().exists() == false || getDataFolder().isDirectory() == false){
      
                    getDataFolder().mkdirs();
      
                     }
                        configFile.createNewFile();
                        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(configFile));
                        ArrayList<String> censor = new ArrayList<String>();
                        oos.writeObject(censor);
                        oos.close();
                        censoredWords = censor;      
                     }
                }catch(Exception e){
                    e.printStackTrace();
            }
        }
    Code:
    public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
          
            if (configFile.exists() && configFile.isFile()){
                configFile.delete();
            }else{
                if(getDataFolder().exists() == false || getDataFolder().isDirectory() == false){
                    getDataFolder().mkdirs();
                }
            }
          
            try{
                configFile.createNewFile();
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(configFile));
                oos.writeObject(censoredWords);
                oos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
          
            saveConfig();
        }
    What my problem is, is that it will save and when I reload the server then when I say the word from the arraylist, it will tell me not to say the word but it does NOT cancel the event if there are more than 2+ words in the arraylist and the server is reloaded. Here is the chatEvent:

    Code:
    @EventHandler
        public void censoredWords(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();
            for(int i = 0; i < censoredWords.size(); i++){
                if(event.getMessage().contains(censoredWords.get(i))){
                    event.setCancelled(true);
                    p.sendMessage(prefix + " Please do not say that word!");
                }
            }
        }
    [​IMG]

    OVERALL:
    This arraylist is not really working for me, could someone show me how to use a config to add/remove curse words? THANKS SO MUCH! <3
     
    Last edited: Feb 19, 2016
  2. Offline

    Skylandz

    I suggest using a stringlist with the badwords , and than check the message if any of the words said are in the stringlist, and if yes, cancel the message.

    I remember that PogoStick29 has an Anticurse tutorial on his channel, althought that video is 4 years old or something around that, and you shouldn't be copying code.

    EDIT: Just thinking about it, saying that Pogo has a video about it but at the same time saying not to use it is like giving a kid a candy and saying not to eat it, lol.
     
    Last edited: Feb 19, 2016
  3. Offline

    Zombie_Striker

    If censoredWords is a List of Strings, you can simply loop through all of the values by using
    for(String cens : censoredWords){
    Make note that :
    1. Message is all lower/upper case
    2. The censored words are all lower/upper case
    And once you set the event to true, there is no reason to continue looping through every single other word in the array. Either use "break;" or "return;" once you know the player has used a bad word.
     
Thread Status:
Not open for further replies.

Share This Page