| 1 | package net.minecraft.client.gui.components.toasts;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.stream.Stream;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.ChatFormatting;
|
| 9 | import net.minecraft.client.Minecraft;
|
| 10 | import net.minecraft.client.gui.Font;
|
| 11 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 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.level.ChunkPos;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class SystemToast implements Toast {
|
| 21 | private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("toast/system");
|
| 22 | private static final int MAX_LINE_SIZE = 200;
|
| 23 | private static final int LINE_SPACING = 12;
|
| 24 | private static final int MARGIN = 10;
|
| 25 | private static final int TEXT_X_START = 18;
|
| 26 | private final SystemToast.SystemToastId id;
|
| 27 | private List<FormattedCharSequence> titleLines;
|
| 28 | private List<FormattedCharSequence> messageLines;
|
| 29 | private long lastChanged;
|
| 30 | private boolean changed;
|
| 31 | private int width;
|
| 32 | private boolean forceHide;
|
| 33 | private Toast.Visibility wantedVisibility = Toast.Visibility.HIDE;
|
| 34 |
|
| 35 | public SystemToast(final SystemToast.SystemToastId id, final Component title, final @Nullable Component message) {
|
| 36 | this.id = id;
|
| 37 | this.update(title, message);
|
| 38 | }
|
| 39 |
|
| 40 | private static List<FormattedCharSequence> nullToEmpty(final @Nullable Component text) {
|
| 41 | return text == null ? ImmutableList.of() : splitToLength(text);
|
| 42 | }
|
| 43 |
|
| 44 | private static List<FormattedCharSequence> splitToLength(final Component text) {
|
| 45 | return Minecraft.getInstance().font.split(text, 200);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public int width() {
|
| 50 | return this.width;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public int height() {
|
| 55 | int titleHeight = (this.titleLines.size() - 1) * 12;
|
| 56 | int messageHeight = Math.max(this.messageLines.size(), 1) * 12;
|
| 57 | return 20 + titleHeight + messageHeight;
|
| 58 | }
|
| 59 |
|
| 60 | public void forceHide() {
|
| 61 | this.forceHide = true;
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public Toast.Visibility getWantedVisibility() {
|
| 66 | return this.wantedVisibility;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void update(final ToastManager manager, final long fullyVisibleForMs) {
|
| 71 | if (this.changed) {
|
| 72 | this.lastChanged = fullyVisibleForMs;
|
| 73 | this.changed = false;
|
| 74 | }
|
| 75 |
|
| 76 | double timeToDisplayUpdate = this.id.displayTime * manager.getNotificationDisplayTimeMultiplier();
|
| 77 | long timeSinceUpdate = fullyVisibleForMs - this.lastChanged;
|
| 78 | this.wantedVisibility = !this.forceHide && timeSinceUpdate < timeToDisplayUpdate ? Toast.Visibility.SHOW : Toast.Visibility.HIDE;
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public void extractRenderState(final GuiGraphicsExtractor graphics, final Font font, final long fullyVisibleForMs) {
|
| 83 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BACKGROUND_SPRITE, 0, 0, this.width(), this.height());
|
| 84 | if (this.messageLines.isEmpty()) {
|
| 85 | this.extractTextLines(graphics, font, this.titleLines, 12, -256);
|
| 86 | } else {
|
| 87 | this.extractTextLines(graphics, font, this.titleLines, 7, -256);
|
| 88 | this.extractTextLines(graphics, font, this.messageLines, 7 + this.titleLines.size() * 12, -1);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | private void extractTextLines(
|
| 93 | final GuiGraphicsExtractor graphics, final Font font, final List<FormattedCharSequence> textLines, final int yStart, final int textColor
|
| 94 | ) {
|
| 95 | for (int i = 0; i < textLines.size(); i++) {
|
| 96 | graphics.text(font, textLines.get(i), 18, yStart + i * 12, textColor, false);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public void reset(final Component title, final @Nullable Component message) {
|
| 101 | this.update(title, message);
|
| 102 | this.changed = true;
|
| 103 | }
|
| 104 |
|
| 105 | private void update(final Component title, final @Nullable Component message) {
|
| 106 | this.titleLines = splitToLength(title);
|
| 107 | this.messageLines = nullToEmpty(message);
|
| 108 | this.recalculateWidth();
|
| 109 | }
|
| 110 |
|
| 111 | public void recalculateWidth() {
|
| 112 | int width = Math.max(160, Stream.concat(this.titleLines.stream(), this.messageLines.stream()).mapToInt(Minecraft.getInstance().font::width).max().orElse(200));
|
| 113 | this.width = width + 30;
|
| 114 | }
|
| 115 |
|
| 116 | public SystemToast.SystemToastId getToken() {
|
| 117 | return this.id;
|
| 118 | }
|
| 119 |
|
| 120 | public static void add(final ToastManager toastManager, final SystemToast.SystemToastId id, final Component title, final @Nullable Component message) {
|
| 121 | toastManager.addToast(new SystemToast(id, title, message));
|
| 122 | }
|
| 123 |
|
| 124 | public static void addOrUpdate(final ToastManager toastManager, final SystemToast.SystemToastId id, final Component title, final @Nullable Component message) {
|
| 125 | SystemToast toast = toastManager.getToast(SystemToast.class, id);
|
| 126 | if (toast == null) {
|
| 127 | add(toastManager, id, title, message);
|
| 128 | } else {
|
| 129 | toast.reset(title, message);
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | public static void forceHide(final ToastManager toastManager, final SystemToast.SystemToastId id) {
|
| 134 | SystemToast toast = toastManager.getToast(SystemToast.class, id);
|
| 135 | if (toast != null) {
|
| 136 | toast.forceHide();
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | public static void onWorldAccessFailure(final Minecraft minecraft, final String levelId) {
|
| 141 | add(
|
| 142 | minecraft.gui.toastManager(),
|
| 143 | SystemToast.SystemToastId.WORLD_ACCESS_FAILURE,
|
| 144 | Component.translatable("selectWorld.access_failure"),
|
| 145 | Component.literal(levelId)
|
| 146 | );
|
| 147 | }
|
| 148 |
|
| 149 | public static void onWorldDeleteFailure(final Minecraft minecraft, final String levelId) {
|
| 150 | add(
|
| 151 | minecraft.gui.toastManager(),
|
| 152 | SystemToast.SystemToastId.WORLD_ACCESS_FAILURE,
|
| 153 | Component.translatable("selectWorld.delete_failure"),
|
| 154 | Component.literal(levelId)
|
| 155 | );
|
| 156 | }
|
| 157 |
|
| 158 | public static void onPackCopyFailure(final Minecraft minecraft, final String extraInfo) {
|
| 159 | add(minecraft.gui.toastManager(), SystemToast.SystemToastId.PACK_COPY_FAILURE, Component.translatable("pack.copyFailure"), Component.literal(extraInfo));
|
| 160 | }
|
| 161 |
|
| 162 | public static void onFileDropFailure(final Minecraft minecraft, final int count) {
|
| 163 | add(
|
| 164 | minecraft.gui.toastManager(),
|
| 165 | SystemToast.SystemToastId.FILE_DROP_FAILURE,
|
| 166 | Component.translatable("gui.fileDropFailure.title"),
|
| 167 | Component.translatable("gui.fileDropFailure.detail", count)
|
| 168 | );
|
| 169 | }
|
| 170 |
|
| 171 | public static void onLowDiskSpace(final Minecraft minecraft) {
|
| 172 | addOrUpdate(
|
| 173 | minecraft.gui.toastManager(),
|
| 174 | SystemToast.SystemToastId.LOW_DISK_SPACE,
|
| 175 | Component.translatable("chunk.toast.lowDiskSpace"),
|
| 176 | Component.translatable("chunk.toast.lowDiskSpace.description")
|
| 177 | );
|
| 178 | }
|
| 179 |
|
| 180 | public static void onChunkLoadFailure(final Minecraft minecraft, final ChunkPos pos) {
|
| 181 | addOrUpdate(
|
| 182 | minecraft.gui.toastManager(),
|
| 183 | SystemToast.SystemToastId.CHUNK_LOAD_FAILURE,
|
| 184 | Component.translatable("chunk.toast.loadFailure", Component.translationArg(pos)).withStyle(ChatFormatting.RED),
|
| 185 | Component.translatable("chunk.toast.checkLog")
|
| 186 | );
|
| 187 | }
|
| 188 |
|
| 189 | public static void onChunkSaveFailure(final Minecraft minecraft, final ChunkPos pos) {
|
| 190 | addOrUpdate(
|
| 191 | minecraft.gui.toastManager(),
|
| 192 | SystemToast.SystemToastId.CHUNK_SAVE_FAILURE,
|
| 193 | Component.translatable("chunk.toast.saveFailure", Component.translationArg(pos)).withStyle(ChatFormatting.RED),
|
| 194 | Component.translatable("chunk.toast.checkLog")
|
| 195 | );
|
| 196 | }
|
| 197 |
|
| 198 | @Environment(EnvType.CLIENT)
|
| 199 | public static class SystemToastId {
|
| 200 | public static final SystemToast.SystemToastId NARRATOR_TOGGLE = new SystemToast.SystemToastId();
|
| 201 | public static final SystemToast.SystemToastId WORLD_BACKUP = new SystemToast.SystemToastId();
|
| 202 | public static final SystemToast.SystemToastId PACK_LOAD_FAILURE = new SystemToast.SystemToastId();
|
| 203 | public static final SystemToast.SystemToastId WORLD_ACCESS_FAILURE = new SystemToast.SystemToastId();
|
| 204 | public static final SystemToast.SystemToastId PACK_COPY_FAILURE = new SystemToast.SystemToastId();
|
| 205 | public static final SystemToast.SystemToastId FILE_DROP_FAILURE = new SystemToast.SystemToastId();
|
| 206 | public static final SystemToast.SystemToastId PERIODIC_NOTIFICATION = new SystemToast.SystemToastId();
|
| 207 | public static final SystemToast.SystemToastId LOW_DISK_SPACE = new SystemToast.SystemToastId(10000L);
|
| 208 | public static final SystemToast.SystemToastId CHUNK_LOAD_FAILURE = new SystemToast.SystemToastId();
|
| 209 | public static final SystemToast.SystemToastId CHUNK_SAVE_FAILURE = new SystemToast.SystemToastId();
|
| 210 | public static final SystemToast.SystemToastId UNSECURE_SERVER_WARNING = new SystemToast.SystemToastId(10000L);
|
| 211 | public static final SystemToast.SystemToastId FRIEND_SYSTEM_NOTIFICATION = new SystemToast.SystemToastId();
|
| 212 | private final long displayTime;
|
| 213 |
|
| 214 | public SystemToastId(final long displayTime) {
|
| 215 | this.displayTime = displayTime;
|
| 216 | }
|
| 217 |
|
| 218 | public SystemToastId() {
|
| 219 | this(5000L);
|
| 220 | }
|
| 221 | }
|
| 222 | }
|
| 223 |
|