Why does this Antispam not work?

Discussion in 'Plugin Development' started by ImAFlo, Jan 1, 2015.

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

    ImAFlo

    Why does this Antispam not work?
    I don't get Errors in the Console or in Eclipse.

    Create Variable on top of the Code:
    Code:
    FileConfiguration ascfg;

    Create File in onEnable():
    Code:
    File asfile = new File(this.getDataFolder() + "/data/antispamfile.yml");
    File asdir = new File(this.getDataFolder() + "/data" );
    if (!(asdir.exists())) {
    asdir.mkdir();
    }
    if (!(asfile.exists())) {
    try {
    asfile.createNewFile();
    } catch (IOException e) {
    System.err.println("[sChat] /sChat/data/antispamfile.yml could not be created");
    }
    }
    ascfg = YamlConfiguration.loadConfiguration(asfile);
    ascfg();
    

    Function ascfg():
    Code:
    private void ascfg() {
    ascfg.options().header("This is a file for temporary variable. Do not modify or delete!");
    }
    

    In AsyncPlayerChatEvent:
    Code:
    if (ascfg.getString(p + ".lastmsg") == null) {
                ascfg.addDefault(p + ".lastmsg", msg);
            } else if (ascfg.getString(p + ".lastmsg") != null) {
                String lastmsg = ascfg.getString(p + ".lastmsg");
                if (lastmsg == e.getMessage()) {
                    e.setCancelled(true);
                    canceled = 1;
                } else {
                    ascfg.set(p + ".lastmsg", e.getMessage());
                }
            }

    In the second config aren't any Strings :/
    Please Help!



    Thanks in Advance

    ~Flo
     
  2. Offline

    SuchSwegMuchWow

    @ImAFlo

    Did you save it? Also I think that a HashMap might be a better way to compare the players last message, but its your choice not mine
     
  3. Offline

    ImAFlo

    What did you mean with save it?
     
  4. Offline

    SuchSwegMuchWow

    @ImAFlo

    Save the file, because it doesn't look like you save it when you put something in
     
  5. Offline

    ImAFlo

    Now I have made a HashMap, but it don't work:

    Code:
    HashMap<String, String> antispam = new HashMap<>();
    
    if (antispam.get(p.getName()) == null) {
    antispam.put(p.getName(), e.getMessage());
    } else {
    if (antispam.get(p.getName()) == e.getMessage()) {
    e.setCancelled(true);
    p.sendMessage("Please dont repeat words.");
    } else {
    antispam.remove(p.getName());
    antispam.put(p.getName(), e.getMessage());
    }
    }
    
     
  6. Offline

    SuchSwegMuchWow

    @ImAFlo

    Is that on AsyncPlayerChatEvent? I don't see the event
     
  7. Offline

    ImAFlo

    Yes.
     
  8. Offline

    SuchSwegMuchWow

    @ImAFlo

    Btw, if you are comparing strings use .equals("String here");

    @ImAFlo

    It should look something like this

    Code:
    if(event.getMessage().equalsIgnoreCase(GetStringFromHashMap){
    
                //Cancel event
                //Put player in HashMap with event.getMessage();
               //Send error message here
    
            }
            else{
               
                //Put player is hash map
               
            }
            
     
    Last edited by a moderator: Jan 5, 2015
  9. Offline

    CGA1123

    @ImAFlo It's better practice to store a players UUID in a hash map, rather than their name. Not that it's a huge thing, but just a little suggestion.
     
  10. Offline

    ImAFlo

    Do not work.
    Code:
    HashMap<UUID, String> antispam = new HashMap<>();
    if (antispam.get(p.getUniqueId()) == null) {
    antispam.put(p.getUniqueId(), e.getMessage());
    } else {
    if (antispam.get(p.getUniqueId()).equals(e.getMessage())) {
    e.setCancelled(true);
    p.sendMessage("Please dont repeat words.");
    } else {
    antispam.remove(p.getUniqueId());
    antispam.put(p.getUniqueId(), e.getMessage());
    }
    }
    p.sendMessage(antispam.get(p.getUniqueId()) + e.getMessage());
    
    
     
  11. Offline

    VG.Developments

    When your setting something in a configuration file i think you have to save if. Essentially i think your first thing you did would have worked if you would have saved the config file. (saveConfig() if its the config.yml).
     
Thread Status:
Not open for further replies.

Share This Page