Solved How Do I Create A scheduleSyncRepeatingTask?

Discussion in 'Plugin Development' started by mkezar, Dec 28, 2013.

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

    mkezar

    Hi. I want to add a scheduleSyncRepeatingTask to my plugin. I am SUPER new to plugin development and i was wondering how do i do it. Here is what my plugin looks like.
    Code:java
    1. if(commandLabel.equalsIgnoreCase("pespawner")) {
    2. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, T);
    3. player.getWorld().playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, T);
    4. player.sendMessage(ChatColor.RED + "Spawner particle has been enabled!");
    5. }
    6. if(commandLabel.equalsIgnoreCase("pesmoke")) {
    7. player.playEffect(player.getLocation(), Effect.EXTINGUISH, 4);
    8. player.getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 4);
    9. player.sendMessage(ChatColor.GOLD + "Smoke particle has been enabled!");
    10. }
    11. if(commandLabel.equalsIgnoreCase("temp")){
    12.  
    13. }
    14. if(commandLabel.equalsIgnoreCase("pesignal")) {
    15. player.playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 4);
    16. player.getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 4);
    17. player.sendMessage(ChatColor.LIGHT_PURPLE + "Ender particle has been enabled!");

    I want those If commands to have a 1 second tick. So every second that command will be activated. How do i do this?
     
  2. Offline

    Door Knob

    The best way in my opinion is using BukkitRunnables.
    Code:java
    1. new BukkitRunnable(){
    2. @Override
    3. public void run(){
    4. //code to be run
    5. }
    6. }.runTaskTimer(this, 0, 1); //(plugin, delay, and the time between each task)
    7. //i usually put the delay to 0 for it to start right away.
     
  3. Offline

    mkezar

    The funny thing is I have no idea what any of that means due to me being so new to java :p could you give me a simple walkthrough on what to edit? :p
     
  4. Offline

    Door Knob

    mkezar
    No problem. I was there once, I know it's very confusing.
    Alright so there's a class in the Bukkit API called BukkitRunnable, which resembles to a java Runnable, but has a better thread or whatever. (a thread is basically code that runs at the same time as the main code, but separately.) Here, we're creating a new BukkitRunnable object.

    The BukkitRunnable has a default method in it called run(), which is basically all the code that needs to be run. We override this method, and fill it in with our own code. Where it says //code to be run is what you want to change into your code that will be run each tick.

    the runTaskTimer(this, 0, 1) part is basically telling our bukkit runnable to run this task multiple times, with no delay, and to run it each tick. 20 ticks is one second, just saying. The "this" in the parameters just mean run this timer on this plugin. The second parameter, 0, is the delay before executing this task for the first time. The last parameter, 1, is how much time to wait between each iteration of the task.

    For example, if you did this:

    Code:java
    1. new BukkitRunnable(){
    2. @Override
    3. public void run(){
    4. player.sendMessage("boo");
    5. }
    6. } .runTaskTimer(this, 0, 20);


    it would send a message ("boo") to the Player player (assuming it's already a variable) every second.
     
    Wizehh likes this.
  5. Offline

    mkezar

    Thank you so much!!! You basically finished my plugin and made my day :) You made a super hard (for me) lines of code SUPER EASY! I will make sure to give you credit once i post my plugin on bukkit :)

    thank you for your support :)

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

    Door Knob

    mkezar no need to give me credit, just happy to help :)
     
Thread Status:
Not open for further replies.

Share This Page