Help with delayed tasks

Discussion in 'Plugin Development' started by OllyHal, Jun 27, 2012.

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

    OllyHal

    Okay so I have the code :
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {


    Code:
    public void run() {
     
    getServer().broadcastMessage("This message is broadcast by the main thread");
     
    }
     
    }, 60L);
    What replaces myPlugin? I tried my main class name however that does not work
     
  2. if you placed the code insie onEnable, it would be "this"
     
  3. Offline

    OllyHal

    ahhh so I have to place it there? D:
     
  4. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
     
  5. Offline

    OllyHal

    but where would I put my event?
     
  6. you never said event, this is not the code for events, its for registering at the schedular?
     
  7. Offline

    OllyHal

    I was told to use this in my event, what would you suggest for a delay I have this code:
    Code:
    public class MyPotionListener implements Listener {
     
        @EventHandler
        public void SplashPotionListener(PlayerInteractEvent event){
        System.out.println("Testing");
        Player p = event.getPlayer();
     
     
        if(p.getItemInHand().getTypeId() == 373 && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)){
            //insert delay
        }
    }
                  
    Bump

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

    MucTweezer

    You could send a reference to the instance of the plugin to your listener when you create it by creating a constructor for your Listener.

    For example

    Code:java
    1. public class MyPotionListener implements Listener {
    2. private MyPlugin plugin;
    3.  
    4. public MyPotionListener(MyPlugin p) {
    5. plugin = p
    6. }
    7.  
    8. @EventHandler
    9. public void SplashPotionListener(PlayerInteractEvent event){
    10. System.out.println("Testing");
    11. Player p = event.getPlayer();
    12.  
    13.  
    14. if(p.getItemInHand().getTypeId() == 373 && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)){
    15. //insert delay
    16. }
    17. }


    Then, you should be able to use plugin.getServer()...

    Then, in your main class, instead of creating a new instance of your listener with just new MyPotionListener();, you would use new MyPotionListener(this);.
     
  9. Or you can just use Bukkit.getServer()... instead :)

    Then why didn't you ask this in that thread or ask the person who told you to do it ? We can't know what you're trying to do if you don't tell us.

    Still, I suggest you read some basic Java tutorials, basic syntax and stuff :)
     
    ferrybig and MucTweezer like this.
  10. Offline

    MucTweezer

    You and your relentless logic :p But yeah, that probably is better.
     
Thread Status:
Not open for further replies.

Share This Page