Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance optimization of chat #3634

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 100 additions & 8 deletions protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
Expand All @@ -21,6 +22,16 @@
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentStyle;
import net.md_5.bungee.chat.ComponentSerializer;
import net.md_5.bungee.protocol.data.NumberFormat;
import net.md_5.bungee.protocol.data.PlayerPublicKey;
import net.md_5.bungee.protocol.data.Property;
import net.md_5.bungee.protocol.util.ChatDeserializable;
import net.md_5.bungee.protocol.util.ChatDeserializableJson;
import net.md_5.bungee.protocol.util.ChatDeserializableTag;
import net.md_5.bungee.protocol.util.ChatNewDeserializable;
import net.md_5.bungee.protocol.util.ChatNewDeserializableTag;
import net.md_5.bungee.protocol.util.Either;
import net.md_5.bungee.protocol.util.TagUtil;
import se.llbit.nbt.ErrorTag;
import se.llbit.nbt.NamedTag;
import se.llbit.nbt.SpecificTag;
Expand Down Expand Up @@ -93,17 +104,33 @@ public static String readString(ByteBuf buf, int maxLen)
return s;
}

public static Either<String, BaseComponent> readEitherBaseComponent(ByteBuf buf, int protocolVersion, boolean string)
public static Either<String, ChatDeserializable> readEitherBaseComponent(ByteBuf buf, int protocolVersion, boolean string)
{
return ( string ) ? Either.left( readString( buf ) ) : Either.right( readBaseComponent( buf, protocolVersion ) );
return string ? Either.left( readString( buf ) ) : Either.right( readBaseComponent( buf, protocolVersion ) );
}

public static BaseComponent readBaseComponent(ByteBuf buf, int protocolVersion)
public static ChatDeserializable readBaseComponent(ByteBuf buf, int protocolVersion)
{
return readBaseComponent( buf, Short.MAX_VALUE, protocolVersion );
}

public static BaseComponent readBaseComponent(ByteBuf buf, int maxStringLength, int protocolVersion)
public static ChatDeserializable readBaseComponent(ByteBuf buf, int maxStringLength, int protocolVersion)
{
if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 )
{
return new ChatDeserializableTag( (SpecificTag) readTag( buf, protocolVersion ) );
} else
{
return new ChatDeserializableJson( readString( buf, maxStringLength ) );
}
}

public static BaseComponent readRawComponent(ByteBuf buf, int protocolVersion)
{
return readRawComponent( buf, Short.MAX_VALUE, protocolVersion );
}

public static BaseComponent readRawComponent(ByteBuf buf, int maxStringLength, int protocolVersion)
{
if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 )
{
Expand All @@ -119,6 +146,13 @@ public static BaseComponent readBaseComponent(ByteBuf buf, int maxStringLength,
}
}

public static ChatNewDeserializable readNewBaseComponent(ByteBuf buf, int protocolVersion)
{
SpecificTag nbt = (SpecificTag) readTag( buf, protocolVersion );

return new ChatNewDeserializableTag( nbt );
}

