Timer not working!

Discussion in 'Plugin Development' started by jusjus112, Aug 22, 2014.

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

    jusjus112

    hey, i have createdd a timer for a minigame, when a player steps on a pressureplate the timer goes on. But it didnt work, he says "timer started" and after you step again on it, he says "you time: null"
    I dont know what i am doing wrong? B.T.W the timer is for each player!
    Here is the code:
    Code:java
    1. //Main class
    2. public static HashMap<Player, Integer> timerhash = new HashMap<Player, Integer>();
    3.  
    4. //Listener class
    5. @EventHandler
    6. public void onWalkOnPP(final PlayerInteractEvent event) {
    7. if (SpeedRunManager.getManager().players.contains(event.getPlayer())) {
    8. final Player p = event.getPlayer();
    9. if(event.getAction() == Action.PHYSICAL) {
    10. if (SpeedRunManager.getManager().normal.contains(event.getPlayer())) {
    11. if (!Main.timer.contains(p.getName())) {
    12. Main.timer.add(p.getName());
    13. p.sendMessage("Timer Started!");
    14. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    15. @Override
    16. public void run() {
    17. if (SpeedRunManager.getManager().players.contains(event)) {
    18. if (Main.timerhash.isEmpty()) {
    19. Main.timerhash.put(p, 1);
    20. }else {
    21. Main.timerhash.put(p, Main.timerhash.get(p)+1);
    22. }
    23. }
    24. }
    25. }, 20, 20);
    26. }else {
    27. Main.timer.remove(p.getName());
    28. p.sendMessage("Your time is: " + Main.timerhash.get(p));
    29. }
    30. }
    31. }
    32. }
    33. }
     
  2. Offline

    Totom3

    jusjus112 The actual error is at line 17. You are testing for an event in a HashMap of Players.
     
  3. Offline

    jusjus112

    Totom3
    At line 17, its not the hashmap. it is testing if the player is in the arena, so thats working!
     
  4. Offline

    reider45


    Are you sure?

    I think it should be

    Code:java
    1. if (SpeedRunManager.getManager().players.contains(event.getPlayer())) {
    2.  


    Note the .getPlayer()
     
  5. Offline

    Totom3

    jusjus112 Yeah, when I said line 17 I meant line 17 of the code you posted. The line reider45 posted above
     
  6. Offline

    jusjus112

    reider45 Totom3
    ow, now i see it! -,- sorry, i gone fix it

    Now i got another problem erything works but, if 2 poeple walk over the pressure plate, 1e player got"Your time is: 50 seconds", and the 2e player got "Your time is: null seconds"
    Here is the code:
    Code:java
    1. @EventHandler
    2. public void onWalkOnPP(final PlayerInteractEvent event) {
    3. if (SpeedRunManager.getManager().players.contains(event.getPlayer())) {
    4. final Player p = event.getPlayer();
    5. if(event.getAction() == Action.PHYSICAL) {
    6. if (SpeedRunManager.getManager().normal.contains(event.getPlayer())) {
    7. if (!Main.timer.contains(p.getName())) {
    8. Main.timer.add(p.getName());
    9. p.sendMessage(ChatColor.GREEN + "Timer Started!");
    10. id = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    11. @Override
    12. public void run() {
    13. if (SpeedRunManager.getManager().players.contains(event.getPlayer())) {
    14. if (Main.timerhash.isEmpty()) {
    15. Main.timerhash.put(p, 1);
    16. }else {
    17. Main.timerhash.put(p, Main.timerhash.get(p)+1);
    18. }
    19. }
    20. }
    21. }, 20, 20);
    22. }else {
    23. p.sendMessage(ChatColor.GREEN + "Your time is: " + Main.timerhash.get(p) + " seconds!");
    24. Bukkit.getServer().getScheduler().cancelTask(id);
    25. Main.timerhash.remove(p);
    26. Main.timer.remove(p.getName());
    27. }
    28. }
    29. }
    30. }
    31. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  7. Offline

    Marten Mooij

    jusjus112 I think this should help you out:
    Code:java
    1. cooldownRTime = new HashMap<Player, Integer>();
    2. cooldownRTask = new HashMap<Player, BukkitRunnable>();

    Code:java
    1. //Cooldown
    2. cooldownRTime.put(player, 10));
    3. cooldownRTask.put(player, new BukkitRunnable() {
    4. public void run() {
    5. cooldownRTime.put(player, cooldownRTime.get(player) - 1);
    6. if (cooldownRTime.get(player) == 0) {
    7. cooldownRTime.remove(player);
    8. cooldownRTask.remove(player);
    9. cancel();
    10. }
    11. }
    12. });
    13. cooldownRTask.get(player).runTaskTimer(this, 20, 20);
    14. }
    15. }
    16. }

    Code:java
    1. if (cooldownRTime.containsKey(player)) {
    2. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.RED + "EXAMPLE" + ChatColor.DARK_GRAY + "]:" + ChatColor.WHITE + " You must wait " + cooldownRTime.get(player) + " more seconds.");
    3. return;
    4. }
     
  8. Offline

    Dragonphase

    jusjus112

    Why is your timerhash field static? Please do not use statics where they absolutely aren't necessary.

    jusjus112

    You have not set up your groups.yml file correctly. Use this YAML Parser to see where things went wrong.

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

    Laxer21117

    I made a timer a bit differently. If you want to see mine.
     
  10. Offline

    jusjus112

    Marten Mooij
    i dont know if you get my problem, but the thing i want is to make a TIMER, not a cooldown. My problem isnt fiksed, i get the same message. I CANT set a timer for more then 2 players!
     
Thread Status:
Not open for further replies.

Share This Page