From 7ca0689989951d62003b29e8f011f1bf17f98085 Mon Sep 17 00:00:00 2001 From: Adrian <68704415+4drian3d@users.noreply.github.com> Date: Thu, 22 Feb 2024 12:48:36 -0500 Subject: [PATCH] Fixed duplicate sending of resource packs responses to backend server (#1249) Also fixes the sending of SUCCESSFUL responses to the backend server that will apply a resource pack in case Velocity has already applied one or more resource packs to the player --- .../client/ClientConfigSessionHandler.java | 3 --- .../resourcepack/ModernResourcePackHandler.java | 12 +++++++++++- .../player/resourcepack/ResourcePackHandler.java | 12 ++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java index 99a916242..48ea03c41 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientConfigSessionHandler.java @@ -95,9 +95,6 @@ public class ClientConfigSessionHandler implements MinecraftSessionHandler { @Override public boolean handle(ResourcePackResponsePacket packet) { - if (player.getConnectionInFlight() != null) { - player.getConnectionInFlight().ensureConnected().write(packet); - } return player.resourcePackHandler().onResourcePackResponse( new ResourcePackResponseBundle(packet.getId(), packet.getHash(), diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/player/resourcepack/ModernResourcePackHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/player/resourcepack/ModernResourcePackHandler.java index 640c7ed55..5ba74a273 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/player/resourcepack/ModernResourcePackHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/player/resourcepack/ModernResourcePackHandler.java @@ -146,10 +146,20 @@ public final class ModernResourcePackHandler extends ResourcePackHandler { } // The resource pack has been applied correctly. case SUCCESSFUL -> { + pendingResourcePacks.remove(uuid); if (queued != null) { appliedResourcePacks.put(uuid, queued); + } else { + // When transitioning to another server that has a resource pack to apply, + // if one or more resource packs have already been applied from Velocity, + // the player sends more than 1 SUCCESSFUL response to the backend server, + // which results in the server receiving more resource pack responses + // than the server has sent requests to the player + final ResourcePackInfo appliedPack = appliedResourcePacks.get(uuid); + if (appliedPack != null) { + return handleResponseResult(appliedPack, bundle); + } } - pendingResourcePacks.remove(uuid); } // An error occurred while trying to download the resource pack to the client, // so the resource pack cannot be applied. diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/player/resourcepack/ResourcePackHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/player/resourcepack/ResourcePackHandler.java index 68640fb89..e2160f8ec 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/player/resourcepack/ResourcePackHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/player/resourcepack/ResourcePackHandler.java @@ -20,6 +20,7 @@ package com.velocitypowered.proxy.connection.player.resourcepack; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.player.ResourcePackInfo; import com.velocitypowered.proxy.VelocityServer; +import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.player.VelocityResourcePackInfo; import com.velocitypowered.proxy.protocol.packet.ResourcePackRequestPacket; @@ -147,12 +148,15 @@ public abstract sealed class ResourcePackHandler final @Nullable ResourcePackInfo queued, final @NotNull ResourcePackResponseBundle bundle ) { + // If Velocity, through a plugin, has sent a resource pack to the client, + // there is no need to report the status of the response to the server + // since it has no information that a resource pack has been sent final boolean handled = queued != null - && queued.getOriginalOrigin() != ResourcePackInfo.Origin.DOWNSTREAM_SERVER; + && queued.getOriginalOrigin() == ResourcePackInfo.Origin.PLUGIN_ON_PROXY; if (!handled) { - if (player.getConnectionInFlight() != null - && player.getConnectionInFlight().getConnection() != null) { - player.getConnectionInFlight().getConnection().write(new ResourcePackResponsePacket( + final VelocityServerConnection connectionInFlight = player.getConnectionInFlight(); + if (connectionInFlight != null && connectionInFlight.getConnection() != null) { + connectionInFlight.getConnection().write(new ResourcePackResponsePacket( bundle.uuid(), bundle.hash(), bundle.status())); } }