why doesn't it countdown?

Discussion in 'Plugin Development' started by BeastCraft3, Nov 19, 2014.

?

Have you also tried to figure this out and just can't find a solution?

  1. Yes

    50.0%
  2. No

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

    BeastCraft3

    Hellau MrPeople. I'm having some issuess with my code. It won't countdown. Please if you know what I've done wrong respond.
    Code:java
    1. package com.MrBeast.PrisonPurge;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class PrisonPurge extends JavaPlugin{
    12.  
    13. public HashMap<String, Long> cooldowns = new HashMap<String, Long>();
    14.  
    15. public void onEnable() {
    16. System.out.println("PrisonPurge Enabled!");
    17. }
    18.  
    19. public void onDisable() {
    20. System.out.println("PrisonPurge Disable!");
    21. }
    22.  
    23. @Override
    24. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    25. if(label.equalsIgnoreCase("purge")) {
    26. Player p = (Player) sender;
    27. if(p.hasPermission("PrisonPurge.start")) {
    28. int cooldownTime = 960;
    29. if(cooldowns.containsKey(sender.getName())) {
    30. long secondsLeft = ((cooldowns.get(sender.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
    31. if(secondsLeft>0) {
    32. sender.sendMessage("You cant use that commands for another "+ secondsLeft +" seconds!");
    33. return true;
    34. }
    35. }
    36. // No cooldown found or cooldown has expired, save new cooldown
    37. cooldowns.put(sender.getName(), System.currentTimeMillis());
    38. if(cooldownTime == 960) {
    39. Bukkit.broadcastMessage("§cThe Purge will begin in 1 minute!");
    40. } else if(cooldownTime == 930) {
    41. Bukkit.broadcastMessage("§cThe Purge will begin in 30 seconds!");
    42. } else if(cooldownTime == 910) {
    43. Bukkit.broadcastMessage("§cThe Purge will begin in 10 seconds!");
    44. } else if(cooldownTime == 903) {
    45. Bukkit.broadcastMessage("§cThe Purge will begin in 3 seconds!");
    46. } else if(cooldownTime == 900) {
    47. Bukkit.broadcastMessage("§cThe Purge has now begin, pvp is allowed EVERYWHERE for 15 minutes. Goodluck");
    48. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag __global__ -w world pvp allow");
    49. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag __global__ -w plotworld pvp allow");
    50. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag ow pvp allow");
    51. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "region flag __global__ -w free pvp allow");
    52. } else if(cooldownTime == 180) {
    53. Bukkit.broadcastMessage("§cThe Purge will end in 3 minutes!");
    54. } else if(cooldownTime == 60) {
    55. Bukkit.broadcastMessage("§cThe Purge will end in 1 minute!");
    56. } else if(cooldownTime == 30) {
    57. Bukkit.broadcastMessage("§cThe Purge will end in 30 seconds!");
    58. } else if(cooldownTime == 10) {
    59. Bukkit.broadcastMessage("§cThe Purge will end in 10 seconds!");
    60. } else if(cooldownTime == 5) {
    61. Bukkit.broadcastMessage("§cThe Purge will end in 5 seconds!");
    62. } else if(cooldownTime < 1) {
    63. Bukkit.broadcastMessage("§cThe purge has now ended, Thanks for playing.");
    64. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag __global__ -w world pvp deny");
    65. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag __global__ -w plotworld pvp deny");
    66. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag ow pvp deny");
    67. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "region flag __global__ -w free pvp deny");
    68. }
    69. }
    70. }
    71. return true;
    72. }
    73.  
    74. }
    75.  
     
  2. Offline

    teej107

    Because the code is only being executed once and you aren't decrementing cooldownTime.
     
  3. Offline

    Rocoty

    I tried magic once too. It didn't quite work as I expected.
     
    es359 likes this.
  4. Offline

    BeastCraft3

    teej107 Rocoty
    What exacly should I add? what do you mean with "decrementing cooldownTime." ???
     
  5. Offline

    guitargun

    BeastCraft3 I think he means that your cooldown isn't changing in value with a lesser value than it first was
     
  6. Offline

    JordyPwner

    you dont have a int for the countdown.. also you are using the cooldown WTF? do you even know basic java?
     
  7. Offline

    BeastCraft3

    JordyPwner
    I got it under controll. I'll create a new class which extends runnable and make the countdown thingy there and just import it into the main class.
     
  8. Offline

    JordyPwner

    What wont work ei
    ther lol. If you really had waited then i had given it to you but you cant wait cause you think you need to finish it when people want it.
     
  9. Offline

    teej107

    He does have an int, he is just not decrementing the value. From looking at it, the code looks fine looks like it works except for the part where he forgot about using Runnables.

    EDIT: Saw your unchecked casting. :p
     
    Rocoty likes this.
  10. Offline

    BeastCraft3

    teej107
    lol yeah, xD. I only got one issue now. I'm trying to "import" or whatever its called my Runnable to make the countdown work. this is both my classes. I don't know how I should make the Runnable thingy in my main class:

    Main:
    Code:java
    1. package com.MrBeast.PrisonPurge;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class PrisonPurge extends JavaPlugin{
    12.  
    13. public HashMap<String, Long> cooldowns = new HashMap<String, Long>();
    14.  
    15. public void onEnable() {
    16. System.out.println("PrisonPurge Enabled!");
    17. }
    18.  
    19. public void onDisable() {
    20. System.out.println("PrisonPurge Disable!");
    21. }
    22.  
    23. @Override
    24. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    25. if(label.equalsIgnoreCase("purge")) {
    26. Player p = (Player) sender;
    27. if(p.hasPermission("PrisonPurge.start")) {
    28. int cooldownTime = 960;
    29. if(cooldowns.containsKey(sender.getName())) {
    30. long secondsLeft = ((cooldowns.get(sender.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
    31. if(secondsLeft>0) {
    32. sender.sendMessage("You cant use that commands for another "+ secondsLeft +" seconds!");
    33. return true;
    34. }
    35. }
    36. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new PrisonRunnable() {
    37.  
    38. }
    39.  
    40. //Ignore this part, I'll remove this after I get the runnable/schefuleSyncDelayedTask to work.
    41. cooldowns.put(sender.getName(), System.currentTimeMillis());
    42. if(cooldownTime == 960) {
    43. Bukkit.broadcastMessage("§cThe Purge will begin in 1 minute!");
    44. } else if(cooldownTime == 930) {
    45. Bukkit.broadcastMessage("§cThe Purge will begin in 30 seconds!");
    46. } else if(cooldownTime == 910) {
    47. Bukkit.broadcastMessage("§cThe Purge will begin in 10 seconds!");
    48. } else if(cooldownTime == 903) {
    49. Bukkit.broadcastMessage("§cThe Purge will begin in 3 seconds!");
    50. } else if(cooldownTime == 900) {
    51. Bukkit.broadcastMessage("§cThe Purge has now begin, pvp is allowed EVERYWHERE for 15 minutes. Goodluck");
    52. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag __global__ -w world pvp allow");
    53. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag __global__ -w plotworld pvp allow");
    54. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag ow pvp allow");
    55. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "region flag __global__ -w free pvp allow");
    56. } else if(cooldownTime == 180) {
    57. Bukkit.broadcastMessage("§cThe Purge will end in 3 minutes!");
    58. } else if(cooldownTime == 60) {
    59. Bukkit.broadcastMessage("§cThe Purge will end in 1 minute!");
    60. } else if(cooldownTime == 30) {
    61. Bukkit.broadcastMessage("§cThe Purge will end in 30 seconds!");
    62. } else if(cooldownTime == 10) {
    63. Bukkit.broadcastMessage("§cThe Purge will end in 10 seconds!");
    64. } else if(cooldownTime == 5) {
    65. Bukkit.broadcastMessage("§cThe Purge will end in 5 seconds!");
    66. } else if(cooldownTime < 1) {
    67. Bukkit.broadcastMessage("§cThe purge has now ended, Thanks for playing.");
    68. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag __global__ -w world pvp deny");
    69. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag __global__ -w plotworld pvp deny");
    70. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "Region flag ow pvp deny");
    71. Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), "region flag __global__ -w free pvp deny");
    72. }
    73. }
    74. }
    75. return true;
    76. }
    77.  
    78. }
    79.  


    My PrisonRunnable / my runnable class:
    Code:java
    1. package com.MrBeast.PrisonPurge;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.scheduler.BukkitRunnable;
    5.  
    6. public class PrisonRunnable extends BukkitRunnable {
    7.  
    8. private final JavaPlugin plugin;
    9.  
    10. private int counter;
    11.  
    12. public PrisonRunnable(JavaPlugin plugin, int counter) {
    13. this.plugin = plugin;
    14. if (counter < 1) {
    15. throw new IllegalArgumentException("counter must be greater than 1");
    16. } else {
    17. this.counter = counter;
    18. }
    19. }
    20.  
    21. @Override
    22. public void run() {
    23. // What you want to schedule goes here
    24. if (counter > 0) {
    25. if(counter == 960){
    26. plugin.getServer().broadcastMessage("§cThe Purge will begin in 1 minute!" + counter--);
    27. } else if(counter == 930){
    28. plugin.getServer().broadcastMessage("§cThe Purge will begin in 30 seconds!" + counter--);
    29. } else if(counter == 910){
    30. plugin.getServer().broadcastMessage("§cThe Purge will begin in 10 seconds!" + counter--);
    31. } else if(counter == 903){
    32. plugin.getServer().broadcastMessage("§cThe Purge will begin in 3 seconds!" + counter--);
    33. } else if(counter == 900){
    34. plugin.getServer().broadcastMessage("§cThe Purge has now begin, pvp is allowed EVERYWHERE for 15 minutes. Goodluck" + counter--);
    35. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "Region flag __global__ -w world pvp allow");
    36. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "Region flag __global__ -w plotworld pvp allow");
    37. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "Region flag ow pvp allow");
    38. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "region flag __global__ -w free pvp allow");
    39. } else if(counter == 180){
    40. plugin.getServer().broadcastMessage("§cThe Purge will end in 3 minutes!" + counter--);
    41. } else if(counter == 60){
    42. plugin.getServer().broadcastMessage("§cThe Purge will end in 1 minute!" + counter--);
    43. } else if(counter == 30){
    44. plugin.getServer().broadcastMessage("§cThe Purge will end in 30 seconds!" + counter--);
    45. } else if(counter == 10){
    46. plugin.getServer().broadcastMessage("§cThe Purge will end in 10 seconds!" + counter--);
    47. } else if(counter == 5){
    48. plugin.getServer().broadcastMessage("§cThe Purge will end in 5 seconds!" + counter--);
    49. } else if(counter <= 1){
    50. plugin.getServer().broadcastMessage("§cThe purge has now ended, Thanks for playing." + counter--);
    51. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "Region flag __global__ -w world pvp deny");
    52. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "Region flag __global__ -w plotworld pvp deny");
    53. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "Region flag ow pvp deny");
    54. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "region flag __global__ -w free pvp deny");
    55. }
    56. } else {
    57. this.cancel();
    58. }
    59. }
    60.  
    61. }
    62.  
     
  11. Offline

    SyTeck

    Not to be rude, but you should probably try to learn some more Java before starting harder projects.
    It's not very hard, but it may be a bit complex if you don't know what you're doing.

    You should create a timer that decrement every second or so, and then try to check if the counter value has reached a value where you would like to do something.
     
  12. Offline

    Burnsalan18

    Instead of giving your runnable its own class, give it a method and call it when you need it.

    Code:
            public void countdownTask(final Player p, int countdown){
                final int tid = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
            
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        countdown--;
                 
                        if(countdown == 200){
                            p.sendMessage(ChatColor.GREEN + "200 seconds left!");
                        }
                        if(countdown == 100){
                            p.sendMessage(ChatColor.GREEN + "100 seconds left!");
                        }
                        if(countdown == 0){
                            p.sendMessage(ChatColor.GREEN + "0 Seconds left!");
                            //Do something else here!
                        }
                    }
                 
                },  0L, 20L);
             
            }
     
  13. Offline

    JordyPwner

    Your code is so totally wrong...
     
Thread Status:
Not open for further replies.

Share This Page