Repeating Task

Discussion in 'Plugin Development' started by peteyandpika, Jul 5, 2013.

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

    peteyandpika

    i'm trying to check every 2 ticks if an entity is close to me with a repeating task. But for some reason, my code isnt working: (this is in my LISTENER class)
    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask((Plugin) this, new BukkitRunnable() {
    2.  
    3. @Override
    4. public void run() {
    5. for( Entity e : ents ) {
    6. e.setFireTicks(100);
    7. bat.remove();
    8. this.cancel();
    9. }
    10. }
    11.  
    12. }, 0, 2);
    13. }
    14. }
    15. }
    16. }
    17. }
    18. }
    19.  
     
  2. Offline

    xTrollxDudex

    peteyandpika
    You need like a
    Code:java
    1. private JavaPlugin plugin;
    2. //in your field declarations in the class that does not extend JavaPlugin

    And use the plugin reference in the place of (Plugin) this
     
  3. Offline

    peteyandpika

    why does it have to be private?
     
  4. Offline

    iFamasssxD

    You are not giving us much to work with... Try
    Code:
    for(Entity e : player.getNearbyEntities(3.0D, 3.0D, 3.0D) //3.0 is the distance of blocks to check.
    {
    Player p = (Player) e.getEntity()
    //yourcode here
    }
    This is off the top of my head but it should work.
     
  5. Offline

    peteyandpika

    sigh, i just can't get the scheduleSyncRepeatingTask to work!
    Code:java
    1. private JavaPlugin plugin;
    2.  
    3. Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    4.  
     
  6. Offline

    xTrollxDudex

    peteyandpika
    It doesn't have to be private. It's private because no other class is using it
     
  7. Offline

    iFamasssxD

    Add this to the listener class and remove private JavaPlugin plugin;
    Code:
    MainClassNameHere plugin;
     
    private ListenerClassNameHere(MainClassNameHere instance){
    plugin = instance;
    }
     
    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
    //your code here
    },0, 20);
     
  8. Offline

    peteyandpika

    Is this defining a new constructor?
     
  9. Offline

    xTrollxDudex

  10. Offline

    peteyandpika

    it is messing up my main class!
    it is messing up this line in my main class:

    public MyListener ml = new MyListener();

    are u sure i can't just use:
    private JavaPlugin plugin; ?

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

    xTrollxDudex

    peteyandpika
    How is it messing up the line? Makes no sense.
    Also, you don't have to check with us. Just experiment, play around. You might learn something as well :)
     
  12. Offline

    iFamasssxD

    Make sure onEnable that you are registering your listener class.
    And yes remove the public MyListener part.
     
  13. Offline

    peteyandpika

    sigh, the new constructor private MyListener(MainClass instance)
    is making an error in my main class on the line public MyListener ml = new Mylistener();
    that line is the line i need to register my listener class on the onEnable method where i put getServer().getPluginManager().registerEvents(this.ml, this);
     
  14. Offline

    xTrollxDudex

    peteyandpika
    Can't you just use
    Code:java
    1. getServer().getPluginManager().registerEvents(new MyListener(), this);
     
  15. Offline

    peteyandpika

    No because if i use new myListener() it will bring an error because the new constructor u gave me uses the argument Core as put in MyListener class "private MyListener(Core instance)" so when i use registerEvents(new MyListener() it asks that an argument like new registerEvents(new MyListener(Core) be given.

    and of course if i use registerEvents(new MyListener(Core)) another error is given because there is no member named Core in my main class.

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

    iFamasssxD

    Add the this into the constructor
    Code:
    getServer().getPluginManager().registerEvents(new MyListener(this), this);
     
  17. Offline

    xTrollxDudex

    tills13 likes this.
  18. Offline

    tills13

    Can you provide us with a LITTLE more code. What you have given us (in all your comments) is incredibly vague.

    Edit: and brush up on some Java basics, too. It'll help you out a lot.
     
    xTrollxDudex likes this.
  19. Offline

    peteyandpika

    Not working. I do believe i didnt give u enough to look at. ^_^
     
  20. Offline

    tills13

    did you copy paste that line or did you change the Listener class name?
     
  21. Offline

    Compressions

  22. Offline

    Unknowncmbk

    Yes because your call to the new object must take a parameter (as that is what your construct takes).

    public MyListener ml = new MyListener(plugin); <-- main class declaration.

    If you're not even storing the ml as a ref variable, there's not use calling it as such. Change to:

    new MyListener(plugin); <-- main class declaration.
     
  23. Offline

    peteyandpika

    ya i tried that too

    and my variable plugin is declared as such:
    public static Core plugin;
    (Core is my Main class name)

    *Sigh* MyListener is my Listeners class NAME! XD lol

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

    tills13

    OK. Without being vague, what isn't working? Errors? Undesired functionality?
     
    xTrollxDudex likes this.
  25. Offline

    peteyandpika

    i found the issue,
    Code:java
    1. Core plugin;
    2. /*
    3. * new constructor cannot be private because the main class needs to register the listener class with the
    4. * listener classes constructor. it can't do this if the new listener constructor has visibility set to private
    5. */
    6. private MyListener(Core instance) {
    7. plugin = instance;
    8.  

    So i took the private modifier out ^_^
     
  26. Offline

    tills13

    is that everything?
     
  27. Offline

    peteyandpika

    yes thx for asking and helping
     
  28. Offline

    tills13

    you should mark as solved.
     
  29. Offline

    SoThatsIt

    peteyandpika also please watch/read some basic java tutorials, all this problem was is a lack of basic java knowledge.
     
    tills13 likes this.
  30. Offline

    peteyandpika

    is it possible to do this by making your Listener class extend JavaPlugin?
     
Thread Status:
Not open for further replies.

Share This Page