Creating a loop.

Discussion in 'Plugin Development' started by SirLittlemonster7, Dec 28, 2012.

Thread Status:
Not open for further replies.
  1. Hi, Im making an announcer thing for my server and I need to know how to create a constant loop.
    I have tryed a new runnable but it only does it once.

    Heres the type of runnable I tryed.

    Code:
        this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            public void run() {
                Bukkit.broadcastMessage("This is a test message run by littles plugin.");
            }
            },60);
        }
     
  2. Offline

    dreadiscool

    Hi,

    You need to use scheduleSyncRepeatingTask, not scheduleSyncDelayedTask

    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
        public void run() { /* code */ }
    }, 20, 200);
    
    There are 20 ticks per second in Minecraft.

    The above code adds a new synchronous runnable to the server that will get start getting called after 1 second. After it starts getting called, it will get called after 10 seconds each time. The 20 specifies the original delay to wait for, and the 200 specifies how much to wait between each re-call.

    Hope this helped!
    dreadiscool
     
  3. Offline

    skipperguy12

    ...

    "scheduleSyncDelayedTask"

    lets look at that again:

    "DelayedTask"

    this is to delay a task that will be run. To make a constant repeating task:

    Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {

    Also, it takes more numbers at the bottom:
    }, 0L, (2 * 20));

    Tip:
    Every 20 ticks = 1 second, so at the bottom you can do something like seconds * 20 for your loop to work.

    I think you should read up on:
    http://wiki.bukkit.org/Scheduler_Programming
     
  4. Offline

    dreadiscool

    He is probably not trying to create an asynchronous task (most Bukkit API calls are not threadsafe) therefore, he should use Sync instead of Async.
     
  5. Offline

    RealDope

    skipperguy12
    Well in his case it didn't take more numbers at the bottom since his was delayed not repeating :3

    Also I'm curious about the difference between scheduling a task with a new Runnable() and with a new BukkitRunnable(). What's the difference?
     
  6. Offline

    gomeow

    If it is a broadcasting plugin, then async would be fine. Otherwise use a sync task
     
  7. Offline

    chaseoes

    No, broadcastMessage() isn't thread safe.
     
    ZeusAllMighty11 likes this.
  8. Offline

    fireblast709

    A BukkitRunnable has methods to start running, get its own ID, and (if you use the BukkitRunnable's methods to start it) can cancel itself
     
  9. Wow thanks guys :)
     
  10. Offline

    danthonywalker

    I like keeping my code structured cleanly, however, whenever I try it my way of code it throws errors of missing closing brackets.

    How I structured the code originally, it's still the same way as your guy's way of doing it:

    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
        {
     
        public void run()
            {
            }
     
        }, 20, 200);
    How Eclipse only will accept it without errors. Of course still trying to follow my own way of rules of structuring code.

    Code:
        {
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
        {
     
        public void run()
            {
            }
     
        }, 20, 200);}
    I literally have no idea what's happening or why it wants to structure it the way it wants to. Can someone explain? And yes, I did try just straight up following your guys' way of doing it, and the wiki's way of doing it, still threw closing/opening bracket errors.
     
  11. Offline

    ZeusAllMighty11

    Well if you're new to java, every time you open a statement, with { ,you need to close it with }. There are no exceptions.

    So for every bracket you see, make sure it closes. Some things, like this runnable, will need to have it liked }); because as you see it says (this, new runnable() X , where X is a missing paranethese, which gets put at the end
     
  12. Offline

    danthonywalker

    ZeusAllMighty11

    I realize the open and closing statements, that's pretty much something you learn right off the getco. What I can't seem to understand is how this runnable is special where it needs 2 opening and 2 closings for 1 statement (excluding the public void run.)
     
  13. Offline

    chaseoes

    It makes more sense when you look at it as a single line:
    Code:
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() { public void run() { getServer().broadcastMessage("This message is broadcast by the main thread");} }, 60L);
    It's actually just a giant method - check out how it starts and ends with parentheses and the ;.
     
  14. Offline

    danthonywalker

    Ok, I understand now, but now I can't seem to be able to implement a way of giving players something. Anyway of doing it so every second it will give everyone an emerald?
     
  15. Offline

    chaseoes

    Code:java
    1. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    2. public void run() {
    3. ItemStack i = new ItemStack(Material.EMERALD);
    4. for (Player player : plugin.getServer().getOnlinePlayers()) {
    5. player.getInventory().addItem(i);
    6. }
    7. }
    8. }, 0L, 20L);
     
    danthonywalker likes this.
  16. Offline

    danthonywalker

    Strange. When I tried that earlier it failed and caused a bunch of errors, now it works fine. :/ Thank you kind sir.
     
Thread Status:
Not open for further replies.

Share This Page