From c246a32844a78ceaddd1319af36534c066667c1e Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 17 Apr 2020 13:29:35 +0200 Subject: [PATCH] Fix brand --- .../java/JavaPluginMessageTranslator.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaPluginMessageTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaPluginMessageTranslator.java index 60227aa4..e6af7eb3 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaPluginMessageTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaPluginMessageTranslator.java @@ -25,6 +25,8 @@ package org.geysermc.connector.network.translators.java; +import com.github.steveice10.packetlib.io.NetOutput; +import com.github.steveice10.packetlib.io.buffer.ByteBufferNetOutput; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; @@ -33,14 +35,37 @@ import org.geysermc.connector.network.translators.Translator; import com.github.steveice10.mc.protocol.packet.ingame.client.ClientPluginMessagePacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerPluginMessagePacket; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; + @Translator(packet = ServerPluginMessagePacket.class) public class JavaPluginMessageTranslator extends PacketTranslator { + + private static final int MAX_VAR_INT_LENGTH = 5; + @Override public void translate(ServerPluginMessagePacket packet, GeyserSession session) { if (packet.getChannel().equals("minecraft:brand")) { + byte[] data; + try { + data = writeString(GeyserConnector.NAME); + } catch (IOException e) { + e.printStackTrace(); + return; + } session.getDownstream().getSession().send( - new ClientPluginMessagePacket(packet.getChannel(), GeyserConnector.NAME.getBytes()) + new ClientPluginMessagePacket(packet.getChannel(), data) ); } } + + private byte[] writeString(String string) throws IOException { + byte[] data = string.getBytes(StandardCharsets.UTF_8); + ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_VAR_INT_LENGTH + data.length); + NetOutput output = new ByteBufferNetOutput(byteBuffer); + output.writeVarInt(data.length); + output.writeBytes(data); + return byteBuffer.array(); + } }