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>() {
                @Override
                public void onSuccess(final UUID done) {
                    test = done;
                    return;
                }
               
                @Override
                public void onFailure(final Throwable cause) {
                    if (cause != null) {
                        cause.printStackTrace();
                    }
                }

                @Override
                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() {
                @SuppressWarnings("deprecation")
                @Override
                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 (result.next()) {
                            uuid = UUID.fromString(result.getString("UUID"));
                            ++count;
                        }
                        if (count > 1) {
                            sql.handleDuplicateNames(Bukkit.getOfflinePlayer(name));
                            callback.onDuplicate(name);
                            //Previous return (no async) return nameToUUID(name);
                        }
                        callback.onSuccess(uuid);
                    } catch (SQLException e) {
                        e.printStackTrace();
                        callback.onFailure(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.