Solved Clearing a HashMap

Discussion in 'Plugin Development' started by Horsey, Apr 4, 2017.

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

    Horsey

    Hi there everyone!
    I recently started getting into plugin development, and I seem to have managed to understand the basics ;)

    However, I have been trying to make a custom Anti-spam for a server I play on. The problem with traditional anti-spam is that it would just prevent you from spamming, however, that is not what I am searching for. I need a plugin that allows you to spam, but if you keep doing it, you get progressively worse punishments.

    Unfortunately, I havent gotten to the fun punishment part yet, and I have to work out how to clear a hashmap. Ill post my Events class below (as that's the only related one).
    Code:
    package me.kyle.hmpunish.events;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    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.Plugin;
    import me.kyle.hmpunish.Main;
    import net.md_5.bungee.api.ChatColor;
    
    public class Events implements Listener {
      
        Plugin plugin = Main.getPlugin(Main.class); //Loading in main class
      
        Map<String, Long> wait = new HashMap<String, Long>(); //Hashmap for last time a player sent a message
        Map<String, String> message = new HashMap<String, String>(); // Hashmap for the last message sent
        Map<String, Long> infractions = new HashMap<String, Long>(); //Hashmap for the number of infractions  a player recieved for what the server considered spamming
      
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
          
          
            Player player = event.getPlayer();  //Get player
            Long time = wait.get(player.getName());  //Get time of last message
            Long infr = infractions.get(player.getName()); //Get the infractions a player has
          
          
            if(player.hasPermission("hmpunish.bypass")){
                return; //If player has bypass permission, dont execute code
            }
          
            int time2 = plugin.getConfig().getInt("time"); // Get the time between messages to prevent getting an infraction from config
          
            if (time == null || (System.currentTimeMillis()-time) >= (time2*1000)){ //Compares time from last message
                String lastmessage = message.get(player.getName()); //Gets players last message
                    if (lastmessage == null || !lastmessage.equals(event.getMessage())){ //Compares last message
                      
                        message.put(player.getName(), event.getMessage());//Saves new message as last message
                        wait.put(player.getName(), System.currentTimeMillis());//Saves time as wait
                  
                    }
                    else {
                        infractions.put(player.getName(), (infr+1)); //Saves infraction
                        player.sendMessage(ChatColor.RED+"This message too similar to the previous one you sent! This is your "+ChatColor.GOLD+infr+ChatColor.RED+"  infraction."); //Sends player warning message
                    ;
                    }
    
            }else{
                infractions.put(player.getName(), (infr+1)); //Saves infraction
                player.sendMessage(ChatColor.RED+"Slow down! This is your "+ChatColor.GOLD+infr+ChatColor.RED+ " infraction."); //Sends player warning message
                ;
            }
              
          
          
        }  
        public void clearMap(){ //A method that is declared in onEnable, to clear the infractions hashmap
            int clear = plugin.getConfig().getInt("clear");//Gets the time between each clear from config
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin , new Runnable() {
            @Override
            public void run() {
              
                infractions.clear(); //clears infractions
    
            }          
            },0L, 20L*clear);
        }
    
    
    }
    
    Everything seems to work except the part that clears the infractions, as even after it runs the loop (Checked with a broadcast) the infractions keep counting upwards.

    Hope you guys can help me! :)
     
  2. Offline

    Zombie_Striker

    @Horsey
    You never call clearMap, nor do you even subtract and value from the infractions map. I would recommend you create a DelayedTask inside the event for the infractions. You can either subtract 1 from the infractions after a certain amount of time, or you can use the task to check if the player is no longer spamming, and if so, remove the player from infractions.
     
  3. Offline

    Horsey

    @Zombie_Striker
    clearMap is called in the onEnable, however, I do like your idea of using a delayed task that would clear only one player from the list, rather than remove the entire server.

    I'll test it out when I can get on my computer and then I'll post an update. Thanks for the reply.

    Update-

    The code was working, so I proceeded with the plugin, unfortunately, it seems to not be working again. My new code:
    Code:
    package me.kyle.hmpunish.events;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.Plugin;
    import me.kyle.hmpunish.Main;
    import net.md_5.bungee.api.ChatColor;
    
    public class Events implements Listener {
    
        Plugin plugin = Main.getPlugin(Main.class);
    
        Map<String, Long> wait = new HashMap<String, Long>();
        Map<String, String> message = new HashMap<String, String>();
        Map<String, Long> infractions = new HashMap<String, Long>();
    
        @EventHandler
        public void onCmd(PlayerCommandPreprocessEvent event){
            Player player = event.getPlayer();
            String cmd = event.getMessage();
            Long time = wait.get(player.getName());
            Long infr = infractions.get(player.getName());
            int time2 = plugin.getConfig().getInt("time");
        
        
            if (player.hasPermission("hmpunish.bypass")){
                return;
            }
            if (infr == null){
                infr = (long)1;
            }
        
            if (cmd.equalsIgnoreCase("/afk") || cmd.equalsIgnoreCase("/suicide") ){
            
                if(time == null || (System.currentTimeMillis()-time) >= (time2*1000)){
                
                    String lastmessage = message.get(player.getName());
                    if (lastmessage == null || !lastmessage.equals(event.getMessage())){
                    
                        message.put(player.getName(), event.getMessage());
                        wait.put(player.getName(), System.currentTimeMillis());
                
                    }else{
                        infractions.put(player.getName(), (infr+1));
                        player.sendMessage(ChatColor.RED+"This message is too similar to the previous one you sent! You now have "+ChatColor.GOLD+infr+ChatColor.RED+ " infraction(s).");
                        int clear = plugin.getConfig().getInt("clear");
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            @Override
                            public void run() {
                                infractions.put(player.getName(),  (long) 1);
                                Bukkit.broadcastMessage("Infractions cleared!");
                            }        
                        }, 20L*clear);
                    
                    }
                
                }else {
                    infractions.put(player.getName(), (infr+1));
                    player.sendMessage(ChatColor.RED+"Slow down! You now have "+ChatColor.GOLD+infr+ChatColor.RED+ " infraction(s).");
                    int clear = plugin.getConfig().getInt("clear");
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        @Override
                        public void run() {
                    
                            infractions.put(player.getName(),  (long) 1);
                            Bukkit.broadcastMessage("Infractions cleared!");
                        }        
                    }, 20L*clear);
                
                }
                
                }
            
            int a = plugin.getConfig().getInt("infractions1");
            int b =plugin.getConfig().getInt("infractions2");
            int c =plugin.getConfig().getInt("infractions3");
            int d = plugin.getConfig().getInt("infractions4");
            int e = plugin.getConfig().getInt("infractions5");
            String f = plugin.getConfig().getString("warn");
        
            long infr2 = infr;
        
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == a){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),plugin.getConfig().getString("command1")+" "+player.getName()+ " "+plugin.getConfig().getString("command1b")+" "+f);
                        }
                  }
                });
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == b){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), plugin.getConfig().getString("command2")+" "+player.getName()+ " "+plugin.getConfig().getString("command2b")+" "+f);
                            }
                  }
                });
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == c){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), plugin.getConfig().getString("command3")+" "+player.getName()+ " "+plugin.getConfig().getString("command3b")+" "+f);
                            }
                  }
                });
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == d){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),plugin.getConfig().getString("command4")+" "+player.getName()+ " "+plugin.getConfig().getString("command4b")+" "+f);
                        }
                  }
                });
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == e){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), plugin.getConfig().getString("command5")+" "+player.getName()+ " "+plugin.getConfig().getString("command5b")+" "+f);
                            }
                  }
                });
        
        
        
            
            }
        
    
    
    
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
        
        
            Player player = event.getPlayer();
            Long time = wait.get(player.getName());
            Long infr = infractions.get(player.getName());
        
            if (infr == null){
                infr = (long) 1;
            }
        
        
            if(player.hasPermission("hmpunish.bypass")){
                return;
            }
        
            int time2 = plugin.getConfig().getInt("time");
        
            if (time == null || (System.currentTimeMillis()-time) >= (time2*1000)){
                String lastmessage = message.get(player.getName());
                    if (lastmessage == null || !lastmessage.equals(event.getMessage())){
                    
                        message.put(player.getName(), event.getMessage());
                        wait.put(player.getName(), System.currentTimeMillis());
                
                    }
                    else {
                        infractions.put(player.getName(), (infr+1));
                        player.sendMessage(ChatColor.RED+"This message too similar to the previous one you sent! You now have "+ChatColor.GOLD+infr+ChatColor.RED+"  infraction(s)");
                        int clear = plugin.getConfig().getInt("clear");
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            @Override
                            public void run() {
                                infractions.put(player.getName(), (long)1);
                                Bukkit.broadcastMessage("Infractions cleared!");
                            }        
                        }, 20L*clear);
                    }
    
            }else{
                infractions.put(player.getName(), (infr+1));
                player.sendMessage(ChatColor.RED+"Slow down! You now have "+ChatColor.GOLD+infr+ChatColor.RED+ " infraction(s).");
                int clear = plugin.getConfig().getInt("clear");
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    @Override
                    public void run() {
                        infractions.put(player.getName(), (long) 1);
                        Bukkit.broadcastMessage("Infractions cleared!");
                    }        
                }, 20L*clear);
            
            }
            int a = plugin.getConfig().getInt("infractions1");
            int b =plugin.getConfig().getInt("infractions2");
            int c =plugin.getConfig().getInt("infractions3");
            int d = plugin.getConfig().getInt("infractions4");
            int e = plugin.getConfig().getInt("infractions5");
            String f = plugin.getConfig().getString("warn");
        
            long infr2 = infr;
        
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == a){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),plugin.getConfig().getString("command1")+" "+player.getName()+ " "+plugin.getConfig().getString("command1b")+" "+f);
                        }
                  }
                });
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == b){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), plugin.getConfig().getString("command2")+" "+player.getName()+ " "+plugin.getConfig().getString("command2b")+" "+f);
                            }
                  }
                });
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == c){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), plugin.getConfig().getString("command3")+" "+player.getName()+ " "+plugin.getConfig().getString("command3b")+" "+f);
                            }
                  }
                });
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == d){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),plugin.getConfig().getString("command4")+" "+player.getName()+ " "+plugin.getConfig().getString("command4b")+" "+f);
                        }
                  }
                });
            Bukkit.getScheduler().runTask(plugin, new Runnable() {
                  public void run() {
                      if(infr2 == e){    
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), plugin.getConfig().getString("command5")+" "+player.getName()+ " "+plugin.getConfig().getString("command5b")+" "+f);
                            }
                  }
                });
            
        
        }
    
    
    
    }
    That part of the code does not seem to be triggering at all, the broadcast doesnt even go off. Could someone point out where I goofed up the code :confused:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 5, 2017
  4. Offline

    AminerTheGamer


    Ok i think the Problem is From the Config
    when it gets the ClearTime from config
    let find that does it has problem or not you can esealy broadcast the Clear time by:

    int clear = plugin.getConfig().getInt("clear");
    bukkit.broadcastMessage(clear+"");

    then see what it will send you
    if it is "0" so it will cause the Task dont run because the delay of task will be 0

    ill notes you in this Codes you have sent there is not any infractions.clear();

    oh BTW im sorry for my bad ENG
     
  5. Offline

    Horsey

    Found out what was causing the problem; after changing the plugin name in the plugin.yml, I was updating the config file for the previous name :p
     
Thread Status:
Not open for further replies.

Share This Page