Solved How to run scheduled task onPlayerMove

Discussion in 'Plugin Development' started by i3ick, Oct 26, 2015.

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

    i3ick

    So I want to run a task 5s after a player enters an area. To see if the player is in an area I use this code


    Code:
     public void onPlayerMove(PlayerMoveEvent e){
            int x = (int) e.getPlayer().getLocation().getX();
            int y = (int) e.getPlayer().getLocation().getY();
            int z = (int) e.getPlayer().getLocation().getZ();
            SpawnAreas area = gameController.getAreaIn(x,y,z, e.getPlayer());
            if(area == null){
                if(!(gameController.getPlayerInfo(e.getPlayer()).isInArea())) {
                    return;
                }
                this.dummyobj.decreasePCount();
                e.getPlayer().sendMessage(dummyobj.getPCount() + "");
                gameController.getPlayerInfo(e.getPlayer()).areaInSet(false);
                return;
            }
            else{
                if(gameController.getPlayerInfo(e.getPlayer()).isInArea()) {
                return;
                }
                    dummyobj = area;
                    area.increasePCount();
                    e.getPlayer().sendMessage(area.getPCount() + "");
                gameController.getPlayerInfo(e.getPlayer()).areaInSet(true);
            }
    
    
        }
    For running the scheduler i use a boolean as a safeguard.

    Code:
    public void increasePCount(){
            this.playerCount = playerCount + 1;
            plugin.getLogger().info("dadadww2223wa");
            if(doatask == true){
                plugin.getLogger().info("dadawa");
                canDoTask(false);
                spawnpattern();
            }
        }
    
    
        public void decreasePCount(){
            this.playerCount = playerCount - 1;
            if(doatask == true){
                canDoTask(false);
                spawnpattern();
            }
    
        }
    
    
    boolean doatask = true;
    public void canDoTask(boolean doatask){
    this.doatask = doatask;
    }
    
    public void spawnpattern(){
    while(playerCount > 1){
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
    public void run() {
    plugin.getLogger().info("dafawffwa");
    canDoTask(true);
    }
    }, 60*1000);
    }
    }
    
    
    
    
    However the output is still

    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    [10:07:04 INFO]: [Factions_Multi_Upgrade] dafawffwa
    ...


    What could be the problem? why is it running so often?
    The boolean does work! but it doesn't seem to work fast enough

    Oh wow, it was so obviously that while loop, haha

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 26, 2015
  2. Offline

    DoggyCode™

    Haha :p
     
Thread Status:
Not open for further replies.

Share This Page