Fix brand

This commit is contained in:
Marco 2020-04-17 13:29:35 +02:00
parent e96863e941
commit c246a32844
1 changed files with 26 additions and 1 deletions

View File

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