| 1 | package com.mojang.realmsclient.util;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.client.RealmsClient;
|
| 5 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 6 | import java.time.Instant;
|
| 7 | import java.util.UUID;
|
| 8 | import java.util.concurrent.CompletableFuture;
|
| 9 | import java.util.function.Consumer;
|
| 10 | import java.util.function.Function;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.client.Minecraft;
|
| 14 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 15 | import net.minecraft.client.gui.components.PlayerFaceExtractor;
|
| 16 | import net.minecraft.client.gui.screens.Screen;
|
| 17 | import net.minecraft.network.chat.Component;
|
| 18 | import net.minecraft.util.Util;
|
| 19 | import net.minecraft.world.item.component.ResolvableProfile;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class RealmsUtil {
|
| 25 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 26 | private static final Component RIGHT_NOW = Component.translatable("mco.util.time.now");
|
| 27 | private static final int MINUTES = 60;
|
| 28 | private static final int HOURS = 3600;
|
| 29 | private static final int DAYS = 86400;
|
| 30 |
|
| 31 | public static Component convertToAgePresentation(final long timeDiff) {
|
| 32 | if (timeDiff < 0L) {
|
| 33 | return RIGHT_NOW;
|
| 34 | } else {
|
| 35 | long timeDiffInSeconds = timeDiff / 1000L;
|
| 36 | if (timeDiffInSeconds < 60L) {
|
| 37 | return Component.translatable("mco.time.secondsAgo", timeDiffInSeconds);
|
| 38 | } else if (timeDiffInSeconds < 3600L) {
|
| 39 | long minutes = timeDiffInSeconds / 60L;
|
| 40 | return Component.translatable("mco.time.minutesAgo", minutes);
|
| 41 | } else if (timeDiffInSeconds < 86400L) {
|
| 42 | long hours = timeDiffInSeconds / 3600L;
|
| 43 | return Component.translatable("mco.time.hoursAgo", hours);
|
| 44 | } else {
|
| 45 | long days = timeDiffInSeconds / 86400L;
|
| 46 | return Component.translatable("mco.time.daysAgo", days);
|
| 47 | }
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | public static Component convertToAgePresentationFromInstant(final Instant date) {
|
| 52 | return convertToAgePresentation(System.currentTimeMillis() - date.toEpochMilli());
|
| 53 | }
|
| 54 |
|
| 55 | public static void extractPlayerFace(final GuiGraphicsExtractor graphics, final int x, final int y, final int size, final UUID playerId) {
|
| 56 | PlayerFaceExtractor.extractRenderState(graphics, ResolvableProfile.createUnresolved(playerId), x, y, size);
|
| 57 | }
|
| 58 |
|
| 59 | public static <T> CompletableFuture<T> supplyAsync(final RealmsUtil.RealmsIoFunction<T> function, final @Nullable Consumer<RealmsServiceException> onFailure) {
|
| 60 | return CompletableFuture.supplyAsync(() -> {
|
| 61 | RealmsClient client = RealmsClient.getOrCreate();
|
| 62 |
|
| 63 | try {
|
| 64 | return function.apply(client);
|
| 65 | } catch (Throwable t) {
|
| 66 | if (t instanceof RealmsServiceException e) {
|
| 67 | if (onFailure != null) {
|
| 68 | onFailure.accept(e);
|
| 69 | }
|
| 70 | } else {
|
| 71 | LOGGER.error("Unhandled exception", t);
|
| 72 | }
|
| 73 |
|
| 74 | throw new RuntimeException(t);
|
| 75 | }
|
| 76 | }, Util.nonCriticalIoPool());
|
| 77 | }
|
| 78 |
|
| 79 | public static CompletableFuture<Void> runAsync(final RealmsUtil.RealmsIoConsumer function, final @Nullable Consumer<RealmsServiceException> onFailure) {
|
| 80 | return supplyAsync(function, onFailure);
|
| 81 | }
|
| 82 |
|
| 83 | public static Consumer<RealmsServiceException> openScreenOnFailure(final Function<RealmsServiceException, Screen> errorScreen) {
|
| 84 | Minecraft minecraft = Minecraft.getInstance();
|
| 85 | return e -> minecraft.execute(() -> minecraft.gui.setScreen(errorScreen.apply(e)));
|
| 86 | }
|
| 87 |
|
| 88 | public static Consumer<RealmsServiceException> openScreenAndLogOnFailure(final Function<RealmsServiceException, Screen> errorScreen, final String errorMessage) {
|
| 89 | return openScreenOnFailure(errorScreen).andThen(e -> LOGGER.error("{}", errorMessage, e));
|
| 90 | }
|
| 91 |
|
| 92 | @FunctionalInterface
|
| 93 | @Environment(EnvType.CLIENT)
|
| 94 | public interface RealmsIoConsumer extends RealmsUtil.RealmsIoFunction<Void> {
|
| 95 | void accept(final RealmsClient client) throws RealmsServiceException;
|
| 96 |
|
| 97 | default Void apply(final RealmsClient client) throws RealmsServiceException {
|
| 98 | this.accept(client);
|
| 99 | return null;
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | @FunctionalInterface
|
| 104 | @Environment(EnvType.CLIENT)
|
| 105 | public interface RealmsIoFunction<T> {
|
| 106 | T apply(final RealmsClient client) throws RealmsServiceException;
|
| 107 | }
|
| 108 | }
|
| 109 |
|