| 1 | package net.minecraft.client.gui.components.toasts;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.UUID;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.Font;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.components.PlayerFaceExtractor;
|
| 11 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 12 | import net.minecraft.client.renderer.RenderPipelines;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.util.FormattedCharSequence;
|
| 16 | import net.minecraft.world.item.component.ResolvableProfile;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class FriendToast implements Toast {
|
| 21 | private static final WidgetSprites BACKGROUND_SPRITE = new WidgetSprites(Identifier.withDefaultNamespace("friends/toast_background"));
|
| 22 | private static final int FACE_SIZE = 20;
|
| 23 | private static final int TEXT_LEFT_WITH_FACE = 30;
|
| 24 | private static final int TEXT_LEFT_NO_FACE = 7;
|
| 25 | private static final int PADDING_TOP = 7;
|
| 26 | private static final int PADDING_BOTTOM = 3;
|
| 27 | private static final int LINE_SPACING = 11;
|
| 28 | private static final long DEFAULT_DISPLAY_TIME_MS = 5000L;
|
| 29 | private final @Nullable ResolvableProfile skinProfile;
|
| 30 | private final List<FormattedCharSequence> messageLines;
|
| 31 | private final long displayTimeMs;
|
| 32 | private Toast.Visibility visibility = Toast.Visibility.SHOW;
|
| 33 |
|
| 34 | public FriendToast(final Font font, final @Nullable ResolvableProfile skinProfile, final Component message) {
|
| 35 | this(font, skinProfile, message, 5000L);
|
| 36 | }
|
| 37 |
|
| 38 | public FriendToast(final Font font, final @Nullable ResolvableProfile skinProfile, final Component message, final long displayTimeMs) {
|
| 39 | this.skinProfile = skinProfile;
|
| 40 | int textLeft = skinProfile != null ? 30 : 7;
|
| 41 | this.messageLines = font.split(message, 160 - textLeft - 4);
|
| 42 | this.displayTimeMs = displayTimeMs;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public Toast.Visibility getWantedVisibility() {
|
| 47 | return this.visibility;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void update(final ToastManager manager, final long fullyVisibleForMs) {
|
| 52 | if (fullyVisibleForMs >= this.displayTimeMs * manager.getNotificationDisplayTimeMultiplier()) {
|
| 53 | this.visibility = Toast.Visibility.HIDE;
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public int height() {
|
| 59 | return 7 + this.contentHeight() + 3;
|
| 60 | }
|
| 61 |
|
| 62 | private int contentHeight() {
|
| 63 | return Math.max(this.messageLines.size(), 2) * 11;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public void extractRenderState(final GuiGraphicsExtractor graphics, final Font font, final long fullyVisibleForMs) {
|
| 68 | int height = this.height();
|
| 69 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BACKGROUND_SPRITE.get(true, false), 0, 0, this.width(), height);
|
| 70 | int textLeft;
|
| 71 | if (this.skinProfile != null) {
|
| 72 | PlayerFaceExtractor.extractRenderState(graphics, this.skinProfile, 6, 6, 20);
|
| 73 | textLeft = 30;
|
| 74 | } else {
|
| 75 | textLeft = 7;
|
| 76 | }
|
| 77 |
|
| 78 | int totalTextHeight = this.messageLines.size() * 11;
|
| 79 | int textTop = 7 + (this.contentHeight() - totalTextHeight) / 2;
|
| 80 |
|
| 81 | for (int i = 0; i < this.messageLines.size(); i++) {
|
| 82 | graphics.text(font, this.messageLines.get(i), textLeft, textTop + i * 11, -1, false);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public void hide() {
|
| 87 | this.visibility = Toast.Visibility.HIDE;
|
| 88 | }
|
| 89 |
|
| 90 | public static void add(final ToastManager toastManager, final Font font, final @Nullable ResolvableProfile skinProfile, final Component message) {
|
| 91 | toastManager.addToast(new FriendToast(font, skinProfile, message));
|
| 92 | }
|
| 93 |
|
| 94 | private static void add(final Minecraft minecraft, final @Nullable ResolvableProfile skinProfile, final Component message) {
|
| 95 | add(minecraft.gui.toastManager(), minecraft.font, skinProfile, message);
|
| 96 | }
|
| 97 |
|
| 98 | private static void addWithSkin(final Minecraft minecraft, final UUID playerId, final Component message) {
|
| 99 | ResolvableProfile skinProfile = ResolvableProfile.createUnresolved(playerId);
|
| 100 | add(minecraft, skinProfile, message);
|
| 101 | }
|
| 102 |
|
| 103 | public static void showFriendRequestSent(final Minecraft minecraft, final String nickname) {
|
| 104 | add(minecraft, null, Component.translatable("gui.friends.toast.request_sent.message", nickname));
|
| 105 | }
|
| 106 |
|
| 107 | public static void showFriendRequestReceived(final Minecraft minecraft, final String nickname, final UUID playerId) {
|
| 108 | addWithSkin(minecraft, playerId, Component.translatable("gui.friends.toast.request_received.message", nickname));
|
| 109 | }
|
| 110 |
|
| 111 | public static void showFriendRequestAccepted(final Minecraft minecraft, final String nickname, final UUID playerId) {
|
| 112 | addWithSkin(minecraft, playerId, Component.translatable("gui.friends.toast.request_accepted.message", nickname));
|
| 113 | }
|
| 114 |
|
| 115 | public static void showFriendAdded(final Minecraft minecraft, final String nickname, final UUID playerId) {
|
| 116 | addWithSkin(minecraft, playerId, Component.translatable("gui.friends.toast.friend_added.message", nickname));
|
| 117 | }
|
| 118 |
|
| 119 | @FunctionalInterface
|
| 120 | @Environment(EnvType.CLIENT)
|
| 121 | public interface SkinToastEmitter {
|
| 122 | void emit(Minecraft minecraft, String playerName, UUID playerId);
|
| 123 | }
|
| 124 | }
|
| 125 |
|