Solved BarAPI Countdown

Discussion in 'Plugin Development' started by Buizelfan2, Aug 11, 2014.

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

    Buizelfan2

    Thanks for all the help.

    Code if anyone ever needs it:
    Code:java
    1. @Override
    2. public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) {
    3. if (cmd.getName().equalsIgnoreCase("test")) {
    4. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    5. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    6. int time = 10;
    7. float health = 100;
    8. @Override
    9. public void run() {
    10. if (time >= 1) {
    11. for (Player p : Bukkit.getOnlinePlayers()) {
    12. BarAPI.setMessage(p, "Time Left: " + time, health);
    13. }
    14. time--;
    15. health = health - 10;
    16. } else if (time == 0) {
    17. for (Player p : Bukkit.getOnlinePlayers()) {
    18. BarAPI.removeBar(p);
    19. Bukkit.getScheduler().cancelAllTasks();
    20. }
    21. }
    22. }
    23. }, 0L, 20L);
    24. }
    25. return false;
    26. }
     
  2. The reason the bar isn't going down in health is because you're not telling it to!
    Use:
    BarAPI.setMessage(Player player, String message, float percent)
     
  3. Offline

    Buizelfan2

    I tried using the
    BarAPI.setMessage(Player player, String message, second int)
    and had it be for 10 seconds cause that's how long the timer is but it still wont move
    New Code:
    Code:java
    1. public final int time = 10;
    2.  
    3. @Override
    4. public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) {
    5. if (cmd.getName().equalsIgnoreCase("test")) {
    6. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    7. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    8. int time = 10;
    9. @Override
    10. public void run() {
    11. if (time >= 1) {
    12. for (Player p : Bukkit.getOnlinePlayers()) {
    13. BarAPI.setMessage(p, "Time Left: " + time, 10);
    14. }
    15. time--;
    16. } else {
    17. for (Player p : Bukkit.getOnlinePlayers()) {
    18. BarAPI.removeBar(p);
    19. Bukkit.getScheduler().cancelAllTasks();
    20. }
    21. }
    22. }
    23. }, 0L, 20L);
    24. }
    25. return false;
    26. }
     
  4. Offline

    KaitouKidFTW

    @Buizelfan2It doesn't work because, you are running a repeating task and that is causing the bar health to 100%
     
  5. Offline

    Buizelfan2

    That's what i figured I'm just not sure how I can fix that
     
  6. Offline

    KaitouKidFTW

    Buizelfan2 Maybe create another message outside of the loop?
     
  7. Offline

    Buizelfan2

    Now the bar counts down but the numbers don't countdown
     
  8. Offline

    KaitouKidFTW

    Buizelfan2 I mean that you keep the one in runnable, but have another outside of it and see if it works
     
  9. Offline

    Buizelfan2

    It didn't work all it does is once it goes through the runnable sets its with the new message of the countdown and makes the health always be 100% every time it goes through
     
  10. Offline

    PixeledCow

    You should create a runnable that subtracts one from the int time, and then it should update with the message.
     
  11. Offline

    KaitouKidFTW

    Buizelfan2 In the runnable only set message. Outside of runnable have a message with the int seconds
     
  12. Offline

    Zandor300

    You don't even subtract one from time... And time can't be a final becouse final can't be editted...

    Add time--; in your runnable and remove the final statement from line 1.
     
  13. Offline

    TheHandfish

    Time has to be final since it's in an inner class I believe.

    Just make a local variable. You can make it "final int time" (no public/private modifiers required).
     
  14. Offline

    Buizelfan2

    I already have time-- the message counts down like it suppose to it's just the Boss Bars health doesn't go down with it.
     
  15. Buizelfan2
    Code:java
    1. BarApi.setMessage(p, "time left: " + time, time);
     
  16. Offline

    Buizelfan2

    bwfcwalshy TheHandfish Zandor300 PixeledCow KaitouKidFTW RenegadeEagle
    Thanks for the help guys I figured it out.
    New Code:
    Code:java
    1. @Override
    2. public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) {
    3. if (cmd.getName().equalsIgnoreCase("test")) {
    4. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    5. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    6. int time = 10;
    7. float health = 100;
    8. @Override
    9. public void run() {
    10. if (time >= 1) {
    11. for (Player p : Bukkit.getOnlinePlayers()) {
    12. BarAPI.setMessage(p, "Time Left: " + time, health);
    13. }
    14. time--;
    15. health = health - 10;
    16. } else if (time == 0) {
    17. for (Player p : Bukkit.getOnlinePlayers()) {
    18. BarAPI.removeBar(p);
    19. Bukkit.getScheduler().cancelAllTasks();
    20. }
    21. }
    22. }
    23. }, 0L, 20L);
    24. }
    25. return false;
    26. }
     
    PixeledCow and TheHandfish like this.
Thread Status:
Not open for further replies.

Share This Page