Solved Accessing variables from runnables

Discussion in 'Plugin Development' started by Lightcaster5, Mar 1, 2020.

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

    Lightcaster5

    I'm trying to use an asynchronous connection between my SQL server and my plugin. Everything is working correctly until I attempting to access a variable from inside the runnable. Heres my code:
    Code:
        public boolean checkPlayer(UUID uuid) {
            boolean exists = false;
            BukkitRunnable runnable = new BukkitRunnable() {
                @Override
                public void run() {
                    try {
                        Statement statement = plugin.connection.createStatement();
                        ResultSet getnumbers = statement
                                .executeQuery("SELECT * FROM `PlayerData` WHERE `UUID` ='" + uuid.toString() + "';");
                        if (getnumbers.next()) {
    
                            exists = true; // This is here it doesnt work
    
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            };
            runnable.runTaskAsynchronously(plugin);
            return exists;
        }
    The code error states the following:
    Code:
    Local variable exists defined in an enclosing scope must be final or effectively final
    I have a lot of methods like this that return values from the database just for refrence, I need a way to do this. Thanks in advanced!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Lightcaster5 You need to call a different function instead, the return will be called before the task gets finished.
     
  3. Offline

    Lightcaster5

    I honestly don't even know where to start then...
     
  4. Offline

    timtower Administrator Administrator Moderator

    What are you trying to do?
     
  5. Offline

    Lightcaster5

    Check if a player exists in my sql db. Look at the code entirely and you can see
     
  6. Offline

    timtower Administrator Administrator Moderator

    And then do what with it? Give him a cookie?
    What is the end goal of this lookup?
     
  7. Offline

    Lightcaster5

    ...
    if player doesnt exist, create new plater
    how are you gonna know if they exist?
    will they throw a cookie at you?

    its supposed to return whether or not the player exists
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Lightcaster5 Create the player in the runnable.
    And because it is using an async task that would be impossible.
     
  9. Offline

    Lightcaster5

    ok

    EDIT: close this thread
    EDIT2: Wait would it be possible to create a public variable and use something like ClassName.getTheVariable()
     
  10. Offline

    Machine Maker

    You could use an AtomicBoolean type as well probably.
     
  11. Offline

    Lightcaster5

    That sounds cool but I don't even know what it does...
    I just took a look, it sounds really useful and I'll give it a shot, thanks!
     
  12. Offline

    timtower Administrator Administrator Moderator

    How do you know when to get it though?
     
  13. Offline

    Lightcaster5

    it wont just work?
     
  14. Offline

    timtower Administrator Administrator Moderator

    That is why you need to do it in the runnable.
    And as it is async already: no need to make a new runnable.
     
  15. Offline

    Lightcaster5

    what do you mean "in the runnable"
     
  16. Offline

    Machine Maker

    Yeah, actually reading more into your code, you have a bigger issue. You are returning the "exists" variable right after telling the code to asynchronously check if the player exists. The code will ALWAYS return whatever the initial value of "exists" is.
     
  17. Offline

    timtower Administrator Administrator Moderator

    Y0u are doing a select query to check if the player exists.
    When the select is done and you know if it is there or not: create a player if it is missing.
    Within the same run function.
     
  18. Offline

    Lightcaster5

    do I even need this to be async because I feel like this is becoming way to complicated
     
  19. Offline

    timtower Administrator Administrator Moderator

    Safest if you are using databases.
    You can also start using the config.
     
  20. Offline

    Machine Maker

    Its probably good form to do this async.

    You just can't include ANY information from inside the runnable in the return statement for the checkPlayer method.

    Inside the runnable after you check if the player is in the database, you then need to call some method from inside that. either create the player, or do whatever.
     
  21. Offline

    Lightcaster5

    Alright thanks guys!
     
  22. Offline

    Machine Maker

    upload_2020-3-1_12-12-16.png
    Here is a quick diagram of why. When you tell the plugin to do something Async it does it on another thread. Immediately moving on to the next statement (in this case, return exists). so return exists will always be true.
     
Thread Status:
Not open for further replies.

Share This Page