Exp Level Countdown

Discussion in 'Plugin Development' started by x128, Aug 6, 2013.

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

    x128

    Hi,
    I am relatively new to Bukkit development. I am trying to add a exp level countdown to one of my plugins, but I can't figure out how to do it! I need the bar to start when I call the function, and when it ends it needs to run another bit of code. I have been searching Google / docs and experimenting with various code snippets for about 2 hours, but have not found anything. Help would b greatly appreciated.
    Thank you!
     
  2. Offline

    xTrollxDudex

    x128
    Use the scheduler to subtract exp from the player
     
  3. Offline

    x128

    xTrollxDudex
    I know that - but I can't figure out how to do the scheduler
     
  4. Offline

    Woodenplanks320

    x128
    Code:java
    1. Bukkit.getScheduler().scheduleAsyncRepeatingTask(YOURMAINCLASS, new Runnable(){
    2. public void run(){
    3. int countdown = 20;
    4. for(Player p : Bukkit.getOnlinePlayers()){
    5. p.setLevel(countdown);
    6. }
    7. countdown--;
    8. }
    9. }, 0L, 20);
     
    AndyMcB1 likes this.
  5. Offline

    Seadragon91

    @Woodenplanks320 Async task? I think you should never access the Bukkit API from a async task.
     
  6. Offline

    Woodenplanks320

  7. Offline

    Seadragon91


    Read here.
     
  8. Offline

    AndyMcB1

    I changed "YOURMAINCLASS" with mine; "First" however it didn't like it..
    It suggested I change it to "plugin"

    I have this at the top of my code
    Code:java
    1. public static First plugin;


    however it throws an error when using "plugin"

    Any help would be awesome as I have no yet discovered how to use this and the potential behind it is amazing.
     
  9. Offline

    Woodenplanks320

    Seadragon91 okay i changed it

    if you scheduler is in your main class you can use "this"

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

    AndyMcB1

    Code:java
    1. Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    2. public void run(){
    3. int countdown = 20;
    4. for(Player p : Bukkit.getOnlinePlayers()){
    5. p.setLevel(countdown);
    6. p.sendMessage(ChatColor.GREEN + "This is a basic test loop! " + countdown);
    7. }
    8. countdown--;
    9. }
    10. }, 0L, 20);
    11.  
    12.  
    13.  
    14. }



    Code:java
    1. Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    2. int countdown = 20;
    3. public void run(){
    4.  
    5. for(Player p : Bukkit.getOnlinePlayers()){
    6. p.setLevel(countdown);
    7. p.sendMessage(ChatColor.GREEN + "This is a basic test loop! " + countdown);
    8. }
    9. countdown--;
    10. }
    11. }, 0L, 20);
    12.  
    13.  
    14.  
    15. }


    The loop is working! (thanks!) but countdown--; isn't working.. any clue why?


    edit: (see second block of code) fixed!
     
  11. Offline

    Tarestudio

    AndyMcB1
    You will have a problem canceling this.
    Code:java
    1. BukkitRunnable task = new BukkitRunnable() {
    2. int countdown = 20;
    3. @Overwrite
    4. public void run(){
    5. for(Player p : Bukkit.getOnlinePlayers()){
    6. p.setLevel(countdown);
    7. p.sendMessage(ChatColor.GREEN + "This is a basic test loop! " + countdown);
    8. }
    9. countdown--;
    10. if (countdown < 0) {
    11. this.cancel();
    12. }
    13. }
    14. };
    15. task.runTaskTimer(this, 0L, 20L);
     
  12. Offline

    AndyMcB1

    Won't it auto cancel once "countdown" is less than 0?
     
  13. Offline

    Tarestudio

    AndyMcB1
    No, why should it do that? Nothing tells the task to stop if some value of countdown is reached. Maybe setLevel(-1) throws an error, so the task is broken and is 'canceled', but you should not program this way.
     
  14. Offline

    AndyMcB1

    oh right, I meant this though -
    Code:java
    1. if (countdown < 0) {
    2. this.cancel();
    3. }
     
  15. Offline

    Tarestudio

    AndyMcB1
    Oh, if you mean my comment at the beginning of my post, that was related to the code shown before. My code of course should not have this problem ^^
     
  16. Offline

    AndyMcB1

    hah. thought so. Still getting used to "this.x" hard concept to grab for me.

    Anyway, I'm off to bed. Thanks a ton for the help. I really appreciate it.

    -Andy
     
  17. Offline

    redraskal

    Is there a way to subtract a certain amount off of it instead of 1 like 25?
     
Thread Status:
Not open for further replies.

Share This Page