Solved Timer isn't working

Discussion in 'Plugin Development' started by MieskeB, Oct 18, 2017.

Thread Status:
Not open for further replies.
  1. Hey everyone! I am new to coding in Java (have some experience in C#) and I was trying to develop a Minecraft plugin. After some coding I found out that my timer isn't working... I want a timer to start when there are more or equal then 2 player in the game. Only nothing happens:

    Here is my code for the timer part:
    UHC.java:
    Code:
    ublic class UHC extends JavaPlugin {
      
        public static int startCountdownId;
    
        public void OnEnable() {
    
            GameState.setState(GameState.IN_LOBBY);
    
            setupConfig();
            registerKits();
    
            startCountdown();
        }
    
        //Some other code to register my listeners and setup my config file here...
    
        @SuppressWarnings("deprecation")
        public void startCountdown() {
            StartCountdown.timeUntilStart = 60;
            startCountdownId = getServer().getScheduler().scheduleSyncRepeatingTask(this, new StartCountdown(this), 201, 201);
        }
    
        public void stopCountdown() {
            getServer().getScheduler().cancelTask(startCountdownId);
        }
    
        public void restartCountdown() {
            stopCountdown();
            startCountdown();
        }
    }
    StartCountdown.java
    Code:
    public class StartCountdown extends BukkitRunnable {
    
        UHC plugin;
      
        public StartCountdown(UHC pl) {
            plugin = pl;
        }
      
        public static int timeUntilStart;
    
        public void run() {
    
            if (timeUntilStart == 0) {
                if (!Game.canStart()) {
                    plugin.restartCountdown();
                    ChatUtilities.broadcast("Cannot start game. Restarting countdown!");
                    return;
                }
                Game.start();
            }
    
            if (timeUntilStart % 10 == 0 || timeUntilStart < 10) {
    
                ChatUtilities.broadcast(String.valueOf(timeUntilStart) + " seconds until the game starts!");
    
            }
          
            timeUntilStart -= 1;
        }
    }
    PlayerJoin.java
    Code:
    public class PlayerJoin extends MGListener{
    
        public PlayerJoin(UHC pl) {
            super(pl);
        }
      
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
                  
            Game.setCanStart(Bukkit.getOnlinePlayers().size() >= 2 );
          
            LocationUtilities.teleportToSpawn(player);
            InventoryUtilities.clearInventory(player);
          
            ItemStack is = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(ChatColor.GREEN + "Kits");
            is.setItemMeta(im);
            player.getInventory().addItem(is);
            player.updateInventory();
        }
    }
    I know it is a lot I am asking for but I would be grateful if someone wants to help me with this problem. THANKS IN ADVANCE!!!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MieskeB onEnable needs to start with lowercase
     
  3. Offline

    MightyOne

    1 second are 20 ticks. Your Runnable runs every 201 ticks = ~10s. That is not 1s.
     
  4. Well I feel so stupid now! Thank you so much!

    Of course, thats true :) thank you very much!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  5. Offline

    MightyOne

    @MieskeB well i do not suppose that this forum is used often to discuss efficiency? But in your case Id actually like to talk about how to improve your code. I mean ibwould strucrure the first 2 classes quiet different
     
  6. I would love to get tips on how to improve my coding! If you want to and like to do that I would greatly appreciate it! :D
     
  7. Offline

    MightyOne

    I hope this is not as subjective as it seems to me. Splitting a Task into the scheduler part and the runnable part was a bit difficult to read for me. In case you want to make a task for many players seperatedly I suggest you to put the whole scheduler thing in one class so you can start it multiple times next to each other. But you just want to have 1 countdown here... Well maybe just put the BukkitRunnable part in the JavaPlugin extending class. It is not 3 miles long or anything so just make it more compact and clearly arranged? And then just start the task with

    Code:
    countDownId = Bukkit#getScheduler#scheduleSyncRepeatingTask(this, new Runnable() {
        @Override
        public void run() {}
    }, delay, period);
    
    Idk just go on if you use scheduler alot you will learn how to deal with it. I had to figure it out as well. I am not sure if there is one perfect way
     
Thread Status:
Not open for further replies.

Share This Page