|
1 | 1 | package nl.motionlesstrain.createcolonies.network.messages; |
2 | 2 |
|
3 | 3 | import com.mojang.logging.LogUtils; |
| 4 | +import io.netty.buffer.ByteBuf; |
4 | 5 | import io.netty.buffer.ByteBufInputStream; |
5 | 6 | import io.netty.buffer.ByteBufOutputStream; |
6 | 7 | import net.minecraft.client.Minecraft; |
7 | 8 | import net.minecraft.nbt.CompoundTag; |
| 9 | +import net.minecraft.nbt.NbtAccounter; |
8 | 10 | import net.minecraft.nbt.NbtIo; |
9 | 11 | import net.minecraft.network.FriendlyByteBuf; |
| 12 | +import net.minecraft.network.RegistryFriendlyByteBuf; |
10 | 13 | import net.minecraft.network.chat.Component; |
| 14 | +import net.minecraft.network.codec.ByteBufCodecs; |
| 15 | +import net.minecraft.network.codec.StreamCodec; |
| 16 | +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; |
| 17 | +import net.minecraft.resources.ResourceLocation; |
11 | 18 | import net.minecraft.world.entity.player.Player; |
| 19 | +import net.neoforged.neoforge.common.util.FriendlyByteBufUtil; |
| 20 | +import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs; |
12 | 21 | import net.neoforged.neoforge.network.NetworkEvent; |
| 22 | +import net.neoforged.neoforge.network.handling.IPayloadContext; |
| 23 | +import net.neoforged.neoforge.network.handling.IPayloadHandler; |
| 24 | +import nl.motionlesstrain.createcolonies.network.NetworkMessage; |
| 25 | +import org.jetbrains.annotations.NotNull; |
13 | 26 | import org.slf4j.Logger; |
14 | 27 |
|
15 | 28 | import javax.annotation.Nullable; |
16 | 29 | import java.io.IOException; |
17 | 30 | import java.nio.file.Path; |
18 | 31 |
|
19 | | -public class SaveNBTFileMessage extends ClientBoundNetworkMessage { |
20 | | - private static final Logger LOGGER = LogUtils.getLogger(); |
21 | | - private final String filePath; |
22 | | - private final CompoundTag fileContents; |
23 | | - |
24 | | - public SaveNBTFileMessage(String filePath, CompoundTag contents) { |
25 | | - this.filePath = filePath; |
26 | | - fileContents = contents; |
27 | | - } |
| 32 | +import static nl.motionlesstrain.createcolonies.CreateColonies.MODID; |
28 | 33 |
|
29 | | - public SaveNBTFileMessage(FriendlyByteBuf buffer) { |
30 | | - filePath = buffer.readUtf(); |
31 | | - CompoundTag contents = null; |
32 | | - try (ByteBufInputStream input = new ByteBufInputStream(buffer)){ |
33 | | - contents = NbtIo.readCompressed(input); |
34 | | - } catch (IOException e) { |
35 | | - LOGGER.error("Could not read file contents", e); |
36 | | - } |
37 | | - fileContents = contents; |
38 | | - } |
| 34 | +public record SaveNBTFileMessage(String filePath, CompoundTag fileContents) implements NetworkMessage { |
| 35 | + private static final CustomPacketPayload.Type<SaveNBTFileMessage> TYPE = new CustomPacketPayload.Type<>( |
| 36 | + ResourceLocation.fromNamespaceAndPath(MODID, "save_nbt_file") |
| 37 | + ); |
39 | 38 |
|
40 | 39 | @Override |
41 | | - public void encode(FriendlyByteBuf buffer) { |
42 | | - buffer.writeUtf(filePath); |
43 | | - try (ByteBufOutputStream output = new ByteBufOutputStream(buffer)) { |
44 | | - NbtIo.writeCompressed(fileContents, output); |
45 | | - } catch (IOException e) { |
46 | | - LOGGER.error("Could not write file contents", e); |
47 | | - } |
| 40 | + public @NotNull Type<? extends CustomPacketPayload> type() { |
| 41 | + return TYPE; |
48 | 42 | } |
49 | 43 |
|
50 | | - @Override |
51 | | - protected ClientsideHandler createHandler() { |
52 | | - return new ClientNBTSaver(); |
53 | | - } |
| 44 | + public static final StreamCodec<ByteBuf, SaveNBTFileMessage> STREAM_CODEC = StreamCodec.composite( |
| 45 | + ByteBufCodecs.STRING_UTF8, |
| 46 | + SaveNBTFileMessage::filePath, |
| 47 | + ByteBufCodecs.compoundTagCodec(NbtAccounter::unlimitedHeap), |
| 48 | + SaveNBTFileMessage::fileContents, |
| 49 | + SaveNBTFileMessage::new |
| 50 | + ); |
54 | 51 |
|
55 | | - private class ClientNBTSaver implements ClientsideHandler { |
| 52 | + private static final Logger LOGGER = LogUtils.getLogger(); |
56 | 53 |
|
| 54 | + private static class ClientNBTSaver implements IPayloadHandler<SaveNBTFileMessage> { |
57 | 55 | @Override |
58 | | - public void handlePacket(NetworkEvent.Context context) { |
| 56 | + public void handle(SaveNBTFileMessage message, @NotNull IPayloadContext context) { |
59 | 57 | final Path gamePath = Minecraft.getInstance().gameDirectory.toPath(); |
60 | | - final Path saveFile = gamePath.resolve(filePath); |
| 58 | + final Path saveFile = gamePath.resolve(message.filePath); |
61 | 59 | try { |
62 | | - NbtIo.writeCompressed(fileContents, saveFile.toFile()); |
| 60 | + NbtIo.writeCompressed(message.fileContents, saveFile.toFile()); |
63 | 61 | final @Nullable Player player = Minecraft.getInstance().player; |
64 | 62 | if (player != null) { |
65 | 63 | player.displayClientMessage(Component.translatable("nl.motionlesstrain.createcolonies.convert.confirm"), false); |
66 | 64 | } |
67 | 65 | } catch (IOException e) { |
68 | | - LOGGER.error("Could not save file {}", filePath, e); |
| 66 | + LOGGER.error("Could not save file {}", message.filePath, e); |
69 | 67 | } |
70 | 68 | } |
71 | 69 | } |
| 70 | + |
| 71 | + public static class Factory implements NetworkMessage.Factory<SaveNBTFileMessage> { |
| 72 | + |
| 73 | + @Override |
| 74 | + public Type<SaveNBTFileMessage> type() { |
| 75 | + return TYPE; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public StreamCodec<? super RegistryFriendlyByteBuf, SaveNBTFileMessage> streamCodec() { |
| 80 | + return STREAM_CODEC; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public IPayloadHandler<SaveNBTFileMessage> handler() { |
| 85 | + return new ClientNBTSaver(); |
| 86 | + } |
| 87 | + } |
72 | 88 | } |
0 commit comments