-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And moves / adds typical serialisation static functions to the Packet interface.
- Loading branch information
1 parent
13afe57
commit b460252
Showing
11 changed files
with
99 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 67 additions & 9 deletions
76
mod/common/src/main/java/gjum/minecraft/mapsync/common/net/Packet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,77 @@ | ||
package gjum.minecraft.mapsync.common.net; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import java.nio.charset.StandardCharsets; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public abstract class Packet { | ||
public abstract void write(ByteBuf buf); | ||
public interface Packet { | ||
default void write(@NotNull ByteBuf out) { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected static byte[] readByteArray(ByteBuf in) { | ||
int length = in.readInt(); | ||
byte[] array = new byte[length]; | ||
in.readBytes(array); | ||
static byte @NotNull [] readIntLengthByteArray( | ||
final @NotNull ByteBuf in | ||
) { | ||
final var array = new byte[in.readInt()]; | ||
if (array.length > 0) { | ||
in.readBytes(array); | ||
} | ||
return array; | ||
} | ||
|
||
protected static void writeByteArray(ByteBuf out, byte[] array) { | ||
out.writeInt(array.length); | ||
out.writeBytes(array); | ||
static void writeIntLengthByteArray( | ||
final @NotNull ByteBuf out, | ||
final byte @NotNull [] array | ||
) { | ||
if (array.length > 0) { | ||
out.writeInt(array.length); | ||
out.writeBytes(array); | ||
} | ||
else { | ||
out.writeInt(0); | ||
} | ||
} | ||
|
||
static @NotNull String readUtf8String( | ||
final @NotNull ByteBuf in | ||
) { | ||
return new String( | ||
readIntLengthByteArray(in), | ||
StandardCharsets.UTF_8 | ||
); | ||
} | ||
|
||
static void writeUtf8String( | ||
final @NotNull ByteBuf out, | ||
final @NotNull String string | ||
) { | ||
writeIntLengthByteArray( | ||
out, | ||
string.getBytes(StandardCharsets.UTF_8) | ||
); | ||
} | ||
|
||
static <T, R extends ResourceKey<Registry<T>>> @NotNull ResourceKey<T> readResourceKey( | ||
final @NotNull ByteBuf in, | ||
final @NotNull R registry | ||
) { | ||
return ResourceKey.create( | ||
registry, | ||
new ResourceLocation(readUtf8String(in)) | ||
); | ||
} | ||
|
||
static void writeResourceKey( | ||
final @NotNull ByteBuf out, | ||
final @NotNull ResourceKey<?> resourceKey | ||
) { | ||
writeUtf8String( | ||
out, | ||
resourceKey.location().toString() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.