Skip to content

Commit cf37760

Browse files
committed
improvement: Split packet version handling for clientbound and serverbound
1 parent 0e5cae4 commit cf37760

12 files changed

+78
-51
lines changed

src/main/java/net/hypixel/modapi/HypixelModAPI.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
import net.hypixel.modapi.handler.ClientboundPacketHandler;
66
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
77
import net.hypixel.modapi.packet.PacketRegistry;
8-
import net.hypixel.modapi.packet.impl.VersionedPacket;
9-
import net.hypixel.modapi.packet.impl.clientbound.ClientboundLocationPacket;
10-
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPartyInfoPacket;
11-
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPingPacket;
12-
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPlayerInfoPacket;
8+
import net.hypixel.modapi.packet.impl.clientbound.*;
139
import net.hypixel.modapi.packet.impl.serverbound.ServerboundLocationPacket;
1410
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPartyInfoPacket;
1511
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPingPacket;
@@ -72,7 +68,7 @@ public void handle(String identifier, PacketSerializer serializer) {
7268
}
7369

7470
ClientboundHypixelPacket packet = registry.createClientboundPacket(identifier, serializer);
75-
if (packet instanceof VersionedPacket && !((VersionedPacket) packet).isExpectedVersion()) {
71+
if (packet instanceof ClientboundVersionedPacket && !((ClientboundVersionedPacket) packet).isExpectedVersion()) {
7672
// Ignore packets that don't match our expected version, these could be received due to other mods requesting them
7773
return;
7874
}

src/main/java/net/hypixel/modapi/packet/impl/VersionedPacket.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Represents a packet that is backed by a version. Versioned packets will only be handled if the incoming packet matches the version of the packet known.
88
*/
99
public abstract class VersionedPacket implements HypixelPacket {
10-
private int version;
10+
protected int version;
1111

1212
public VersionedPacket(int version) {
1313
this.version = version;
@@ -20,10 +20,7 @@ public VersionedPacket(PacketSerializer serializer) {
2020
/**
2121
* @return true if reading was successful, false if otherwise (such as due to a mismatch in version)
2222
*/
23-
protected boolean read(PacketSerializer serializer) {
24-
this.version = serializer.readVarInt();
25-
return isExpectedVersion();
26-
}
23+
protected abstract boolean read(PacketSerializer serializer);
2724

2825
@Override
2926
public void write(PacketSerializer serializer) {
@@ -34,12 +31,6 @@ public int getVersion() {
3431
return version;
3532
}
3633

37-
protected abstract int getLatestVersion();
38-
39-
public boolean isExpectedVersion() {
40-
return getVersion() == getLatestVersion();
41-
}
42-
4334
@Override
4435
public String toString() {
4536
return "VersionedPacket{" +

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundLocationPacket.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import net.hypixel.data.region.Environment;
44
import net.hypixel.data.type.ServerType;
55
import net.hypixel.modapi.handler.ClientboundPacketHandler;
6-
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
7-
import net.hypixel.modapi.packet.impl.VersionedPacket;
86
import net.hypixel.modapi.serializer.PacketSerializer;
97
import org.jetbrains.annotations.Nullable;
108

119
import java.util.Optional;
1210

13-
public class ClientboundLocationPacket extends VersionedPacket implements ClientboundHypixelPacket {
11+
public class ClientboundLocationPacket extends ClientboundVersionedPacket {
1412
private static final int CURRENT_VERSION = 1;
1513

1614
private Environment environment;

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundPartyInfoPacket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.util.*;
1010

11-
public class ClientboundPartyInfoPacket extends VersionedPacket implements ClientboundHypixelPacket {
11+
public class ClientboundPartyInfoPacket extends ClientboundVersionedPacket {
1212
private static final int CURRENT_VERSION = 1;
1313

1414
private boolean inParty;

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundPingPacket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import net.hypixel.modapi.packet.impl.VersionedPacket;
66
import net.hypixel.modapi.serializer.PacketSerializer;
77

8-
public class ClientboundPingPacket extends VersionedPacket implements ClientboundHypixelPacket {
8+
public class ClientboundPingPacket extends ClientboundVersionedPacket {
99
private static final int CURRENT_VERSION = 1;
1010

1111
private String response;

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundPlayerInfoPacket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import java.util.Optional;
1313

14-
public class ClientboundPlayerInfoPacket extends VersionedPacket implements ClientboundHypixelPacket {
14+
public class ClientboundPlayerInfoPacket extends ClientboundVersionedPacket {
1515
private static final int CURRENT_VERSION = 1;
1616

1717
private PlayerRank playerRank;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.hypixel.modapi.packet.impl.clientbound;
2+
3+
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
4+
import net.hypixel.modapi.packet.impl.VersionedPacket;
5+
import net.hypixel.modapi.serializer.PacketSerializer;
6+
7+
/**
8+
* Represents a packet that is backed by a version. Clientbound versioned packets will only be handled if the incoming packet matches the version of the packet known.
9+
*/
10+
public abstract class ClientboundVersionedPacket extends VersionedPacket implements ClientboundHypixelPacket {
11+
12+
public ClientboundVersionedPacket(int version) {
13+
super(version);
14+
}
15+
16+
public ClientboundVersionedPacket(PacketSerializer serializer) {
17+
super(serializer);
18+
}
19+
20+
/**
21+
* @return true if reading was successful, false if otherwise (such as due to a mismatch in version)
22+
*/
23+
protected boolean read(PacketSerializer serializer) {
24+
this.version = serializer.readVarInt();
25+
return isExpectedVersion();
26+
}
27+
28+
@Override
29+
public void write(PacketSerializer serializer) {
30+
serializer.writeVarInt(version);
31+
}
32+
33+
protected abstract int getLatestVersion();
34+
35+
public boolean isExpectedVersion() {
36+
return getVersion() == getLatestVersion();
37+
}
38+
39+
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package net.hypixel.modapi.packet.impl.serverbound;
22

3-
import net.hypixel.modapi.packet.impl.VersionedPacket;
43
import net.hypixel.modapi.serializer.PacketSerializer;
54

6-
public class ServerboundLocationPacket extends VersionedPacket {
5+
public class ServerboundLocationPacket extends ServerboundVersionedPacket {
76
private static final int CURRENT_VERSION = 1;
87

98
public ServerboundLocationPacket() {
@@ -14,9 +13,4 @@ public ServerboundLocationPacket(PacketSerializer serializer) {
1413
super(serializer);
1514
}
1615

17-
@Override
18-
protected int getLatestVersion() {
19-
return CURRENT_VERSION;
20-
}
21-
2216
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package net.hypixel.modapi.packet.impl.serverbound;
22

3-
import net.hypixel.modapi.packet.impl.VersionedPacket;
43
import net.hypixel.modapi.serializer.PacketSerializer;
54

6-
public class ServerboundPartyInfoPacket extends VersionedPacket {
5+
public class ServerboundPartyInfoPacket extends ServerboundVersionedPacket {
76
private static final int CURRENT_VERSION = 1;
87

98
public ServerboundPartyInfoPacket() {
@@ -14,9 +13,4 @@ public ServerboundPartyInfoPacket(PacketSerializer serializer) {
1413
super(serializer);
1514
}
1615

17-
@Override
18-
protected int getLatestVersion() {
19-
return CURRENT_VERSION;
20-
}
21-
2216
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package net.hypixel.modapi.packet.impl.serverbound;
22

3-
import net.hypixel.modapi.packet.impl.VersionedPacket;
43
import net.hypixel.modapi.serializer.PacketSerializer;
54

6-
public class ServerboundPingPacket extends VersionedPacket {
5+
public class ServerboundPingPacket extends ServerboundVersionedPacket {
76
private static final int CURRENT_VERSION = 1;
87

98
public ServerboundPingPacket() {
@@ -14,9 +13,4 @@ public ServerboundPingPacket(PacketSerializer serializer) {
1413
super(serializer);
1514
}
1615

17-
@Override
18-
protected int getLatestVersion() {
19-
return CURRENT_VERSION;
20-
}
21-
2216
}

0 commit comments

Comments
 (0)