[Urgent] BarAPI CountDown Help

Discussion in 'Plugin Development' started by XxZHALO13Xx, Jun 27, 2014.

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

    XxZHALO13Xx

    Hello, I've been trying to make a countdown that uses the boss bar and changes the message and health of the bar. How would i do that? dont say we arent going to spoonfeed. I cant learn if i cant see what makes it run. So showing me the code helps me figure it out. Thanks.
     
  2. Offline

    JasonDL13

    Take a look here: http://wiki.bukkit.org/Scheduler_Programming

    I'm not going to code it for you, but if you look at:

    Bukkit.getServer().getScheduler().runTaskTimer(Plugin, Runnable, Int, Int)

    It will run every so often ticks

    for example:

    Code:java
    1. plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
    2.  
    3. public void run() {
    4.  
    5. Bukkit.broadcastMessage("Hello!");
    6.  
    7. }
    8.  
    9. }, 10, 20);


    will schedule the code in public void run() {} to be run after 10 ticks, then repeating every 20 ticks.

    So the way I would do that is make a class that implements Runnable. And make it have a varible as an int that will be the health to set it at. Then the timer will decrease it.

    Code:
    public class Timer implements Runnable {
     
    int health = 0;
     
    public void run() {
    //This method is required by Runnable
     
    health++;
    //Every so often ticks the health will increase.
    //This can be called by replacing new Runnable() with new Timer() or whatever your class name is, since this class is now an instanceof a runnable
     
    }
     
    }
     
  3. Offline

    XxZHALO13Xx

    JasonDL13 what do i change plugin to?

    plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {


    that... what do i change plugin to? i get a error right now

    JasonDL13
    ok so heres what i have right now

    Code:java
    1. package BarCount;
    2.  
    3. import me.confuser.barapi.BarAPI;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. import org.bukkit.scheduler.BukkitScheduler;
    8.  
    9. /**
    10. * Created by ZH on 6/27/2014.
    11. */
    12. public class Bar extends JavaPlugin {
    13.  
    14. public void onEnable() {
    15. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    16. scheduler.scheduleSyncDelayedTask(this, new Runnable() {
    17. @Override
    18. public void run() {
    19. BarAPI.setMessage(ChatColor.GOLD + "Test");
    20. }
    21. }, 20L);
    22. }
    23. }


    So how do i make the Bar say the time and count down from 60? please help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    LightMC

    ATM you have a delayed task so it will run in 1 second(If I'm not mistaken), heres what you would want if you want the timer to start immediately at startup,
    Code:java
    1. @Override
    2. public void onEnable() {
    3. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    4. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    5. int time = 60;
    6. @Override
    7. public void run() {
    8. if (time >= 1) {
    9. for (Player p : Bukkit.getOnlinePlayers()) {
    10. BarAPI.setMessage(p, ChatColor.GOLD + "Time left " + time, time);
    11. }
    12. time--;
    13. } else {
    14. for (Player p : Bukkit.getOnlinePlayers()) {
    15. BarAPI.setMessage("Timer is done.");
    16. Bukkit.getScheduler().cancelAllTasks();
    17. }
    18. }
    19. }
    20. }, 0L, 20L);
    21. }
     
  5. Offline

    xTigerRebornx

  6. Offline

    ArthurHoeke

    XxZHALO13Xx

    Code:
                    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            if(number != -1) {
                                if (number!= 0) {
                                    BarAPI.setMessage("Time left: " + number);
                                    number--;
                                } else {
                                    number--;
    And number is an int.
     
  7. Offline

    XxZHALO13Xx

    LightMC ArthurHoeke neither work. Heres my class file...

    Code:java
    1. package BarCount;
    2.  
    3. import me.confuser.barapi.BarAPI;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.scheduler.BukkitRunnable;
    9. import org.bukkit.scheduler.BukkitScheduler;
    10.  
    11. /**
    12. * Created by ZH on 6/27/2014.
    13. */
    14. public class Bar extends JavaPlugin {
    15.  
    16. @Override
    17. public void onEnable() {
    18. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    19. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    20. int time = 60;
    21.  
    22. @Override
    23. public void run() {
    24. if(time != -1) {
    25. if (time!= 0) {
    26. BarAPI.setMessage("Time left: " + time);
    27. time--;
    28. } else {
    29. time--;
    30. }
    31. }
    32. }
    33. }, 0L, 20L);
    34. }
    35. }
     
  8. Offline

    ArthurHoeke

    XxZHALO13Xx You have BarAPI.setMessage----, But dont you need to define the player?
     
  9. Offline

    XxZHALO13Xx

    ArthurHoeke how do i define the player? i use events when i define but this isnt exactly a event like that

    ArthurHoeke

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    ArthurHoeke

    XxZHALO13Xx
    BarAPI.setMessage(Player player, String message)
     
  11. Offline

    XxZHALO13Xx

    ArthurHoeke doesnt work. Says cant resolve player. heres the file

    Code:java
    1. package BarCount;
    2.  
    3. import me.confuser.barapi.BarAPI;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. import org.bukkit.scheduler.BukkitScheduler;
    8.  
    9. /**
    10. * Created by ZH on 6/27/2014.
    11. */
    12. public class Bar extends JavaPlugin {
    13.  
    14.  
    15. @Override
    16. public void onEnable() {
    17. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    18. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    19. int time = 60;
    20.  
    21.  
    22. @Override
    23. public void run() {
    24. if(time != -1) {
    25. if (time!= 0) {
    26.  
    27. BarAPI.setMessage(Player player, "Time left: " + time);
    28. time--;
    29. } else {
    30. time--;
    31. }
    32. }
    33. }
    34. }, 0L, 20L);
    35. }
    36. }
     
  12. Offline

    ArthurHoeke

    XxZHALO13Xx Why you put that code in your onenable -,-
    You have to put the code not in your onenable and then you can say Player player = e.getPlayer();
     
  13. Offline

    XxZHALO13Xx

    ArthurHoeke i changed it to a command..

    heres the code

    Code:java
    1. package BarCount;
    2.  
    3. import me.confuser.barapi.BarAPI;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Sound;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.scheduler.BukkitScheduler;
    12.  
    13. /**
    14. * Created by ZH on 6/27/2014.
    15. */
    16. public class Bar extends JavaPlugin {
    17.  
    18. public final int time = 60;
    19.  
    20. @Override
    21. public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) {
    22.  
    23. if (cmd.getName().equalsIgnoreCase("uhcstart")){ //Command To Start UHC
    24. final Player player = (Player) sender;
    25. BarAPI.setMessage(player, "Time Left: " + time);
    26. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    27. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    28.  
    29.  
    30.  
    31. int time = 60;
    32. @Override
    33. public void run() {
    34. if(time != -1) {
    35. if (time!= 0) {
    36.  
    37. Bukkit.broadcastMessage(ChatColor.GOLD + "Game Starting In " + ChatColor.GREEN + time);
    38.  
    39. player.setLevel(time);
    40.  
    41. if (time == 10){
    42. Bukkit.broadcastMessage(ChatColor.GOLD + "Game Starting In" + ChatColor.GREEN + " 10 Seconds...");
    43. }
    44. time--;
    45. } else {
    46. time--;
    47. }
    48. if (time == 0){
    49. Bukkit.broadcastMessage(ChatColor.RED + "Started!");
    50. player.setLevel(0);
    51. player.playSound(player.getLocation(), Sound.AMBIENCE_THUNDER, 600, 600);
    52. player.playSound(player.getLocation(), Sound.EXPLODE, 600, 600);
    53. }
    54. }
    55. }
    56. }, 0L, 20L);
    57. }
    58.  
    59. return false;
    60. }
    61.  
    62. }




    heres the error

    PHP:
    [10:59:13 INFO]: XxZHALO13Xx[/127.0.0.1:63186logged in with entity id 321 at (
    [
    world261.30000001192172.082.28324422829803)
    [
    10:59:15 INFO]: XxZHALO13Xx issued server command: /uhcstart
    [10:59:15 ERROR]: null
    org
    .bukkit.command.CommandExceptionUnhandled exception executing command 'uhcs
    tart' 
    in plugin GameAPI v1.0 Beta
            at org
    .bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit
    .jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0
    ) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r
    .java:703) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection
    .java:953) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :815) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit
    .jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
    Caused byjava.lang.NoClassDefFoundErrorme/confuser/barapi/BarAPI
            at BarCount
    .Bar.onCommand(Bar.java:25) ~[?:?]
            
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit
    .jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            ... 
    13 more
    Caused by
    java.lang.ClassNotFoundExceptionme.confuser.barapi.BarAPI
            at java
    .net.URLClassLoader$1.run(Unknown Source) ~[?:1.7.0_51]
            
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.7.0_51]
            
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0_
    51
    ]
            
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.7.0_51]
            
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:77) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.7.0_51]
            
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.7.0_51]
            
    at BarCount.Bar.onCommand(Bar.java:25) ~[?:?]
            
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit
    .jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            ... 
    13 more
    >
     
  14. Offline

    LightMC

    If you want to have it show to every single player then you will have to loop through all of the players, thats what this part of the code was for,
    Code:java
    1. for (Player p : Bukkit.getOnlinePlayers()) {
    That is so you can set the message for all the players, heres a complete version of what you would want.
    Code:java
    1. public void onEnable() {
    2. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    3. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    4. int time = 60;
    5. // Sets the time to 60 seconds
    6. @Override
    7. public void run() {
    8. if (time >= 1) {
    9. //loops through all online players and puts them individualized.
    10. for (Player p : Bukkit.getOnlinePlayers()) {
    11. BarAPI.setMessage(p, "Time left is " + time);
    12. //p is the variable for the player from the loop, this will set it to all players.
    13. }
    14. time--;
    15. //removes time by 1
    16. } else {
    17. for (Player p : Bukkit.getOnlinePlayers()) {
    18. BarAPI.removeBar(p);
    19. //Remove the bar and cancel the tasks.
    20. Bukkit.getScheduler().cancelAllTasks();
    21. }
    22. }
    23. }
    24. }, 0L, 20L);
    25. //runs a repeating task every 20 ticks
    26. // 20 ticks = 1 second
    27.  

    Screenshot of what this would look like, http://prntscr.com/3xfxcn
    And your error is that you don't have BarAPI installed on your server.
     
Thread Status:
Not open for further replies.

Share This Page