From 50bba3eaa104500a95794f2c73b6f4d2066c8522 Mon Sep 17 00:00:00 2001 From: "Josiah (Gaming32) Glosson" Date: Sun, 21 Jan 2024 14:58:58 -0600 Subject: [PATCH] Fix a bug where a friend's icon wouldn't show up in the "went online" toast --- .../github/gaming32/worldhost/WorldHost.java | 38 +++++++++++++------ .../worldhost/gui/screen/AddFriendScreen.java | 2 +- .../worldhost/gui/screen/FriendsScreen.java | 2 +- .../gui/screen/OnlineFriendsScreen.java | 2 +- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/gaming32/worldhost/WorldHost.java b/src/main/java/io/github/gaming32/worldhost/WorldHost.java index bfa9e91..55406b0 100644 --- a/src/main/java/io/github/gaming32/worldhost/WorldHost.java +++ b/src/main/java/io/github/gaming32/worldhost/WorldHost.java @@ -26,6 +26,8 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.multiplayer.ServerData; import net.minecraft.client.multiplayer.resolver.ServerAddress; +import net.minecraft.client.resources.DefaultPlayerSkin; +import net.minecraft.client.resources.PlayerSkin; import net.minecraft.client.resources.SkinManager; import net.minecraft.client.server.IntegratedServer; import net.minecraft.commands.CommandSourceStack; @@ -48,6 +50,7 @@ import java.nio.file.Path; import java.security.SecureRandom; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; import java.util.function.Consumer; import java.util.function.Supplier; @@ -492,21 +495,33 @@ public static GameProfileCache getProfileCache() { return profileCache; } - public static ResourceLocation getInsecureSkinLocation(GameProfile gameProfile) { + public static CompletableFuture getInsecureSkinLocation(GameProfile gameProfile) { final SkinManager skinManager = Minecraft.getInstance().getSkinManager(); //#if MC >= 1.20.2 - return skinManager.getInsecureSkin(gameProfile).texture(); - //#elseif MC >= 1.19.2 - //$$ return skinManager.getInsecureSkinLocation(gameProfile); + return skinManager.getOrLoad(gameProfile).thenApply(PlayerSkin::texture); //#else - //$$ final MinecraftProfileTexture texture = skinManager.getInsecureSkinInformation(gameProfile) - //$$ .get(MinecraftProfileTexture.Type.SKIN); - //$$ return texture != null - //$$ ? skinManager.registerTexture(texture, MinecraftProfileTexture.Type.SKIN) - //$$ : DefaultPlayerSkin.getDefaultSkin(Player.createPlayerUUID(gameProfile)); + //$$ return CompletableFuture.supplyAsync(() -> { + //$$ //#if MC >= 1.19.2 + //$$ //$$ return skinManager.getInsecureSkinLocation(gameProfile); + //$$ //#else + //$$ //$$ final MinecraftProfileTexture texture = skinManager.getInsecureSkinInformation(gameProfile) + //$$ //$$ .get(MinecraftProfileTexture.Type.SKIN); + //$$ //$$ return texture != null + //$$ //$$ ? skinManager.registerTexture(texture, MinecraftProfileTexture.Type.SKIN) + //$$ //$$ : DefaultPlayerSkin.getDefaultSkin(Player.createPlayerUUID(gameProfile)); + //$$ //#endif + //$$ }, Util.backgroundExecutor()); //#endif } + public static ResourceLocation getSkinLocationNow(GameProfile gameProfile) { + final ResourceLocation location = getInsecureSkinLocation(gameProfile).getNow(null); + if (location == null) { + return DefaultPlayerSkin.get(gameProfile).texture(); + } + return location; + } + public static void getMaybeAsync(GameProfileCache cache, String name, Consumer> action) { //#if MC >= 1.20.2 cache.getAsync(name).thenAccept(action); @@ -563,8 +578,7 @@ public static boolean isFriend(UUID user) { public static void showFriendOrOnlineToast(UUID user, String title, String description, int ticks, Runnable clickAction) { Util.backgroundExecutor().execute(() -> { final GameProfile profile = fetchProfile(Minecraft.getInstance().getMinecraftSessionService(), user); - Minecraft.getInstance().execute(() -> { - final ResourceLocation skinTexture = getInsecureSkinLocation(profile); + getInsecureSkinLocation(profile).thenAcceptAsync(skinTexture -> { WHToast.builder(Components.translatable(title, getName(profile))) .description(Components.translatable(description)) .icon((context, x, y, width, height) -> { @@ -576,7 +590,7 @@ public static void showFriendOrOnlineToast(UUID user, String title, String descr .ticks(ticks) .important() .show(); - }); + }, Minecraft.getInstance()); }); } diff --git a/src/main/java/io/github/gaming32/worldhost/gui/screen/AddFriendScreen.java b/src/main/java/io/github/gaming32/worldhost/gui/screen/AddFriendScreen.java index 81756b9..eea095e 100644 --- a/src/main/java/io/github/gaming32/worldhost/gui/screen/AddFriendScreen.java +++ b/src/main/java/io/github/gaming32/worldhost/gui/screen/AddFriendScreen.java @@ -173,7 +173,7 @@ public void render( if (friendProfile != null) { assert minecraft != null; - final ResourceLocation skinTexture = WorldHost.getInsecureSkinLocation(friendProfile); + final ResourceLocation skinTexture = WorldHost.getSkinLocationNow(friendProfile); WorldHost.color(1f, 1f, 1f, 1f); RenderSystem.enableBlend(); final int size = addFriendButton.getY() - 110; diff --git a/src/main/java/io/github/gaming32/worldhost/gui/screen/FriendsScreen.java b/src/main/java/io/github/gaming32/worldhost/gui/screen/FriendsScreen.java index 56f39f3..6afd64e 100644 --- a/src/main/java/io/github/gaming32/worldhost/gui/screen/FriendsScreen.java +++ b/src/main/java/io/github/gaming32/worldhost/gui/screen/FriendsScreen.java @@ -258,7 +258,7 @@ public void render( //#endif int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta ) { - final ResourceLocation skinTexture = WorldHost.getInsecureSkinLocation(profile); + final ResourceLocation skinTexture = WorldHost.getSkinLocationNow(profile); WorldHost.color(1f, 1f, 1f, 1f); RenderSystem.enableBlend(); blit(context, skinTexture, x, y, 32, 32, 8, 8, 8, 8, 64, 64); diff --git a/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java b/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java index e07c35d..1199b1a 100644 --- a/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java +++ b/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java @@ -375,7 +375,7 @@ public void render( // Since when does a value marked as @Nullable never satisfy == null? //noinspection ConstantValue if (icon == null) { - final ResourceLocation skinTexture = WorldHost.getInsecureSkinLocation(profile); + final ResourceLocation skinTexture = WorldHost.getSkinLocationNow(profile); RenderSystem.enableBlend(); blit(context, skinTexture, x, y, 32, 32, 8, 8, 8, 8, 64, 64); blit(context, skinTexture, x, y, 32, 32, 40, 8, 8, 8, 64, 64);