remove bytebuffer

This commit is contained in:
Marco 2020-04-17 16:35:48 +02:00
parent c246a32844
commit 1beea43a1e

View file

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