package com.mojang.realmsclient.util; import com.mojang.authlib.yggdrasil.ProfileResult; import com.mojang.logging.LogUtils; import com.mojang.realmsclient.client.RealmsClient; import com.mojang.realmsclient.exception.RealmsServiceException; import java.util.Date; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.Util; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.PlayerFaceRenderer; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.resources.DefaultPlayerSkin; import net.minecraft.client.resources.PlayerSkin; import net.minecraft.network.chat.Component; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @Environment(EnvType.CLIENT) public class RealmsUtil { private static final Logger LOGGER = LogUtils.getLogger(); private static final Component RIGHT_NOW = Component.translatable("mco.util.time.now"); private static final int MINUTES = 60; private static final int HOURS = 3600; private static final int DAYS = 86400; public static Component convertToAgePresentation(long millis) { if (millis < 0L) { return RIGHT_NOW; } else { long l = millis / 1000L; if (l < 60L) { return Component.translatable("mco.time.secondsAgo", l); } else if (l < 3600L) { long m = l / 60L; return Component.translatable("mco.time.minutesAgo", m); } else if (l < 86400L) { long m = l / 3600L; return Component.translatable("mco.time.hoursAgo", m); } else { long m = l / 86400L; return Component.translatable("mco.time.daysAgo", m); } } } public static Component convertToAgePresentationFromInstant(Date date) { return convertToAgePresentation(System.currentTimeMillis() - date.getTime()); } public static void renderPlayerFace(GuiGraphics guiGraphics, int x, int y, int size, UUID playerUuid) { Minecraft minecraft = Minecraft.getInstance(); ProfileResult profileResult = minecraft.getMinecraftSessionService().fetchProfile(playerUuid, false); PlayerSkin playerSkin = profileResult != null ? minecraft.getSkinManager().getInsecureSkin(profileResult.profile()) : DefaultPlayerSkin.get(playerUuid); PlayerFaceRenderer.draw(guiGraphics, playerSkin, x, y, size); } public static CompletableFuture supplyAsync(RealmsUtil.RealmsIoFunction action, @Nullable Consumer onError) { return CompletableFuture.supplyAsync(() -> { RealmsClient realmsClient = RealmsClient.getOrCreate(); try { return action.apply(realmsClient); } catch (Throwable var5) { if (var5 instanceof RealmsServiceException realmsServiceException) { if (onError != null) { onError.accept(realmsServiceException); } } else { LOGGER.error("Unhandled exception", var5); } throw new RuntimeException(var5); } }, Util.nonCriticalIoPool()); } public static CompletableFuture runAsync(RealmsUtil.RealmsIoConsumer action, @Nullable Consumer onError) { return supplyAsync(action, onError); } public static Consumer openScreenOnFailure(Function screenSupplier) { Minecraft minecraft = Minecraft.getInstance(); return realmsServiceException -> minecraft.execute(() -> minecraft.setScreen((Screen)screenSupplier.apply(realmsServiceException))); } public static Consumer openScreenAndLogOnFailure(Function screenSupplier, String errorMessage) { return openScreenOnFailure(screenSupplier).andThen(realmsServiceException -> LOGGER.error(errorMessage, (Throwable)realmsServiceException)); } @FunctionalInterface @Environment(EnvType.CLIENT) public interface RealmsIoConsumer extends RealmsUtil.RealmsIoFunction { void accept(RealmsClient realmsClient) throws RealmsServiceException; default Void apply(RealmsClient realmsClient) throws RealmsServiceException { this.accept(realmsClient); return null; } } @FunctionalInterface @Environment(EnvType.CLIENT) public interface RealmsIoFunction { T apply(RealmsClient realmsClient) throws RealmsServiceException; } }