2525
2626import static nl .motionlesstrain .createcolonies .CreateColonies .MODID ;
2727
28- public record SaveNBTFileMessage ( String filePath , CompoundTag fileContents ) implements NetworkMessage {
28+ public class SaveNBTFileMessage extends ClientBoundNetworkMessage {
2929 private static final CustomPacketPayload .Type <SaveNBTFileMessage > TYPE = new CustomPacketPayload .Type <>(
3030 ResourceLocation .fromNamespaceAndPath (MODID , "save_nbt_file" )
3131 );
3232
33+ private final String filePath ;
34+ private final CompoundTag fileContents ;
35+
36+ public SaveNBTFileMessage (final String filePath , final CompoundTag fileContents ) {
37+ this .filePath = filePath ;
38+ this .fileContents = fileContents ;
39+ }
40+
41+ public String filePath () { return filePath ; }
42+ public CompoundTag fileContents () { return fileContents ; }
43+
3344 @ Override
3445 public @ NotNull Type <? extends CustomPacketPayload > type () {
3546 return TYPE ;
@@ -44,24 +55,29 @@ public record SaveNBTFileMessage(String filePath, CompoundTag fileContents) impl
4455
4556 private static final Logger LOGGER = LogUtils .getLogger ();
4657
47- private static class ClientNBTSaver implements IPayloadHandler <SaveNBTFileMessage > {
58+ @ Override
59+ ClientSideHandler getHandler () {
60+ return new ClientNBTSaver ();
61+ }
62+
63+ private class ClientNBTSaver implements ClientBoundNetworkMessage .ClientSideHandler {
4864 @ Override
49- public void handle ( SaveNBTFileMessage message , @ NotNull IPayloadContext context ) {
65+ public void handleMessage ( @ NotNull IPayloadContext context ) {
5066 final Path gamePath = Minecraft .getInstance ().gameDirectory .toPath ();
51- final Path saveFile = gamePath .resolve (message . filePath );
67+ final Path saveFile = gamePath .resolve (filePath );
5268 try {
53- NbtIo .writeCompressed (message . fileContents , saveFile );
69+ NbtIo .writeCompressed (fileContents , saveFile );
5470 final @ Nullable Player player = Minecraft .getInstance ().player ;
5571 if (player != null ) {
5672 player .displayClientMessage (Component .translatable ("nl.motionlesstrain.createcolonies.convert.confirm" ), false );
5773 }
5874 } catch (IOException e ) {
59- LOGGER .error ("Could not save file {}" , message . filePath , e );
75+ LOGGER .error ("Could not save file {}" , filePath , e );
6076 }
6177 }
6278 }
6379
64- public static class Factory implements NetworkMessage .Factory <SaveNBTFileMessage > {
80+ public static class Factory extends ClientBoundNetworkMessage .Factory <SaveNBTFileMessage > {
6581
6682 @ Override
6783 public Type <SaveNBTFileMessage > type () {
@@ -72,10 +88,5 @@ public Type<SaveNBTFileMessage> type() {
7288 public StreamCodec <? super RegistryFriendlyByteBuf , SaveNBTFileMessage > streamCodec () {
7389 return STREAM_CODEC ;
7490 }
75-
76- @ Override
77- public IPayloadHandler <SaveNBTFileMessage > handler () {
78- return new ClientNBTSaver ();
79- }
8091 }
8192}
0 commit comments