implement register/unregister to specification, hide unnecessary/false warning

This commit is contained in:
asie 2019-02-10 11:47:29 +01:00
parent 33c36f8f29
commit c5b4685bdc
3 changed files with 43 additions and 7 deletions

View file

@ -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<Identifier> 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<Identifier> 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<Identifier> target = getIdCollectionFor(context);

View file

@ -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");
}

View file

@ -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())) {