Solved Runnable not working in a static void [Solved]

Discussion in 'Plugin Development' started by Just_Jitse, Aug 19, 2014.

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

    Just_Jitse

    Hi I am trying to make a ParticleMenu but my code is not working. Can someone help me please?

    (the error is that I can't use "this" in a static void...)


    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
     
    public class ParticleManager{
     
        public static void FireParticle(final Player p){
                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                 
                    @Override
                    public void run() {
                        ParticleEffect.FLAME.display(p.getLocation().add(0, 2, 0), 0, 0, 0, 0, 10);
                     
                    }
                }, 0, 10);
     
        }}
    
     
  2. Offline

    Rocoty

    Well obviously you can't use 'this' In static context. What did you expect?
     
    AoH_Ruthless likes this.
  3. Offline

    Just_Jitse

    well Rocoty, do you have any other idea for I could make a runnable here?
     
  4. Offline

    AoH_Ruthless

    Just_Jitse
    Learn Java. That would help you the most. Static does not exist for your own convenience.
     
    Garris0n and Gerov like this.
  5. Offline

    Just_Jitse

    AoH_Ruthless I am trying to learn it, but i got an error. So I thought let's aks it the pro coders..
     
  6. Offline

    teej107

  7. Offline

    ZachBora

  8. Offline

    AoH_Ruthless

    ZachBora

    The response isn't out of spite or rudeness. It is clearly evident OP does not have sufficient understanding of Java and is best served to learn the language the third party API is built for. Say you were illiterate and attempting to write a novel. Would it be wrong to tell you to learn to actually read?
     
    _Filip likes this.
  9. Offline

    Just_Jitse

    Still didn't solved the problem...
     
  10. Offline

    Hoolean

    Just_Jitse

    The argument where "this" currently is residing should be a JavaPlugin Object. "this" refers to the current instance of the Object, which is non-existent as static methods do not need the Object to be instantiated.

    You should change it to the instance of your JavaPlugin Object.
     
    Lactem likes this.
  11. Offline

    Lactem

    Hoolean See that's a good answer. Not "learn java."
     
  12. Offline

    Hoolean

    Lactem

    I personally think that it is best to do a mash of both; just saying "learn java" doesn't help, neither does explaining in entirely technical terms, so I usually like to try and explain in a way that makes sense to those who know Java but is also allows users to know which specific concepts to learn if they wish to understand it. :)

    Whilst saying "learn java" might provoke response, it is of my personal opinion that it is generally far more useful to at least say "learn about x in Java".
     
    Lactem likes this.
  13. Offline

    Zacky1

    Heres what you need to do:

    Since you can't reference this in a static object (you can but it doesnt do what you want it to), you need to reference the JavaPlugin object, which is your main class. What you can do is create a static variable that references the object. ex:

    Code:java
    1. public class ParticleManager{
    2. public static void ParticleManager instance;
    3. public ParticleManager(){
    4. instance = this;
    5. }
    6. public static void FireParticle(final Player p){
    7. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(ParticleManager.instance , new Runnable() {
    8.  
    9. @Override
    10. public void run() {
    11. ParticleEffect.FLAME.display(p.getLocation().add(0, 2, 0), 0, 0, 0, 0, 10);
    12.  
    13. }
    14. }, 0, 10);
    15.  
    16. }
    17. }
     
  14. Offline

    ZachBora

    Zacky1 I don't think that'd work, ParticleManager isn't an instance of Plugin.
     
  15. Offline

    teej107

    Zacky1 To get Just_Jitse code to work, he would have to pass his plugin through the the parameters of the method to use in the parameters of the scheduleSyncRepeatingTask. You should read the JavaDocs.
     
  16. Offline

    Zacky1

    Oups minor over sight, didnt see the particle manager didnt extend JavaPlugin
     
  17. Offline

    Just_Jitse

Thread Status:
Not open for further replies.

Share This Page