Can you parse variables into a BukkitRunnable?

Discussion in 'Plugin Development' started by TheSporech, Apr 28, 2016.

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

    TheSporech

    ^

    I know I can make the variable final, but say I wanted to parse the BukkitRunnable between objects. Is there a way to do this?
     
  2. Offline

    WolfMage1

    @TheSporech Be a bit more descriptive of what you want to achieve.
     
    mine-care likes this.
  3. Offline

    mine-care

    @TheSporech a BukkitRunnable is an Object, so yes as with all objects you can pass its pointer between classes.
    But as it was said above we need more information to grip on and explain.

    And before you or anyone says it, No. Static isn't a solution.
     
  4. Offline

    TheSporech

    If I had something like a class constructor or method which took a BukkitRunnable as a parameter, the and the runnable uses attributes of the class, when you define the runnable in another class, it won't have any idea what the attributes of the other class are, and would error.

    Hope this explains it, it isn't easy to explain.
     
  5. Offline

    mine-care

    @TheSporech
    You pass an instance of the class so yes it has all it needs :/ Have a look at Objects and follow the tutorials as they go where you can possibly find a more detailed answer explained better.
    Also dont foget to tahg me otherwise i won't respond (do @mine-care).
     
  6. Offline

    TheSporech

    @mine-care
    Okay, i'll remember to tag you.

    I understand classes and objects, it's just runnables that confuse me.
    I could try adding parameters so it uses the same data so that I parse it to the same object, that might work.

    My train of thought here is that run(), (just like any other method) can have values parsed into it. I guess it doesn't work like this though.
     
    Last edited: Apr 28, 2016
  7. Offline

    mine-care

    @TheSporech
    Well a runnable has the method run() that is called when it executes. So you can have all the variables you need in this method as class variables and use them like this. Example given in some sort of pseudocode:
    Code:
    Runnable class{
    int variable;
       constructor(int variable){
          this.variable = variable;
       }
    
       run(){
          print("Variable is: "+variable);
       }
    }
    
     
  8. Offline

    Lordloss

    As far as i know if you put arguments into it, it changes its method signature and wont be recognized as the Run() method anymore.
     
  9. Offline

    mine-care

    @Lordloss Indeed.
    At the end of the day you are overriding a method from the supperclass (Runnable) so you need to take it as it is.
    Botom line is that when the API or any other API calls run() from the Runnable, it doesnt know what sort of parameters you expect, and furthermore even if it did, it wouldn't be able to provide them in the first place.
     
  10. Offline

    Davesan

    With local class (optional) (open)

    Without making a class which extends Runnable separatedly, you can define a local class in your code too:

    Code:
    final Player p = ...; //final variables are visible inside local classes
    
    //#from here
    BukkitRunnable r = new BukkitRunnable() {
        @Override
        public void run() {
            p.sendMessage("Hello.");
        }
    }
    //#to here : We define the code of a class, which extends BukkitRunnable,
    //   however this class has no name, because it is a local class.
    //_and_ also we make an instance (object) of this class and put it into "r"
    
    //Now in the "r" variable you've got a BukkitRunnable object.
    //All methods of this object can access the Player object stored in variable "p"
    So if you do this way, all you have to do is mark those variables final, which you want to be able to use in the local class, which you will work with in it. Also, you can define class variables above the @Override line in my code, if you want.

    It works just like what @mine-care wrote, but his solution is a statically defined class, while mine is locally. Both have their advantages in some cases.

    The one you are looking for however, (I think) is what @mine-care wrote. (because it is simpler that way to understand maybe, don't know what level of programming you have)
    Instead of adding the values into the run() method by parameters it uses the variables of the object. So to be clear, istead of this:
    Code:
    public void run(Player p) {
        p.setHealth(0);
        p.sendMessage("You were killed by a plugin!");
    }
    You will use it this way:
    Code:
    Player p;
    public void run() {
        p.setHealth(0);
        p.sendMessage("You were killed by a plugin!");
    }
    And to do that, you must of course define a specific BukkitRunnable, so a class which extends it. Either statically (the usual way, simply writing a new class) or locally as I showed it is possible that way too.
    Further local class clarification (open)
    You have no class variables in the local class definition example, because the final variables outside the class functions just like class/object variables. But if you want you can have class variables in local classes too and write the data into them when starting, like:
    Code:
    final Player p = ...;
    BukkitRunnable br = new BukkitRunnable() {
        Player player = p; //this is not needed to be final, u see
        public void run() {
            player = player.getKiller();
            player.setExp(p.getExp() + 100);
            player.sendMessage("You killed " + p.getDisplayName() + " so you got 100 xp!");
        }
    }

    I mentioned local classes you to see some interesting and later maybe effective stuff. Sometimes you could be in a situation, that you must define many classes which are short and only used in one piece of code, then local classes are really handy. Runnables are tipically the class which are like that. You must for example delay some lines of code, you don't have to define a class in another file, just write a local class with some lines there and that's it.
     
    Last edited: Apr 29, 2016
    mine-care likes this.
  11. Offline

    EnervateD

    Google "Callable<?>"
     
  12. Offline

    mine-care

    @EnervateD You are right it is similar, but technically Runnable is not a Callable thus i think googling 'Runnable' directly would be better ;)
     
  13. Offline

    EnervateD

    @mine-care Oh, sorry, I misunderstood the request of the thread :c
     
Thread Status:
Not open for further replies.

Share This Page