001/*
002 * This file is part of Baritone.
003 *
004 * Baritone is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Lesser General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * Baritone is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License
015 * along with Baritone.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package baritone.api.utils.gui;
019
020import net.minecraft.client.gui.toasts.GuiToast;
021import net.minecraft.client.gui.toasts.IToast;
022import net.minecraft.client.renderer.GlStateManager;
023import net.minecraft.util.ResourceLocation;
024import net.minecraft.util.text.ITextComponent;
025
026public class BaritoneToast implements IToast {
027    private String title;
028    private String subtitle;
029    private long firstDrawTime;
030    private boolean newDisplay;
031    private long totalShowTime;
032
033    public BaritoneToast(ITextComponent titleComponent, ITextComponent subtitleComponent, long totalShowTime) {
034        this.title = titleComponent.getFormattedText();
035        this.subtitle = subtitleComponent == null ? null : subtitleComponent.getFormattedText();
036        this.totalShowTime = totalShowTime;
037    }
038
039    public Visibility draw(GuiToast toastGui, long delta) {
040        if (this.newDisplay) {
041            this.firstDrawTime = delta;
042            this.newDisplay = false;
043        }
044
045        toastGui.getMinecraft().getTextureManager().bindTexture(new ResourceLocation("textures/gui/toasts.png"));
046        GlStateManager.color(1.0F, 1.0F, 1.0F, 255.0f);
047        toastGui.drawTexturedModalRect(0, 0, 0, 32, 160, 32);
048
049        if (this.subtitle == null) {
050            toastGui.getMinecraft().fontRenderer.drawString(this.title, 18, 12, -11534256);
051        } else {
052            toastGui.getMinecraft().fontRenderer.drawString(this.title, 18, 7, -11534256);
053            toastGui.getMinecraft().fontRenderer.drawString(this.subtitle, 18, 18, -16777216);
054        }
055
056        return delta - this.firstDrawTime < totalShowTime ? Visibility.SHOW : Visibility.HIDE;
057    }
058
059    public void setDisplayedText(ITextComponent titleComponent, ITextComponent subtitleComponent) {
060        this.title = titleComponent.getFormattedText();
061        this.subtitle = subtitleComponent == null ? null : subtitleComponent.getFormattedText();
062        this.newDisplay = true;
063    }
064
065    public static void addOrUpdate(GuiToast toast, ITextComponent title, ITextComponent subtitle, long totalShowTime) {
066        BaritoneToast baritonetoast = toast.getToast(BaritoneToast.class, new Object());
067
068        if (baritonetoast == null) {
069            toast.add(new BaritoneToast(title, subtitle, totalShowTime));
070        } else {
071            baritonetoast.setDisplayedText(title, subtitle);
072        }
073    }
074
075    public static void addOrUpdate(ITextComponent title, ITextComponent subtitle) {
076        addOrUpdate(net.minecraft.client.Minecraft.getMinecraft().getToastGui(), title, subtitle, baritone.api.BaritoneAPI.getSettings().toastTimer.value);
077    }
078}