Using callback to return variable

Discussion in 'Spigot Plugin Development' started by Random_Cow, May 30, 2016.

  1. Would there be a better way to do the following:
    Code (Text):
        UUID test = null;
        public UUID nameToUUIDTest(final String name) {
            util.nameToUUIDTest(name, new Callback<UUID>() {
                public void onSuccess(final UUID done) {
                    test = done;
                public void onFailure(final Throwable cause) {
                    if (cause != null) {

                public void onDuplicate(String name) {
                    // TODO Auto-generated method stub
            return test;

              //Previous method (no async)        return util.nameToUUID(name);
    MySQL Call:
    Code (Text):
        public void nameToUUIDTest(final String name, final Callback<UUID> callback) {
            Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
                public void run() {
                    Connection connection = null;
                    PreparedStatement ps = null;
                    ResultSet result = null;
                    try {
                        connection = pool.getConnection();
                        ps = connection.prepareStatement("SELECT * FROM players WHERE UPPER(Username)=?");
                        ps.setString(1, name.toUpperCase());
                        result = ps.executeQuery();
                        UUID uuid = null;
                        int count = 0;
                        while ( {
                            uuid = UUID.fromString(result.getString("UUID"));
                        if (count > 1) {
                            //Previous return (no async) return nameToUUID(name);
                    } catch (SQLException e) {
                    } finally {
                        pool.close(connection, ps, null);
    Im trying to return the UUID of a player from their name stored in a database by doing this. This current method works, but it returns the previous saved variable test upon first execution. Im running the MySQL call on an async thread so i created a callback method to return the UUID. I also havent included the method to return duplicate names either. Any suggestions on improving this code would be nice as well.
  2. Bump, any help appreciated.