Counting seconds doesn't work

Discussion in 'Spigot Plugin Development' started by FroZenKeyboard, Apr 18, 2017.

  1. So i got this code:

    And when i place one sandstone, it always says that i have 1 sec. Btw the message is for debug. Anyone know whats wrong?
    Code (Text):
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            if(e.getBlock().getType() == Material.SANDSTONE) {
                Main.player.add(e.getPlayer());
                Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable()
                {
                  public void run()
                  {
                      if(Main.s == 60) {
                          for(Player p : Main.player) {
                              Main.time.put(p, +1);
                              e.getPlayer().sendMessage("§cYou have built in" + Main.time.get(e.getPlayer()));
                              Main.s = 60;
                          }
                      }
                  }
                }, 20L,20L);
              }
            }
     
  2. Because you're putting the value 1 in the map for the player. Also don't store player objects, it's leaky.
     
  3. I put +1? and if i try, Main.time.put(p, Main.time.get(p)+1); Causing error
     
  4. I don't spoonfeed, but it's easier to explain this way. Gimme a second and let me rewrite this for you properly.
     
  5. https://gist.github.com/TadahTech/71c9262b72830878ae8bed436155c329

    Ok, that's the proper way of doing it.

    You had to storage fields (Main.player & Main.time) which is unneeded. You were setting a field to same value over and over (Main.s = 60) and I'm not sure why, as you were never changing it. I assume it was an incorrect timing system. You were also creating a new listener for every block placed, which is really really bad and wouldn't work. Since you're updating all the fields with the same value on the same interval, you can just use a global task.

    Note: If you're wanting to be super precise, you should update a player's value according to when he was inserted into the map, but this is just the basic way of showing you how runnables work.