Solved Is player online? (Asynchronously)

Discussion in 'Plugin Development' started by 9903286, Nov 18, 2013.

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

    9903286

    Okay, so i basically HAVE to call some kind of "is player online" function inside an asynchronos task, but since i can't use Bukkit calls and only have access to the players name, how would i do it?

    Thanks.
     
  2. Offline

    bartboy8

    9903286
    Please post your code or else it is very hard for us to understand what you are trying to do..
     
  3. Offline

    Peary

    9903286 define: "kind of player"
     
  4. Offline

    9903286

    bartboy8
    My code:
    Code:
    Bukkit.getScheduler().runTaskTimerAsynchronously(instance, new Runnable() {
                public void run() {
                    try {
                       
                        Statement st;
                        Connection con = DriverManager.getConnection(MPSPoints.url, MPSPoints.user, MPSPoints.password);
                        st = con.createStatement();
                       
                        ResultSet rset = st.executeQuery("SELECT * FROM stuff;");
                        if(!rset.next()) {
                            return;
                        }
                        while (rset.next()) {
                            CHECK IF rset.getString(2) is online!
                        }
     
                        con.close();
                       
                    } catch (SQLException e) {}
                }
            }, 20, 1200);
    Peary
    It was meant to be "to call some kind of "is player online" function inside an".
     
  5. Offline

    1Rogue

    Use a Callable
    Code:java
    1. public class getPlayer<Player> implements Callable<Player> {
    2.  
    3. private final String name;
    4.  
    5. public getPlayer(String name) {
    6. this.name = name;
    7. }
    8.  
    9. public Player call() {
    10. return Bukkit.getPlayer(this.name);
    11. }
    12.  
    13. }


    Then just do a null check.


    That would cause a connection leak when you return before closing. Also, you don't need sql to verify if someone is online, just call the method from the main thread.

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

    9903286

    1Rogue
    Uhm, well, i'm getting the player name from the SQL, so i probably do.
    It's supposed to get "playername, items" from mysql, check if "playername" is online, and if he is add "item" to inventory.

    And i'm stuck on the checking online part, but i'll try your solution.
     
  7. Offline

    The_Doctor_123

    9903286
    Code:java
    1. final String player = "someGuy";
    2. Future f = Bukkit.getScheduler().callSyncMethod(<Plugin Instance>, new Callable<Boolean>()
    3. {
    4. @Override
    5. public Boolean call() throws Exception
    6. {
    7. return Bukkit.getPlayerExact(player) != null;
    8. }
    9.  
    10. });
    11. boolean playerOnline;
    12. try
    13. {
    14. playerOnline = ((Boolean) f.get()).booleanValue();
    15. }
    16. catch (Exception e)
    17. {
    18. e.printStackTrace();
    19. }
     
    9903286 likes this.
  8. Offline

    9903286

Thread Status:
Not open for further replies.

Share This Page