diff --git a/VelocityCore/src/de/steamwar/velocitycore/network/handlers/PlayerSkinHandler.java b/VelocityCore/src/de/steamwar/velocitycore/network/handlers/PlayerSkinHandler.java index f33b0b9b..ff18d309 100644 --- a/VelocityCore/src/de/steamwar/velocitycore/network/handlers/PlayerSkinHandler.java +++ b/VelocityCore/src/de/steamwar/velocitycore/network/handlers/PlayerSkinHandler.java @@ -54,7 +54,7 @@ public class PlayerSkinHandler extends PacketHandler { VelocityCore.getProxy().getEventManager().register(VelocityCore.get(), this); } - private Map skins = new LinkedHashMap<>(maxCacheSize, 1, true) { + private final Map skins = new LinkedHashMap<>(maxCacheSize, 1, true) { @Override protected boolean removeEldestEntry(Map.Entry 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 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 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) {}