From c246a32844a78ceaddd1319af36534c066667c1e Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 17 Apr 2020 13:29:35 +0200 Subject: [PATCH 1/2] 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 60227aa4a..e6af7eb33 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(); + } } From 1beea43a1ec69ec13443dae487c2c6916720294c Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 17 Apr 2020 16:35:48 +0200 Subject: [PATCH 2/2] remove bytebuffer --- .../java/JavaPluginMessageTranslator.java | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) 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 e6af7eb33..cd0161dbc 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,8 +25,6 @@ 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; @@ -35,37 +33,56 @@ 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; + + private static byte[] brandData; + + static { + byte[] data = GeyserConnector.NAME.getBytes(StandardCharsets.UTF_8); + byte[] varInt = writeVarInt(data.length); + brandData = new byte[varInt.length + data.length]; + System.arraycopy(varInt, 0, brandData, 0, varInt.length); + System.arraycopy(data, 0, brandData, varInt.length, data.length); + } + @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(), data) + new ClientPluginMessagePacket(packet.getChannel(), brandData) ); } } - 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(); + private static byte[] writeVarInt(int value) { + byte[] data = new byte[getVarIntLength(value)]; + int index = 0; + do { + byte temp = (byte)(value & 0b01111111); + value >>>= 7; + if (value != 0) { + temp |= 0b10000000; + } + data[index] = temp; + index++; + } while (value != 0); + return data; + } + + private static int getVarIntLength(int number) { + if ((number & 0xFFFFFF80) == 0) { + return 1; + } else if ((number & 0xFFFFC000) == 0) { + return 2; + } else if ((number & 0xFFE00000) == 0) { + return 3; + } else if ((number & 0xF0000000) == 0) { + return 4; + } + return 5; } }