public static ComponentStyle readComponentStyle(ByteBuf buf, int protocolVersion)
{
SpecificTag nbt = (SpecificTag) readTag( buf, protocolVersion );
Expand All @@ -127,7 +161,7 @@ public static ComponentStyle readComponentStyle(ByteBuf buf, int protocolVersion
return ComponentSerializer.deserializeStyle( json );
}

public static void writeEitherBaseComponent(Either<String, BaseComponent> message, ByteBuf buf, int protocolVersion)
public static void writeEitherBaseComponent(Either<String, ChatDeserializable> message, ByteBuf buf, int protocolVersion)
{
if ( message.isLeft() )
{
Expand All @@ -138,7 +172,52 @@ public static void writeEitherBaseComponent(Either<String, BaseComponent> messag
}
}

public static void writeBaseComponent(BaseComponent message, ByteBuf buf, int protocolVersion)
public static void writeBaseComponent(ChatDeserializable message, ByteBuf buf, int protocolVersion)
{
if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 )
{
if ( message.hasDeserialized() )
{
BaseComponent baseComponent = message.get();
JsonElement json = ComponentSerializer.toJson( baseComponent );
SpecificTag nbt = TagUtil.fromJson( json );

writeTag( nbt, buf, protocolVersion );
} else
{
Either<String, SpecificTag> eitherStrJsonElem = message.original();
if ( eitherStrJsonElem.isLeft() )
{
writeTag( TagUtil.fromJson( JsonParser.parseString( eitherStrJsonElem.getLeft() ) ), buf, protocolVersion );
} else
{
writeTag( eitherStrJsonElem.getRight(), buf, protocolVersion );
}
}
} else
{
if ( message.hasDeserialized() )
{
String string = ComponentSerializer.toString( message.get() );

writeString( string, buf );
} else
{
Either<String, SpecificTag> eitherStrJsonElem = message.original();
if ( eitherStrJsonElem.isLeft() )
{
writeString( eitherStrJsonElem.getLeft(), buf );
} else
{
String string = ComponentSerializer.toString( TagUtil.toJson( eitherStrJsonElem.getRight() ) );

writeString( string, buf );
}
}
}
}

public static void writeRawComponent(BaseComponent message, ByteBuf buf, int protocolVersion)
{
if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 )
{
Expand All @@ -154,6 +233,19 @@ public static void writeBaseComponent(BaseComponent message, ByteBuf buf, int pr
}
}

public static void writeNewBaseComponent(ChatNewDeserializable message, ByteBuf buf, int protocolVersion)
{
SpecificTag nbt;
if ( message.hasDeserialized() )
{
nbt = TagUtil.fromJson( ComponentSerializer.toJson( message.get() ) );
} else
{
nbt = message.original();
}
writeTag( nbt, buf, protocolVersion );
}

public static void writeComponentStyle(ComponentStyle style, ByteBuf buf, int protocolVersion)
{
JsonElement json = ComponentSerializer.toJson( style );
Expand Down Expand Up @@ -398,7 +490,7 @@ public static void writeNumberFormat(NumberFormat format, ByteBuf buf, int proto
writeComponentStyle( (ComponentStyle) format.getValue(), buf, protocolVersion );
break;
case FIXED:
writeBaseComponent( (BaseComponent) format.getValue(), buf, protocolVersion );
writeRawComponent( (BaseComponent) format.getValue(), buf, protocolVersion );
break;
}
}
Expand All @@ -413,7 +505,7 @@ public static NumberFormat readNumberFormat(ByteBuf buf, int protocolVersion)
case 1:
return new NumberFormat( NumberFormat.Type.STYLED, readComponentStyle( buf, protocolVersion ) );
case 2:
return new NumberFormat( NumberFormat.Type.FIXED, readBaseComponent( buf, protocolVersion ) );
return new NumberFormat( NumberFormat.Type.FIXED, readRawComponent( buf, protocolVersion ) );
default:
throw new IllegalArgumentException( "Unknown number format " + format );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.md_5.bungee.protocol;
package net.md_5.bungee.protocol.data;

import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
Expand All @@ -9,6 +9,9 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.ProtocolConstants;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.md_5.bungee.protocol;
package net.md_5.bungee.protocol.data;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.md_5.bungee.protocol;
package net.md_5.bungee.protocol.data;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.md_5.bungee.protocol;
package net.md_5.bungee.protocol.data;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.md_5.bungee.protocol;
package net.md_5.bungee.protocol.data;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package net.md_5.bungee.protocol;
package net.md_5.bungee.protocol.data;

import io.netty.buffer.ByteBuf;
import java.util.BitSet;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.ProtocolConstants;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.util.ChatDeserializable;

@Data
@NoArgsConstructor
Expand All @@ -18,7 +18,7 @@ public class BossBar extends DefinedPacket

private UUID uuid;
private int action;
private BaseComponent title;
private ChatDeserializable title;
private float health;
private int color;
private int division;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.ChatChain;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.SeenMessages;
import net.md_5.bungee.protocol.data.ChatChain;
import net.md_5.bungee.protocol.data.SeenMessages;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.ChatChain;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.SeenMessages;
import net.md_5.bungee.protocol.data.ChatChain;
import net.md_5.bungee.protocol.data.SeenMessages;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.util.ChatDeserializable;
import net.md_5.bungee.protocol.util.ChatDeserializableJson;

@Data
@NoArgsConstructor
Expand All @@ -19,14 +20,14 @@
public class Kick extends DefinedPacket
{

private BaseComponent message;
private ChatDeserializable message;

@Override
public void read(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
{
if ( protocol == Protocol.LOGIN )
{
message = ComponentSerializer.deserialize( readString( buf ) );
message = new ChatDeserializableJson( readString( buf ) );
} else
{
message = readBaseComponent( buf, protocolVersion );
Expand All @@ -38,7 +39,7 @@ public void write(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction di
{
if ( protocol == Protocol.LOGIN )
{
writeString( ComponentSerializer.toString( message ), buf );
writeString( ComponentSerializer.toString( message.get() ), buf );
} else
{
writeBaseComponent( message, buf, protocolVersion );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.Location;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.data.Location;
import se.llbit.nbt.Tag;

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.PlayerPublicKey;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.data.PlayerPublicKey;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.Property;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.data.Property;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.util.ChatDeserializable;

@Data
@NoArgsConstructor
Expand All @@ -17,8 +17,8 @@
public class PlayerListHeaderFooter extends DefinedPacket
{

private BaseComponent header;
private BaseComponent footer;
private ChatDeserializable header;
private ChatDeserializable footer;

@Override
public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.PlayerPublicKey;
import net.md_5.bungee.protocol.Property;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.data.PlayerPublicKey;
import net.md_5.bungee.protocol.data.Property;
import net.md_5.bungee.protocol.util.ChatDeserializable;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -143,7 +143,6 @@ public static class Item
Integer ping;

// ADD_PLAYER & UPDATE_DISPLAY_NAME
BaseComponent displayName;

ChatDeserializable displayName;
}
}
Loading