Solved Countdown interupt when player is gone

Discussion in 'Plugin Development' started by HenkDeKipGaming, Oct 28, 2015.

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

    HenkDeKipGaming

    Hey everyone,

    It might be a little hard to understand, but i'm still gonna try to explain my problem ;)
    So I made a command (/thief <name> ) . When this command is excecuted, it's gonna check if the player is nearby. The player has to stay within 3 blocks of the other player to steal something random from their inv.
    so I made this:

    Code:
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                            public void run() {
                                 if(countNumber !=-1) {
                                 if(countNumber !=0) {
                                     if(countNumber == 5) {                
                                        for(Entity p : ((Entity) sender).getNearbyEntities(3, 0, 3)){
                                            if(p == player) {
                                                sender.sendMessage(ChatColor.GREEN + "Aan het stelen van " + args[0] + "!" + ChatColor.RED + " Nog 5 sec. tot succes!");
                                                countNumber--;
                                            }
                                    }
                                   
                                     } else if(countNumber == 4) {
                                        for(Entity p : ((Entity) sender).getNearbyEntities(3, 0, 3)){
                                            if(p == player) {
                                                sender.sendMessage(ChatColor.GREEN + "Aan het stelen van " + args[0] + "!" + ChatColor.RED + " Nog 4 sec. tot succes!");
                                                countNumber--;
                                            }
                                        }
                                     } else if(countNumber == 3) {
                                        for(Entity p : ((Entity) sender).getNearbyEntities(3, 0, 3)){
                                            if(p == player) {
                                                sender.sendMessage(ChatColor.GREEN + "Aan het stelen van " + args[0] + "!" + ChatColor.RED + " Nog 3 sec. tot succes!");
                                                countNumber--;
                                            }
                                        }
                                     } else if(countNumber == 2) {
                                        for(Entity p : ((Entity) sender).getNearbyEntities(3, 0, 3)){
                                            if(p == player) {
                                                sender.sendMessage(ChatColor.GREEN + "Aan het stelen van " + args[0] + "!" + ChatColor.RED + " Nog 2 sec. tot succes!");
                                                countNumber--;
                                            }
                                        }
                                     } else if(countNumber == 1) {
                                        for(Entity p : ((Entity) sender).getNearbyEntities(3, 0, 3)){
                                            if(p == player) {
                                                sender.sendMessage(ChatColor.GREEN + "Aan het stelen van " + args[0] + "!" + ChatColor.RED + " Nog 1 sec. tot succes!");
                                                countNumber--;
                                            }
                                        }
                                     } else {
                                        for(Entity p : ((Entity) sender).getNearbyEntities(3, 0, 3)){
                                            if(p == player) {
                                                sender.sendMessage(ChatColor.GREEN + "Je hebt succesvol van " + args[0] + " gestolen!");
                                                countNumber--;
                                            }
                                        }
                                     }
                                     }
                                 }
                                         }
                            }, 20L, 20L);
    
    It's in dutch, sorry for that, but it should be understandable if you only look at the code.
    The problem is that when the player walks away and comes back e.g 2 hours later, the countdown will still count down the seconds that weren't count down yet.
    So for example if the countdown is at 3 and the player walks away. then the player comes back e.g an hour later, the countdown will still continue to count down then.
    don't know if I explained that really well, but I think the code will explain itself.
    Thanks!

    EDIT: also when the command is excecuted for the second time after a reload, the countdown goes waaaay to fast... How to prevent this?
     
    Last edited: Oct 28, 2015
  2. Offline

    MordorKing78

    Use a hashmap? When a player is in the range add him to a hashmap if the player isn't in the range anymore remove him from the map. Just check if someone is not in the map the countdown should start from the top.
    (Thats probally how I would of done it)
     
  3. Offline

    Scimiguy

    Use a BukkitRunnable()

    You can cancel it internally with this.cancel()
    @HenkDeKipGaming
     
  4. Offline

    HenkDeKipGaming

    Yeah that's a reallly good solution!
    But with the for loop I use, I can't check if the player is NOT nearby the player anymore, so then there is nowhere I can put this.cancel

    SOLVED!

    Changed it to:

    Code:
    if(countNumber == 5) {                  
                                        List<Entity> nearby =  ((Entity) sender).getNearbyEntities(3,0,3);
                                        if(nearby.contains(player)) {
                                                sender.sendMessage(ChatColor.GREEN + "Aan het stelen van " + args[0] + "!" + ChatColor.RED + " Nog 5 sec. tot succes!");
                                                countNumber--;
                                        } else {
                                            sender.sendMessage(ChatColor.DARK_RED + "Stelen onderbroken. Blijf binnen 3 blokken van de speler af!");
                                            countNumber = 0;
                                        }
                                     
                                     }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  5. Offline

    WHQ

  6. Offline

    HenkDeKipGaming

    Haha indeed ;)
    Was feeling random while choosing this name ;)
     
    WHQ likes this.
Thread Status:
Not open for further replies.

Share This Page