Senden Sie "Registrierungsdaten" -Paket für Minecraft ServerJava

Java-Forum
Anonymous
 Senden Sie "Registrierungsdaten" -Paket für Minecraft Server

Post by Anonymous »

Ich entwickle meine eigene Version eines Minecraft -Servers mithilfe der Netty -Bibliothek.
Nach dem Implementieren aller Handshake -Materie stecke ich beim Senden des Registrierungsdatenpakets fest. Paketstruktur von 'Registry Data'. Der Code, den ich verwende, um diese Registrierung an den Client zu senden: < /p>

Code: Select all

private void sendBannerPatternRegistry(Channel client) {
ByteBuf bannerPatternRegistry = Unpooled.buffer();

this.writeVarInt(bannerPatternRegistry, 0x07); // the id of the 'Registry Data' packet

this.writeString(bannerPatternRegistry, "minecraft:banner_pattern"); // the 'Registry ID'

// And now, we have a 'Prefixed Array' with all registry values

this.writeVarInt(bannerPatternRegistry, 1); // Here is a length of 'Prefixed Array'.  It's 1 because we add only one value

// Now, we have to add the Id of registry entry
this.writeString(bannerPatternRegistry, "minecraft:base");

// And here, we have to add the data of registry, witch it's 'Prefixed Optional NBT'

this.writeBoolean(bannerPatternRegistry, true); // This means that our value is present

Map bannerPatternData = new HashMap();
bannerPatternData.put("asset_id", "minecraft:diagonal_left");
bannerPatternData.put("translation_key", "block.minecraft.banner.diagonal_left.blue");

this.writeCompoundTag(bannerPatternRegistry, "", bannerPatternData); // Here we write the NBT data

client.writeAndFlush(bannerPatternData);
}
< /code>
Hier sind alle Methoden, die ich verwende: < /p>
public final void writeVarInt(ByteBuf buf, int value) {
while ((value & 0xFFFFFF80) != 0L) {
buf.writeByte((value & 0x7F) | 0x80);
value >>>= 7;
}
buf.writeByte(value & 0x7F);
}

public final void writeString(ByteBuf buf, String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
if (bytes.length > 32767 * 4) {
throw new RuntimeException("String too long");
}
this.writeVarInt(buf, bytes.length);
buf.writeBytes(bytes);
}

public final void writeBoolean(ByteBuf buf, boolean value) {
buf.writeBoolean(value);
}
< /code>
Und hier sind alle Methoden, die ich verwenden kann, um NBT und zu verzeichnen: < /p>
private void writeTagHeader(ByteBuf out, TagType type, String name) {
out.writeByte(type.getId());
if (type != TagType.END) {
this.writeString(out, name);
}
}

public void writeByteTag(ByteBuf out, String name, byte value) {
writeTagHeader(out, TagType.BYTE, name);
out.writeByte(value);
}

public void writeShortTag(ByteBuf out, String name, short value) {
writeTagHeader(out, TagType.SHORT, name);
out.writeShort(value);
}

public void writeIntTag(ByteBuf out, String name, int value) {
writeTagHeader(out, TagType.INT, name);
out.writeInt(value);
}

public void writeLongTag(ByteBuf out, String name, long value) {
writeTagHeader(out, TagType.LONG, name);
out.writeLong(value);
}

public void writeFloatTag(ByteBuf out, String name, float value) {
writeTagHeader(out, TagType.FLOAT, name);
out.writeFloat(value);
}

public void writeDoubleTag(ByteBuf out, String name, double value) {
writeTagHeader(out, TagType.DOUBLE, name);
out.writeDouble(value);
}

public void writeStringTag(ByteBuf out, String name, String value) {
writeTagHeader(out, TagType.STRING, name);
writeString(out, value);
}

public void writeByteArrayTag(ByteBuf out, String name, byte[] data) {
writeTagHeader(out, TagType.BYTE_ARRAY, name);
out.writeInt(data.length);
out.writeBytes(data);
}

public void writeIntArrayTag(ByteBuf out, String name, int[] data) {
writeTagHeader(out, TagType.INT_ARRAY, name);
out.writeInt(data.length);
for (int v : data) out.writeInt(v);
}

public void writeLongArrayTag(ByteBuf out, String name, long[] data) {
writeTagHeader(out, TagType.LONG_ARRAY, name);
out.writeInt(data.length);
for (long v : data) out.writeLong(v);
}

public void writeCompoundTag(ByteBuf out, String name, Map values) {
writeTagHeader(out, TagType.COMPOUND, name);

for (Map.Entry  entry : values.entrySet()) {
Object val = entry.getValue();
String key = entry.getKey();

if (val instanceof Byte) writeByteTag(out, key, (Byte) val);
else if (val instanceof Short) writeShortTag(out, key, (Short) val);
else if (val instanceof Integer) writeIntTag(out, key, (Integer) val);
else if (val instanceof Long) writeLongTag(out, key, (Long) val);
else if (val instanceof Float) writeFloatTag(out, key, (Float) val);
else if (val instanceof Double) writeDoubleTag(out, key, (Double) val);
else if (val instanceof String) writeStringTag(out, key, (String) val);
else if (val instanceof byte[]) writeByteArrayTag(out, key, (byte[]) val);
else if (val instanceof int[]) writeIntArrayTag(out, key, (int[]) val);
else if (val instanceof long[]) writeLongArrayTag(out, key, (long[]) val);
else if (val instanceof Map) {
writeCompoundTag(out, key, (Map) val);
} else {
throw new IllegalArgumentException("Unsupported NBT type for key: " + key);
}
}

out.writeByte(TagType.END.getId());
}

public void writeListTag(ByteBuf out, String name, TagType elementType, List values) {
writeTagHeader(out, TagType.LIST, name);
out.writeByte(elementType.getId());
out.writeInt(values.size());

for (Object val : values) {
if (elementType == TagType.BYTE) out.writeByte((Byte) val);
else if (elementType == TagType.SHORT) out.writeShort((Short) val);
else if (elementType == TagType.INT) out.writeInt((Integer) val);
else if (elementType == TagType.LONG) out.writeLong((Long) val);
else if (elementType == TagType.FLOAT) out.writeFloat((Float) val);
else if (elementType == TagType.DOUBLE) out.writeDouble((Double) val);
else if (elementType == TagType.STRING) writeString(out, (String) val);
else throw new IllegalArgumentException("Unsupported list element type: " + elementType);
}
}

public enum TagType {
END(0),
BYTE(1),
SHORT(2),
INT(3),
LONG(4),
FLOAT(5),
DOUBLE(6),
BYTE_ARRAY(7),
STRING(8),
LIST(9),
COMPOUND(10),
INT_ARRAY(11),
LONG_ARRAY(12);

private final int id;
TagType(int id) { this.id = id; }
public int getId() { return id; }
}
And, now, when I send this packet, I get the following error:
Image

Can someone help me with this error? Was habe ich falsch gemacht? Kann mir jemand mit einem Code -Beispiel helfen?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post