#EOFException problem mod-->plugin

1 messages ยท Page 1 of 1 (latest)

brave salmon
#

EOFException problem mod-->plugin

#

I tried with readLine() it works without exception but I get: (ASCII value of the string length + the string)

real eagle
#

Try just new String(bytes)

unkempt shore
brave salmon
brave salmon
unkempt shore
#

I have no Forge experience, you should ask someone from their community

brave salmon
#

Because I think the way I skip the byte and readline, it is the the proper manner of handling packet

#

I say that because I never see the others plugins do it

unkempt shore
#

Just skipping the length would actually introduce a serious issue if you were programming in lets say C, but here since you're just encoding&decoding an UUID it's okay I guess

brave salmon
#

You are right, I was speaking when my packet will have several information

real eagle
#

Looking at the source the first bytes is a varint for length and the rest is the string

unkempt shore
#

Oh, wait, I see
It seems like Minecraft's PacketBuffer encodes the length into a single byte in this example, while on plugin's side the ByteArrayDataInput#readUTF() reads exactly 2 bytes to determine the length, so it makes sense why the EOF ๐Ÿ˜„

brave salmon
#

Thank for your precisions, I have write my own writeString() and instead of this.writeVarInt() i put a writeShort(), and it works !

unkempt shore
#

Well, if you'd need to read/write anything longer, here's a method to read the varint. PacketByteBuf is a netty thing, I believe it's the same your buffer

private static int readVarInt(final PacketByteBuf buf) {
    byte cur;
    int varInt = 0;
    int i = 0;
    do {
        varInt |= ((cur = buf.readByte()) & 0b0111_1111) << (i++ * 7);
    } while ((cur & 0b1000_0000) == 0b1000_0000);
    return varInt;
}

Tested with this code, should work ๐Ÿ˜„

String longStr = Strings.repeat("a", Short.MAX_VALUE / 2);
var longStrBuf = new PacketByteBuf(Unpooled.buffer(longStr.length() + 2));
longStrBuf.writeString(longStr);
int length = readVarInt(longStrBuf);
byte[] bytes = new byte[length];
longStrBuf.readBytes(bytes);
System.out.printf("Read length: %d, actual length: %d\n", length, longStr.length());
System.out.printf("Decoded str equals original: %b\n", new String(bytes).equals(longStr));
brave salmon
#

(I develop in 1.16.5 forge)

real eagle
#

It should be called FriendlyByteBuf

#

And it does contain that readVarInt code