Changing BarAPI message

Discussion in 'Plugin Development' started by ImPhantom, Apr 16, 2014.

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

    ImPhantom

    So. If you read my post yesterday. I was asking how to repeat an action every couple of seconds. After i got that working. I began having issues with actually changing the message. So i tried this but with BarAPI i dont think there is an easy way to get the current message that is being displayed. Here is what i tried.

    Code:java
    1. public void initBar() {
    2. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    3. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    4. @Override
    5. public void run() {
    6. for(final Player players : Bukkit.getOnlinePlayers()) {
    7. BarAPI.setMessage("Remember to /vote");
    8. if(BarAPI.getMessage(players) == "Remember to /vote") {
    9. BarAPI.setMessage("2nd Message");
    10. }
    11.  
    12. if (BarAPI.getMessage(players) == "2nd Message") {
    13. BarAPI.setMessage("Remember to /vote");
    14. }
    15. }
    16. }
    17. }, 0L, 20L);
    18. }


    How and what would be the best way of doing this?
     
  2. Offline

    Jozeth

    ImPhantom Use:
    Code:java
    1.  
    2. BarAPI.getMessage(players).eqauls("<message>")
    3.  
     
  3. Offline

    ImPhantom

    Jozeth
    It still stays "Remember to /vote" and doesnt change.
     
  4. Offline

    Jozeth

    I know it would, I was just suggesting a better way of seeing what a message equals.
     
  5. Offline

    ImPhantom

    Jozeth
    Oh. Haha. Well thanks for the help. Ill try and work this out. If any others wish to help, tahg me.
     
  6. Offline

    MCForger

    ImPhantom
    Why are you looping over all the players and calling the setMessage method that does not take in a player? Not sure if you knew this but calling the BarAPI.setMessage(String message) loops through all the players already on the server and sets their message to the message passed.

    MCForger
    If you wanted to make it so it only set it for the player you have in your loop pass the player object as an argument before the message.

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

    Jozeth

    ImPhantom
    "BarAPI.setMessage(Player player, String message)

    Set a message for the player. It will remain there until the player logs off or another plugin overrides it.
    BarAPI.setMessage(Player player, String message, float percent)

    Same as above except you can set the % of the health bar. 100 shows the entire health bar, 50 shows half the health bar and so on.
    BarAPI.setMessage(final Player player, String message, int seconds)

    Sets a timed message for the player. It will remain until the timer runs out. The health automatically reduces based on how long the timer is."

    - http://dev.bukkit.org/bukkit-plugins/bar-api/

    You're setting the message wrong...

    Edit:
    Code:java
    1. BarAPI.setMessage(players, "Remember to /vote");
    2. BarAPI.setMessage(players, "2nd Message");
    3.  


    To be honest you could just do:
    Code:java
    1. public void initBar() {
    2. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    3. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    4. @Override
    5. public void run() {
    6. for(final Player players : Bukkit.getOnlinePlayers()) {
    7. BarAPI.setMessage(players, "Remember to /vote", 2);
    8. if(BarAPI.getMessage(players).equals("Remember to /vote")) {
    9. BarAPI.setMessage(players, "2nd Message", 2);
    10. }
    11.  
    12. if (BarAPI.getMessage(players).equals("2nd Message")) {
    13. BarAPI.setMessage(players, "3rd Message", 2);
    14. }
    15. }
    16. }
    17. }, 0L, 7*20L);
    18. }

    Because you're repeating it every second and you are checking if it equals a message then it checks again and gives you back the original message.
    Code:java
    1. public void initBar() {
    2. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    3. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    4. @Override
    5. public void run() {
    6. for(final Player players : Bukkit.getOnlinePlayers()) {
    7. BarAPI.setMessage(players, "Remember to /vote");
    8. if(BarAPI.getMessage(players).equals("Remember to /vote")) {
    9. BarAPI.setMessage(players, "2nd Message");
    10. }
    11.  
    12. if (BarAPI.getMessage(players).equals("2nd Message")) {
    13. BarAPI.setMessage("Remember to /vote");
    14. }
    15. }
    16. }
    17. }, 0L, 20L);
    18. }


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

    Garris0n

    Put continue statements after each setMessage so it doesn't check and loop back to the original message. Also, don't use a plural noun in the loop. It's misleading because it implies that the variable represents every player at once instead of each in sequence.
     
  9. Offline

    ImPhantom

    Garris0n Jozeth MCForger
    Okay. Would it be easier to just iterate through a List<String> that contains all of the strings instead of having to to the if statement stuff?
     
  10. Offline

    Garris0n

Thread Status:
Not open for further replies.

Share This Page