forked from jeremylvln/Shulker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shulker-sdk): add convenient Java wrapper
- Loading branch information
1 parent
13d142e
commit 4ac0c8c
Showing
6 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
javaOnly=true | ||
isLibrary=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
isLibrary=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
javaOnly=true | ||
isLibrary=true |
7 changes: 7 additions & 0 deletions
7
packages/shulker-sdk/bindings/java/src/main/java/io/shulkermc/sdk/ShulkerSDK.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.shulkermc.sdk; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public interface ShulkerSDK { | ||
CompletableFuture<String> summonFromFleet(String namespace, String fleetName); | ||
} |
76 changes: 76 additions & 0 deletions
76
packages/shulker-sdk/bindings/java/src/main/java/io/shulkermc/sdk/ShulkerSDKImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.shulkermc.sdk; | ||
|
||
import com.google.common.util.concurrent.FutureCallback; | ||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import io.shulkermc.sdk.v1alpha1.MinecraftServerFleetServiceGrpc; | ||
import io.shulkermc.sdk.v1alpha1.SummonFromFleetReply; | ||
import io.shulkermc.sdk.v1alpha1.SummonFromFleetRequest; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public final class ShulkerSDKImpl implements ShulkerSDK { | ||
private final ExecutorService executor = Executors.newCachedThreadPool(); | ||
private final MinecraftServerFleetServiceGrpc.MinecraftServerFleetServiceFutureStub minecraftServerFleetServiceFutureStub; | ||
|
||
private ShulkerSDKImpl(ManagedChannel channel) { | ||
this.minecraftServerFleetServiceFutureStub = MinecraftServerFleetServiceGrpc.newFutureStub(channel); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<String> summonFromFleet(String namespace, String fleetName) { | ||
return this.wrapGrpcFuture(this.minecraftServerFleetServiceFutureStub.summonFromFleet(SummonFromFleetRequest.newBuilder() | ||
.setNamespace(namespace) | ||
.setName(fleetName) | ||
.build() | ||
)).thenApply(SummonFromFleetReply::getGameServerId); | ||
} | ||
|
||
private <T> CompletableFuture<T> wrapGrpcFuture(ListenableFuture<T> listenableFuture) { | ||
CompletableFuture<T> future = new CompletableFuture<>() { | ||
@Override | ||
public boolean cancel(boolean mayInterruptIfRunning) { | ||
boolean cancelled = listenableFuture.cancel(mayInterruptIfRunning); | ||
super.cancel(cancelled); | ||
return cancelled; | ||
} | ||
}; | ||
|
||
Futures.addCallback( | ||
listenableFuture, | ||
new FutureCallback<T>() { | ||
@Override | ||
public void onSuccess(T result) { | ||
future.complete(result); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable t) { | ||
future.completeExceptionally(t); | ||
} | ||
}, | ||
this.executor | ||
); | ||
|
||
return future; | ||
} | ||
|
||
public static ShulkerSDK createFromEnvironment() { | ||
String hostString = System.getenv("SHULKER_SDK_GRPC_HOST"); | ||
if (hostString == null) { | ||
throw new IllegalStateException("Missing SHULKER_SDK_GRPC_HOST environment variable"); | ||
} | ||
|
||
String portString = System.getenv("SHULKER_SDK_GRPC_PORT"); | ||
if (portString == null) { | ||
throw new IllegalStateException("Missing SHULKER_SDK_GRPC_PORT environment variable"); | ||
} | ||
|
||
ManagedChannel channel = ManagedChannelBuilder.forAddress(hostString, Integer.parseInt(portString)).usePlaintext().build(); | ||
return new ShulkerSDKImpl(channel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
isLibrary=true |