Skip to content

Commit a523979

Browse files
committed
Use an optional int for command redirection
1 parent 24cb5ff commit a523979

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/main/java/com/github/steveice10/mc/protocol/data/game/command/CommandNode.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import lombok.Data;
66
import lombok.NonNull;
77

8+
import java.util.OptionalInt;
9+
810
@Data
911
@AllArgsConstructor
1012
public class CommandNode {
@@ -24,9 +26,9 @@ public class CommandNode {
2426
private final @NonNull int[] childIndices;
2527

2628
/**
27-
* Redirect index, or -1 if none is set.
29+
* Redirect index, or empty if none is set.
2830
*/
29-
private final int redirectIndex;
31+
private final OptionalInt redirectIndex;
3032

3133
/**
3234
* Name of the node, if type is LITERAL or ARGUMENT.

src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import lombok.NonNull;
1313
import lombok.With;
1414

15+
import java.util.OptionalInt;
16+
1517
@Data
1618
@With
1719
@AllArgsConstructor
@@ -42,9 +44,11 @@ public ClientboundCommandsPacket(ByteBuf in, MinecraftCodecHelper helper) {
4244
children[j] = helper.readVarInt(in);
4345
}
4446

45-
int redirectIndex = 0;
47+
OptionalInt redirectIndex;
4648
if ((flags & FLAG_REDIRECT) != 0) {
47-
redirectIndex = helper.readVarInt(in);
49+
redirectIndex = OptionalInt.of(helper.readVarInt(in));
50+
} else {
51+
redirectIndex = OptionalInt.empty();
4852
}
4953

5054
String name = null;
@@ -163,7 +167,7 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
163167
flags |= FLAG_EXECUTABLE;
164168
}
165169

166-
if (node.getRedirectIndex() != 0) {
170+
if (node.getRedirectIndex().isPresent()) {
167171
flags |= FLAG_REDIRECT;
168172
}
169173

@@ -178,8 +182,8 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
178182
helper.writeVarInt(out, childIndex);
179183
}
180184

181-
if (node.getRedirectIndex() != 0) {
182-
helper.writeVarInt(out, node.getRedirectIndex());
185+
if (node.getRedirectIndex().isPresent()) {
186+
helper.writeVarInt(out, node.getRedirectIndex().getAsInt());
183187
}
184188

185189
if (node.getType() == CommandType.LITERAL || node.getType() == CommandType.ARGUMENT) {

src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacketTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.github.steveice10.mc.protocol.packet.PacketTest;
1010
import org.junit.Before;
1111

12+
import java.util.OptionalInt;
13+
1214
public class ClientboundCommandsPacketTest extends PacketTest {
1315
@Before
1416
public void setup() {
@@ -18,7 +20,7 @@ public void setup() {
1820
CommandType.ROOT,
1921
true,
2022
new int[]{1, 2},
21-
0,
23+
OptionalInt.empty(),
2224
null,
2325
null,
2426
null,
@@ -28,7 +30,7 @@ public void setup() {
2830
CommandType.LITERAL,
2931
false,
3032
new int[]{3, 4},
31-
0,
33+
OptionalInt.empty(),
3234
"Literal",
3335
null,
3436
null,
@@ -38,7 +40,7 @@ public void setup() {
3840
CommandType.ARGUMENT,
3941
false,
4042
new int[0],
41-
3,
43+
OptionalInt.of(3),
4244
"Argument1",
4345
CommandParser.DOUBLE,
4446
new DoubleProperties(),
@@ -48,7 +50,7 @@ public void setup() {
4850
CommandType.ARGUMENT,
4951
false,
5052
new int[0],
51-
0,
53+
OptionalInt.empty(),
5254
"Argument2",
5355
CommandParser.DOUBLE,
5456
new DoubleProperties(0, 100),
@@ -58,7 +60,7 @@ public void setup() {
5860
CommandType.ARGUMENT,
5961
false,
6062
new int[0],
61-
0,
63+
OptionalInt.empty(),
6264
"Argument3",
6365
CommandParser.STRING,
6466
StringProperties.SINGLE_WORD,
@@ -68,7 +70,7 @@ public void setup() {
6870
CommandType.ARGUMENT,
6971
false,
7072
new int[0],
71-
0,
73+
OptionalInt.empty(),
7274
"Argument4",
7375
CommandParser.STRING,
7476
StringProperties.SINGLE_WORD,

0 commit comments

Comments
 (0)