Scheduler Issues?

Discussion in 'Plugin Development' started by TehCoderHD, Aug 31, 2016.

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

    TehCoderHD

    Okay, so basically I have an issue with my plugin, which is when I schedule a delayed task for 20 ticks, it responds in less than 20 ticks if you get what I mean. I made debug code to test.


    Code:
    package me.traineee.anticheat;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import me.traineee.panic.API;
    import me.traineee.panic.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class Autoclicker implements Listener{
    
        public static Map<Player, Integer> clicks = new HashMap();
        public static Map<Player, Integer> vl = new HashMap();
       
        @EventHandler
        public void AutoClicker(PlayerInteractEvent e){
            final Player p = e.getPlayer();
            if(!e.getAction().name().contains("LEFT")){
                return;
            }
            if(clicks.get(p) == null){
                clicks.put(p, 1);
            }else{
                clicks.put(p, (Integer)clicks.get(p) + 1);
            }
           
            Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable(){
    
                @Override
                public void run() {
                    if((Integer)clicks.get(p) > 15){
                        if(!vl.containsKey(p)){
                            vl.put(p, 1);
                        }else{
                            vl.put(p, vl.get(p) + 1);
                        }
                        for(Player all : Bukkit.getOnlinePlayers()){
                            Integer click = (Integer)clicks.get(p);
                            if(all.hasPermission(API.staffPerm)){
                                if((Integer)vl.get(p) < 5){
                                    all.sendMessage("§8[§9RogueNoCheat§8] §6" + p.getName() + " §7is possibly autoclicking §8[§c" + clicks + " CPS§8] §8[§cVL " + vl + "§8]");
                                }else if((Integer)vl.get(p) < 10){
                                    all.sendMessage("§8[§9RogueNoCheat§8] §6" + p.getName() + " §7is likely to be autoclicking §8[§c" + clicks + " CPS§8] §8[§cVL " + vl + "§8]");
                                }else if((Integer)vl.get(p) < 15){
                                    all.sendMessage("§8[§9RogueNoCheat§8] §6" + p.getName() + " §7is most likely autoclicking §8[§c" + clicks + " CPS§8] §8[§cVL " + vl + "§8]");
                                }else if((Integer)vl.get(p) > 15){
                                    all.sendMessage("§8[§9RogueNoCheat§8] §6" + p.getName() + " §7is autoclicking §8[§c" + clicks + " CPS§8] §8[§cVL " + vl + "§8]");
                                }
                                if((Integer)vl.get(p) > 30){
                                    Bukkit.broadcastMessage("§4" + p.getName() + " §cis cheating and has been removed from the network by §9RogueNoCheat§c.");
                                    p.chat("Bye everyone! I've been caught by RogueNoCheat.");
                                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "ban " + p.getName() + " &8[&9RogueNoCheat&8] &cAutoclicking -s");
                                }
                            }
                        }
                    }
                    p.sendMessage("Debug! " + (Integer) clicks.get(p));
                    clicks.remove(p);
                   
                   
                }
               
            }, 20L);
        }
       
       
    
    }
    
    Basically, the scheduler in the code above was sending the debug message less than a second after it was fired, I don't know if im just being dumb or not but I had a 20 cps autoclicker and it wasnt triggering and it spammed the Debug message, which should only be fired every 20 ticks/second.

    The error/thing that is wrong (open)

    [20:53:28] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:29] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:30] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:31] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:31] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:31] [Client thread/INFO]: [CHAT] Debug! 2
    [20:53:31] [Client thread/INFO]: [CHAT] Debug! 1
    [20:53:31] [Client thread/INFO]: [CHAT] Debug! 1


    As you can see in the spoiler above, it was triggering the delayed task like 9 times a second, and the number next to it is the cps registered, I was autoclicking 20 cps and it said I was only doing 1 or 2 cps.

    Please help me asap, ive tried recoding this like 10 times. I'm sure I'm just doing it a dodgy way but this works in most anticheats.

    - Traineeee

    bump

    anyone help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 31, 2016
  2. Offline

    Zombie_Striker

    @TehCoderHD
    That is because you are removing the player from the config every time they click. You are basically resetting the score every time they click. Remove that line to potentially fix your problem.
     
  3. Offline

    TehCoderHD

    @Zombie_Striker
    Its meant to remove the score/count every second. the code calculates how many times they clicked in that second before being removed and if it is over 15 it sends off autoclicker alerts.

    I only just got back into coding a few weeks ago so if im being a noob just say lol
     
  4. @TehCoderHD Why are you casting Integer to Integer? And please follow naming conventions
     
Thread Status:
Not open for further replies.

Share This Page