6
6
import org .jetbrains .annotations .NotNull ;
7
7
8
8
import java .io .IOException ;
9
+ import java .util .Arrays ;
9
10
import java .util .concurrent .CompletableFuture ;
10
11
11
12
public class EInstanceModule {
@@ -21,7 +22,7 @@ public CompletableFuture<EInstance> getInstance(String instance) {
21
22
CompletableFuture <EInstance > future = new CompletableFuture <>();
22
23
23
24
Request request = new Request .Builder ()
24
- .url (EpsilonEnvironments .getEpsilonURL ("/instance/get_from_name /" + instance ))
25
+ .url (EpsilonEnvironments .getEpsilonURL ("/instance/get /" + instance ))
25
26
.build ();
26
27
27
28
okHttp .newCall (request ).enqueue (new Callback () {
@@ -46,30 +47,39 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
46
47
return future ;
47
48
}
48
49
50
+ public CompletableFuture <EInstance []> getInstances () {
51
+ CompletableFuture <EInstance []> future = new CompletableFuture <>();
52
+
53
+ Request request = new Request .Builder ()
54
+ .url (EpsilonEnvironments .getEpsilonURL ("/instance/get_all" ))
55
+ .build ();
56
+
57
+ getInstances (future , request );
58
+
59
+ return future ;
60
+ }
61
+
49
62
public CompletableFuture <EInstance []> getInstances (String template ) {
50
63
CompletableFuture <EInstance []> future = new CompletableFuture <>();
51
64
52
65
Request request = new Request .Builder ()
53
- .url (EpsilonEnvironments .getEpsilonURL ("/instance" + ( template != null ? "/get /" + template : "/get_all" ) ))
66
+ .url (EpsilonEnvironments .getEpsilonURL ("/instance/get_from_template /" + template ))
54
67
.build ();
55
68
56
- okHttp .newCall (request ).enqueue (new Callback () {
57
- @ Override
58
- public void onFailure (@ NotNull Call call , @ NotNull IOException e ) {
59
- e .printStackTrace ();
60
- }
69
+ getInstances (future , request );
61
70
62
- @ Override
63
- public void onResponse (@ NotNull Call call , @ NotNull Response response ) throws IOException {
64
- try (ResponseBody body = response .body ()) {
65
- if (!response .isSuccessful ()) throw new IOException ("Unexpected code " + response );
71
+ return future ;
72
+ }
66
73
67
- assert body != null ;
68
- EInstanceList instances = gson . fromJson ( body . string (), EInstanceList . class );
74
+ public CompletableFuture < EInstance []> getInstances ( EType type ) {
75
+ CompletableFuture < EInstance []> future = new CompletableFuture <>( );
69
76
70
- future .complete (instances .getInstances ());
71
- }
72
- }
77
+ getInstances ().whenCompleteAsync ((instances , error ) -> {
78
+ EInstance [] array = Arrays .stream (instances )
79
+ .filter (instance -> instance .getType () == type )
80
+ .toArray (EInstance []::new );
81
+
82
+ future .complete (array );
73
83
});
74
84
75
85
return future ;
@@ -134,4 +144,25 @@ public boolean inGameInstance(String name) {
134
144
135
145
return false ;
136
146
}
147
+
148
+ private void getInstances (CompletableFuture <EInstance []> future , Request request ) {
149
+ okHttp .newCall (request ).enqueue (new Callback () {
150
+ @ Override
151
+ public void onFailure (@ NotNull Call call , @ NotNull IOException e ) {
152
+ e .printStackTrace ();
153
+ }
154
+
155
+ @ Override
156
+ public void onResponse (@ NotNull Call call , @ NotNull Response response ) throws IOException {
157
+ try (ResponseBody body = response .body ()) {
158
+ if (!response .isSuccessful ()) throw new IOException ("Unexpected code " + response );
159
+
160
+ assert body != null ;
161
+ EInstanceList instances = gson .fromJson (body .string (), EInstanceList .class );
162
+
163
+ future .complete (instances .getInstances ());
164
+ }
165
+ }
166
+ });
167
+ }
137
168
}
0 commit comments