Skip to content

Commit

Permalink
Implement tab list entry uuid rewrite
Browse files Browse the repository at this point in the history
unlink bungee, rewrite the current player only
  • Loading branch information
Fallen-Breath committed Oct 29, 2023
1 parent 54634a7 commit 3ab03b3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A list of small tweaks I made:
- Supported proxy types: `socks4`, `socks5`, `http`
- Due to [an issue](https://github.com/AsyncHttpClient/async-http-client/issues/1913) in the http library velocity uses, you can only use `http` proxy for now
- If enabled, velocity will firstly try authenticating with the given proxy, if failed it will try again without the proxy
- Implement UUID rewrite for TabList packets, like what bungeecord does

# Velocity

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfo;
import com.velocitypowered.proxy.protocol.packet.config.StartUpdate;
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import com.velocitypowered.proxy.tablist.TabListUuidRewriter;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
Expand Down Expand Up @@ -263,18 +264,27 @@ public boolean handle(TabCompleteResponse packet) {

@Override
public boolean handle(LegacyPlayerListItem packet) {
// [fallen's fork] tab list entry uuid rewrite: impl
TabListUuidRewriter.rewrite(serverConn.getPlayer(), packet);

serverConn.getPlayer().getTabList().processLegacy(packet);
return false;
}

@Override
public boolean handle(UpsertPlayerInfo packet) {
// [fallen's fork] tab list entry uuid rewrite: impl
TabListUuidRewriter.rewrite(serverConn.getPlayer(), packet);

serverConn.getPlayer().getTabList().processUpdate(packet);
return false;
}

@Override
public boolean handle(RemovePlayerInfo packet) {
// [fallen's fork] tab list entry uuid rewrite: impl
TabListUuidRewriter.rewrite(serverConn.getPlayer(), packet);

serverConn.getPlayer().getTabList().processRemove(packet);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.util.GameProfile;
import com.velocitypowered.api.util.ModInfo;
import com.velocitypowered.api.util.UuidUtils;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation;
Expand Down Expand Up @@ -168,6 +169,9 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
private final ChatQueue chatQueue;
private final ChatBuilderFactory chatBuilderFactory;

// [fallen's fork] tab list entry uuid rewrite: store offline uuid for reuse
private final UUID offlineUuid;

ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection,
@Nullable InetSocketAddress virtualHost, boolean onlineMode,
@Nullable IdentifiedKey playerKey) {
Expand All @@ -189,6 +193,9 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
this.playerKey = playerKey;
this.chatQueue = new ChatQueue(this);
this.chatBuilderFactory = new ChatBuilderFactory(this.getProtocolVersion());

// [fallen's fork] tab list entry uuid rewrite: store offline uuid for reuse
this.offlineUuid = UuidUtils.generateOfflinePlayerUuid(this.getUsername());
}

public ChatBuilderFactory getChatBuilderFactory() {
Expand Down Expand Up @@ -227,6 +234,11 @@ public UUID getUniqueId() {
return profile.getId();
}

// [fallen's fork] tab list entry uuid rewrite: store offline uuid for reuse
public UUID getOfflineUuid() {
return offlineUuid;
}

@Override
public Optional<ServerConnection> getCurrentServer() {
return Optional.ofNullable(connectedServer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2018-2023 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.velocitypowered.proxy.tablist;

import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItem;
import com.velocitypowered.proxy.protocol.packet.RemovePlayerInfo;
import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfo;
import java.util.stream.Collectors;

/**
* [fallen's fork] tab list entry uuid rewrite: rewrite logic.
*/
public class TabListUuidRewriter {
/**
* Rewrite uuid for a LegacyPlayerListItem packet.
*/
public static void rewrite(ConnectedPlayer player, LegacyPlayerListItem packet) {
packet.getItems().replaceAll(item -> {
if (player.getOfflineUuid().equals(item.getUuid())) {
var newItem = new LegacyPlayerListItem.Item(player.getUniqueId());

newItem.setName(item.getName());
newItem.setProperties(item.getProperties());
newItem.setGameMode(item.getGameMode());
newItem.setLatency(item.getLatency());
newItem.setDisplayName(item.getDisplayName());
newItem.setPlayerKey(item.getPlayerKey());

item = newItem;
}

return item;
});
}

/**
* Rewrite uuid for a UpsertPlayerInfo packet.
*/
public static void rewrite(ConnectedPlayer player, UpsertPlayerInfo packet) {
packet.getEntries().replaceAll(entry -> {
if (player.getOfflineUuid().equals(entry.getProfileId())) {
var newEntry = new UpsertPlayerInfo.Entry(player.getUniqueId());

newEntry.setProfile(player.getGameProfile());
newEntry.setListed(entry.isListed());
newEntry.setLatency(entry.getLatency());
newEntry.setGameMode(entry.getGameMode());
newEntry.setDisplayName(entry.getDisplayName());
newEntry.setChatSession(entry.getChatSession());

entry = newEntry;
}

return entry;
});
}

/**
* Rewrite uuid for a RemovePlayerInfo packet.
*/
public static void rewrite(ConnectedPlayer player, RemovePlayerInfo packet) {
var newProfiles = packet.getProfilesToRemove().stream()
.map(uuid -> {
if (player.getOfflineUuid().equals(uuid)) {
uuid = player.getUniqueId();
}
return uuid;
})
.collect(Collectors.toList());
packet.setProfilesToRemove(newProfiles);
}
}

0 comments on commit 3ab03b3

Please sign in to comment.