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!
x128 Code:java Bukkit.getScheduler().scheduleAsyncRepeatingTask(YOURMAINCLASS, new Runnable(){ public void run(){ int countdown = 20; for(Player p : Bukkit.getOnlinePlayers()){ p.setLevel(countdown); } countdown--; } }, 0L, 20);
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 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.
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.
Code:java Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){public void run(){int countdown = 20;for(Player p : Bukkit.getOnlinePlayers()){p.setLevel(countdown);p.sendMessage(ChatColor.GREEN + "This is a basic test loop! " + countdown);}countdown--;}}, 0L, 20); } Code:java Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){int countdown = 20;public void run(){ for(Player p : Bukkit.getOnlinePlayers()){p.setLevel(countdown);p.sendMessage(ChatColor.GREEN + "This is a basic test loop! " + countdown);}countdown--;}}, 0L, 20); } The loop is working! (thanks!) but countdown--; isn't working.. any clue why? edit: (see second block of code) fixed!
AndyMcB1 You will have a problem canceling this. Code:java BukkitRunnable task = new BukkitRunnable() {int countdown = 20;@Overwritepublic void run(){for(Player p : Bukkit.getOnlinePlayers()){p.setLevel(countdown);p.sendMessage(ChatColor.GREEN + "This is a basic test loop! " + countdown);}countdown--;if (countdown < 0) {this.cancel();}}};task.runTaskTimer(this, 0L, 20L);
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.
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 ^^
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