Add ability to skip packet translating

This commit is contained in:
Kieran Wallbanks 2021-03-12 15:51:48 +00:00
parent 931d790702
commit f7b683c343
3 changed files with 22 additions and 6 deletions

View File

@ -13,7 +13,7 @@ import java.net.SocketAddress;
public class FakePlayerConnection extends PlayerConnection {
@Override
public void sendPacket(@NotNull ServerPacket serverPacket) {
public void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating) {
if (shouldSendPacket(serverPacket)) {
getFakePlayer().getController().consumePacket(serverPacket);
}

View File

@ -125,7 +125,7 @@ public class NettyPlayerConnection extends PlayerConnection {
* @param serverPacket the packet to write
*/
@Override
public void sendPacket(@NotNull ServerPacket serverPacket) {
public void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating) {
if (!channel.isActive())
return;
@ -138,7 +138,7 @@ public class NettyPlayerConnection extends PlayerConnection {
if (identifier == null) {
// This packet explicitly asks to do not retrieve the cache
write(serverPacket);
write(serverPacket, skipTranslating);
} else {
final long timestamp = cacheablePacket.getTimestamp();
// Try to retrieve the cached buffer
@ -163,7 +163,7 @@ public class NettyPlayerConnection extends PlayerConnection {
}
} else {
write(serverPacket);
write(serverPacket, skipTranslating);
}
} else {
// Player is probably not logged yet
@ -173,6 +173,10 @@ public class NettyPlayerConnection extends PlayerConnection {
}
public void write(@NotNull Object message) {
this.write(message, false);
}
public void write(@NotNull Object message, boolean skipTranslating) {
if (message instanceof FramedPacket) {
final FramedPacket framedPacket = (FramedPacket) message;
synchronized (tickBuffer) {
@ -183,7 +187,7 @@ public class NettyPlayerConnection extends PlayerConnection {
} else if (message instanceof ServerPacket) {
final ServerPacket serverPacket = (ServerPacket) message;
if (getPlayer() != null && serverPacket instanceof ComponentHoldingServerPacket) {
if (!skipTranslating && getPlayer() != null && serverPacket instanceof ComponentHoldingServerPacket) {
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component -> MinecraftServer.getSerializationManager().translate(component, getPlayer()));
}

View File

@ -90,7 +90,19 @@ public abstract class PlayerConnection {
* @param serverPacket the packet to send
* @see #shouldSendPacket(ServerPacket)
*/
public abstract void sendPacket(@NotNull ServerPacket serverPacket);
public void sendPacket(@NotNull ServerPacket serverPacket) {
this.sendPacket(serverPacket, false);
}
/**
* Serializes the packet and send it to the client, skipping the translation phase.
* <p>
* Also responsible for executing {@link ConnectionManager#onPacketSend(ServerPacketConsumer)} consumers.
*
* @param serverPacket the packet to send
* @see #shouldSendPacket(ServerPacket)
*/
public abstract void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating);
protected boolean shouldSendPacket(@NotNull ServerPacket serverPacket) {
return player == null ||