Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit db88554

Browse files
committed
Events - work in progress
1 parent 0b96fcf commit db88554

File tree

18 files changed

+401
-30
lines changed

18 files changed

+401
-30
lines changed

.gradle/4.3/fileHashes/fileHashes.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

build.gradle

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,35 @@ apply plugin: 'eclipse'
44
jar.archiveName = project.name + '.jar'
55

66
repositories {
7-
jcenter()
7+
jcenter()
88
}
99

1010
dependencies {
1111

1212
//Nullable annotation
1313
compile (group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.annotation', version: '1.1.0')
1414

15+
//Reflections
16+
compile (group: 'org.reflections', name: 'reflections', version: '0.9.11')
17+
1518
//Gson
1619
compile (group: 'com.google.code.gson', name: 'gson', version: '2.8.5')
17-
18-
//Reflections
19-
compile (group: 'org.reflections', name: 'reflections', version: '0.9.11')
20-
21-
// Configurations
22-
testCompile 'org.apache.commons:commons-configuration2:2.2'
23-
testCompile 'org.apache.commons:commons-lang3:3.3.2'
24-
testCompile 'commons-logging:commons-logging:1.1.3'
25-
testCompile 'commons-beanutils:commons-beanutils:1.9.2'
26-
testCompile 'commons-codec:commons-codec:1.9'
27-
testCompile 'commons-jxpath:commons-jxpath:1.3'
28-
testCompile 'org.apache.commons:commons-jexl:2.1.1'
20+
21+
//org.json
22+
compile (group: 'org.json', name: 'json', version: '20180813')
23+
24+
//Socket.IO
25+
compile ('io.socket:socket.io-client:1.0.0') {
26+
exclude group: 'org.json', module: 'json'
27+
}
28+
29+
// Configurations
30+
testCompile 'org.apache.commons:commons-configuration2:2.2'
31+
testCompile 'org.apache.commons:commons-lang3:3.3.2'
32+
testCompile 'commons-logging:commons-logging:1.1.3'
33+
testCompile 'commons-beanutils:commons-beanutils:1.9.2'
34+
testCompile 'commons-codec:commons-codec:1.9'
35+
testCompile 'commons-jxpath:commons-jxpath:1.3'
36+
testCompile 'org.apache.commons:commons-jexl:2.1.1'
2937

3038
}

src/main/java/me/limeglass/streamelements/api/StreamElements.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public interface StreamElements {
3535
*/
3636
List<Points> getUserPoints(Collection<User> users);
3737

38+
/**
39+
* May only be a positive number.
40+
*
41+
* @param points The amount of current points to set.
42+
* @param existing An existing Points instance.
43+
*/
44+
void setCurrentUserPoints(long points, Points... existing);
45+
3846
/**
3947
* May only be a positive number.
4048
*
@@ -75,6 +83,13 @@ public interface StreamElements {
7583
*/
7684
void addPoints(User user, long points);
7785

86+
/**
87+
* Removes a user from the points system.
88+
*
89+
* @param user The User to set points of.
90+
*/
91+
void removeUserPoints(User user);
92+
7893
/**
7994
* @param username The username to get points from.
8095
* @return The points of a user by string username.
@@ -87,4 +102,14 @@ public interface StreamElements {
87102
*/
88103
Points getUserPoints(User user);
89104

105+
/**
106+
* Resets all the current points.
107+
*/
108+
void resetCurrentPoints();
109+
110+
/**
111+
* Resets the all-time points.
112+
*/
113+
void resetAllPoints();
114+
90115
}

src/main/java/me/limeglass/streamelements/api/StreamElementsBuilder.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public final class StreamElementsBuilder {
77

88
private String token, accountID;
99
private int timeout = 20 * 1000;
10+
private Class<?>[] listeners;
1011

1112
/**
1213
* For those whom love keeping clean methods per line.
@@ -27,6 +28,16 @@ public StreamElementsBuilder(String token, String accountID) {
2728
this.token = token;
2829
}
2930

31+
/**
32+
* Register any listeners.
33+
* If a method contains 1 parameter of an ElementsEvent and has the @EventListener annotation,
34+
* it will be called when an event happens.
35+
*/
36+
public StreamElementsBuilder registerListeners(Class<?>... listeners) {
37+
this.listeners = listeners;
38+
return this;
39+
}
40+
3041
/**
3142
* Set the connection timeout of the client.
3243
* Recommended to set one.
@@ -51,14 +62,23 @@ public StreamElementsBuilder withToken(String token) {
5162
this.token = token;
5263
return this;
5364
}
54-
65+
5566
/**
56-
* @return The timeout defined by the client.
67+
* NOTE: These classes aren't registered until the build method has been triggered.
68+
*
69+
* @return The classes that have been scanned and registered listeners for.
5770
*/
58-
public int getTimeout() {
59-
return timeout;
71+
public Class<?>[] getListeners() {
72+
return listeners;
6073
}
6174

75+
/**
76+
* @return The StreamElements Account ID of the client.
77+
*/
78+
public String getAccountID() {
79+
return accountID;
80+
}
81+
6282
/**
6383
* @return The StreamElements JWT token of the client.
6484
*/
@@ -67,16 +87,17 @@ public String getToken() {
6787
}
6888

6989
/**
70-
* @return the StreamElements Account ID of the client.
90+
* @return The timeout defined by the client.
7191
*/
72-
public String getAccountID() {
73-
return accountID;
92+
public int getTimeout() {
93+
return timeout;
7494
}
7595

7696
//TODO
7797
public StreamElements build() {
78-
if (token == null || accountID == null) throw new StreamElementsException("The StreamElements token or account ID was not set!");
79-
return new StreamElementsClient(token, accountID, timeout);
98+
if (token == null || accountID == null)
99+
throw new StreamElementsException("The StreamElements token or account ID was not set!");
100+
return new StreamElementsClient(token, accountID, timeout, listeners);
80101
}
81102

82103
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package me.limeglass.streamelements.api.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface EventListener {
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package me.limeglass.streamelements.api.events;
2+
3+
import me.limeglass.streamelements.internals.events.ElementsEvent;
4+
5+
public class ConnectingEvent extends ElementsEvent {
6+
7+
public ConnectingEvent() {}
8+
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package me.limeglass.streamelements.api.events;
2+
3+
import me.limeglass.streamelements.internals.events.ElementsEvent;
4+
5+
public class ConnectionEvent extends ElementsEvent {
6+
7+
private final boolean successful;
8+
9+
public ConnectionEvent(boolean successful) {
10+
this.successful = successful;
11+
}
12+
13+
/**
14+
* @return If the connection is successful.
15+
*/
16+
public boolean isSuccessful() {
17+
return successful;
18+
}
19+
20+
}

src/main/java/me/limeglass/streamelements/internals/StreamElementsClient.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import java.util.List;
55
import java.util.stream.Collectors;
66

7+
import io.socket.client.Socket;
78
import me.limeglass.streamelements.api.StreamElements;
89
import me.limeglass.streamelements.api.objects.Activity;
910
import me.limeglass.streamelements.api.objects.Points;
1011
import me.limeglass.streamelements.api.objects.User;
12+
import me.limeglass.streamelements.internals.events.EventDispatcher;
13+
import me.limeglass.streamelements.internals.events.SocketListener;
1114
import me.limeglass.streamelements.internals.handlers.ElementsReaderHandler;
1215
import me.limeglass.streamelements.internals.handlers.ElementsRequest;
1316
import me.limeglass.streamelements.internals.handlers.ElementsRequest.HttpMethod;
@@ -17,20 +20,32 @@
1720
public class StreamElementsClient implements StreamElements {
1821

1922
private final String token, account;
23+
private Class<?>[] listeners;
2024
private final int timeout;
25+
private Socket socket;
2126

2227
/**
2328
* The StreamElements client Constructor
2429
*
2530
* @param token The JWT token used from StreamElements. (Found under account)
2631
* @param accountID The account ID used from StreamElements. (Found under account)
2732
*/
28-
public StreamElementsClient(String token, String account, int timeout) {
33+
public StreamElementsClient(String token, String account, int timeout, Class<?>[] listeners) {
2934
ElementsReaderHandler.load("me.limeglass.streamelements.internals.readers");
35+
if (listeners != null && listeners.length > 0)
36+
this.listeners = EventDispatcher.registerListeners(listeners);
37+
this.socket = SocketListener.registerSocket(this);
3038
this.account = account;
3139
this.timeout = timeout;
3240
this.token = token;
3341
}
42+
43+
/**
44+
* @return The classes that have been scanned and registered listeners for.
45+
*/
46+
public Class<?>[] getListeners() {
47+
return listeners;
48+
}
3449

3550
/**
3651
* @return the StreamElements Account ID of the client.
@@ -40,6 +55,13 @@ public String getAccountID() {
4055
return account;
4156
}
4257

58+
/**
59+
* @return The Socket.IO for realtime.streamelements.com
60+
*/
61+
public Socket getSocket() {
62+
return socket;
63+
}
64+
4365
/**
4466
* @return The StreamElements JWT token of the client.
4567
*/
@@ -91,6 +113,11 @@ public void setCurrentUserPoints(String username, long points) {
91113
String update = "/" + (current >= points ? "-" + (current - points) : points - current);
92114
ElementsRequest.makeRequest(this, PointsResponse.class, HttpMethod.PUT, ElementsEndpoints.POINTS + account + "/" + username + update);
93115
}
116+
117+
@Override
118+
public void setCurrentUserPoints(long points, Points... existing) {
119+
for (Points user : existing) setCurrentUserPoints(user.getUser(), points);
120+
}
94121

95122
@Override
96123
public void setCurrentUserPoints(User user, long points) {
@@ -117,4 +144,19 @@ public void addPoints(User user, long points) {
117144
addPoints(user.getName(), points);
118145
}
119146

147+
@Override
148+
public void removeUserPoints(User user) {
149+
ElementsRequest.makeRequest(this, PointsResponse.class, HttpMethod.DELETE, ElementsEndpoints.POINTS + account + "/" + user.getName());
150+
}
151+
152+
@Override
153+
public void resetCurrentPoints() {
154+
ElementsRequest.makeRequest(this, PointsResponse.class, HttpMethod.DELETE, ElementsEndpoints.POINTS + account + "/reset/current");
155+
}
156+
157+
@Override
158+
public void resetAllPoints() {
159+
ElementsRequest.makeRequest(this, PointsResponse.class, HttpMethod.DELETE, ElementsEndpoints.POINTS + account + "/reset/alltime");
160+
}
161+
120162
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.limeglass.streamelements.internals.events;
2+
3+
import io.socket.client.Socket;
4+
5+
public class ElementsEvent {
6+
7+
protected static Socket socket;
8+
9+
public static void setSocket(Socket s) {
10+
socket = s;
11+
}
12+
13+
}

0 commit comments

Comments
 (0)