From d6665f1cd57e8a8846edab137259fe5af9e2d684 Mon Sep 17 00:00:00 2001 From: CDAGaming Date: Tue, 18 Aug 2020 12:20:40 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Added=20Toast=20API=20Support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds in a logAsToast and toastTimer option, and is a minified form of Indrit's initial implementation previously used from fabritone --- src/api/java/baritone/api/Settings.java | 12 +++ src/api/java/baritone/api/utils/Helper.java | 97 ++++++++++++++++--- .../baritone/api/utils/gui/BaritoneToast.java | 86 ++++++++++++++++ 3 files changed, 183 insertions(+), 12 deletions(-) create mode 100644 src/api/java/baritone/api/utils/gui/BaritoneToast.java diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 46e3fbf5..129c3279 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -529,6 +529,18 @@ public final class Settings { */ public final Setting backfill = new Setting<>(false); + /** + * Shows popup message in the upper right corner, similarly to when you make an advancement + */ + public final Setting logAsToast = new Setting<>(false); + + /** + * The time of how long the message in the pop-up will display + *

+ * If below 1000L (1sec), it's better to disable this + */ + public final Setting toastTimer = new Setting<>(5000L); + /** * Print all the debug messages to chat */ diff --git a/src/api/java/baritone/api/utils/Helper.java b/src/api/java/baritone/api/utils/Helper.java index 3cda1834..1fe5b421 100755 --- a/src/api/java/baritone/api/utils/Helper.java +++ b/src/api/java/baritone/api/utils/Helper.java @@ -18,6 +18,7 @@ package baritone.api.utils; import baritone.api.BaritoneAPI; +import baritone.api.utils.gui.BaritoneToast; import net.minecraft.client.Minecraft; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; @@ -63,6 +64,35 @@ public interface Helper { return prefix; } + /** + * Send a message to display as a toast popup + * + * @param title The title to display in the popup + * @param message The message to display in the popup + */ + default void logToast(ITextComponent title, ITextComponent message) { + BaritoneToast.addOrUpdate(mc.getToastGui(), title, message, BaritoneAPI.getSettings().toastTimer.value); + } + + /** + * Send a message to display as a toast popup + * + * @param title The title to display in the popup + * @param message The message to display in the popup + */ + default void logToast(String title, String message) { + logToast(new TextComponentString(title), new TextComponentString(message)); + } + + /** + * Send a message to display as a toast popup + * + * @param message The message to display in the popup + */ + default void logToast(String message) { + logToast(Helper.getPrefix(), new TextComponentString(message)); + } + /** * Send a message to chat only if chatDebug is on * @@ -74,7 +104,31 @@ public interface Helper { //System.out.println(message); return; } - logDirect(message); + // We won't log debug chat into toasts + // Because only a madman would want that extreme spam -_- + logDirect(message, false); + } + + /** + * Send components to chat with the [Baritone] prefix + * + * @param logAsToast Whether to log as a toast notification + * @param components The components to send + */ + default void logDirect(boolean logAsToast, ITextComponent... components) { + ITextComponent component = new TextComponentString(""); + if (!logAsToast) { + // If we are not logging as a Toast + // Append the prefix to the base component line + component.appendSibling(getPrefix()); + component.appendSibling(new TextComponentString(" ")); + } + Arrays.asList(components).forEach(component::appendSibling); + if (logAsToast) { + logToast(getPrefix(), component); + } else { + mc.addScheduledTask(() -> BaritoneAPI.getSettings().logger.value.accept(component)); + } } /** @@ -83,11 +137,23 @@ public interface Helper { * @param components The components to send */ default void logDirect(ITextComponent... components) { - ITextComponent component = new TextComponentString(""); - component.appendSibling(getPrefix()); - component.appendSibling(new TextComponentString(" ")); - Arrays.asList(components).forEach(component::appendSibling); - Minecraft.getMinecraft().addScheduledTask(() -> BaritoneAPI.getSettings().logger.value.accept(component)); + logDirect(BaritoneAPI.getSettings().logAsToast.value, components); + } + + /** + * Send a message to chat regardless of chatDebug (should only be used for critically important messages, or as a + * direct response to a chat command) + * + * @param message The message to display in chat + * @param color The color to print that message in + * @param logAsToast Whether to log as a toast notification + */ + default void logDirect(String message, TextFormatting color, boolean logAsToast) { + Stream.of(message.split("\n")).forEach(line -> { + ITextComponent component = new TextComponentString(line.replace("\t", " ")); + component.getStyle().setColor(color); + logDirect(logAsToast, component); + }); } /** @@ -98,11 +164,18 @@ public interface Helper { * @param color The color to print that message in */ default void logDirect(String message, TextFormatting color) { - Stream.of(message.split("\n")).forEach(line -> { - ITextComponent component = new TextComponentString(line.replace("\t", " ")); - component.getStyle().setColor(color); - logDirect(component); - }); + logDirect(message, color, BaritoneAPI.getSettings().logAsToast.value); + } + + /** + * Send a message to chat regardless of chatDebug (should only be used for critically important messages, or as a + * direct response to a chat command) + * + * @param message The message to display in chat + * @param logAsToast Whether to log as a toast notification + */ + default void logDirect(String message, boolean logAsToast) { + logDirect(message, TextFormatting.GRAY, logAsToast); } /** @@ -112,6 +185,6 @@ public interface Helper { * @param message The message to display in chat */ default void logDirect(String message) { - logDirect(message, TextFormatting.GRAY); + logDirect(message, BaritoneAPI.getSettings().logAsToast.value); } } diff --git a/src/api/java/baritone/api/utils/gui/BaritoneToast.java b/src/api/java/baritone/api/utils/gui/BaritoneToast.java new file mode 100644 index 00000000..fa3d7739 --- /dev/null +++ b/src/api/java/baritone/api/utils/gui/BaritoneToast.java @@ -0,0 +1,86 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.utils.gui; + +import net.minecraft.client.gui.toasts.GuiToast; +import net.minecraft.client.gui.toasts.IToast; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.text.ITextComponent; + +public class BaritoneToast implements IToast +{ + private String title; + private String subtitle; + private long firstDrawTime; + private boolean newDisplay; + private long totalShowTime; + + public BaritoneToast(ITextComponent titleComponent, ITextComponent subtitleComponent, long totalShowTime) + { + this.title = titleComponent.getFormattedText(); + this.subtitle = subtitleComponent == null ? null : subtitleComponent.getFormattedText(); + this.totalShowTime = totalShowTime; + } + + public Visibility draw(GuiToast toastGui, long delta) + { + if (this.newDisplay) + { + this.firstDrawTime = delta; + this.newDisplay = false; + } + + toastGui.getMinecraft().getTextureManager().bindTexture(new ResourceLocation("textures/gui/toasts.png")); + GlStateManager.color(1.0F, 1.0F, 1.0F, 255.0f); + toastGui.drawTexturedModalRect(0, 0, 0, 32, 160, 32); + + if (this.subtitle == null) + { + toastGui.getMinecraft().fontRenderer.drawString(this.title, 18, 12, -11534256); + } + else + { + toastGui.getMinecraft().fontRenderer.drawString(this.title, 18, 7, -11534256); + toastGui.getMinecraft().fontRenderer.drawString(this.subtitle, 18, 18, -16777216); + } + + return delta - this.firstDrawTime < totalShowTime ? Visibility.SHOW : Visibility.HIDE; + } + + public void setDisplayedText(ITextComponent titleComponent, ITextComponent subtitleComponent) + { + this.title = titleComponent.getFormattedText(); + this.subtitle = subtitleComponent == null ? null : subtitleComponent.getFormattedText(); + this.newDisplay = true; + } + + public static void addOrUpdate(GuiToast toast, ITextComponent title, ITextComponent subtitle, long totalShowTime) + { + BaritoneToast baritonetoast = toast.getToast(BaritoneToast.class, new Object()); + + if (baritonetoast == null) + { + toast.add(new BaritoneToast(title, subtitle, totalShowTime)); + } + else + { + baritonetoast.setDisplayedText(title, subtitle); + } + } +} From d2c625e1c9bf710c15007a31f5b32437026b8a6a Mon Sep 17 00:00:00 2001 From: CDAGaming Date: Wed, 14 Oct 2020 13:14:23 -0500 Subject: [PATCH 2/2] Apply Review Comments --- src/api/java/baritone/api/utils/Helper.java | 2 +- .../baritone/api/utils/gui/BaritoneToast.java | 32 ++++++------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/api/java/baritone/api/utils/Helper.java b/src/api/java/baritone/api/utils/Helper.java index 1fe5b421..324efbef 100755 --- a/src/api/java/baritone/api/utils/Helper.java +++ b/src/api/java/baritone/api/utils/Helper.java @@ -71,7 +71,7 @@ public interface Helper { * @param message The message to display in the popup */ default void logToast(ITextComponent title, ITextComponent message) { - BaritoneToast.addOrUpdate(mc.getToastGui(), title, message, BaritoneAPI.getSettings().toastTimer.value); + mc.addScheduledTask(() -> BaritoneToast.addOrUpdate(mc.getToastGui(), title, message, BaritoneAPI.getSettings().toastTimer.value)); } /** diff --git a/src/api/java/baritone/api/utils/gui/BaritoneToast.java b/src/api/java/baritone/api/utils/gui/BaritoneToast.java index fa3d7739..82df6741 100644 --- a/src/api/java/baritone/api/utils/gui/BaritoneToast.java +++ b/src/api/java/baritone/api/utils/gui/BaritoneToast.java @@ -23,25 +23,21 @@ import net.minecraft.client.renderer.GlStateManager; import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.ITextComponent; -public class BaritoneToast implements IToast -{ +public class BaritoneToast implements IToast { private String title; private String subtitle; private long firstDrawTime; private boolean newDisplay; private long totalShowTime; - public BaritoneToast(ITextComponent titleComponent, ITextComponent subtitleComponent, long totalShowTime) - { + public BaritoneToast(ITextComponent titleComponent, ITextComponent subtitleComponent, long totalShowTime) { this.title = titleComponent.getFormattedText(); this.subtitle = subtitleComponent == null ? null : subtitleComponent.getFormattedText(); this.totalShowTime = totalShowTime; } - public Visibility draw(GuiToast toastGui, long delta) - { - if (this.newDisplay) - { + public Visibility draw(GuiToast toastGui, long delta) { + if (this.newDisplay) { this.firstDrawTime = delta; this.newDisplay = false; } @@ -50,12 +46,9 @@ public class BaritoneToast implements IToast GlStateManager.color(1.0F, 1.0F, 1.0F, 255.0f); toastGui.drawTexturedModalRect(0, 0, 0, 32, 160, 32); - if (this.subtitle == null) - { + if (this.subtitle == null) { toastGui.getMinecraft().fontRenderer.drawString(this.title, 18, 12, -11534256); - } - else - { + } else { toastGui.getMinecraft().fontRenderer.drawString(this.title, 18, 7, -11534256); toastGui.getMinecraft().fontRenderer.drawString(this.subtitle, 18, 18, -16777216); } @@ -63,23 +56,18 @@ public class BaritoneToast implements IToast return delta - this.firstDrawTime < totalShowTime ? Visibility.SHOW : Visibility.HIDE; } - public void setDisplayedText(ITextComponent titleComponent, ITextComponent subtitleComponent) - { + public void setDisplayedText(ITextComponent titleComponent, ITextComponent subtitleComponent) { this.title = titleComponent.getFormattedText(); this.subtitle = subtitleComponent == null ? null : subtitleComponent.getFormattedText(); this.newDisplay = true; } - public static void addOrUpdate(GuiToast toast, ITextComponent title, ITextComponent subtitle, long totalShowTime) - { + public static void addOrUpdate(GuiToast toast, ITextComponent title, ITextComponent subtitle, long totalShowTime) { BaritoneToast baritonetoast = toast.getToast(BaritoneToast.class, new Object()); - if (baritonetoast == null) - { + if (baritonetoast == null) { toast.add(new BaritoneToast(title, subtitle, totalShowTime)); - } - else - { + } else { baritonetoast.setDisplayedText(title, subtitle); } }