Skip to content

Commit 36e22b5

Browse files
committed
feat: player info rework
1 parent 8702fe0 commit 36e22b5

5 files changed

Lines changed: 140 additions & 19 deletions

File tree

pojos/src/main/java/dev/qixils/crowdcontrol/socket/Request.java

Lines changed: 114 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class Request implements JsonObject, Respondable {
5656
private String login;
5757
@Nullable
5858
private String password;
59+
@Nullable
60+
private Target player;
5961

6062
/**
6163
* Instantiates an empty {@link Request}.
@@ -107,6 +109,9 @@ private Request(Request.@NotNull Builder builder) throws IllegalArgumentExceptio
107109

108110
if (builder.password == null && this.type == Type.LOGIN)
109111
throw new IllegalArgumentException("password cannot be null for login packets");
112+
113+
if (builder.player == null && this.type == Type.PLAYER_INFO)
114+
throw new IllegalArgumentException("player cannot be null for player info packets");
110115
}
111116

112117
// other arguments
@@ -132,6 +137,7 @@ private Request(Request.@NotNull Builder builder) throws IllegalArgumentExceptio
132137
this.originatingSocket = builder.originatingSocket;
133138
this.login = builder.login;
134139
this.password = builder.password;
140+
this.player = builder.player;
135141

136142
// validate targets are not null
137143
if (builder.targets != null) {
@@ -363,6 +369,19 @@ public String getPassword() {
363369
return password;
364370
}
365371

372+
/**
373+
* If this is a {@link Type#PLAYER_INFO} packet, gets the player's info.
374+
*
375+
* @return player info
376+
* @since 3.6.2
377+
*/
378+
@ApiStatus.AvailableSince("3.6.2")
379+
@Nullable
380+
@CheckReturnValue
381+
public Target getPlayer() {
382+
return player;
383+
}
384+
366385
/**
367386
* Gets the {@link Socket} that this {@link Request} originated from.
368387
*
@@ -469,12 +488,15 @@ public boolean equals(@Nullable Object o) {
469488
&& Objects.equals(duration, request.duration)
470489
// && Objects.equals(originatingSocket, request.originatingSocket)
471490
&& Objects.equals(source, request.source)
472-
&& Objects.equals(value, request.value);
491+
&& Objects.equals(value, request.value)
492+
&& Objects.equals(login, request.login)
493+
&& Objects.equals(password, request.password)
494+
&& Objects.equals(player, request.player);
473495
}
474496

475497
@Override
476498
public int hashCode() {
477-
int result = Objects.hash(id, type, effect, message, viewer, cost, duration, source, value);
499+
int result = Objects.hash(id, type, effect, message, viewer, cost, duration, source, value, login, password, player);
478500
result = 31 * result + Arrays.hashCode(getTargets());
479501
result = 31 * result + Arrays.hashCode(getParameters());
480502
return result;
@@ -495,6 +517,9 @@ public String toString() {
495517
", parameters=" + Arrays.toString(parameters) +
496518
", source=" + source +
497519
", value=" + value +
520+
", login=" + repr(login) +
521+
", password=" + repr(password) +
522+
", player=" + player +
498523
'}';
499524
}
500525

@@ -632,11 +657,15 @@ public TriState usesIncrementalIds() {
632657
*/
633658
@ApiStatus.AvailableSince("3.0.0")
634659
public static final class Target {
660+
@SerializedName(value = "id", alternate = {"originID"})
635661
private @Nullable String id;
636662
private @Nullable String name;
637663
private @Nullable String login;
664+
@SerializedName(value = "avatar", alternate = {"image"})
638665
private @Nullable String avatar;
666+
@SerializedName(value = "service", alternate = {"profile"})
639667
private @Nullable String service;
668+
private @Nullable String ccUID;
640669

641670
/**
642671
* Instantiates an empty {@link Target}.
@@ -653,6 +682,7 @@ private Target(@NotNull Builder builder) {
653682
this.login = builder.login;
654683
this.avatar = builder.avatar;
655684
this.service = builder.service;
685+
this.ccUID = builder.ccUID;
656686
}
657687

658688
/**
@@ -727,6 +757,19 @@ public String getService() {
727757
return service;
728758
}
729759

760+
/**
761+
* Gets the Crowd Control user ID of the recipient.
762+
*
763+
* @return Crowd Control user ID
764+
* @since 3.6.2
765+
*/
766+
@ApiStatus.AvailableSince("3.6.2")
767+
@Nullable
768+
@CheckReturnValue
769+
public String getCCUID() {
770+
return ccUID;
771+
}
772+
730773
@Override
731774
@CheckReturnValue
732775
public boolean equals(@Nullable Object o) {
@@ -737,7 +780,8 @@ public boolean equals(@Nullable Object o) {
737780
&& Objects.equals(getName(), target.getName())
738781
&& Objects.equals(getLogin(), target.getLogin())
739782
&& Objects.equals(getAvatar(), target.getAvatar())
740-
&& Objects.equals(getService(), target.getService());
783+
&& Objects.equals(getService(), target.getService())
784+
&& Objects.equals(getCCUID(), target.getCCUID());
741785
}
742786

743787
/**
@@ -754,13 +798,15 @@ public boolean equalsRoughly(@Nullable Object o) {
754798
if (this == o) return true;
755799
if (o == null || getClass() != o.getClass()) return false;
756800
Target target = (Target) o;
757-
return (getId() != null && getId().equals(target.getId())) || equals(o);
801+
return (getId() != null && getId().equals(target.getId()))
802+
|| (getCCUID() != null && getCCUID().equals(target.getCCUID()))
803+
|| equals(o);
758804
}
759805

760806
@Override
761807
@CheckReturnValue
762808
public int hashCode() {
763-
return Objects.hash(getId(), getName(), getLogin(), getAvatar(), getService());
809+
return Objects.hash(getId(), getName(), getLogin(), getAvatar(), getService(), getCCUID());
764810
}
765811

766812
@Override
@@ -771,6 +817,7 @@ public String toString() {
771817
", login=" + repr(login) +
772818
", avatar=" + repr(avatar) +
773819
", service=" + repr(service) +
820+
", ccUID=" + repr(ccUID) +
774821
'}';
775822
}
776823

@@ -798,6 +845,7 @@ public static final class Builder implements Cloneable {
798845
private @Nullable String login;
799846
private @Nullable String avatar;
800847
private @Nullable String service;
848+
private @Nullable String ccUID;
801849

802850
// constructors //
803851

@@ -815,6 +863,7 @@ private Builder(@NotNull Target target) {
815863
this.login = target.login;
816864
this.avatar = target.avatar;
817865
this.service = target.service;
866+
this.ccUID = target.ccUID;
818867
}
819868

820869
private Builder(@NotNull Builder builder) {
@@ -823,6 +872,7 @@ private Builder(@NotNull Builder builder) {
823872
this.login = builder.login;
824873
this.avatar = builder.avatar;
825874
this.service = builder.service;
875+
this.ccUID = builder.ccUID;
826876
}
827877

828878
// setters //
@@ -902,6 +952,21 @@ public Builder service(@Nullable String service) {
902952
return this;
903953
}
904954

955+
/**
956+
* Sets the Crowd Control user ID of the recipient.
957+
*
958+
* @param ccUID Crowd Control user ID
959+
* @return this builder
960+
* @since 3.6.2
961+
*/
962+
@ApiStatus.AvailableSince("3.6.2")
963+
@NotNull
964+
@Contract("_ -> this")
965+
public Builder ccUID(@Nullable String ccUID) {
966+
this.ccUID = ccUID;
967+
return this;
968+
}
969+
905970
// getters //
906971

907972
/**
@@ -969,6 +1034,19 @@ public String service() {
9691034
return service;
9701035
}
9711036

1037+
/**
1038+
* Gets the Crowd Control user ID of the recipient.
1039+
*
1040+
* @return Crowd Control user ID
1041+
* @since 3.6.2
1042+
*/
1043+
@ApiStatus.AvailableSince("3.6.2")
1044+
@Nullable
1045+
@CheckReturnValue
1046+
public String ccUID() {
1047+
return ccUID;
1048+
}
1049+
9721050
// misc
9731051

9741052
/**
@@ -1278,6 +1356,7 @@ public static class Builder implements Cloneable {
12781356
private @Nullable Integer quantity;
12791357
private @Nullable String login;
12801358
private @Nullable String password;
1359+
private @Nullable Target player;
12811360

12821361
/**
12831362
* Creates a new builder.
@@ -1312,6 +1391,7 @@ private Builder(@NotNull Request source) {
13121391
this.quantity = source.quantity;
13131392
this.login = source.login;
13141393
this.password = source.password;
1394+
this.player = source.player;
13151395
}
13161396

13171397
/**
@@ -1338,6 +1418,7 @@ private Builder(@NotNull Builder builder) {
13381418
this.quantity = builder.quantity;
13391419
this.login = builder.login;
13401420
this.password = builder.password;
1421+
this.player = builder.player;
13411422
}
13421423

13431424
// setters
@@ -1568,6 +1649,21 @@ public Builder password(@Nullable String password) {
15681649
return this;
15691650
}
15701651

1652+
/**
1653+
* Sets the {@link Type#PLAYER_INFO} data.
1654+
*
1655+
* @param player a player target
1656+
* @return this builder
1657+
* @since 3.6.2
1658+
*/
1659+
@ApiStatus.AvailableSince("3.6.2")
1660+
@NotNull
1661+
@Contract("_ -> this")
1662+
public Builder player(@Nullable Target player) {
1663+
this.player = player;
1664+
return this;
1665+
}
1666+
15711667
// getters
15721668

15731669
/**
@@ -1763,6 +1859,19 @@ public String password() {
17631859
return password;
17641860
}
17651861

1862+
/**
1863+
* Gets the {@link Type#PLAYER_INFO} data.
1864+
*
1865+
* @return a player target
1866+
* @since 3.6.2
1867+
*/
1868+
@ApiStatus.AvailableSince("3.6.2")
1869+
@Nullable
1870+
@CheckReturnValue
1871+
public Target player() {
1872+
return player;
1873+
}
1874+
17661875
// build
17671876

17681877
/**

pojos/src/test/java/dev/qixils/crowdcontrol/socket/RequestTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public void constructorTest() {
3434
// using value with non-remote function result test
3535
Assertions.assertThrows(IllegalArgumentException.class,
3636
() -> new Request.Builder().id(1).type(Request.Type.START).effect("summon").viewer("qixils").value(true).build());
37+
// null player test
38+
Assertions.assertThrows(IllegalArgumentException.class,
39+
() -> new Request.Builder().type(Request.Type.PLAYER_INFO).build());
3740
// valid builder test
3841
Assertions.assertDoesNotThrow(
3942
() -> new Request.Builder().id(1).type(Request.Type.START).effect("summon").viewer("qixils").build());
@@ -87,6 +90,7 @@ public void getterTest() {
8790
.source(new Request.Source.Builder().target(source).build())
8891
.login("qixils")
8992
.password("password")
93+
.player(source)
9094
.build();
9195
Assertions.assertEquals(1, request.getId());
9296
Assertions.assertEquals(Request.Type.START, request.getType());
@@ -100,6 +104,7 @@ public void getterTest() {
100104
Assertions.assertFalse(request.isGlobal());
101105
Assertions.assertEquals("qixils", request.getLogin());
102106
Assertions.assertEquals("password", request.getPassword());
107+
Assertions.assertEquals(source, request.getPlayer());
103108
Assertions.assertEquals(2, request.getTargets().length);
104109
Assertions.assertEquals(3, request.getQuantity());
105110
// target 1
@@ -227,6 +232,10 @@ public void builderTest() {
227232
Assertions.assertNull(builder.quantity());
228233
Assertions.assertEquals(4, builder.quantity(4).quantity());
229234

235+
// player test
236+
Assertions.assertNull(builder.player());
237+
Assertions.assertEquals(targetBuilder.build(), builder.player(targetBuilder.build()).player());
238+
230239
// test cloning and building/toBuilder
231240
Request request = builder.clone().build().toBuilder().build();
232241
Assertions.assertEquals(1, request.getId());
@@ -241,6 +250,7 @@ public void builderTest() {
241250
Assertions.assertFalse(request.isGlobal());
242251
Assertions.assertEquals("qixils", request.getLogin());
243252
Assertions.assertEquals("password", request.getPassword());
253+
Assertions.assertEquals(targetBuilder.build(), request.getPlayer());
244254
Assertions.assertEquals(2, builder.targets().length);
245255
Assertions.assertEquals(4, request.getQuantity());
246256
Assertions.assertEquals(1, request.toBuilder().quantity(null).build().getQuantityOrDefault());
@@ -290,9 +300,10 @@ public void serializationTest() {
290300
.quantity(3)
291301
.login("qixils")
292302
.password("password")
303+
.player(new Request.Target.Builder().id("493").name("epic streamer 493").ccUID("blahblah").avatar("https://i.qixils.dev/favicon.png").service("TWITCH").clone().build().toBuilder().build())
293304
.clone()
294305
.build();
295-
String json = "{\"id\":1,\"type\":1,\"code\":\"summon\",\"viewer\":\"qixils\",\"message\":\"Hello\",\"cost\":10,\"duration\":10000,\"targets\":[{\"id\":\"493\",\"name\":\"epic streamer 493\",\"login\":\"streamer\",\"avatar\":\"https://i.qixils.dev/favicon.png\",\"service\":\"TWITCH\"},{}],\"parameters\":[5.0],\"quantity\":3,\"login\":\"qixils\",\"password\":\"password\"}";
306+
String json = "{\"id\":1,\"type\":1,\"code\":\"summon\",\"viewer\":\"qixils\",\"message\":\"Hello\",\"cost\":10,\"duration\":10000,\"targets\":[{\"id\":\"493\",\"name\":\"epic streamer 493\",\"login\":\"streamer\",\"avatar\":\"https://i.qixils.dev/favicon.png\",\"service\":\"TWITCH\"},{}],\"parameters\":[5.0],\"quantity\":3,\"login\":\"qixils\",\"password\":\"password\",\"player\":{\"originID\":\"493\",\"name\":\"epic streamer 493\",\"image\":\"https://i.qixils.dev/favicon.png\",\"profile\":\"TWITCH\",\"ccUID\":\"blahblah\"}}";
296307
Assertions.assertEquals(request, Request.fromJSON(json), () -> "JSONs: " + request.toJSON() + " vs " + json);
297308
Assertions.assertEquals(Request.fromJSON(request.toJSON()), Request.fromJSON(json), () -> "JSONs: " + request.toJSON() + " vs " + json);
298309
}

receiver/src/main/java/dev/qixils/crowdcontrol/socket/ClientSocketManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void shutdown(@Nullable Request cause, @Nullable String reason) throws IO
131131

132132
@Override
133133
public @NotNull Set<Request.Source> getSources() {
134-
if (effectExecutor == null || effectExecutor.getSource() == null)
134+
if (effectExecutor == null)
135135
return Collections.emptySet();
136136
return Collections.singleton(effectExecutor.getSource());
137137
}

0 commit comments

Comments
 (0)