Solved Chronometre not reseting problem

Discussion in 'Spigot Plugin Development' started by PainDeMie, Jan 20, 2020.

  1. Hello everyone, today, my problem is simple:
    I have a class that detects whenever a player click in an inventory, what is it supposed to do:
    When I click once, it sends in chat First Click and when I click another time, it sends Second Click and the time between the click. When I reclick again it should do the same exact thing.
    What it does: When I click once, it sends in chat First Click and when I click another time, it sends Second Click and the time between the click BUT when I do that again, it uses the previous time and even speed up the counter.

    at the top of the class:
    Code (Java):
    private Main main;
    private Map<UUID, Chronometre> click = new HashMap<UUID, Chronometre>();

    Inside the inventory click event:

    Code (Java):

                    if(click.containsKey(player.getUniqueId())) {
                        // Second Click & Record sent to player

                        click.get(player.getUniqueId()).cancel();
                        click.remove(player.getUniqueId());
                        player.sendMessage("Second click!");
                 
                    }else {
                        // First Click & Record started

                        click.put(player.getUniqueId(), new Chronometre());
                        player.sendMessage("First click!");
                        click.get(player.getUniqueId()).runTaskTimer(main, 0, 1);

                    }
     

    Inside the Chronometre class:
    Code (Java):
    public class Chronometre extends BukkitRunnable {

        public static double time = 0;
     
        @Override
        public void run() {
       
            time += 0.05;
            Bukkit.broadcastMessage(Double.valueOf(time).toString());
       
        }

    }
     

    I don't really know where it comes from. Any answer would be appreciated!
     
    #1 PainDeMie, Jan 20, 2020
    Last edited: Jan 20, 2020
  2. FrostedSnowman

    Resource Staff

    Remove the static keyword from your “time”‘variable. Make it private then apply getters if needed.
     
  3. Thanks, that fixed it, I don't even know why I did this xD