CountDowns

Discussion in 'Plugin Development' started by TehVoyager, Nov 11, 2013.

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

    TehVoyager

    Ok, So in my Plugin There is a countdown method:
    Code:java
    1. public void MinuteCountDown(){
    2.  
    3. cd = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    4.  
    5. GameMode gm = null;
    6. String Mapname = null;
    7.  
    8. @Override
    9. public void run() {
    10. if(counter == 0){
    11. Bukkit.broadcastMessage(cc.prefix + cc.maincolor + "The Game Has Begun!");
    12. Bukkit.getScheduler().cancelTask(cd);
    13. GameManager.getManager().getArena().startGame(gm, Mapname);
    14. }
    15. if(counter == Ranks.getInstance().getConfig().getInt("seconds-inbetween-games")){
    16. Random gametypechooser = new Random();
    17. int mode = gametypechooser.nextInt(3);
    18. if(mode == 0){
    19. gm = GameMode.OITC;
    20. }else if(mode == 1){
    21. gm = GameMode.FFA;
    22. }else{
    23. gm = GameMode.TDM;
    24. }
    25. int maps = Map.getInstance().getMap().getInt("mapid");
    26. int mapid = gametypechooser.nextInt(maps);
    27. for(String mn : Map.getInstance().getMap().getStringList("mapnames")){
    28. if(Map.getInstance().getMap().getInt("maps." + mn + ".id") == mapid){
    29. Mapname = mn;
    30. }
    31. }
    32. }
    33. if(SayNumber.contains(counter)){
    34. int x = counter / 60;
    35. int y = counter % 60;
    36. Bukkit.broadcastMessage(cc.prefix + cc.maincolor + " The Game Will Begin In " + cc.secondcolor + cc.bold + x + cc.maincolor + " Minutes and " + cc.secondcolor + cc.bold + y + cc.maincolor + " Seconds!");
    37. Bukkit.broadcastMessage(cc.prefix + cc.maincolor + " GameMode: " + cc.secondcolor + cc.bold + gm + cc.maincolor + " Map: " + cc.secondcolor + cc.bold + Mapname);
    38. }
    39. counter--;
    40. }
    41.  
    42. }, 0, 20);
    43.  
    44. }

    and then the game starts. When the game ends I call a method I made called endGame and when I call the minute countdown method inside of it it doesn't work. It says its on line 140 of GameArena.java so this line:
    Code:java
    1. plugin.MinuteCountDown();

    How can I fix this?
    Tag Me if you need more info
    Thanks For the Help.

    Umm 1 view?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. I don't know if it's any help or anything, but something I'm doing involves a recursive count-down.

    Code:java
    1. public class CountDownTask extends BukkitRunnable
    2. {
    3. public CountDownTask(int count)
    4. { currentcount = count; }
    5.  
    6. int currentcount;
    7.  
    8. @Override
    9. public void run()
    10. {
    11. if(currentcount <= 0)
    12. {
    13. // Thing to do when countdown finishes.
    14. }
    15. else
    16. {
    17. for(Player player : Bukkit.getOnlinePlayers())
    18. player.setLevel(currentcount); // Or any other way of displaying a count-down.
    19.  
    20. new CountDownTask(currentcount - 1).runTaskLater(SM.getPlugin(), 20);
    21. // Calls the next instance with a decremented counter.
    22. }
    23. }
    24. }


    This can be run by calling new CountDownTask(<number of seconds>).run() (no assignment needed).
     
  3. Offline

    TehVoyager

    Hanii Puppy
    I'll see if I can implement it tomorrow. Could / Should work Thanks

    Hanii Puppy
    What does SM stand for in
    SM.getPlugin()

    I did public static main plugin; then put plugin there but it didn't work.

    B
    U
    M
    P

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Sorry, every time I start a new bukkit plugin, one of the first thing I do is create a class for miscellaneous static methods and fill it with the below code. It gives me a central repository for static methods that don't really go anywhere, and saves me having to do some things like pass references to the plugin around everywhere.

    SM.getPlugin() is just a reference to your plugin, which you can either pass in from wherever you're starting the countdown from, or access from a static class.

    Code:java
    1. public class SM
    2. {
    3. public static void initialise(JavaPlugin plugin)
    4. {
    5. SM.plugin = plugin;
    6. logger = plugin.getLogger();
    7. }
    8.  
    9. static JavaPlugin plugin;
    10. static Logger logger;
    11.  
    12. public static JavaPlugin getPlugin()
    13. { return plugin; }
    14.  
    15. public static void registerListener(Listener listenertoregister)
    16. { Bukkit.getPluginManager().registerEvents(listenertoregister, plugin); }
    17.  
    18. public static void print(String message)
    19. { logger.info(message); }
    20. }
     
  5. Offline

    TehVoyager

Thread Status:
Not open for further replies.

Share This Page