Synchronize access to skins map in PlayerSkinHandler to ensure thread safety and prevent race conditions.

This commit is contained in:
2025-10-14 17:45:58 +02:00
parent 7179daedae
commit 089503e0a0
@@ -54,7 +54,7 @@ public class PlayerSkinHandler extends PacketHandler {
VelocityCore.getProxy().getEventManager().register(VelocityCore.get(), this);
}
private Map<UUID, SkinData> skins = new LinkedHashMap<>(maxCacheSize, 1, true) {
private final Map<UUID, SkinData> skins = new LinkedHashMap<>(maxCacheSize, 1, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<UUID, SkinData> eldest) {
return size() > maxCacheSize;
@@ -64,38 +64,40 @@ public class PlayerSkinHandler extends PacketHandler {
@Handler
@SneakyThrows
public void handle(PlayerSkinRequestPacket packet) {
if (skins.containsKey(packet.getUuid())) {
SkinData skinData = skins.get(packet.getUuid());
NetworkSender.send(((ServerMetaInfo) packet.getMetaInfos()).sender().getServer(), new PlayerSkinResponsePacket(packet.getUuid(), skinData.skin, skinData.signature));
return;
}
String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + packet.getUuid().toString().replace("-", "") + "?unsigned=false";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestProperty("User-Agent", "SkinFetcher");
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return;
}
InputStream is = connection.getInputStream();
String json = new BufferedReader(new InputStreamReader(is))
.lines().collect(Collectors.joining("\n"));
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
JsonArray properties = obj.getAsJsonArray("properties");
for (JsonElement propElement : properties) {
JsonObject prop = propElement.getAsJsonObject();
if (prop.get("name").getAsString().equals("textures")) {
String skin = prop.get("value").getAsString();
String signature = prop.get("signature").getAsString();
skins.put(packet.getUuid(), new SkinData(skin, signature));
NetworkSender.send(((ServerMetaInfo) packet.getMetaInfos()).sender().getServer(), new PlayerSkinResponsePacket(packet.getUuid(), skin, signature));
synchronized (skins) {
if (skins.containsKey(packet.getUuid())) {
SkinData skinData = skins.get(packet.getUuid());
NetworkSender.send(((ServerMetaInfo) packet.getMetaInfos()).sender().getServer(), new PlayerSkinResponsePacket(packet.getUuid(), skinData.skin, skinData.signature));
return;
}
String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + packet.getUuid().toString().replace("-", "") + "?unsigned=false";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestProperty("User-Agent", "SkinFetcher");
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return;
}
InputStream is = connection.getInputStream();
String json = new BufferedReader(new InputStreamReader(is))
.lines().collect(Collectors.joining("\n"));
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
JsonArray properties = obj.getAsJsonArray("properties");
for (JsonElement propElement : properties) {
JsonObject prop = propElement.getAsJsonObject();
if (prop.get("name").getAsString().equals("textures")) {
String skin = prop.get("value").getAsString();
String signature = prop.get("signature").getAsString();
skins.put(packet.getUuid(), new SkinData(skin, signature));
NetworkSender.send(((ServerMetaInfo) packet.getMetaInfos()).sender().getServer(), new PlayerSkinResponsePacket(packet.getUuid(), skin, signature));
return;
}
}
}
}
@@ -105,14 +107,17 @@ public class PlayerSkinHandler extends PacketHandler {
GameProfile gameProfile = player.getGameProfile();
GameProfile.Property property = gameProfile.getProperties().stream().filter(p -> p.getName().equals("textures")).findFirst().orElse(null);
if (property == null) return;
skins.put(player.getUniqueId(), new SkinData(property.getValue(), property.getSignature()));
Set<UUID> uuidSet = skins.keySet();
VelocityCore.getProxy().getAllServers().forEach(server -> {
for (UUID uuid : uuidSet) {
NetworkSender.send(server, new PlayerSkinResponsePacket(uuid, property.getValue(), property.getSignature()));
}
});
synchronized (skins) {
skins.put(player.getUniqueId(), new SkinData(property.getValue(), property.getSignature()));
Set<UUID> uuidSet = skins.keySet();
VelocityCore.getProxy().getAllServers().forEach(server -> {
for (UUID uuid : uuidSet) {
NetworkSender.send(server, new PlayerSkinResponsePacket(uuid, property.getValue(), property.getSignature()));
}
});
}
}
public record SkinData(String skin, String signature) {}