Change variable in Runnable?

Discussion in 'Plugin Development' started by Symphonic, Dec 8, 2019.

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

    Symphonic

    Essentially I am trying to change the animationstage variable, defined at the beginning of the onJoin handler, within the delayed task on line 45 but it won't let me because it doesn't know which variable i'm talking about. Any ideas?

    Code:
    package net.audaxgames.spaceintro;
    
    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.util.Vector;
    
    public class introlistener implements Listener {
       
        private final Plugin plugin;
       
        public introlistener(Plugin plugin) {
           
            this.plugin = plugin;
           
        }
       
        @EventHandler
        public void onJoin(PlayerLoginEvent event) {
           
            Player player = event.getPlayer();
            public int animationstage = 1;
           
            boolean hasplayerjoined = plugin.getConfig().getBoolean("users." + player.getName() + ".hasjoined");
           
            if (hasplayerjoined == false) {
               
                this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                    @Override
                    public void run() {
                       
                        Location loc = new Location(Bukkit.getWorld("world"), 32, 97, -36);
                        player.teleport(loc);
                        player.setGameMode(GameMode.SPECTATOR);
                        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                           
                            public void run() {
                               
                                animationstage = 2;
                               
                            }
                           
                        }, 60L);
                        while (animationstage == 1) {
                           
                            player.setVelocity(new Vector(3, 0, 0));
                           
                        }
                        player.setGameMode(GameMode.SURVIVAL);
                    }
                   
                }, 1L);
               
                this.plugin.getConfig().set("users." + player.getName() + ".hasjoined", true);
               
            }
           
        }
       
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @Symphonic Put the variable in the runnable.
    And that while loop will kill the server.
     
  3. Offline

    Symphonic

    Okay, so how do I get the variable output out of the runnable now, and how do I avoid that loop killing the server
     
  4. Online

    timtower Administrator Administrator Moderator

    Why do you need it outside the variable?
    And you need a repeating task for that.

    What is the end goal of this plugin, because we might now a better way of doing things.
     
  5. Offline

    Symphonic

    Okay Ill put the tasks schedueler on one tick with a repeating task for that. I need the variable outside of the runnable so it stops that while loop.

    Essentially this plugin is a little animation that plays the first time a player joins the server, It moves them around and such.
     
  6. Online

    timtower Administrator Administrator Moderator

    Then I suggest using a list or map as field in the class itself instead of a local variable.
     
  7. Offline

    Symphonic

    Why a map or a list? How would I pass that to and from the runnable?
     
  8. Online

    timtower Administrator Administrator Moderator

    Because with a map you can have a variable per player with it.
    And you don't need to pass it, you can just use it.
     
  9. Offline

    Symphonic

    Wow, I'm really lost. The purpose of the plugin right now is to see if a player has joined before and then do this for 60 ticks:

    Code:
    player.setVelocity(new Vector(3, 0, 0));
    I don't see what purpose a list or a map would have. If I put this in a repeating runnable for 1 tick, All i would want to do is to stop it after 60 ticks. Okay rubber duck debugging here is telling me that I can put a counter in the Runnable and make it self-cancelling, but I still don't understand why I would need a map, where I would define it and where I would use it.
     
  10. Online

    timtower Administrator Administrator Moderator

    @Symphonic Knowing which player needs the effects.
     
  11. Offline

    Symphonic

    But that's happening inside the listener, right?
     
  12. Online

    timtower Administrator Administrator Moderator

    You have a very different setup then how I would do it.
    I would take it outside of the listener with an external runnable that handles all players in the map instead of a single one.
     
  13. Offline

    Symphonic

    But the server would create a new instance of the listener for each player that joins, right? What would the point be?
     
  14. Online

    timtower Administrator Administrator Moderator

    No, it would not. It would call the event, the listener stays the same.
    And again: it is a different setup, does not mean that one is better then the other.
     
  15. Offline

    Symphonic

    But that means that if multiple people joined in quick succession it would break? That's what I was asking anyways
     
  16. Online

    timtower Administrator Administrator Moderator

    Depends on your new code.
     
  17. Offline

    Symphonic

    After about an hour of headscratching, I got this to work.


    Code:
    package net.audaxgames.spaceintro;
    
    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;
    
    public class introlistener implements Listener {
       
       
        //gets plugin info
        public final Plugin plugin;
       
        public introlistener(Plugin plugin) {
           
            this.plugin = plugin;
           
        }
       
        @EventHandler
        public void onJoin(PlayerLoginEvent event) {
           
            Player player = event.getPlayer();
           
            boolean hasplayerjoined = plugin.getConfig().getBoolean("users." + player.getName() + ".hasjoined");
           
            //tests if player has joined
           
            if (hasplayerjoined == false) {
               
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                    @Override
                    public void run() {
                       
                        //plays intro
                        Location loc = new Location(Bukkit.getWorld("world"), 32, 97, -36);
                        player.teleport(loc);
                        player.setGameMode(GameMode.ADVENTURE);
                        new BukkitRunnable() {
    
                            int counter = 0;
                           
                            @Override
                            public void run() {
    
                                player.setVelocity(new Vector(0, 0, 3));
                                counter++;
                               
                                if (counter >= 60) {
                                   
                                    player.setGameMode(GameMode.CREATIVE);
                                    this.cancel();
                                    return;
                                   
                                }
                               
                            }
                           
                        }.runTaskTimer(plugin, 0, 1);
    
                    }
                   
                }, 1L);
               
                //sets player to have joined
                this.plugin.getConfig().set("users." + player.getName() + ".hasjoined", true);
               
            }
           
        }
       
    }
    
     
  18. Online

    timtower Administrator Administrator Moderator

  19. Offline

    Symphonic

    Okay, Thanks for all the help!
     
Thread Status:
Not open for further replies.

Share This Page