Skip to content

Commit 7026ed2

Browse files
committed
feat: loosen NotNull checks
1 parent 2de7d1a commit 7026ed2

3 files changed

Lines changed: 26 additions & 35 deletions

File tree

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,26 @@ private Request(Request.@NotNull Builder builder) throws IllegalArgumentExceptio
8585

8686
// validate type & related arguments
8787
this.type = ExceptionUtil.validateNotNull(builder.type, "type");
88-
if (this.type.isEffectType()) {
89-
if (builder.effect == null)
90-
throw new IllegalArgumentException("effect cannot be null for effect packets");
91-
if (builder.viewer == null)
92-
throw new IllegalArgumentException("viewer cannot be null for effect packets");
93-
} else {
94-
if (builder.effect != null)
95-
throw new IllegalArgumentException("effect cannot be non-null for non-effect packets");
96-
if (builder.viewer != null)
97-
throw new IllegalArgumentException("viewer cannot be non-null for non-effect packets");
98-
if (builder.cost != null)
99-
throw new IllegalArgumentException("cost cannot be non-null for non-effect packets");
100-
if (builder.targets != null)
101-
throw new IllegalArgumentException("targets cannot be non-null for non-effect packets");
102-
if (builder.quantity != null)
103-
throw new IllegalArgumentException("quantity cannot be non-null for non-effect packets");
104-
105-
if (builder.password == null && this.type == Type.LOGIN)
106-
throw new IllegalArgumentException("password cannot be null for login packets");
107-
108-
if (builder.player == null && this.type == Type.PLAYER_INFO)
109-
throw new IllegalArgumentException("player cannot be null for player info packets");
110-
}
111-
112-
// other arguments
88+
if (!this.type.isEffectType()) {
89+
if (builder.effect != null)
90+
throw new IllegalArgumentException("effect cannot be non-null for non-effect packets");
91+
if (builder.viewer != null)
92+
throw new IllegalArgumentException("viewer cannot be non-null for non-effect packets");
93+
if (builder.cost != null)
94+
throw new IllegalArgumentException("cost cannot be non-null for non-effect packets");
95+
if (builder.targets != null)
96+
throw new IllegalArgumentException("targets cannot be non-null for non-effect packets");
97+
if (builder.quantity != null)
98+
throw new IllegalArgumentException("quantity cannot be non-null for non-effect packets");
99+
100+
if (builder.password == null && this.type == Type.LOGIN)
101+
throw new IllegalArgumentException("password cannot be null for login packets");
102+
103+
if (builder.player == null && this.type == Type.PLAYER_INFO)
104+
throw new IllegalArgumentException("player cannot be null for player info packets");
105+
}
106+
107+
// other arguments
113108
this.effect = builder.effect == null ? null : builder.effect.toLowerCase(Locale.ENGLISH);
114109
this.viewer = builder.viewer;
115110
this.message = builder.message;

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ public void postProcessorTest() {
1919
public void constructorTest() {
2020
// new constructor tests //
2121

22-
// null effect test
23-
Assertions.assertThrows(IllegalArgumentException.class,
24-
() -> new Request.Builder().id(1).type(Request.Type.START).viewer("qixils").build());
25-
// null viewer test
26-
Assertions.assertThrows(IllegalArgumentException.class,
27-
() -> new Request.Builder().id(1).type(Request.Type.START).effect("summon").build());
2822
// negative cost test
2923
Assertions.assertThrows(IllegalArgumentException.class,
3024
() -> new Request.Builder().id(1).type(Request.Type.START).effect("summon").viewer("qixils").cost(-1).build());

receiver/src/main/java/dev/qixils/crowdcontrol/CrowdControl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ else if (!annotation.effect().isEmpty())
328328
* @since 1.0.0
329329
*/
330330
@ApiStatus.AvailableSince("1.0.0")
331-
public void registerHandler(@NotNull String effect, @NotNull Function<Request, Response> handler) {
332-
effect = effect.toLowerCase(Locale.ENGLISH);
331+
public void registerHandler(@Nullable String effect, @NotNull Function<Request, Response> handler) {
332+
if (effect != null)
333+
effect = effect.toLowerCase(Locale.ENGLISH);
333334
if (effectHandlers.containsKey(effect) || asyncHandlers.containsKey(effect)) {
334335
throw new IllegalArgumentException("The effect \"" + effect + "\" already has a handler.");
335336
}
@@ -346,8 +347,9 @@ public void registerHandler(@NotNull String effect, @NotNull Function<Request, R
346347
* @since 2.0.0
347348
*/
348349
@ApiStatus.AvailableSince("2.0.0")
349-
public void registerHandler(@NotNull String effect, @NotNull Consumer<Request> handler) {
350-
effect = effect.toLowerCase(Locale.ENGLISH);
350+
public void registerHandler(@Nullable String effect, @NotNull Consumer<Request> handler) {
351+
if (effect != null)
352+
effect = effect.toLowerCase(Locale.ENGLISH);
351353
if (effectHandlers.containsKey(effect) || asyncHandlers.containsKey(effect)) {
352354
throw new IllegalArgumentException("The effect \"" + effect + "\" already has a handler.");
353355
}

0 commit comments

Comments
 (0)