Added Toast API Support

This adds in a logAsToast and toastTimer option, and is a minified form of Indrit's initial implementation previously used from fabritone
This commit is contained in:
CDAGaming 2020-08-18 12:20:40 -05:00
parent 8ddc795158
commit d6665f1cd5
No known key found for this signature in database
GPG Key ID: 0304C9C2F35557FB
3 changed files with 183 additions and 12 deletions

View File

@ -529,6 +529,18 @@ public final class Settings {
*/
public final Setting<Boolean> backfill = new Setting<>(false);
/**
* Shows popup message in the upper right corner, similarly to when you make an advancement
*/
public final Setting<Boolean> logAsToast = new Setting<>(false);
/**
* The time of how long the message in the pop-up will display
* <p>
* If below 1000L (1sec), it's better to disable this
*/
public final Setting<Long> toastTimer = new Setting<>(5000L);
/**
* Print all the debug messages to chat
*/

View File

@ -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);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}
}