11package com .fox2code .foxloader .loader .packet ;
22
3+ import com .fox2code .foxloader .loader .ModLoader ;
34import com .fox2code .foxloader .registry .EntityTypeRegistryEntry ;
45import com .fox2code .foxloader .registry .RegistryEntry ;
56
89import java .io .IOException ;
910import java .util .HashMap ;
1011import java .util .Map ;
12+ import java .util .logging .Level ;
1113
1214public final class ServerHello extends FoxPacket {
13- public static final int SERVER_HELLO_VERSION = 1 ;
14- private static final int SERVER_HELLO_VERSION_NEXT = 1 ;
15+ /**
16+ * <p>The current version of the Server Hello packet.</p>
17+ * <b>V0</b> - <i>Block and Item registry entries and metadata</i>.<br/>
18+ * <b>V1</b> - <i>Entity type registry metadata appended to the end</i>.
19+ */
20+ public static final short SERVER_HELLO_VERSION = 1 ;
21+ /**
22+ * <p>The minimum version of ServerHello on older clients for the server to be compatible with.</p>
23+ * V0 clients will always expect exactly {@literal 0}.
24+ */
25+ private static final short CLIENT_BACKWARD_COMPATIBILITY_VERSION = 0 ;
1526
1627 public HashMap <String , RegistryEntry > registryEntries ;
1728 public HashMap <String , String > metadata ;
@@ -30,13 +41,15 @@ public ServerHello(HashMap<String, RegistryEntry> registryEntries,
3041 this .entityTypeRegistryEntries = entityTypeRegistryEntries ;
3142 }
3243
44+ /**
45+ * @throws IOException if the data cannot be read as instructed.
46+ */
3347 @ Override
3448 public void readData (DataInputStream inStream ) throws IOException {
3549 int serverHelloVersion = inStream .readUnsignedShort ();
36- if (serverHelloVersion >= SERVER_HELLO_VERSION_NEXT &&
37- // Next field is how much backward compatible is the packet
38- inStream .readUnsignedShort () > SERVER_HELLO_VERSION_NEXT ) {
39- throw new RuntimeException ("Client is critically out of date, please update FoxLoader" );
50+ int clientBackwardCompatibilityVersion = inStream .readUnsignedShort ();
51+ if (SERVER_HELLO_VERSION < clientBackwardCompatibilityVersion ) {
52+ throw new IOException ("Client is critically out of date, please update FoxLoader." );
4053 }
4154
4255 int entries = inStream .readUnsignedShort ();
@@ -51,6 +64,7 @@ public void readData(DataInputStream inStream) throws IOException {
5164 if (inStream .available () < 2 ) {
5265 // Too few bytes to read the next short.
5366 this .metadata = new HashMap <>();
67+ ModLoader .getModLoaderLogger ().log (Level .WARNING , "Server Hello: Too few bytes to read metadata." );
5468 } else {
5569 entries = inStream .readUnsignedShort ();
5670 metadata = new HashMap <>();
@@ -60,8 +74,8 @@ public void readData(DataInputStream inStream) throws IOException {
6074 }
6175
6276 if (inStream .available () < 4 ) {
63- // Too few bytes to read the next integer.
6477 this .entityTypeRegistryEntries = new HashMap <>();
78+ ModLoader .getModLoaderLogger ().log (Level .WARNING , "Server Hello: Too few bytes to read entity data." );
6579 } else {
6680 int entityEntries = inStream .readInt ();
6781 entityTypeRegistryEntries = new HashMap <>(entityEntries );
@@ -76,6 +90,7 @@ public void readData(DataInputStream inStream) throws IOException {
7690 @ Override
7791 public void writeData (DataOutputStream outStream ) throws IOException {
7892 outStream .writeShort (SERVER_HELLO_VERSION );
93+ outStream .writeShort (CLIENT_BACKWARD_COMPATIBILITY_VERSION );
7994
8095 outStream .writeShort (this .registryEntries .size ());
8196 for (RegistryEntry registryEntry : registryEntries .values ()) {
0 commit comments