Skip to content

Commit

Permalink
Fix a bug where a friend's icon wouldn't show up in the "went online"…
Browse files Browse the repository at this point in the history
… toast
  • Loading branch information
Gaming32 committed Jan 21, 2024
1 parent 1eebfd0 commit 50bba3e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
38 changes: 26 additions & 12 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -492,21 +495,33 @@ public static GameProfileCache getProfileCache() {
return profileCache;
}

public static ResourceLocation getInsecureSkinLocation(GameProfile gameProfile) {
public static CompletableFuture<ResourceLocation> 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<Optional<GameProfile>> action) {
//#if MC >= 1.20.2
cache.getAsync(name).thenAccept(action);
Expand Down Expand Up @@ -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) -> {
Expand All @@ -576,7 +590,7 @@ public static void showFriendOrOnlineToast(UUID user, String title, String descr
.ticks(ticks)
.important()
.show();
});
}, Minecraft.getInstance());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 50bba3e

Please sign in to comment.