| 1 | package net.minecraft.client.gui.components.toasts;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.Font;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.renderer.RenderPipelines;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 | import net.minecraft.util.context.ContextMap;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 | import net.minecraft.world.item.crafting.display.RecipeDisplay;
|
| 15 | import net.minecraft.world.item.crafting.display.SlotDisplayContext;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class RecipeToast implements Toast {
|
| 19 | private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("toast/recipe");
|
| 20 | private static final long DISPLAY_TIME = 5000L;
|
| 21 | private static final Component TITLE_TEXT = Component.translatable("recipe.toast.title");
|
| 22 | private static final Component DESCRIPTION_TEXT = Component.translatable("recipe.toast.description");
|
| 23 | private final List<RecipeToast.Entry> recipeItems = new ArrayList<>();
|
| 24 | private long lastChanged;
|
| 25 | private boolean changed;
|
| 26 | private Toast.Visibility wantedVisibility = Toast.Visibility.HIDE;
|
| 27 | private int displayedRecipeIndex;
|
| 28 |
|
| 29 | private RecipeToast() {
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public Toast.Visibility getWantedVisibility() {
|
| 34 | return this.wantedVisibility;
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void update(final ToastManager manager, final long fullyVisibleForMs) {
|
| 39 | if (this.changed) {
|
| 40 | this.lastChanged = fullyVisibleForMs;
|
| 41 | this.changed = false;
|
| 42 | }
|
| 43 |
|
| 44 | if (this.recipeItems.isEmpty()) {
|
| 45 | this.wantedVisibility = Toast.Visibility.HIDE;
|
| 46 | } else {
|
| 47 | this.wantedVisibility = fullyVisibleForMs - this.lastChanged >= 5000.0 * manager.getNotificationDisplayTimeMultiplier()
|
| 48 | ? Toast.Visibility.HIDE
|
| 49 | : Toast.Visibility.SHOW;
|
| 50 | }
|
| 51 |
|
| 52 | this.displayedRecipeIndex = (int)(
|
| 53 | fullyVisibleForMs / Math.max(1.0, 5000.0 * manager.getNotificationDisplayTimeMultiplier() / this.recipeItems.size()) % this.recipeItems.size()
|
| 54 | );
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public void extractRenderState(final GuiGraphicsExtractor graphics, final Font font, final long fullyVisibleForMs) {
|
| 59 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BACKGROUND_SPRITE, 0, 0, this.width(), this.height());
|
| 60 | graphics.text(font, TITLE_TEXT, 30, 7, -11534256, false);
|
| 61 | graphics.text(font, DESCRIPTION_TEXT, 30, 18, -16777216, false);
|
| 62 | RecipeToast.Entry items = this.recipeItems.get(this.displayedRecipeIndex);
|
| 63 | graphics.pose().pushMatrix();
|
| 64 | graphics.pose().scale(0.6F, 0.6F);
|
| 65 | graphics.fakeItem(items.categoryItem(), 3, 3);
|
| 66 | graphics.pose().popMatrix();
|
| 67 | graphics.fakeItem(items.unlockedItem(), 8, 8);
|
| 68 | }
|
| 69 |
|
| 70 | private void addItem(final ItemStack craftingStation, final ItemStack unlockedItem) {
|
| 71 | this.recipeItems.add(new RecipeToast.Entry(craftingStation, unlockedItem));
|
| 72 | this.changed = true;
|
| 73 | }
|
| 74 |
|
| 75 | public static void addOrUpdate(final ToastManager toastManager, final RecipeDisplay recipe) {
|
| 76 | RecipeToast toast = toastManager.getToast(RecipeToast.class, NO_TOKEN);
|
| 77 | if (toast == null) {
|
| 78 | toast = new RecipeToast();
|
| 79 | toastManager.addToast(toast);
|
| 80 | }
|
| 81 |
|
| 82 | ContextMap context = SlotDisplayContext.fromLevel(toastManager.getMinecraft().level);
|
| 83 | ItemStack categoryItem = recipe.craftingStation().resolveForFirstStack(context);
|
| 84 | ItemStack unlockedItem = recipe.result().resolveForFirstStack(context);
|
| 85 | toast.addItem(categoryItem, unlockedItem);
|
| 86 | }
|
| 87 |
|
| 88 | @Environment(EnvType.CLIENT)
|
| 89 | private record Entry(ItemStack categoryItem, ItemStack unlockedItem) {
|
| 90 | }
|
| 91 | }
|
| 92 |
|