3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-12-27 08:30:12 +01:00

remove bytebuffer

Dieser Commit ist enthalten in:
Marco 2020-04-17 16:35:48 +02:00
Ursprung c246a32844
Commit 1beea43a1e

Datei anzeigen

@ -25,8 +25,6 @@
package org.geysermc.connector.network.translators.java; 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.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator; 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.client.ClientPluginMessagePacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerPluginMessagePacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerPluginMessagePacket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@Translator(packet = ServerPluginMessagePacket.class) @Translator(packet = ServerPluginMessagePacket.class)
public class JavaPluginMessageTranslator extends PacketTranslator<ServerPluginMessagePacket> { 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 @Override
public void translate(ServerPluginMessagePacket packet, GeyserSession session) { public void translate(ServerPluginMessagePacket packet, GeyserSession session) {
if (packet.getChannel().equals("minecraft:brand")) { if (packet.getChannel().equals("minecraft:brand")) {
byte[] data;
try {
data = writeString(GeyserConnector.NAME);
} catch (IOException e) {
e.printStackTrace();
return;
}
session.getDownstream().getSession().send( session.getDownstream().getSession().send(
new ClientPluginMessagePacket(packet.getChannel(), data) new ClientPluginMessagePacket(packet.getChannel(), brandData)
); );
} }
} }
private byte[] writeString(String string) throws IOException { private static byte[] writeVarInt(int value) {
byte[] data = string.getBytes(StandardCharsets.UTF_8); byte[] data = new byte[getVarIntLength(value)];
ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_VAR_INT_LENGTH + data.length); int index = 0;
NetOutput output = new ByteBufferNetOutput(byteBuffer); do {
output.writeVarInt(data.length); byte temp = (byte)(value & 0b01111111);
output.writeBytes(data); value >>>= 7;
return byteBuffer.array(); 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;
} }
} }