Skip to content

Commit df17e6f

Browse files
committed
Remove Minestom Check usage for robustness.
Update to v1.3.2
1 parent 721decc commit df17e6f

File tree

4 files changed

+77
-76
lines changed

4 files changed

+77
-76
lines changed

src/main/java/dev/kerman/freight/BungeeMessage.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import net.minestom.server.network.packet.server.common.PluginMessagePacket;
88
import net.minestom.server.network.player.PlayerConnection;
99
import net.minestom.server.utils.PacketSendingUtils;
10-
import net.minestom.server.utils.validate.Check;
1110
import org.jetbrains.annotations.NotNull;
1211
import org.jetbrains.annotations.Nullable;
1312

1413
import java.util.ArrayList;
1514
import java.util.Collection;
1615
import java.util.Collections;
1716
import java.util.List;
17+
import java.util.Objects;
1818

1919
/**
2020
* BungeeCord Messaging interface.
@@ -95,7 +95,7 @@ static boolean isIdentifier(@Nullable String channel) {
9595
* @return the byte array containing the serialized message
9696
*/
9797
static byte @NotNull [] write(@NotNull BungeeMessage message) {
98-
Check.notNull(message, "Message cannot be null");
98+
Objects.requireNonNull(message, "Message cannot be null");
9999
return switch (message) {
100100
case BungeeRequest request -> writeRequest(request);
101101
case BungeeResponse response -> writeResponse(response);
@@ -112,7 +112,7 @@ static boolean isIdentifier(@Nullable String channel) {
112112
* @return the byte array containing the serialized request
113113
*/
114114
static byte @NotNull [] writeRequest(@NotNull BungeeRequest request) {
115-
Check.notNull(request, "Request cannot be null");
115+
Objects.requireNonNull(request, "Request cannot be null");
116116
return NetworkBuffer.makeArray(BungeeRequest.SERIALIZER, request);
117117
}
118118

@@ -125,7 +125,7 @@ static boolean isIdentifier(@Nullable String channel) {
125125
* @throws IllegalStateException if there are leftover bytes in the buffer after reading the request
126126
*/
127127
static @NotNull BungeeRequest readRequest(byte @NotNull [] bytes) throws IllegalStateException {
128-
Check.notNull(bytes, "Bytes cannot be null!");
128+
Objects.requireNonNull(bytes, "Bytes cannot be null!");
129129
return readRequest(NetworkBuffer.wrap(bytes, 0, bytes.length));
130130
}
131131

@@ -138,7 +138,7 @@ static boolean isIdentifier(@Nullable String channel) {
138138
* @throws IllegalStateException if there are leftover bytes in the buffer after reading the request
139139
*/
140140
static @NotNull BungeeRequest readRequest(@NotNull NetworkBuffer buffer) throws IllegalStateException {
141-
Check.notNull(buffer, "Buffer cannot be null!");
141+
Objects.requireNonNull(buffer, "Buffer cannot be null!");
142142
return BungeeProtocol.read(buffer, BungeeRequest.SERIALIZER);
143143
}
144144

@@ -151,7 +151,7 @@ static boolean isIdentifier(@Nullable String channel) {
151151
* @return the byte array containing the serialized response
152152
*/
153153
static byte @NotNull [] writeResponse(@NotNull BungeeResponse response) {
154-
Check.notNull(response, "Response cannot be null");
154+
Objects.requireNonNull(response, "Response cannot be null");
155155
return NetworkBuffer.makeArray(BungeeResponse.SERIALIZER, response);
156156
}
157157

@@ -163,7 +163,7 @@ static boolean isIdentifier(@Nullable String channel) {
163163
* @throws IllegalStateException if there are leftover bytes in the buffer after reading the request
164164
*/
165165
static @NotNull BungeeResponse readResponse(byte @NotNull [] bytes) throws IllegalStateException {
166-
Check.notNull(bytes, "Bytes cannot be null!");
166+
Objects.requireNonNull(bytes, "Bytes cannot be null!");
167167
return readResponse(NetworkBuffer.wrap(bytes, 0, bytes.length));
168168
}
169169

@@ -176,7 +176,7 @@ static boolean isIdentifier(@Nullable String channel) {
176176
* @throws IllegalStateException if there are leftover bytes in the buffer after reading the request
177177
*/
178178
static @NotNull BungeeResponse readResponse(@NotNull NetworkBuffer buffer) throws IllegalStateException {
179-
Check.notNull(buffer, "Buffer cannot be null!");
179+
Objects.requireNonNull(buffer, "Buffer cannot be null!");
180180
return BungeeProtocol.read(buffer, BungeeResponse.SERIALIZER);
181181
}
182182

@@ -191,7 +191,7 @@ static boolean isIdentifier(@Nullable String channel) {
191191
* @throws IllegalStateException if there are leftover bytes in the buffer after reading the request
192192
*/
193193
static @Nullable BungeeResponse readResponse(@NotNull PlayerPluginMessageEvent event) throws IllegalStateException {
194-
Check.notNull(event, "Event cannot be null");
194+
Objects.requireNonNull(event, "Event cannot be null");
195195
if (!isIdentifier(event.getIdentifier())) return null;
196196
return readResponse(event.getMessage());
197197
}
@@ -207,8 +207,8 @@ static boolean isIdentifier(@Nullable String channel) {
207207
* @param message the message to send
208208
*/
209209
static void send(@NotNull PlayerConnection connection, @NotNull BungeeMessage message) {
210-
Check.notNull(connection, "Connection cannot be null");
211-
Check.notNull(message, "Message cannot be null");
210+
Objects.requireNonNull(connection, "Connection cannot be null");
211+
Objects.requireNonNull(message, "Message cannot be null");
212212
connection.sendPacket(message.toPacket());
213213
}
214214

@@ -222,8 +222,8 @@ static void send(@NotNull PlayerConnection connection, @NotNull BungeeMessage me
222222
* @param message the message to send
223223
*/
224224
static void send(@NotNull Audience audience, @NotNull BungeeMessage message) {
225-
Check.notNull(audience, "Audience cannot be null");
226-
Check.notNull(message, "Message cannot be null");
225+
Objects.requireNonNull(audience, "Audience cannot be null");
226+
Objects.requireNonNull(message, "Message cannot be null");
227227
PacketSendingUtils.sendPacket(audience, message.toPacket());
228228
}
229229

@@ -236,9 +236,9 @@ static void send(@NotNull Audience audience, @NotNull BungeeMessage message) {
236236
* @throws IllegalArgumentException if the collection is empty
237237
*/
238238
static void sendSingle(@NotNull Collection<? extends Audience> audiences, @NotNull BungeeMessage message) {
239-
Check.notNull(audiences, "Audiences cannot be null");
240-
Check.notNull(message, "Message cannot be null");
241-
Check.argCondition(audiences.isEmpty(), "Audiences cannot be empty");
239+
Objects.requireNonNull(audiences, "Audiences cannot be null");
240+
Objects.requireNonNull(message, "Message cannot be null");
241+
if (audiences.isEmpty()) throw new IllegalArgumentException("Audiences cannot be empty");
242242
List<Audience> collection = new ArrayList<>(audiences);
243243
Collections.shuffle(collection);
244244
BungeeMessage.send(collection.getFirst(), message);
@@ -278,7 +278,7 @@ static void sendSingle(@NotNull Collection<? extends Audience> audiences, @NotNu
278278
* @param connection the player connection to send the message to
279279
*/
280280
default void send(@NotNull PlayerConnection connection) {
281-
Check.notNull(connection, "Connection cannot be null");
281+
Objects.requireNonNull(connection, "Connection cannot be null");
282282
BungeeMessage.send(connection, this);
283283
}
284284

@@ -291,7 +291,7 @@ default void send(@NotNull PlayerConnection connection) {
291291
* @param audience the audience to send the message
292292
*/
293293
default void send(@NotNull Audience audience) {
294-
Check.notNull(audience, "Audience cannot be null");
294+
Objects.requireNonNull(audience, "Audience cannot be null");
295295
BungeeMessage.send(audience, this);
296296
}
297297

@@ -305,8 +305,8 @@ default void send(@NotNull Audience audience) {
305305
* @throws IllegalArgumentException if the collection is empty
306306
*/
307307
default void sendSingle(@NotNull Collection<? extends Audience> audiences) {
308-
Check.notNull(audiences, "Audience cannot be null");
309-
Check.argCondition(audiences.isEmpty(), "Audiences cannot be empty");
308+
Objects.requireNonNull(audiences, "Audience cannot be null");
309+
if (audiences.isEmpty()) throw new IllegalArgumentException("Audiences cannot be empty");
310310
BungeeMessage.sendSingle(audiences, this);
311311
}
312312
}

src/main/java/dev/kerman/freight/BungeeProtocol.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dev.kerman.freight;
22

33
import net.minestom.server.network.NetworkBuffer;
4-
import net.minestom.server.utils.validate.Check;
54
import org.jetbrains.annotations.NotNull;
65
import org.jetbrains.annotations.Nullable;
76

@@ -33,16 +32,16 @@ final class BungeeProtocol {
3332
@Override
3433
public void write(@NotNull NetworkBuffer buffer, byte @NotNull [] value) {
3534
final int length = value.length;
36-
Check.argCondition(length > 65535, "Value too long");
35+
if (length > 65535) throw new IllegalStateException("Value too long");
3736
buffer.write(NetworkBuffer.UNSIGNED_SHORT, length);
3837
buffer.write(NetworkBuffer.FixedRawBytes(length), value);
3938
}
4039

4140
@Override
4241
public byte[] read(@NotNull NetworkBuffer buffer) {
4342
final int length = buffer.read(NetworkBuffer.UNSIGNED_SHORT);
44-
Check.stateCondition(length > 65535, "Value too long");
45-
Check.stateCondition(buffer.readableBytes() > length, "Value too long to read");
43+
if (length > 65535) throw new IllegalStateException("Value too long");
44+
if (buffer.readableBytes() > length) throw new IllegalStateException("Value too long to read");
4645
return buffer.read(NetworkBuffer.FixedRawBytes(length));
4746
}
4847
};

src/main/java/dev/kerman/freight/BungeeRequest.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import net.minestom.server.network.NetworkBufferTemplate;
1111
import net.minestom.server.network.packet.client.common.ClientPluginMessagePacket;
1212
import net.minestom.server.network.packet.server.common.PluginMessagePacket;
13-
import net.minestom.server.utils.validate.Check;
1413
import org.jetbrains.annotations.ApiStatus;
1514
import org.jetbrains.annotations.NotNull;
1615

1716
import java.util.Arrays;
17+
import java.util.Objects;
1818

1919
/**
2020
* BungeeCord request interface.
@@ -41,7 +41,7 @@ record Connect(@NotNull String serverName) implements BungeeRequest {
4141
);
4242

4343
public Connect {
44-
Check.notNull(serverName, "Server name cannot be null");
44+
serverName = Objects.requireNonNull(serverName, "Server name cannot be null");
4545
}
4646
}
4747

@@ -59,8 +59,8 @@ record ConnectOther(@NotNull String playerName, @NotNull String serverName) impl
5959
);
6060

6161
public ConnectOther {
62-
Check.notNull(playerName, "Player name cannot be null");
63-
Check.notNull(serverName, "Server name cannot be null");
62+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
63+
serverName = Objects.requireNonNull(serverName, "Server name cannot be null");
6464
}
6565

6666
/**
@@ -93,7 +93,7 @@ record IPOther(@NotNull String playerName) implements BungeeRequest {
9393
);
9494

9595
public IPOther {
96-
Check.notNull(playerName, "Player name cannot be null");
96+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
9797
}
9898

9999
/**
@@ -117,7 +117,7 @@ record PlayerCount(@NotNull String serverName) implements BungeeRequest {
117117
);
118118

119119
public PlayerCount {
120-
Check.notNull(serverName, "Server name cannot be null");
120+
serverName = Objects.requireNonNull(serverName, "Server name cannot be null");
121121
}
122122

123123
/**
@@ -140,7 +140,7 @@ record PlayerList(@NotNull String serverName) implements BungeeRequest {
140140
);
141141

142142
public PlayerList {
143-
Check.notNull(serverName, "Server name cannot be null");
143+
serverName = Objects.requireNonNull(serverName, "Server name cannot be null");
144144
}
145145

146146
/**
@@ -173,8 +173,8 @@ record Message(@NotNull String playerName, @NotNull String message) implements B
173173
);
174174

175175
public Message {
176-
Check.notNull(playerName, "Player name cannot be null");
177-
Check.notNull(message, "Message cannot be null");
176+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
177+
message = Objects.requireNonNull(message, "Message cannot be null");
178178
}
179179

180180
/**
@@ -219,8 +219,8 @@ record MessageRaw(@NotNull String playerName, @NotNull String message) implement
219219
);
220220

221221
public MessageRaw {
222-
Check.notNull(playerName, "Player name cannot be null");
223-
Check.notNull(message, "Message cannot be null");
222+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
223+
message = Objects.requireNonNull(message, "Message cannot be null");
224224
}
225225

226226
/**
@@ -289,7 +289,7 @@ record GetPlayerServer(@NotNull String playerName) implements BungeeRequest {
289289
);
290290

291291
public GetPlayerServer {
292-
Check.notNull(playerName, "Player name cannot be null");
292+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
293293
}
294294

295295
public GetPlayerServer(@NotNull Pointered player) {
@@ -317,7 +317,7 @@ record UUIDOther(@NotNull String playerName) implements BungeeRequest {
317317
);
318318

319319
public UUIDOther {
320-
Check.notNull(playerName, "Player name cannot be null");
320+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
321321
}
322322

323323
public UUIDOther(@NotNull Pointered player) {
@@ -337,7 +337,7 @@ record ServerIP(@NotNull String serverName) implements BungeeRequest {
337337
);
338338

339339
public ServerIP {
340-
Check.notNull(serverName, "Server name cannot be null");
340+
serverName = Objects.requireNonNull(serverName, "Server name cannot be null");
341341
}
342342
}
343343

@@ -355,8 +355,8 @@ record KickPlayer(@NotNull String playerName, @NotNull String reason) implements
355355
);
356356

357357
public KickPlayer {
358-
Check.notNull(playerName, "Player name cannot be null");
359-
Check.notNull(reason, "Reason cannot be null");
358+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
359+
reason = Objects.requireNonNull(reason, "Reason cannot be null");
360360
}
361361

362362
/**
@@ -383,8 +383,8 @@ record KickPlayerRaw(@NotNull String playerName, @NotNull String reason) impleme
383383
);
384384

385385
public KickPlayerRaw {
386-
Check.notNull(playerName, "Player name cannot be null");
387-
Check.notNull(reason, "Reason cannot be null");
386+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
387+
reason = Objects.requireNonNull(reason, "Reason cannot be null");
388388
}
389389

390390
/**
@@ -431,10 +431,11 @@ record Forward(@NotNull String serverName, @NotNull String channel, byte @NotNul
431431
);
432432

433433
public Forward {
434-
Check.notNull(serverName, "Server cannot be null");
435-
Check.notNull(channel, "Channel cannot be null");
436-
Check.notNull(data, "Data cannot be null");
437-
Check.argCondition(data.length > 65535, "Data cannot be more than a 65535 in length");
434+
serverName = Objects.requireNonNull(serverName, "Server cannot be null");
435+
channel = Objects.requireNonNull(channel, "Channel cannot be null");
436+
Objects.requireNonNull(data, "Data cannot be null");
437+
if (data.length > 65535) // Check length before cloning
438+
throw new IllegalArgumentException("Data cannot be more than 65535 in length");
438439
data = data.clone();
439440
}
440441

@@ -526,10 +527,11 @@ record ForwardToPlayer(@NotNull String playerName, @NotNull String channel,
526527
);
527528

528529
public ForwardToPlayer {
529-
Check.notNull(playerName, "Player name cannot be null");
530-
Check.notNull(channel, "Channel cannot be null");
531-
Check.notNull(data, "Data cannot be null");
532-
Check.argCondition(data.length > 65535, "Data cannot be more than a 65535 in length");
530+
playerName = Objects.requireNonNull(playerName, "Player name cannot be null");
531+
channel = Objects.requireNonNull(channel, "Channel cannot be null");
532+
Objects.requireNonNull(data, "Data cannot be null");
533+
if (data.length > 65535) // Check length before cloning
534+
throw new IllegalArgumentException("Data cannot be more than 65535 in length");
533535
data = data.clone();
534536
}
535537

0 commit comments

Comments
 (0)