| 1 | package net.minecraft.client.gui.components.toasts;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.advancements.AdvancementHolder;
|
| 8 | import net.minecraft.advancements.AdvancementType;
|
| 9 | import net.minecraft.advancements.DisplayInfo;
|
| 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.resources.Identifier;
|
| 14 | import net.minecraft.sounds.SoundEvent;
|
| 15 | import net.minecraft.sounds.SoundEvents;
|
| 16 | import net.minecraft.util.ARGB;
|
| 17 | import net.minecraft.util.FormattedCharSequence;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class AdvancementToast implements Toast {
|
| 24 | private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("toast/advancement");
|
| 25 | public static final int DISPLAY_TIME = 5000;
|
| 26 | private final AdvancementHolder advancement;
|
| 27 | private Toast.Visibility wantedVisibility = Toast.Visibility.HIDE;
|
| 28 | private final ItemStack iconItem;
|
| 29 |
|
| 30 | public AdvancementToast(final AdvancementHolder advancement) {
|
| 31 | this.advancement = advancement;
|
| 32 | this.iconItem = advancement.value().display().map(d -> d.icon().create()).orElse(ItemStack.EMPTY);
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public Toast.Visibility getWantedVisibility() {
|
| 37 | return this.wantedVisibility;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public void update(final ToastManager manager, final long fullyVisibleForMs) {
|
| 42 | DisplayInfo display = this.advancement.value().display().orElse(null);
|
| 43 | if (display == null) {
|
| 44 | this.wantedVisibility = Toast.Visibility.HIDE;
|
| 45 | } else {
|
| 46 | this.wantedVisibility = fullyVisibleForMs >= 5000.0 * manager.getNotificationDisplayTimeMultiplier() ? Toast.Visibility.HIDE : Toast.Visibility.SHOW;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public @Nullable SoundEvent getSoundEvent() {
|
| 52 | return this.isChallengeAdvancement() ? SoundEvents.UI_TOAST_CHALLENGE_COMPLETE : null;
|
| 53 | }
|
| 54 |
|
| 55 | private boolean isChallengeAdvancement() {
|
| 56 | Optional<DisplayInfo> displayInfo = this.advancement.value().display();
|
| 57 | return displayInfo.isPresent() && displayInfo.get().type().equals(AdvancementType.CHALLENGE);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void extractRenderState(final GuiGraphicsExtractor graphics, final Font font, final long fullyVisibleForMs) {
|
| 62 | DisplayInfo display = this.advancement.value().display().orElse(null);
|
| 63 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BACKGROUND_SPRITE, 0, 0, this.width(), this.height());
|
| 64 | if (display != null) {
|
| 65 | List<FormattedCharSequence> lines = font.split(display.title(), 125);
|
| 66 | int titleColor = display.type() == AdvancementType.CHALLENGE ? -30465 : -256;
|
| 67 | if (lines.size() == 1) {
|
| 68 | graphics.text(font, display.type().getDisplayName(), 30, 7, titleColor, false);
|
| 69 | graphics.text(font, lines.get(0), 30, 18, -1, false);
|
| 70 | } else {
|
| 71 | int unlockTextTime = 1500;
|
| 72 | float unlockFadeTime = 300.0F;
|
| 73 | if (fullyVisibleForMs < 1500L) {
|
| 74 | int alpha = Mth.floor(Mth.clamp((float)(1500L - fullyVisibleForMs) / 300.0F, 0.0F, 1.0F) * 255.0F);
|
| 75 | graphics.text(font, display.type().getDisplayName(), 30, 11, ARGB.color(alpha, titleColor), false);
|
| 76 | } else {
|
| 77 | int alpha = Mth.floor(Mth.clamp((float)(fullyVisibleForMs - 1500L) / 300.0F, 0.0F, 1.0F) * 252.0F);
|
| 78 | int y = this.height() / 2 - lines.size() * 9 / 2;
|
| 79 |
|
| 80 | for (FormattedCharSequence line : lines) {
|
| 81 | graphics.text(font, line, 30, y, ARGB.white(alpha), false);
|
| 82 | y += 9;
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | graphics.fakeItem(this.iconItem, 8, 8);
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|