ProtocolLib

3M Downloads

Can't change server brand

molor opened this issue · 5 comments

commented
  • This issue is not solved in a development build

Describe the bug
Server brand can't be changed by modifying the CUSTOM_PAYLOAD packet.

To Reproduce

public Test(TestPlugin thePlugin) {
	super(thePlugin, ListenerPriority.HIGHEST, List.of(PacketType.Configuration.Server.CUSTOM_PAYLOAD), ListenerOptions.ASYNC);
}

private static final MinecraftKey KEY = new MinecraftKey("brand");
private static final String BRAND = "Test";

@Override
public void onPacketSending(PacketEvent theEvent) {
	if (theEvent.isCancelled())
		return;

	StructureModifier<CustomPacketPayloadWrapper> theModifier =
		theEvent.
			getPacket().
			getCustomPacketPayloads();

	if (theModifier.read(0).getId().equals(KEY))
		theModifier.write(0, new CustomPacketPayloadWrapper(BRAND.getBytes(StandardCharsets.UTF_8), KEY));
}

Then just try to connect to the server.

Expected behavior
"Test" server in [F3]

Screenshots

image

Version Info
Paper 1.20.4-330
PL 5.2.0-SNAPSHOT-678

commented

Hi @derklaro! Because you're the author of #2553, can you please look at this? What I'm doing wrong?

commented

You probably need to serialize the brand as utf (prefix it with a varint indicating the length)

commented

prefix it with a varint indicating the length

How I can do that?

commented

How I can do that?! :/

commented

I haven't tested this, but the following code should address the issue:

try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
     DataOutputStream dataOut = new DataOutputStream(byteOut)) {
    
    StreamSerializer.getDefault().serializeString(dataOut, "custom_brand");
   
    // Obtain the payload for the CustomPacketPayloadWrapper
    byte[] payload = byteOut.toByteArray();
} catch (IOException e) {
    e.printStackTrace();
}