mirror of
https://github.com/ViaVersion/ViaLegacy.git
synced 2025-01-03 18:38:34 +01:00
Cleaned up UUID handling
This commit is contained in:
parent
cfe6c57d0e
commit
877dca1b8d
29
src/main/java/net/raphimc/vialegacy/api/util/UuidUtil.java
Normal file
29
src/main/java/net/raphimc/vialegacy/api/util/UuidUtil.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -856,14 +856,10 @@ public class Protocol1_7_2_5to1_6_4 extends StatelessTransitionProtocol<Clientbo
|
|||||||
pong.send(Protocol1_7_2_5to1_6_4.class);
|
pong.send(Protocol1_7_2_5to1_6_4.class);
|
||||||
});
|
});
|
||||||
this.registerServerboundTransition(ServerboundLoginPackets.HELLO, ServerboundPackets1_6_4.CLIENT_PROTOCOL, wrapper -> {
|
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);
|
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(Type.UNSIGNED_BYTE, (short) LegacyProtocolVersion.getRealProtocolVersion(wrapper.user().getProtocolInfo().getServerProtocolVersion())); // protocol id
|
||||||
wrapper.write(Types1_6_4.STRING, name); // user name
|
wrapper.write(Types1_6_4.STRING, wrapper.read(Type.STRING)); // user name
|
||||||
wrapper.write(Types1_6_4.STRING, handshakeStorage.getHostname()); // hostname
|
wrapper.write(Types1_6_4.STRING, handshakeStorage.getHostname()); // hostname
|
||||||
wrapper.write(Type.INT, handshakeStorage.getPort()); // port
|
wrapper.write(Type.INT, handshakeStorage.getPort()); // port
|
||||||
});
|
});
|
||||||
|
@ -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.BaseProtocol1_7;
|
||||||
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
|
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
|
||||||
import net.raphimc.vialegacy.ViaLegacy;
|
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_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.model.GameProfile;
|
||||||
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher;
|
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 dashedUuid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new GameProfile(name).uuid.toString();
|
return UuidUtil.createOfflinePlayerUuid(name).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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.DataItem;
|
||||||
import com.viaversion.viaversion.api.minecraft.item.Item;
|
import com.viaversion.viaversion.api.minecraft.item.Item;
|
||||||
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
|
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.AbstractProtocol;
|
||||||
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
||||||
import com.viaversion.viaversion.api.protocol.packet.State;
|
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.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.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.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.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.ItemRewriter;
|
||||||
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.TranslationRewriter;
|
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
|
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() {
|
this.registerClientbound(ClientboundPackets1_7_2.KEEP_ALIVE, new PacketHandlers() {
|
||||||
@Override
|
@Override
|
||||||
public void register() {
|
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() {
|
this.registerServerbound(State.LOGIN, ServerboundLoginPackets.ENCRYPTION_KEY.getId(), ServerboundLoginPackets.ENCRYPTION_KEY.getId(), new PacketHandlers() {
|
||||||
@Override
|
@Override
|
||||||
public void register() {
|
public void register() {
|
||||||
@ -1504,6 +1530,11 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol<ClientboundPackets1_
|
|||||||
return (int) Math.floor((yPos - yOffset) * 32F);
|
return (int) Math.floor((yPos - yOffset) * 32F);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void register(ViaProviders providers) {
|
||||||
|
providers.require(GameProfileFetcher.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(UserConnection userConnection) {
|
public void init(UserConnection userConnection) {
|
||||||
userConnection.put(new TablistStorage(userConnection));
|
userConnection.put(new TablistStorage(userConnection));
|
||||||
|
@ -17,7 +17,8 @@
|
|||||||
*/
|
*/
|
||||||
package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model;
|
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.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class GameProfile {
|
public class GameProfile {
|
||||||
@ -37,14 +38,14 @@ public class GameProfile {
|
|||||||
public GameProfile(final String userName) {
|
public GameProfile(final String userName) {
|
||||||
if (userName == null) throw new IllegalStateException("Username can't be null");
|
if (userName == null) throw new IllegalStateException("Username can't be null");
|
||||||
this.userName = userName;
|
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) {
|
public GameProfile(final String userName, final UUID uuid) {
|
||||||
if (userName == null || uuid == null) throw new IllegalStateException("Username and UUID can't be null");
|
if (userName == null || uuid == null) throw new IllegalStateException("Username and UUID can't be null");
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
this.offlineUuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + userName).getBytes(StandardCharsets.UTF_8));
|
this.offlineUuid = UuidUtil.createOfflinePlayerUuid(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addProperty(final Property property) {
|
public void addProperty(final Property property) {
|
||||||
|
@ -24,9 +24,9 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|||||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||||
import com.viaversion.viaversion.api.platform.providers.Provider;
|
import com.viaversion.viaversion.api.platform.providers.Provider;
|
||||||
import net.raphimc.vialegacy.ViaLegacy;
|
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 net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameProfile;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.logging.Level;
|
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();
|
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() + ")");
|
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);
|
UUID_CACHE.put(playerName, uuid);
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user