From 652c3944ac0bcaead185dae946a5a12facbb7e03 Mon Sep 17 00:00:00 2001 From: Fallen_Breath Date: Mon, 6 Nov 2023 02:36:21 +0800 Subject: [PATCH] Update mod litematica-server-paster to v1.2.0 https://github.com/Fallen-Breath/litematica-server-paster/releases/tag/v1.2.0 --- .../LitematicaServerPasterAccess.java | 4 +- .../lmspaster/LitematicaServerPasterMod.java | 22 +++++++- .../lmspaster/network/LmsPasterPayload.java | 54 +++++++++++++++++++ .../lmspaster/network/Network.java | 38 +++++++++---- .../network/ServerNetworkHandler.java | 51 ++++++++++++------ .../lmspaster/util/RegistryUtil.java | 20 +++++++ 6 files changed, 160 insertions(+), 29 deletions(-) create mode 100644 src/main/java/me/fallenbreath/lmspaster/network/LmsPasterPayload.java diff --git a/src/main/java/me/fallenbreath/lmspaster/LitematicaServerPasterAccess.java b/src/main/java/me/fallenbreath/lmspaster/LitematicaServerPasterAccess.java index 42e506d5..3439b9ed 100644 --- a/src/main/java/me/fallenbreath/lmspaster/LitematicaServerPasterAccess.java +++ b/src/main/java/me/fallenbreath/lmspaster/LitematicaServerPasterAccess.java @@ -1,6 +1,7 @@ package me.fallenbreath.lmspaster; import carpet.settings.CarpetSettings; +import me.fallenbreath.lmspaster.network.LmsPasterPayload; import me.fallenbreath.lmspaster.network.Network; import me.fallenbreath.lmspaster.network.ServerNetworkHandler; import net.minecraft.entity.player.EntityPlayerMP; @@ -22,7 +23,8 @@ public static void onPacket(CPacketCustomPayload packetIn, EntityPlayerMP player ResourceLocation channel = packetIn.getChannel(); if (Network.CHANNEL.equals(channel)) { - ServerNetworkHandler.handleClientPacket(packetIn.getData(), player); + LmsPasterPayload payload = new LmsPasterPayload(packetIn.getData()); + ServerNetworkHandler.handleClientPacket(payload, player); } } } diff --git a/src/main/java/me/fallenbreath/lmspaster/LitematicaServerPasterMod.java b/src/main/java/me/fallenbreath/lmspaster/LitematicaServerPasterMod.java index 6015a627..5148f5cd 100644 --- a/src/main/java/me/fallenbreath/lmspaster/LitematicaServerPasterMod.java +++ b/src/main/java/me/fallenbreath/lmspaster/LitematicaServerPasterMod.java @@ -1,3 +1,23 @@ +/* + * This file is part of the Litematica Server Paster project, licensed under the + * GNU Lesser General Public License v3.0 + * + * Copyright (C) 2023 Fallen_Breath and contributors + * + * Litematica Server Paster is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Litematica Server Paster is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Litematica Server Paster. If not, see . + */ + package me.fallenbreath.lmspaster; import org.apache.logging.log4j.LogManager; @@ -9,5 +29,5 @@ public class LitematicaServerPasterMod public static final String MOD_NAME = "Litematica Server Paster"; public static final String MOD_ID = "litematica-server-paster"; - public static final String VERSION = "1.1.0"; + public static final String VERSION = "1.2.0"; } diff --git a/src/main/java/me/fallenbreath/lmspaster/network/LmsPasterPayload.java b/src/main/java/me/fallenbreath/lmspaster/network/LmsPasterPayload.java new file mode 100644 index 00000000..1eaadd8b --- /dev/null +++ b/src/main/java/me/fallenbreath/lmspaster/network/LmsPasterPayload.java @@ -0,0 +1,54 @@ +/* + * This file is part of the Litematica Server Paster project, licensed under the + * GNU Lesser General Public License v3.0 + * + * Copyright (C) 2023 Fallen_Breath and contributors + * + * Litematica Server Paster is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Litematica Server Paster is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Litematica Server Paster. If not, see . + */ + +package me.fallenbreath.lmspaster.network; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.PacketBuffer; +import net.minecraft.util.ResourceLocation; + +public class LmsPasterPayload +{ + public static final ResourceLocation ID = Network.CHANNEL; + + private final int id; + private final NBTTagCompound nbt; + + public LmsPasterPayload(int id, NBTTagCompound nbt) + { + this.id = id; + this.nbt = nbt; + } + + public LmsPasterPayload(PacketBuffer buf) + { + this(buf.readVarInt(), buf.readCompoundTag()); + } + + public int getPacketId() + { + return this.id; + } + + public NBTTagCompound getNbt() + { + return this.nbt; + } +} diff --git a/src/main/java/me/fallenbreath/lmspaster/network/Network.java b/src/main/java/me/fallenbreath/lmspaster/network/Network.java index 4e2158b5..46f37d8c 100644 --- a/src/main/java/me/fallenbreath/lmspaster/network/Network.java +++ b/src/main/java/me/fallenbreath/lmspaster/network/Network.java @@ -1,11 +1,31 @@ +/* + * This file is part of the Litematica Server Paster project, licensed under the + * GNU Lesser General Public License v3.0 + * + * Copyright (C) 2023 Fallen_Breath and contributors + * + * Litematica Server Paster is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Litematica Server Paster is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Litematica Server Paster. If not, see . + */ + package me.fallenbreath.lmspaster.network; import com.google.common.collect.Sets; import io.netty.buffer.Unpooled; import me.fallenbreath.lmspaster.LitematicaServerPasterMod; import me.fallenbreath.lmspaster.util.RegistryUtil; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.PacketBuffer; -import net.minecraft.network.play.client.CPacketCustomPayload; import net.minecraft.network.play.server.SPacketCustomPayload; import net.minecraft.util.ResourceLocation; @@ -16,7 +36,7 @@ public class Network { - public static final ResourceLocation CHANNEL = RegistryUtil.id("network"); + public static final ResourceLocation CHANNEL = RegistryUtil.id("network_v2"); public static class C2S { @@ -57,13 +77,6 @@ public static class C2S ALL_PACKET_IDS[i++] = id; } } - - public static CPacketCustomPayload packet(Consumer byteBufBuilder) - { - PacketBuffer packetByteBuf = new PacketBuffer(Unpooled.buffer()); - byteBufBuilder.accept(packetByteBuf); - return new CPacketCustomPayload(CHANNEL, packetByteBuf); - } } public static class S2C @@ -71,10 +84,13 @@ public static class S2C public static final int HI = 0; public static final int ACCEPT_PACKETS = 1; - public static SPacketCustomPayload packet(Consumer byteBufBuilder) + public static SPacketCustomPayload packet(int packetId, Consumer payloadBuilder) { + NBTTagCompound nbt = new NBTTagCompound(); + payloadBuilder.accept(nbt); PacketBuffer packetByteBuf = new PacketBuffer(Unpooled.buffer()); - byteBufBuilder.accept(packetByteBuf); + packetByteBuf.writeVarInt(packetId); + packetByteBuf.writeCompoundTag(nbt); return new SPacketCustomPayload(CHANNEL, packetByteBuf); } } diff --git a/src/main/java/me/fallenbreath/lmspaster/network/ServerNetworkHandler.java b/src/main/java/me/fallenbreath/lmspaster/network/ServerNetworkHandler.java index 217125fb..bb1fb61b 100644 --- a/src/main/java/me/fallenbreath/lmspaster/network/ServerNetworkHandler.java +++ b/src/main/java/me/fallenbreath/lmspaster/network/ServerNetworkHandler.java @@ -1,9 +1,29 @@ +/* + * This file is part of the Litematica Server Paster project, licensed under the + * GNU Lesser General Public License v3.0 + * + * Copyright (C) 2023 Fallen_Breath and contributors + * + * Litematica Server Paster is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Litematica Server Paster is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Litematica Server Paster. If not, see . + */ + package me.fallenbreath.lmspaster.network; import me.fallenbreath.lmspaster.LitematicaServerPasterMod; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetHandlerPlayServer; -import net.minecraft.network.PacketBuffer; import java.util.Map; import java.util.Objects; @@ -19,28 +39,27 @@ private static Optional getVeryLongChatBuilder(EntityPlayerMP pla return Optional.ofNullable(VERY_LONG_CHATS.get(player.connection)); } - public static void handleClientPacket(PacketBuffer data, EntityPlayerMP player) + public static void handleClientPacket(LmsPasterPayload payload, EntityPlayerMP player) { String playerName = player.getName().getString(); - int id = data.readVarInt(); + int id = payload.getPacketId(); + NBTTagCompound nbt = payload.getNbt(); switch (id) { case Network.C2S.HI: - String clientModVersion = data.readString(Short.MAX_VALUE); + String clientModVersion = nbt.getString("mod_version"); LitematicaServerPasterMod.LOGGER.info("Player {} connected with {} @ {}", playerName, LitematicaServerPasterMod.MOD_NAME, clientModVersion); - player.connection.sendPacket(Network.S2C.packet(buf -> buf. - writeVarInt(Network.S2C.HI). - writeString(LitematicaServerPasterMod.VERSION) - )); - player.connection.sendPacket(Network.S2C.packet(buf -> buf. - writeVarInt(Network.S2C.ACCEPT_PACKETS). - writeVarIntArray(Network.C2S.ALL_PACKET_IDS)) - ); + player.connection.sendPacket(Network.S2C.packet(Network.S2C.HI, nbt2 -> { + nbt2.putString("mod_version", LitematicaServerPasterMod.VERSION); + })); + player.connection.sendPacket(Network.S2C.packet(Network.S2C.ACCEPT_PACKETS, nbt2 -> { + nbt2.putIntArray("ids", Network.C2S.ALL_PACKET_IDS); + })); break; case Network.C2S.CHAT: LitematicaServerPasterMod.LOGGER.debug("Received chat from player {}", playerName); - String message = data.readString(Short.MAX_VALUE); + String message = nbt.getString("chat"); triggerCommand(player, playerName, message); break; @@ -50,9 +69,9 @@ public static void handleClientPacket(PacketBuffer data, EntityPlayerMP player) break; case Network.C2S.VERY_LONG_CHAT_CONTENT: - String content = data.readString(Short.MAX_VALUE); - LitematicaServerPasterMod.LOGGER.debug("Received VERY_LONG_CHAT_CONTENT from player {} with length {}", playerName, content.length()); - getVeryLongChatBuilder(player).ifPresent(builder -> builder.append(content)); + String segment = nbt.getString("segment"); + LitematicaServerPasterMod.LOGGER.debug("Received VERY_LONG_CHAT_CONTENT from player {} with length {}", playerName, segment.length()); + getVeryLongChatBuilder(player).ifPresent(builder -> builder.append(segment)); break; case Network.C2S.VERY_LONG_CHAT_END: diff --git a/src/main/java/me/fallenbreath/lmspaster/util/RegistryUtil.java b/src/main/java/me/fallenbreath/lmspaster/util/RegistryUtil.java index 09584850..ae6fbeb6 100644 --- a/src/main/java/me/fallenbreath/lmspaster/util/RegistryUtil.java +++ b/src/main/java/me/fallenbreath/lmspaster/util/RegistryUtil.java @@ -1,3 +1,23 @@ +/* + * This file is part of the Litematica Server Paster project, licensed under the + * GNU Lesser General Public License v3.0 + * + * Copyright (C) 2023 Fallen_Breath and contributors + * + * Litematica Server Paster is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Litematica Server Paster is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Litematica Server Paster. If not, see . + */ + package me.fallenbreath.lmspaster.util; import me.fallenbreath.lmspaster.LitematicaServerPasterMod;