Cleaned up UUID handling

This commit is contained in:
RaphiMC 2024-02-07 17:01:04 +01:00
parent cfe6c57d0e
commit 877dca1b8d
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
6 changed files with 70 additions and 12 deletions

View File

@ -0,0 +1,29 @@
/*
* This file is part of ViaLegacy - https://github.com/RaphiMC/ViaLegacy
* Copyright (C) 2020-2024 RK_01/RaphiMC and 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 <http://www.gnu.org/licenses/>.
*/
package net.raphimc.vialegacy.api.util;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class UuidUtil {
public static UUID createOfflinePlayerUuid(final String name) {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(StandardCharsets.UTF_8));
}
}

View File

@ -856,14 +856,10 @@ public class Protocol1_7_2_5to1_6_4 extends StatelessTransitionProtocol<Clientbo
pong.send(Protocol1_7_2_5to1_6_4.class);
});
this.registerServerboundTransition(ServerboundLoginPackets.HELLO, ServerboundPackets1_6_4.CLIENT_PROTOCOL, wrapper -> {
final String name = wrapper.read(Type.STRING); // user name
final ProtocolInfo info = wrapper.user().getProtocolInfo();
final HandshakeStorage handshakeStorage = wrapper.user().get(HandshakeStorage.class);
info.setUsername(name);
info.setUuid(ViaLegacy.getConfig().isLegacySkinLoading() ? Via.getManager().getProviders().get(GameProfileFetcher.class).getMojangUUID(name) : new GameProfile(name).uuid);
wrapper.write(Type.UNSIGNED_BYTE, (short) LegacyProtocolVersion.getRealProtocolVersion(info.getServerProtocolVersion())); // protocol id
wrapper.write(Types1_6_4.STRING, name); // user name
wrapper.write(Type.UNSIGNED_BYTE, (short) LegacyProtocolVersion.getRealProtocolVersion(wrapper.user().getProtocolInfo().getServerProtocolVersion())); // protocol id
wrapper.write(Types1_6_4.STRING, wrapper.read(Type.STRING)); // user name
wrapper.write(Types1_6_4.STRING, handshakeStorage.getHostname()); // hostname
wrapper.write(Type.INT, handshakeStorage.getPort()); // port
});

View File

@ -31,6 +31,7 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.protocols.base.BaseProtocol1_7;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.util.UuidUtil;
import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.rewriter.TranslationRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameProfile;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher;
@ -174,7 +175,7 @@ public class Protocol1_7_6_10to1_7_2_5 extends AbstractProtocol<ClientboundPacke
return dashedUuid;
}
}
return new GameProfile(name).uuid.toString();
return UuidUtil.createOfflinePlayerUuid(name).toString();
}
}

View File

@ -29,6 +29,7 @@ import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
@ -59,6 +60,7 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameP
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.MapData;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.MapIcon;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.TabListEntry;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.ChatItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.ItemRewriter;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.TranslationRewriter;
@ -108,6 +110,18 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
map(Type.SHORT_BYTE_ARRAY, Type.BYTE_ARRAY_PRIMITIVE); // verify token
}
});
this.registerClientbound(State.LOGIN, ClientboundLoginPackets.GAME_PROFILE.getId(), ClientboundLoginPackets.GAME_PROFILE.getId(), new PacketHandlers() {
@Override
public void register() {
read(Type.STRING); // uuid
read(Type.STRING); // name
handler(wrapper -> { // 1.7.10 ignores the data from the server
final ProtocolInfo protocolInfo = wrapper.user().getProtocolInfo();
wrapper.write(Type.STRING, protocolInfo.getUuid().toString()); // uuid
wrapper.write(Type.STRING, protocolInfo.getUsername()); // name
});
}
});
this.registerClientbound(ClientboundPackets1_7_2.KEEP_ALIVE, new PacketHandlers() {
@Override
public void register() {
@ -1146,6 +1160,18 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
}
});
this.registerServerbound(State.LOGIN, ServerboundLoginPackets.HELLO.getId(), ServerboundLoginPackets.HELLO.getId(), new PacketHandlers() {
@Override
public void register() {
handler(wrapper -> {
final String name = wrapper.passthrough(Type.STRING); // name
final ProtocolInfo info = wrapper.user().getProtocolInfo();
// Set the information early
info.setUsername(name);
info.setUuid(ViaLegacy.getConfig().isLegacySkinLoading() ? Via.getManager().getProviders().get(GameProfileFetcher.class).getMojangUUID(name) : new GameProfile(name).uuid);
});
}
});
this.registerServerbound(State.LOGIN, ServerboundLoginPackets.ENCRYPTION_KEY.getId(), ServerboundLoginPackets.ENCRYPTION_KEY.getId(), new PacketHandlers() {
@Override
public void register() {
@ -1504,6 +1530,11 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
return (int) Math.floor((yPos - yOffset) * 32F);
}
@Override
public void register(ViaProviders providers) {
providers.require(GameProfileFetcher.class);
}
@Override
public void init(UserConnection userConnection) {
userConnection.put(new TablistStorage(userConnection));

View File

@ -17,7 +17,8 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model;
import java.nio.charset.StandardCharsets;
import net.raphimc.vialegacy.api.util.UuidUtil;
import java.util.*;
public class GameProfile {
@ -37,14 +38,14 @@ public class GameProfile {
public GameProfile(final String userName) {
if (userName == null) throw new IllegalStateException("Username can't be null");
this.userName = userName;
this.offlineUuid = this.uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + userName).getBytes(StandardCharsets.UTF_8));
this.offlineUuid = this.uuid = UuidUtil.createOfflinePlayerUuid(userName);
}
public GameProfile(final String userName, final UUID uuid) {
if (userName == null || uuid == null) throw new IllegalStateException("Username and UUID can't be null");
this.userName = userName;
this.uuid = uuid;
this.offlineUuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + userName).getBytes(StandardCharsets.UTF_8));
this.offlineUuid = UuidUtil.createOfflinePlayerUuid(userName);
}
public void addProperty(final Property property) {

View File

@ -24,9 +24,9 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.viaversion.viaversion.api.platform.providers.Provider;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.util.UuidUtil;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameProfile;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.logging.Level;
@ -63,7 +63,7 @@ public abstract class GameProfileFetcher implements Provider {
while (e instanceof ExecutionException || e instanceof UncheckedExecutionException || e instanceof CompletionException) e = e.getCause();
ViaLegacy.getPlatform().getLogger().log(Level.WARNING, "Failed to load uuid for player '" + playerName + "' (" + e.getClass().getName() + ")");
}
final UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(StandardCharsets.UTF_8));
final UUID uuid = UuidUtil.createOfflinePlayerUuid(playerName);
UUID_CACHE.put(playerName, uuid);
return uuid;
}