2025-11-24 22:52:51 +03:00

188 lines
8.2 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.google.common.collect.ImmutableList
* org.jspecify.annotations.Nullable
*/
package net.minecraft.client.gui.components.toasts;
import com.google.common.collect.ImmutableList;
import java.util.List;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.components.toasts.ToastManager;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.world.level.ChunkPos;
import org.jspecify.annotations.Nullable;
public class SystemToast
implements Toast {
private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("toast/system");
private static final int MAX_LINE_SIZE = 200;
private static final int LINE_SPACING = 12;
private static final int MARGIN = 10;
private final SystemToastId id;
private Component title;
private List<FormattedCharSequence> messageLines;
private long lastChanged;
private boolean changed;
private final int width;
private boolean forceHide;
private Toast.Visibility wantedVisibility = Toast.Visibility.HIDE;
public SystemToast(SystemToastId id, Component title, @Nullable Component message) {
this(id, title, (List<FormattedCharSequence>)SystemToast.nullToEmpty(message), Math.max(160, 30 + Math.max(Minecraft.getInstance().font.width(title), message == null ? 0 : Minecraft.getInstance().font.width(message))));
}
public static SystemToast multiline(Minecraft minecraft, SystemToastId id, Component title, Component message) {
Font font = minecraft.font;
List<FormattedCharSequence> lines = font.split(message, 200);
int width = Math.max(200, lines.stream().mapToInt(font::width).max().orElse(200));
return new SystemToast(id, title, lines, width + 30);
}
private SystemToast(SystemToastId id, Component title, List<FormattedCharSequence> messageLines, int width) {
this.id = id;
this.title = title;
this.messageLines = messageLines;
this.width = width;
}
private static ImmutableList<FormattedCharSequence> nullToEmpty(@Nullable Component message) {
return message == null ? ImmutableList.of() : ImmutableList.of((Object)message.getVisualOrderText());
}
@Override
public int width() {
return this.width;
}
@Override
public int height() {
return 20 + Math.max(this.messageLines.size(), 1) * 12;
}
public void forceHide() {
this.forceHide = true;
}
@Override
public Toast.Visibility getWantedVisibility() {
return this.wantedVisibility;
}
@Override
public void update(ToastManager manager, long fullyVisibleForMs) {
if (this.changed) {
this.lastChanged = fullyVisibleForMs;
this.changed = false;
}
double timeToDisplayUpdate = (double)this.id.displayTime * manager.getNotificationDisplayTimeMultiplier();
long timeSinceUpdate = fullyVisibleForMs - this.lastChanged;
this.wantedVisibility = !this.forceHide && (double)timeSinceUpdate < timeToDisplayUpdate ? Toast.Visibility.SHOW : Toast.Visibility.HIDE;
}
@Override
public void render(GuiGraphics graphics, Font font, long fullyVisibleForMs) {
graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BACKGROUND_SPRITE, 0, 0, this.width(), this.height());
if (this.messageLines.isEmpty()) {
graphics.drawString(font, this.title, 18, 12, -256, false);
} else {
graphics.drawString(font, this.title, 18, 7, -256, false);
for (int i = 0; i < this.messageLines.size(); ++i) {
graphics.drawString(font, this.messageLines.get(i), 18, 18 + i * 12, -1, false);
}
}
}
public void reset(Component title, @Nullable Component message) {
this.title = title;
this.messageLines = SystemToast.nullToEmpty(message);
this.changed = true;
}
@Override
public SystemToastId getToken() {
return this.id;
}
public static void add(ToastManager toastManager, SystemToastId id, Component title, @Nullable Component message) {
toastManager.addToast(new SystemToast(id, title, message));
}
public static void addOrUpdate(ToastManager toastManager, SystemToastId id, Component title, @Nullable Component message) {
SystemToast toast = toastManager.getToast(SystemToast.class, id);
if (toast == null) {
SystemToast.add(toastManager, id, title, message);
} else {
toast.reset(title, message);
}
}
public static void forceHide(ToastManager toastManager, SystemToastId id) {
SystemToast toast = toastManager.getToast(SystemToast.class, id);
if (toast != null) {
toast.forceHide();
}
}
public static void onWorldAccessFailure(Minecraft minecraft, String levelId) {
SystemToast.add(minecraft.getToastManager(), SystemToastId.WORLD_ACCESS_FAILURE, Component.translatable("selectWorld.access_failure"), Component.literal(levelId));
}
public static void onWorldDeleteFailure(Minecraft minecraft, String levelId) {
SystemToast.add(minecraft.getToastManager(), SystemToastId.WORLD_ACCESS_FAILURE, Component.translatable("selectWorld.delete_failure"), Component.literal(levelId));
}
public static void onPackCopyFailure(Minecraft minecraft, String extraInfo) {
SystemToast.add(minecraft.getToastManager(), SystemToastId.PACK_COPY_FAILURE, Component.translatable("pack.copyFailure"), Component.literal(extraInfo));
}
public static void onFileDropFailure(Minecraft minecraft, int count) {
SystemToast.add(minecraft.getToastManager(), SystemToastId.FILE_DROP_FAILURE, Component.translatable("gui.fileDropFailure.title"), Component.translatable("gui.fileDropFailure.detail", count));
}
public static void onLowDiskSpace(Minecraft minecraft) {
SystemToast.addOrUpdate(minecraft.getToastManager(), SystemToastId.LOW_DISK_SPACE, Component.translatable("chunk.toast.lowDiskSpace"), Component.translatable("chunk.toast.lowDiskSpace.description"));
}
public static void onChunkLoadFailure(Minecraft minecraft, ChunkPos pos) {
SystemToast.addOrUpdate(minecraft.getToastManager(), SystemToastId.CHUNK_LOAD_FAILURE, Component.translatable("chunk.toast.loadFailure", Component.translationArg(pos)).withStyle(ChatFormatting.RED), Component.translatable("chunk.toast.checkLog"));
}
public static void onChunkSaveFailure(Minecraft minecraft, ChunkPos pos) {
SystemToast.addOrUpdate(minecraft.getToastManager(), SystemToastId.CHUNK_SAVE_FAILURE, Component.translatable("chunk.toast.saveFailure", Component.translationArg(pos)).withStyle(ChatFormatting.RED), Component.translatable("chunk.toast.checkLog"));
}
public static class SystemToastId {
public static final SystemToastId NARRATOR_TOGGLE = new SystemToastId();
public static final SystemToastId WORLD_BACKUP = new SystemToastId();
public static final SystemToastId PACK_LOAD_FAILURE = new SystemToastId();
public static final SystemToastId WORLD_ACCESS_FAILURE = new SystemToastId();
public static final SystemToastId PACK_COPY_FAILURE = new SystemToastId();
public static final SystemToastId FILE_DROP_FAILURE = new SystemToastId();
public static final SystemToastId PERIODIC_NOTIFICATION = new SystemToastId();
public static final SystemToastId LOW_DISK_SPACE = new SystemToastId(10000L);
public static final SystemToastId CHUNK_LOAD_FAILURE = new SystemToastId();
public static final SystemToastId CHUNK_SAVE_FAILURE = new SystemToastId();
public static final SystemToastId UNSECURE_SERVER_WARNING = new SystemToastId(10000L);
private final long displayTime;
public SystemToastId(long displayTime) {
this.displayTime = displayTime;
}
public SystemToastId() {
this(5000L);
}
}
}