title: Wait for a CompletableFuture to be complete, then return a different value?
body: <p>I have a <code>CompletableFuture&lt;Void&gt;</code> that calls an asynchronous method whose return type I can't change, or anything about it.</p>
  
  <p>I want to wait for this method to be complete (I manually complete it), and then return a String value, how would I do this?</p>
  
  <pre><code>public String getServer(Player p) {
      FutureServer f = new FutureServer(CompletableFuture.runAsync(() -&gt; {
          sendUTF(p, "GetServer");
          try {
              Thread.sleep(10000); //so the future doesnt complete itself
          } catch (Exception e) {
              e.printStackTrace();
          }
      }), p.getUniqueId().toString());
      serverSet.add(f);
      String server = "";
  
      //server isn't final so I can't use it in the lambda
      f.getFutureVoid().whenComplete(v -&gt; server = f.getServer()); 
  
      return server;
  }
  
  public class FutureServer {
  
      private CompletableFuture&lt;Void&gt; futureVoid;
      private String s;
      private String uuid;
  
      public FutureServer(CompletableFuture&lt;Void&gt; futureVoid, String uuid) {
          this.futureVoid = futureVoid;
          this.uuid = uuid;
      }
  
      public String getUuid() {
          return uuid;
      }
  
      public CompletableFuture&lt;Void&gt; getFutureVoid() {
          return futureVoid;
      }
  
      public boolean hasServer() {
          return s != null;
      }
  
      public void setServer(String s) {
          this.s = s;
      }
  
      public String getServer() {
          return s;
      }
  }
  </code></pre>
  
  <p>I want to set string to equal <code>FutureServer#getServer()</code> (own method), but I need to wait until the <code>CompletableFuture&lt;Void&gt;</code> is completed. What do I do?</p>
  
  <p>This is the method that gets called async and is unchangeable... the method I use that calls this other method asynchronously is <code>sendUTF()</code>.</p>
  
  <pre><code>@Override
  public void onPluginMessageReceived(String s, Player p, byte[] bytes) {
      if (!s.equals("BungeeCord")) return;
  
      ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
      String subChannel = in.readUTF();
  
      switch(subChannel) {
          case "GetServer":
              String server = in.readUTF();
              serverSet.stream().filter(f -&gt; f.getUuid().equals(p.getUniqueId().toString())).findFirst().ifPresent(f -&gt; {
                  f.setServer(server); //getting the string I need and placing it into this object
                  f.getFutureVoid().complete(null); //completing the void future manually
              });
              break;
      }
  
  }
  </code></pre>