Countdown Plugin Help

Discussion in 'Plugin Development' started by Ant_, Jul 9, 2015.

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

    Ant_

    Hello, I have been working on my countdown plugin for my game, but it is extremely laggy when activated, and it does not count in seconds. Any Help would be appreciated. ^_^

    Code:
    package me.ant.threads;
    
    import me.ant.main;
    import me.ant.utilities.ChatUtilities;
    
    import org.bukkit.Bukkit;
    
    public class CountdownS implements Runnable {
    
        private static int timeUntilPlay;
    
        @Override
        public void run() {
            timeUntilPlay = 60;
            while (true) {
                timeUntilPlay = 60;
                for (; timeUntilPlay >= 0; timeUntilPlay--) {
                    if (timeUntilPlay == 0) {
                        main.start();
                        break;
    
                    }
                    if (timeUntilPlay % 10 == 0 || timeUntilPlay < 10) {
                        ChatUtilities.broadcast(timeUntilPlay
                                + "Seconds Until Game Start");
                    }
                   
                }
               
            }
    
        }
    
    }
    
     
  2. Offline

    RainoBoy97

    I suspect it might be your infinite loop causing it :)
     
  3. Make the class instead extending BukkitRunnable.
    Then add a no-parameter constructor where you will have this inside:
    Code:
    runTaskTimer(Plugin plugin, long delay, long period);
    plugin is an instance of your main class,
    delay and period are in ticks. 20 ticks = 1 second.
    Then somewhere else in the class define a new integer for the countdown.
    And for the actual countdown use something like this in the run method:
    Code:
    if (countdown <= 0) {
    //End of the countdown
    cancel();
    } else if (countdown % 10 == 0 || countdown <= 10) {
    //broadcast message
    }
    countdown--;
     
  4. Offline

    poepdrolify

  5. Offline

    Ant_

  6. Offline

    WesJD

    @Ant_ I never knew a BukkitTask was an int?
     
  7. Offline

    Ant_

    My bad, still getting errors on the ends of lines 14 and 16 though.
     
  8. Offline

    WesJD

    @Ant_ The fact that loop is equal to nothing is your problem.
     
  9. BukkitTask is an int. You need to do that to save the ID of the task so you can cancel it later.
    @Ant_ the problems you are having are these:

    - Why is it implementing Listener? It is not an EventHandler class, you do not need to implement it.
    - This is not a thread, this is a BukkitTask.

    So, to help you, I have written one for you.


    Countdown:
    Code:
    package me.ant.threads;
    
    import org.bukkit.Bukkit;
    
    import me.ant.main;
    
    public class CountdownS {
      
        private int timeuntilstart = 60;
        private int time = 60;
        private int id = 0;
      
        public void runScheduler() {
          
            id = Bukkit.getScheduler().scheduleSyncRepeatingTask(main.get(), new Runnable() {
              
                public void run() {
                  
                    if (timeuntilstart == 0) {
                      
                        //Add Starting Method Here Pls
                        Bukkit.getScheduler().cancelTask(id);
                      
                    }
                  
                    if (timeuntilstart % 10 == 0 || timeuntilstart == 3 || timeuntilstart == 2 || timeuntilstart == 1) {
                      
                        Bukkit.broadcastMessage("§c[§aBroadcast§c] §bThere are §d" + timeuntilstart + " §bseconds until the game starts!");
                      
                    }
                  
                    timeuntilstart--;
                  
                }
              
            }, 0l, 20l);
          
        }
    
    }
    
    Main:
    Code:
    package me.ant;
    
    
    import me.ant.commands.Fly;
    import me.ant.commands.Ranks;
    import me.ant.commands.Heal;
    import me.ant.games.Spleef;
    import me.ant.listeners.ChatListener;
    import me.ant.threads.CountdownS;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
    
      
        private static main plugin;
        CountdownS c = new CountdownS();
      
    
        @Override
        public void onEnable() {
            plugin = this;
            getLogger().info("SpawnPlugin Activated");
          
            c.runScheduler();
            commandhandler();
            getServer().getPluginManager().registerEvents(new ChatListener(), this);
            getServer().getPluginManager().registerEvents(this, this);
            getServer().getPluginManager().registerEvents(new Spleef(), this);
          
          
    
        }
    
        @Override
        public void onDisable() {
            plugin = null;
            getLogger().info("SpawnPlugin Deactivated");
        }
        public static void start() {
          
        }
        public static void stop() {
          
        }
        public void commandhandler() {
    
            getCommand("fly").setExecutor(new Fly());
            getCommand("rank").setExecutor(new Ranks());
            getCommand("heal").setExecutor(new Heal());
          
        }
    
        {
    
        }
    
        // --------------------------------------------------------------------------------------------------
    
        // --------------------------------------------------------------------------------------------------
    
        @EventHandler
        public void onPlayerFoodChange(FoodLevelChangeEvent e) {
            e.setCancelled(true);
        }
    
        @EventHandler
        public void onEntityDamage(EntityDamageEvent e) {
            if (!(e instanceof Player)) {
                e.setCancelled(true);
            }
    
        }
    
        @EventHandler
        public void onBreak(BlockBreakEvent e) {
            e.setCancelled(true);
        }
    
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            Location location = new Location(Bukkit.getServer().getWorld("world"),
                    -44, 53, 796);
            player.getLocation().getY();
            if (player.getLocation().getY() <= 20) {
                player.teleport(location);
    
            }
    
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            player.setHealth(20);
            player.setFoodLevel(20);
            if (Bukkit.getOnlinePlayers().size() >= 1) {
                for (Player p : Bukkit.getOnlinePlayers()) {
                    p.sendMessage("Game Start in 30 Seconds");
                }
    
            }
    
        }
      
        public static main get() {
          
            return plugin;
          
        }
    
    }
    
     
  10. Offline

    mythbusterma

    @Sulphate

    The spoonfeeding is real.

    Regardless, a BukkitTask is not an int, how can an Object be a primitive? That doesn't make any sense. BukkitTasks, when scheduled, are assigned a unique task id that can be used to identify them to the Scheduler, which is an int.
     
    WesJD likes this.
  11. Offline

    Ant_

    Thanks a ton!
    http://hastebin.com/jubebocale.avrasm
    Now when I try to add the player count for automatic starting, I'm getting errors on line 23 that read:
    - void is an invalid type for the variable
    onPlayerJoin
    - Syntax error on token ")", ; expected
    - Syntax error on token "(", ; expected

    Any Help?
     
  12. That's what I meant .-.

    EDIT: I meant, when you schedule a new task, it returns the task id, which is an int

    First of all, you don't need a PlayerJoinEvent. Completely unnecessary.

    Just add a bit of code that finds the number of players, resetting it every time it loops. Remember, this will infinately loop until cancelled.

    I have given you what you need, it is up to you to figure out what to do next. If you are really stuck, message me.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page