Solved Getting Variables From Outside of a Runnable

Discussion in 'Plugin Development' started by Lactem, May 17, 2013.

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

    Lactem

    I have this code:

    PHP:
    int num 10;
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    @
    Override
    public void run(){
    while(
    num 0){
        
    player.sendMessage(num);
        
    num--;
    }
    }
    }, 
    020l);
    }
    It's supposed to make a runnable that says the amount of time left every second. Before, I had num defined as a global variable and my plugin worked fine with one player, but not so good with multiple players. Now I'm making num local, but if I put it into the Runnable, then num will always equal ten. There are errors on both nums because it wants me to make num (when I define it) final. If I do that, then there's still an error on the num-- because "The final local variable num cannot be assigned, since it is defined in an enclosing type." What should I do to make num count down one every second and send the player a message with the value of num?
     
  2. Lactem
    Instead of using an anonymous class create a class which implements Runnable. And then inside your runnable class you can store say, the name of the player and time remaining. This way you will be able to get it working with multiple players.
     
  3. Offline

    Lactem

    I've already tried using a separate class and that didn't work because the variable num still has to be global in the other class or else whatever num equals will always be the same because it loops through the code again and again and sets it to ten each time, unless it's global. If it's global, then the countdown doesn't work for multiple players.

    The num variable and the player variable both have to be local. It has to be an instance of it, but how am I supposed to make an instance of something from another class unless it's global? That's my dilemma.

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

    Technius

    Lactem
    Design it like this:
    Code:
    public class YourRunnable implements Runnable
    {
         int num;
         public void run()
         {
              //Do stuff
         }
    }
    
    When you make your runnable, do this:
    Code:
    YourRunnable a = new YourRunnable();
    a.num = num;
    
    Make num public if you're doing this in different packages.
     
  5. Offline

    Lactem

    Thanks. I'll try it out.

    Okay now the problem is that I don't know how to get the player from another class. I'm trying to do player.sendMessage(num); in my Runnable class, but I can't do player. Instead, I'm using a global variable in my main class called mainEventPlayer. I do public static Player mainEventPlayer;. Then in onCommand, I do mainEventPlayer = (Player) sender. So to access the commandsender in my Runnable class, I use main.mainEventPlayer. Because mainEventPlayer is global, only one person can be it. It doesn't work with multiple players. For example, let's say num equals ten. Then one player would type the command it it would start counting down. Now let's say the second player comes along and types the command, too. It would start stop the countdown for one player and resume it for the other.

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

    Technius

    Lactem You don't need this mainEventPlayer. Just add a Player variable to the Runnable. Have Runnables for each person:
    Code:
    //This is where you execute your command
    YourRunnable a = new YourRunnable();
    a.player = player;
    a.num = num;
     
    //This can be replaced
    Bukkit.getServer().scheduleSyncDelayedTask(a);
    
     
Thread Status:
Not open for further replies.

Share This Page