From d2843152864523b355b782ad001b27af32ba7f1a Mon Sep 17 00:00:00 2001 From: OliverSchlueter Date: Mon, 12 Aug 2024 17:10:07 +0200 Subject: [PATCH] async --- .../fancynpcs/api/utils/SkinFetcher.java | 142 ++++++++++-------- .../oliver/fancynpcs/v1_19_4/Npc_1_19_4.java | 10 +- .../oliver/fancynpcs/v1_20_1/Npc_1_20_1.java | 10 +- .../oliver/fancynpcs/v1_20_2/Npc_1_20_2.java | 10 +- .../oliver/fancynpcs/v1_20_4/Npc_1_20_4.java | 10 +- .../oliver/fancynpcs/v1_20_6/Npc_1_20_6.java | 10 +- .../de/oliver/fancynpcs/v1_21/Npc_1_21.java | 10 +- 7 files changed, 111 insertions(+), 91 deletions(-) diff --git a/api/src/main/java/de/oliver/fancynpcs/api/utils/SkinFetcher.java b/api/src/main/java/de/oliver/fancynpcs/api/utils/SkinFetcher.java index 4d1ed480..60745543 100644 --- a/api/src/main/java/de/oliver/fancynpcs/api/utils/SkinFetcher.java +++ b/api/src/main/java/de/oliver/fancynpcs/api/utils/SkinFetcher.java @@ -1,6 +1,5 @@ package de.oliver.fancynpcs.api.utils; - import com.google.gson.JsonObject; import com.google.gson.JsonParser; import de.oliver.fancylib.UUIDFetcher; @@ -20,6 +19,7 @@ import java.util.Map; import java.util.Scanner; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; public final class SkinFetcher { @@ -29,81 +29,101 @@ private SkinFetcher() { } /** - * Fetches the skin data from the Mojang API. + * Fetches the skin data from the Mojang API asynchronously. * * @param identifier The identifier of the skin. This can be a UUID, username, URL or a placeholder by PAPI. - * @throws IOException If the skin data could not be fetched. + * @return A CompletableFuture that will contain the SkinData. */ - public static SkinData fetchSkin(String identifier) throws IOException { - String parsedIdentifier = ChatColorHandler.translate(identifier, List.of(PlaceholderAPIParser.class)); + public static CompletableFuture fetchSkin(String identifier) { + return CompletableFuture.supplyAsync(() -> { + String parsedIdentifier = ChatColorHandler.translate(identifier, List.of(PlaceholderAPIParser.class)); - if (skinCache.containsKey(parsedIdentifier)) { - return skinCache.get(parsedIdentifier); - } + if (skinCache.containsKey(parsedIdentifier)) { + return skinCache.get(parsedIdentifier); + } - if (isURL(parsedIdentifier)) { - return fetchSkinByURL(parsedIdentifier); - } + if (isURL(parsedIdentifier)) { + return fetchSkinByURL(parsedIdentifier).join(); + } - if (isUUID(parsedIdentifier)) { - return fetchSkinByUUID(parsedIdentifier); - } + if (isUUID(parsedIdentifier)) { + return fetchSkinByUUID(parsedIdentifier).join(); + } - // assume it's a username - UUID uuid = UUIDFetcher.getUUID(parsedIdentifier); - if (uuid != null) { - return fetchSkinByUUID(uuid.toString()); - } + // assume it's a username + UUID uuid = UUIDFetcher.getUUID(parsedIdentifier); + if (uuid != null) { + return fetchSkinByUUID(uuid.toString()).join(); + } - return null; + return null; + }); } /** - * Fetches the skin data from the Mojang API. + * Fetches the skin data from the Mojang API by UUID asynchronously. * + * @param uuid The UUID of the player. + * @return A CompletableFuture that will contain the SkinData. * @throws IOException If the skin data could not be fetched. */ - public static SkinData fetchSkinByUUID(String uuid) throws IOException { - URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false"); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - - String json = new Scanner(conn.getInputStream(), StandardCharsets.UTF_8).useDelimiter("\\A").next(); - JsonParser parser = new JsonParser(); - JsonObject obj = parser.parse(json).getAsJsonObject(); - - String value = obj.getAsJsonArray("properties").get(0).getAsJsonObject().getAsJsonPrimitive("value").getAsString(); - String signature = obj.getAsJsonArray("properties").get(0).getAsJsonObject().getAsJsonPrimitive("signature").getAsString(); - SkinData skinData = new SkinData(uuid, value, signature); - - skinCache.put(uuid, skinData); - return skinData; + public static CompletableFuture fetchSkinByUUID(String uuid) { + return CompletableFuture.supplyAsync(() -> { + try { + URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + + String json = new Scanner(conn.getInputStream(), StandardCharsets.UTF_8).useDelimiter("\\A").next(); + JsonParser parser = new JsonParser(); + JsonObject obj = parser.parse(json).getAsJsonObject(); + + String value = obj.getAsJsonArray("properties").get(0).getAsJsonObject().getAsJsonPrimitive("value").getAsString(); + String signature = obj.getAsJsonArray("properties").get(0).getAsJsonObject().getAsJsonPrimitive("signature").getAsString(); + SkinData skinData = new SkinData(uuid, value, signature); + + skinCache.put(uuid, skinData); + return skinData; + } catch (IOException e) { + FancyNpcsPlugin.get().getPlugin().getLogger().warning("Failed to fetch skin data for UUID " + uuid); + return null; + } + }); } /** - * Fetches the skin data from the Mojang API. + * Fetches the skin data from the Mojang API by URL asynchronously. * + * @param skinURL The URL of the skin. + * @return A CompletableFuture that will contain the SkinData. * @throws IOException If the skin data could not be fetched. */ - public static SkinData fetchSkinByURL(String skinURL) throws IOException { - URL url = new URL("https://api.mineskin.org/generate/url"); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - conn.setDoOutput(true); - DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream()); - outputStream.writeBytes("url=" + URLEncoder.encode(skinURL, StandardCharsets.UTF_8)); - outputStream.close(); - - String json = new Scanner(conn.getInputStream(), StandardCharsets.UTF_8).useDelimiter("\\A").next(); - JsonParser parser = new JsonParser(); - JsonObject obj = parser.parse(json).getAsJsonObject(); - - String value = obj.getAsJsonObject("data").getAsJsonObject("texture").getAsJsonPrimitive("value").getAsString(); - String signature = obj.getAsJsonObject("data").getAsJsonObject("texture").getAsJsonPrimitive("signature").getAsString(); - SkinData skinData = new SkinData(skinURL, value, signature); - - skinCache.put(skinURL, skinData); - return skinData; + public static CompletableFuture fetchSkinByURL(String skinURL) { + return CompletableFuture.supplyAsync(() -> { + try { + URL url = new URL("https://api.mineskin.org/generate/url"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + conn.setDoOutput(true); + DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream()); + outputStream.writeBytes("url=" + URLEncoder.encode(skinURL, StandardCharsets.UTF_8)); + outputStream.close(); + + String json = new Scanner(conn.getInputStream(), StandardCharsets.UTF_8).useDelimiter("\\A").next(); + JsonParser parser = new JsonParser(); + JsonObject obj = parser.parse(json).getAsJsonObject(); + + String value = obj.getAsJsonObject("data").getAsJsonObject("texture").getAsJsonPrimitive("value").getAsString(); + String signature = obj.getAsJsonObject("data").getAsJsonObject("texture").getAsJsonPrimitive("signature").getAsString(); + SkinData skinData = new SkinData(skinURL, value, signature); + + skinCache.put(skinURL, skinData); + return skinData; + } catch (IOException e) { + FancyNpcsPlugin.get().getPlugin().getLogger().warning("Failed to fetch skin data for URL " + skinURL); + return null; + } + }); } private static boolean isURL(String identifier) { @@ -136,9 +156,9 @@ public record SkinData(@NotNull String identifier, @Nullable String value, @Null public String value() { if (value == null || value.isEmpty()) { try { - SkinData skinData = fetchSkin(identifier); + SkinData skinData = fetchSkin(identifier).join(); return skinData == null ? null : skinData.value(); - } catch (IOException e) { + } catch (Exception e) { FancyNpcsPlugin.get().getPlugin().getLogger().warning("Failed to fetch skin data for " + identifier); } } @@ -155,9 +175,9 @@ public String value() { public String signature() { if (signature == null || signature.isEmpty()) { try { - SkinData skinData = fetchSkin(identifier); + SkinData skinData = fetchSkin(identifier).join(); return skinData == null ? null : skinData.signature(); - } catch (IOException e) { + } catch (Exception e) { FancyNpcsPlugin.get().getPlugin().getLogger().warning("Failed to fetch skin data for " + identifier); } } @@ -165,4 +185,4 @@ public String signature() { return signature; } } -} \ No newline at end of file +} diff --git a/implementation_1_19_4/src/main/java/de/oliver/fancynpcs/v1_19_4/Npc_1_19_4.java b/implementation_1_19_4/src/main/java/de/oliver/fancynpcs/v1_19_4/Npc_1_19_4.java index b51b96bd..f4d3297b 100644 --- a/implementation_1_19_4/src/main/java/de/oliver/fancynpcs/v1_19_4/Npc_1_19_4.java +++ b/implementation_1_19_4/src/main/java/de/oliver/fancynpcs/v1_19_4/Npc_1_19_4.java @@ -66,11 +66,6 @@ public void create() { if (data.getType() == org.bukkit.entity.EntityType.PLAYER) { npc = new ServerPlayer(minecraftServer, serverLevel, new GameProfile(uuid, "")); ((ServerPlayer) npc).gameProfile = gameProfile; - - if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { - // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false - ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); - } } else { EntityType nmsType = BuiltInRegistries.ENTITY_TYPE.get(CraftNamespacedKey.toMinecraft(data.getType().getKey())); EntityType.EntityFactory factory = (EntityType.EntityFactory) ReflectionUtils.getValue(nmsType, MappingKeys1_19_4.ENTITY_TYPE__FACTORY.getMapping()); // EntityType.factory @@ -90,6 +85,11 @@ public void spawn(Player player) { return; } + if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { + // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false + ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); + } + NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player); spawnEvent.callEvent(); if (spawnEvent.isCancelled()) { diff --git a/implementation_1_20_1/src/main/java/de/oliver/fancynpcs/v1_20_1/Npc_1_20_1.java b/implementation_1_20_1/src/main/java/de/oliver/fancynpcs/v1_20_1/Npc_1_20_1.java index b0a11504..06a91dc2 100644 --- a/implementation_1_20_1/src/main/java/de/oliver/fancynpcs/v1_20_1/Npc_1_20_1.java +++ b/implementation_1_20_1/src/main/java/de/oliver/fancynpcs/v1_20_1/Npc_1_20_1.java @@ -67,11 +67,6 @@ public void create() { if (data.getType() == org.bukkit.entity.EntityType.PLAYER) { npc = new ServerPlayer(minecraftServer, serverLevel, new GameProfile(uuid, "")); ((ServerPlayer) npc).gameProfile = gameProfile; - - if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { - // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false - ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); - } } else { EntityType nmsType = BuiltInRegistries.ENTITY_TYPE.get(CraftNamespacedKey.toMinecraft(data.getType().getKey())); EntityType.EntityFactory factory = (EntityType.EntityFactory) ReflectionUtils.getValue(nmsType, MappingKeys1_20_1.ENTITY_TYPE__FACTORY.getMapping()); // EntityType.factory @@ -91,6 +86,11 @@ public void spawn(Player player) { return; } + if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { + // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false + ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); + } + NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player); spawnEvent.callEvent(); if (spawnEvent.isCancelled()) { diff --git a/implementation_1_20_2/src/main/java/de/oliver/fancynpcs/v1_20_2/Npc_1_20_2.java b/implementation_1_20_2/src/main/java/de/oliver/fancynpcs/v1_20_2/Npc_1_20_2.java index 922b5cd6..2fcb1a85 100644 --- a/implementation_1_20_2/src/main/java/de/oliver/fancynpcs/v1_20_2/Npc_1_20_2.java +++ b/implementation_1_20_2/src/main/java/de/oliver/fancynpcs/v1_20_2/Npc_1_20_2.java @@ -69,11 +69,6 @@ public void create() { if (data.getType() == org.bukkit.entity.EntityType.PLAYER) { npc = new ServerPlayer(minecraftServer, serverLevel, new GameProfile(uuid, ""), ClientInformation.createDefault()); ((ServerPlayer) npc).gameProfile = gameProfile; - - if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { - // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false - ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); - } } else { EntityType nmsType = BuiltInRegistries.ENTITY_TYPE.get(CraftNamespacedKey.toMinecraft(data.getType().getKey())); EntityType.EntityFactory factory = (EntityType.EntityFactory) ReflectionUtils.getValue(nmsType, MappingKeys1_20_2.ENTITY_TYPE__FACTORY.getMapping()); // EntityType.factory @@ -93,6 +88,11 @@ public void spawn(Player player) { return; } + if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { + // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false + ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); + } + NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player); spawnEvent.callEvent(); if (spawnEvent.isCancelled()) { diff --git a/implementation_1_20_4/src/main/java/de/oliver/fancynpcs/v1_20_4/Npc_1_20_4.java b/implementation_1_20_4/src/main/java/de/oliver/fancynpcs/v1_20_4/Npc_1_20_4.java index f4b57be3..aebdd48a 100644 --- a/implementation_1_20_4/src/main/java/de/oliver/fancynpcs/v1_20_4/Npc_1_20_4.java +++ b/implementation_1_20_4/src/main/java/de/oliver/fancynpcs/v1_20_4/Npc_1_20_4.java @@ -68,11 +68,6 @@ public void create() { if (data.getType() == org.bukkit.entity.EntityType.PLAYER) { npc = new ServerPlayer(minecraftServer, serverLevel, new GameProfile(uuid, ""), ClientInformation.createDefault()); ((ServerPlayer) npc).gameProfile = gameProfile; - - if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { - // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false - ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); - } } else { EntityType nmsType = BuiltInRegistries.ENTITY_TYPE.get(CraftNamespacedKey.toMinecraft(data.getType().getKey())); EntityType.EntityFactory factory = (EntityType.EntityFactory) ReflectionUtils.getValue(nmsType, MappingKeys1_20_4.ENTITY_TYPE__FACTORY.getMapping()); // EntityType.factory @@ -92,6 +87,11 @@ public void spawn(Player player) { return; } + if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { + // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false + ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); + } + NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player); spawnEvent.callEvent(); if (spawnEvent.isCancelled()) { diff --git a/implementation_1_20_6/src/main/java/de/oliver/fancynpcs/v1_20_6/Npc_1_20_6.java b/implementation_1_20_6/src/main/java/de/oliver/fancynpcs/v1_20_6/Npc_1_20_6.java index ffcdbb13..b655baae 100644 --- a/implementation_1_20_6/src/main/java/de/oliver/fancynpcs/v1_20_6/Npc_1_20_6.java +++ b/implementation_1_20_6/src/main/java/de/oliver/fancynpcs/v1_20_6/Npc_1_20_6.java @@ -71,11 +71,6 @@ public void create() { if (data.getType() == org.bukkit.entity.EntityType.PLAYER) { npc = new ServerPlayer(minecraftServer, serverLevel, new GameProfile(uuid, ""), ClientInformation.createDefault()); ((ServerPlayer) npc).gameProfile = gameProfile; - - if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { - // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false - ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); - } } else { EntityType nmsType = BuiltInRegistries.ENTITY_TYPE.get(CraftNamespacedKey.toMinecraft(data.getType().getKey())); EntityType.EntityFactory factory = (EntityType.EntityFactory) ReflectionUtils.getValue(nmsType, MappingKeys1_20_6.ENTITY_TYPE__FACTORY.getMapping()); // EntityType.factory @@ -95,6 +90,11 @@ public void spawn(Player player) { return; } + if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { + // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false + ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); + } + NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player); spawnEvent.callEvent(); if (spawnEvent.isCancelled()) { diff --git a/implementation_1_21/src/main/java/de/oliver/fancynpcs/v1_21/Npc_1_21.java b/implementation_1_21/src/main/java/de/oliver/fancynpcs/v1_21/Npc_1_21.java index fb2f0146..93d0a97b 100644 --- a/implementation_1_21/src/main/java/de/oliver/fancynpcs/v1_21/Npc_1_21.java +++ b/implementation_1_21/src/main/java/de/oliver/fancynpcs/v1_21/Npc_1_21.java @@ -69,11 +69,6 @@ public void create() { if (data.getType() == org.bukkit.entity.EntityType.PLAYER) { npc = new ServerPlayer(minecraftServer, serverLevel, new GameProfile(uuid, ""), ClientInformation.createDefault()); ((ServerPlayer) npc).gameProfile = gameProfile; - - if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { - // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false - ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); - } } else { EntityType nmsType = BuiltInRegistries.ENTITY_TYPE.get(CraftNamespacedKey.toMinecraft(data.getType().getKey())); EntityType.EntityFactory factory = (EntityType.EntityFactory) ReflectionUtils.getValue(nmsType, MappingKeys1_21.ENTITY_TYPE__FACTORY.getMapping()); // EntityType.factory @@ -93,6 +88,11 @@ public void spawn(Player player) { return; } + if (data.getSkin() != null && data.getSkin().value() != null && data.getSkin().signature() != null) { + // sessionserver.mojang.com/session/minecraft/profile/?unsigned=false + ((ServerPlayer) npc).getGameProfile().getProperties().replaceValues("textures", ImmutableList.of(new Property("textures", data.getSkin().value(), data.getSkin().signature()))); + } + NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player); spawnEvent.callEvent(); if (spawnEvent.isCancelled()) {