Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.

Commit 96c2b0d

Browse files
committed
Refactored PluginFinder
1 parent 2f1aeaf commit 96c2b0d

File tree

3 files changed

+81
-80
lines changed

3 files changed

+81
-80
lines changed

src/main/java/clientapi/event/handle/ClientHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ public enum ClientHandler implements Helper {
5252
*/
5353
@EventHandler
5454
private final Listener<KeyEvent> keyListener = new Listener<>(event -> {
55-
// If the event is cancelled, don't process the KeyEvent
56-
if (event.isCancelled())
57-
return;
58-
5955
// Get all matching keybinds
6056
Stream<Keybind> keybinds = Keybind.getKeybinds().stream()
6157
.filter(bind -> bind.getKey() != KEY_NONE && bind.getKey() == event.getKey());
@@ -67,7 +63,7 @@ public enum ClientHandler implements Helper {
6763
if (keybind.getType() == Keybind.Type.TOGGLE)
6864
keybind.onClick();
6965
});
70-
}, EventPriority.LOWEST);
66+
}, EventPriority.LOWEST, e -> !e.isCancelled());
7167

7268
@EventHandler
7369
private final Listener<KeyUpEvent> keyUpListener = new Listener<>(event ->

src/main/java/clientapi/util/PluginFinder.java renamed to src/main/java/clientapi/util/server/PluginFinder.java

Lines changed: 9 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
package clientapi.util;
17+
package clientapi.util.server;
1818

1919
import clientapi.ClientAPI;
2020
import clientapi.event.defaults.filters.PacketFilter;
2121
import clientapi.event.defaults.game.core.TickEvent;
2222
import clientapi.event.defaults.game.network.PacketEvent;
23+
import clientapi.util.Timer;
2324
import clientapi.util.interfaces.Helper;
2425
import com.google.common.collect.Sets;
2526
import me.zero.alpine.listener.EventHandler;
@@ -31,9 +32,6 @@
3132
import java.util.Set;
3233
import 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
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package clientapi.util.server;
2+
3+
import java.util.Set;
4+
5+
import static clientapi.util.server.PluginFinderResponse.Result.FAILURE;
6+
import static clientapi.util.server.PluginFinderResponse.Result.SUCCESS;
7+
8+
/**
9+
* @author Brady
10+
* @since 4/12/2018 8:38 PM
11+
*/
12+
public final class PluginFinderResponse {
13+
14+
/**
15+
* The list of plugins found
16+
*/
17+
private final Set<String> plugins;
18+
19+
/**
20+
* The last error
21+
*/
22+
private final String error;
23+
24+
/**
25+
* The result status, either SUCCESS or FAILURE
26+
*/
27+
private final Result result;
28+
29+
PluginFinderResponse(String error) {
30+
this.plugins = null;
31+
this.error = error;
32+
this.result = FAILURE;
33+
}
34+
35+
PluginFinderResponse(Set<String> plugins) {
36+
this.plugins = plugins;
37+
this.error = null;
38+
this.result = SUCCESS;
39+
}
40+
41+
/**
42+
* @return The plugins found, if found
43+
*/
44+
public final Set<String> getPlugins() {
45+
if (result != SUCCESS)
46+
throw new UnsupportedOperationException("Cannot get plugins that were retrieved unless response type is SUCCESS");
47+
48+
return this.plugins;
49+
}
50+
51+
/**
52+
* @return The last error, if there is one
53+
*/
54+
public final String getError() {
55+
if (result != FAILURE)
56+
throw new UnsupportedOperationException("Cannot get error that occured unless response type is FAILURE");
57+
58+
return this.error;
59+
}
60+
61+
/**
62+
* @return The outcome of the request, SUCCESS or FAILURE
63+
*/
64+
public final Result getResult() {
65+
return this.result;
66+
}
67+
68+
public enum Result {
69+
SUCCESS, FAILURE
70+
}
71+
}

0 commit comments

Comments
 (0)