From c5b4685bdccc52ee9ce13dfefcbfadceb856ecf7 Mon Sep 17 00:00:00 2001 From: asie Date: Sun, 10 Feb 2019 11:47:29 +0100 Subject: [PATCH] implement register/unregister to specification, hide unnecessary/false warning --- .../impl/network/PacketRegistryImpl.java | 35 ++++++++++++++++--- .../fabric/impl/network/PacketTypes.java | 6 ++-- .../MixinClientPlayNetworkHandler.java | 9 +++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/fabricmc/fabric/impl/network/PacketRegistryImpl.java b/src/main/java/net/fabricmc/fabric/impl/network/PacketRegistryImpl.java index 96b7e4ef7..f20caf335 100644 --- a/src/main/java/net/fabricmc/fabric/impl/network/PacketRegistryImpl.java +++ b/src/main/java/net/fabricmc/fabric/impl/network/PacketRegistryImpl.java @@ -16,6 +16,7 @@ package net.fabricmc.fabric.impl.network; +import com.google.common.base.Charsets; import io.netty.buffer.Unpooled; import net.fabricmc.fabric.api.network.PacketConsumer; import net.fabricmc.fabric.api.network.PacketContext; @@ -25,6 +26,7 @@ import net.minecraft.network.Packet; import net.minecraft.util.Identifier; import net.minecraft.util.PacketByteBuf; +import java.nio.charset.StandardCharsets; import java.util.*; public abstract class PacketRegistryImpl implements PacketRegistry { @@ -67,8 +69,14 @@ public abstract class PacketRegistryImpl implements PacketRegistry { protected Packet createRegisterTypePacket(Identifier id, Collection ids) { PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); + boolean first = true; for (Identifier a : ids) { - buf.writeString(a.toString()); + if (!first) { + buf.writeByte(0); + } else { + first = false; + } + buf.writeBytes(a.toString().getBytes(StandardCharsets.US_ASCII)); } return toPacket(id, buf); } @@ -84,9 +92,28 @@ public abstract class PacketRegistryImpl implements PacketRegistry { public boolean accept(Identifier id, PacketContext context, PacketByteBuf buf) { if (id.equals(PacketTypes.REGISTER) || id.equals(PacketTypes.UNREGISTER)) { Collection ids = new HashSet<>(); - while (buf.readerIndex() < buf.writerIndex() /* TODO: check correctness */) { - Identifier newId = new Identifier(buf.readString(32767)); - ids.add(newId); + + { + StringBuilder sb = new StringBuilder(); + char c; + + while (buf.readerIndex() < buf.writerIndex()) { + c = (char) buf.readByte(); + if (c == 0) { + String s = sb.toString(); + if (!s.isEmpty()) { + ids.add(new Identifier(s)); + } + sb = new StringBuilder(); + } else { + sb.append(c); + } + } + + String s = sb.toString(); + if (!s.isEmpty()) { + ids.add(new Identifier(s)); + } } Collection target = getIdCollectionFor(context); diff --git a/src/main/java/net/fabricmc/fabric/impl/network/PacketTypes.java b/src/main/java/net/fabricmc/fabric/impl/network/PacketTypes.java index c9f3fa502..ec384aa92 100644 --- a/src/main/java/net/fabricmc/fabric/impl/network/PacketTypes.java +++ b/src/main/java/net/fabricmc/fabric/impl/network/PacketTypes.java @@ -19,9 +19,9 @@ package net.fabricmc.fabric.impl.network; import net.minecraft.util.Identifier; public final class PacketTypes { - static final Identifier BRAND = new Identifier("minecraft:brand"); - static final Identifier REGISTER = new Identifier("minecraft:register"); - static final Identifier UNREGISTER = new Identifier("minecraft:unregister"); + public static final Identifier BRAND = new Identifier("minecraft:brand"); + public static final Identifier REGISTER = new Identifier("minecraft:register"); + public static final Identifier UNREGISTER = new Identifier("minecraft:unregister"); public static final Identifier OPEN_CONTAINER = new Identifier("fabric", "container/open"); } diff --git a/src/main/java/net/fabricmc/fabric/mixin/networking/MixinClientPlayNetworkHandler.java b/src/main/java/net/fabricmc/fabric/mixin/networking/MixinClientPlayNetworkHandler.java index cff4985d5..f35a04d56 100644 --- a/src/main/java/net/fabricmc/fabric/mixin/networking/MixinClientPlayNetworkHandler.java +++ b/src/main/java/net/fabricmc/fabric/mixin/networking/MixinClientPlayNetworkHandler.java @@ -22,6 +22,7 @@ import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; import net.fabricmc.fabric.api.network.PacketContext; import net.fabricmc.fabric.impl.network.ClientSidePacketRegistryImpl; import net.fabricmc.fabric.impl.network.PacketRegistryImpl; +import net.fabricmc.fabric.impl.network.PacketTypes; import net.minecraft.class_2901; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.Screen; @@ -53,6 +54,14 @@ public abstract class MixinClientPlayNetworkHandler implements PacketContext { sendPacket(PacketRegistryImpl.createInitialRegisterPacket(ClientSidePacketRegistry.INSTANCE)); } + // Optional hook: it only removes a warning message. + @Inject(method = "onCustomPayload", at = @At(value = "CONSTANT", args = "stringValue=Unknown custom packed identifier: {}"), cancellable = true, require = 0) + public void onCustomPayloadNotFound(CustomPayloadClientPacket packet, CallbackInfo info) { + if (packet.getChannel().equals(PacketTypes.REGISTER) || packet.getChannel().equals(PacketTypes.UNREGISTER)) { + info.cancel(); + } + } + @Inject(method = "onCustomPayload", at = @At("HEAD"), cancellable = true) public void onCustomPayload(CustomPayloadClientPacket packet, CallbackInfo info) { if (((ClientSidePacketRegistryImpl) ClientSidePacketRegistry.INSTANCE).accept(packet.getChannel(), this, packet.getData())) {