1414 * limitations under the License.
1515 */
1616
17- package clientapi .util ;
17+ package clientapi .util . server ;
1818
1919import clientapi .ClientAPI ;
2020import clientapi .event .defaults .filters .PacketFilter ;
2121import clientapi .event .defaults .game .core .TickEvent ;
2222import clientapi .event .defaults .game .network .PacketEvent ;
23+ import clientapi .util .Timer ;
2324import clientapi .util .interfaces .Helper ;
2425import com .google .common .collect .Sets ;
2526import me .zero .alpine .listener .EventHandler ;
3132import java .util .Set ;
3233import java .util .function .Consumer ;
3334
34- import static clientapi .util .PluginFinder .PResponse .Result .FAILURE ;
35- import static clientapi .util .PluginFinder .PResponse .Result .SUCCESS ;
36-
3735/**
3836 * Used to find possible plugins on servers
3937 *
@@ -45,7 +43,7 @@ public final class PluginFinder implements Helper {
4543 /**
4644 * Called in response to finding plugins
4745 */
48- private Consumer <PResponse > callback ;
46+ private Consumer <PluginFinderResponse > callback ;
4947
5048 /**
5149 * Used to keep track of the timeout
@@ -64,7 +62,7 @@ public final class PluginFinder implements Helper {
6462 *
6563 * @param callback Plugin Response callback
6664 */
67- public final void find (Consumer <PResponse > callback ) {
65+ public final void find (Consumer <PluginFinderResponse > callback ) {
6866 this .find (callback , 10000 );
6967 }
7068
@@ -74,9 +72,9 @@ public final void find(Consumer<PResponse> callback) {
7472 * found.
7573 *
7674 * @param callback Plugin Response callback
77- * @param timeout Timeout in MS
75+ * @param timeout Timeout in milliseconds
7876 */
79- public final void find (Consumer <PResponse > callback , long timeout ) {
77+ public final void find (Consumer <PluginFinderResponse > callback , long timeout ) {
8078 packetTimer .reset ();
8179 this .timeout = timeout ;
8280
@@ -87,12 +85,9 @@ public final void find(Consumer<PResponse> callback, long timeout) {
8785
8886 @ EventHandler
8987 private final Listener <TickEvent > tickListener = new Listener <>(event -> {
90- if (!packetTimer .delay (timeout ))
91- return ;
92-
9388 ClientAPI .EVENT_BUS .unsubscribe (this );
94- callback .accept (new PResponse ("Request timed out after " + timeout + "ms" ));
95- });
89+ callback .accept (new PluginFinderResponse ("Request timed out after " + timeout + "ms" ));
90+ }, e -> packetTimer . delay ( timeout ) );
9691
9792 @ EventHandler
9893 private final Listener <PacketEvent .Receive > packetListener = new Listener <>(event -> {
@@ -105,69 +100,8 @@ public final void find(Consumer<PResponse> callback, long timeout) {
105100 plugins .add (plugin );
106101 });
107102
108- callback .accept (new PResponse (plugins ));
103+ callback .accept (new PluginFinderResponse (plugins ));
109104 callback = null ;
110105 ClientAPI .EVENT_BUS .unsubscribe (this );
111106 }, new PacketFilter <>(SPacketTabComplete .class ));
112-
113- public static class PResponse {
114-
115- /**
116- * The list of plugins found
117- */
118- private final Set <String > plugins ;
119-
120- /**
121- * The last error
122- */
123- private final String error ;
124-
125- /**
126- * The result status, either SUCCESS or FAILURE
127- */
128- private final Result result ;
129-
130- private PResponse (String error ) {
131- this .plugins = null ;
132- this .error = error ;
133- this .result = FAILURE ;
134- }
135-
136- private PResponse (Set <String > plugins ) {
137- this .plugins = plugins ;
138- this .error = null ;
139- this .result = SUCCESS ;
140- }
141-
142- /**
143- * @return The plugins found, if found
144- */
145- public final Set <String > getPlugins () {
146- if (result != SUCCESS )
147- throw new UnsupportedOperationException ("Cannot get plugins that were retrieved unless response type is SUCCESS" );
148-
149- return this .plugins ;
150- }
151-
152- /**
153- * @return The last error, if there is one
154- */
155- public final String getError () {
156- if (result != FAILURE )
157- throw new UnsupportedOperationException ("Cannot get error that occured unless response type is FAILURE" );
158-
159- return this .error ;
160- }
161-
162- /**
163- * @return The outcome of the request, SUCCESS or FAILURE
164- */
165- public final Result getResult () {
166- return this .result ;
167- }
168-
169- public enum Result {
170- SUCCESS , FAILURE
171- }
172- }
173107}
0 commit comments