Skip to content

Commit

Permalink
compat network NBT change introduced in mc1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Mar 2, 2024
1 parent 652c394 commit 3deb6d0
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/main/java/carpet/network/CarpetClientNetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import carpet.settings.CarpetSettings;
import carpet.settings.ParsedRule;
import carpet.settings.SettingsManager;
import carpet.utils.NetworkUtil;
import io.netty.buffer.Unpooled;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.nbt.INBTBase;
Expand Down Expand Up @@ -54,6 +55,8 @@ public static void handleData(PacketBuffer data, EntityPlayerSP player)
{
if (data != null)
{
data = ProtocolFixer.fixCarpetPacket(data);

int id = data.readVarInt();
if (id == CarpetClient.HI)
onHi(data);
Expand Down Expand Up @@ -92,7 +95,7 @@ public static void respondHello()

private static void onSyncData(PacketBuffer data, EntityPlayerSP player)
{
NBTTagCompound compound = data.readCompoundTag();
NBTTagCompound compound = NetworkUtil.readNbt(data);
if (compound == null) return;
for (String key: compound.keySet())
{
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/carpet/network/CarpetServerNetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import carpet.settings.CarpetSettings;
import carpet.settings.ParsedRule;
import carpet.settings.SettingsManager;
import carpet.utils.NetworkUtil;
import io.netty.buffer.Unpooled;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.player.EntityPlayerMP;
Expand Down Expand Up @@ -35,6 +36,8 @@ public static void handleData(PacketBuffer data, EntityPlayerMP player)
{
if (data != null)
{
data = ProtocolFixer.fixCarpetPacket(data);

int id = data.readVarInt();
if (id == CarpetClient.HELLO)
onHello(player, data);
Expand Down Expand Up @@ -124,7 +127,7 @@ public void sendFeedback(ITextComponent message, boolean broadcastToOps)

private static void onClientData(EntityPlayerMP player, PacketBuffer data)
{
NBTTagCompound compound = data.readCompoundTag();
NBTTagCompound compound = NetworkUtil.readNbt(data);
if (compound == null) return;
for (String key: compound.keySet())
{
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/carpet/network/ProtocolFixer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package carpet.network;

import carpet.utils.NetworkUtil;
import com.google.common.collect.Lists;
import io.netty.buffer.Unpooled;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;

import java.util.List;
import java.util.Objects;

public class ProtocolFixer
{
public static PacketBuffer fixCarpetPacket(PacketBuffer buf)
{
buf.markReaderIndex();
try
{
// try the old v1 protocol
int id = buf.readVarInt();
switch (id)
{
case CarpetClient.HI:
case CarpetClient.HELLO:
buf.readString(64);
break;
case CarpetClient.DATA:
NetworkUtil.readNbt(buf);
break;
}
return buf; // ok, return the buf directly
}
catch (Exception ignored)
{
}
finally
{
buf.resetReaderIndex();
}

// try protocol v2 from fabric-carpet >= 1.4.114
NBTTagCompound nbt = Objects.requireNonNull(NetworkUtil.readNbt(buf));
PacketBuffer newBuf = new PacketBuffer(Unpooled.buffer());

List<String> keys = Lists.newArrayList(nbt.keySet());
String id = keys.get(0);
if (id.equals("69") || id.equals("420"))
{
// v1 carpet hi / hello, format: varint (69 or 420) + string
newBuf.writeVarInt(Integer.parseInt(id));
newBuf.writeString(nbt.getString(id));
}
else
{
// v1 carpet data, format: varint + nbt
newBuf.writeVarInt(CarpetClient.DATA);
newBuf.writeCompoundTag(nbt);
}
return newBuf;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import carpet.CarpetServer;
import carpet.helpers.ServerMsptMetricsDataSyncer;
import carpet.utils.NbtUtil;
import carpet.utils.NetworkUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -44,7 +45,7 @@ public static TISCMClientPacketHandler getInstance()
public void dispatch(NetHandlerPlayClient networkHandler, PacketBuffer packetByteBuf)
{
String packetId = packetByteBuf.readString(Short.MAX_VALUE);
NBTTagCompound payload = packetByteBuf.readCompoundTag();
NBTTagCompound payload = NetworkUtil.readNbt(packetByteBuf);
TISCMProtocol.S2C.fromId(packetId).
map(this.handlers::get).
ifPresent( handler -> handler.accept(new HandlerContext.S2C(networkHandler, payload)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import carpet.CarpetServer;
import carpet.utils.NbtUtil;
import carpet.utils.NetworkUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -42,7 +43,7 @@ public static TISCMServerPacketHandler getInstance()
public void dispatch(NetHandlerPlayServer networkHandler, PacketBuffer packetByteBuf)
{
String packetId = packetByteBuf.readString(Short.MAX_VALUE);
NBTTagCompound payload = packetByteBuf.readCompoundTag();
NBTTagCompound payload = NetworkUtil.readNbt(packetByteBuf);
HandlerContext.C2S ctx = new HandlerContext.C2S(networkHandler, payload);
ctx.runSynced(() -> TISCMProtocol.C2S.fromId(packetId).
map(this.handlers::get).
Expand Down
135 changes: 135 additions & 0 deletions src/main/java/carpet/utils/NetworkUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* This file is part of the Carpet TIS Addition project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 Fallen_Breath and contributors
*
* Carpet TIS Addition 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.
*
* Carpet TIS Addition 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 Carpet TIS Addition. If not, see <https://www.gnu.org/licenses/>.
*/

package carpet.utils;

import io.netty.buffer.Unpooled;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;

import javax.annotation.Nullable;

public class NetworkUtil
{
/**
* See <a href="https://wiki.vg/NBT">https://wiki.vg/NBT</a>
* for the nbt changes between mc < 1.20.2 and mc >= 1.20.2
*/
public enum NbtStyle
{
UNKNOWN,
OLD, // < 1.20.2
NEW; // >= 1.20.2

public static final NbtStyle CURRENT = OLD;
}

private static final int TAG_ID_COMPOUND = 0x0A;

// Notes: reader index untouched
public static NbtStyle guessNbtStyle(PacketBuffer buf)
{
int n = buf.readableBytes();

buf.markReaderIndex();
try
{
if (n < 2)
{
return NbtStyle.UNKNOWN;
}

byte typeId = buf.readByte();
if (typeId != TAG_ID_COMPOUND)
{
return NbtStyle.UNKNOWN;
}

if (n == 2)
{
if (buf.readByte() == 0)
{
// >=1.20.2, empty nbt
return NbtStyle.NEW;
}
return NbtStyle.UNKNOWN;
}
else // n > 2
{
byte[] bytes = new byte[2];
buf.readBytes(bytes);

// Double 0x00 for the empty root tag name
if (bytes[0] == 0 && bytes[1] == 0)
{
return NbtStyle.OLD;
}
// A valid nbt type id
else if (0 <= bytes[0] && bytes[0] < 13)
{
return NbtStyle.NEW;
}
}
}
finally
{
buf.resetReaderIndex();
}

return NbtStyle.UNKNOWN;
}

/**
* Read an NBT from a {@link PacketBuffer}
*
* Compatible with both mc >= 1.20.2 and mc < 1.20.2 formats
*/
@SuppressWarnings("StatementWithEmptyBody")
@Nullable
public static NBTTagCompound readNbt(PacketBuffer buf)
{
NbtStyle nbtStyle = guessNbtStyle(buf);

if (nbtStyle == NbtStyle.NEW)
{
// I'm < mc1.20.2 (OLD), trying to read a nbt in NEW style

buf.markReaderIndex();
PacketBuffer tweakedBuf = new PacketBuffer(Unpooled.buffer());
tweakedBuf.writeByte(buf.readByte()); // 0x0A, tag type
tweakedBuf.writeByte(0).writeByte(0); // 2* 0x00
tweakedBuf.writeBytes(buf);
buf.resetReaderIndex();

NBTTagCompound nbt = tweakedBuf.readCompoundTag();

int n = tweakedBuf.readerIndex();
buf.readBytes(Math.max(0, n - 2));

return nbt;
}
else if (nbtStyle == NbtStyle.OLD)
{
// do nothing
}

return buf.readCompoundTag();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package me.fallenbreath.lmspaster.network;

import carpet.utils.NetworkUtil;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
Expand All @@ -39,7 +40,7 @@ public LmsPasterPayload(int id, NBTTagCompound nbt)

public LmsPasterPayload(PacketBuffer buf)
{
this(buf.readVarInt(), buf.readCompoundTag());
this(buf.readVarInt(), NetworkUtil.readNbt(buf));
}

public int getPacketId()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package redstone.multimeter.common.network;

import carpet.utils.NetworkUtil;
import io.netty.buffer.Unpooled;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.Packet;
Expand Down Expand Up @@ -38,7 +39,7 @@ protected <P extends RSMMPacket> P decode(PacketBuffer buffer) {
throw new IllegalStateException("Unable to decode packet: " + id);
}

NBTTagCompound data = buffer.readCompoundTag();
NBTTagCompound data = NetworkUtil.readNbt(buffer);
packet.decode(data);

return packet;
Expand Down

0 comments on commit 3deb6d0

Please sign in to comment.