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;
019
020import baritone.api.BaritoneAPI;
021import net.minecraft.client.Minecraft;
022import net.minecraft.util.text.ITextComponent;
023import net.minecraft.util.text.TextComponentString;
024import net.minecraft.util.text.TextFormatting;
025
026import java.util.Arrays;
027import java.util.Calendar;
028import java.util.stream.Stream;
029
030/**
031 * An ease-of-access interface to provide the {@link Minecraft} game instance,
032 * chat and console logging mechanisms, and the Baritone chat prefix.
033 *
034 * @author Brady
035 * @since 8/1/2018
036 */
037public interface Helper {
038
039    /**
040     * Instance of {@link Helper}. Used for static-context reference.
041     */
042    Helper HELPER = new Helper() {};
043
044    /**
045     * Instance of the game
046     */
047    Minecraft mc = Minecraft.getMinecraft();
048
049    static ITextComponent getPrefix() {
050        // Inner text component
051        final Calendar now = Calendar.getInstance();
052        final boolean xd = now.get(Calendar.MONTH) == Calendar.APRIL && now.get(Calendar.DAY_OF_MONTH) <= 3;
053        ITextComponent baritone = new TextComponentString(xd ? "Baritoe" : BaritoneAPI.getSettings().shortBaritonePrefix.value ? "B" : "Baritone");
054        baritone.getStyle().setColor(TextFormatting.LIGHT_PURPLE);
055
056        // Outer brackets
057        ITextComponent prefix = new TextComponentString("");
058        prefix.getStyle().setColor(TextFormatting.DARK_PURPLE);
059        prefix.appendText("[");
060        prefix.appendSibling(baritone);
061        prefix.appendText("]");
062
063        return prefix;
064    }
065
066    /**
067     * Send a message to chat only if chatDebug is on
068     *
069     * @param message The message to display in chat
070     */
071    default void logDebug(String message) {
072        if (!BaritoneAPI.getSettings().chatDebug.value) {
073            //System.out.println("Suppressed debug message:");
074            //System.out.println(message);
075            return;
076        }
077        logDirect(message);
078    }
079
080    /**
081     * Send components to chat with the [Baritone] prefix
082     *
083     * @param components The components to send
084     */
085    default void logDirect(ITextComponent... components) {
086        ITextComponent component = new TextComponentString("");
087        component.appendSibling(getPrefix());
088        component.appendSibling(new TextComponentString(" "));
089        Arrays.asList(components).forEach(component::appendSibling);
090        Minecraft.getMinecraft().addScheduledTask(() -> BaritoneAPI.getSettings().logger.value.accept(component));
091    }
092
093    /**
094     * Send a message to chat regardless of chatDebug (should only be used for critically important messages, or as a
095     * direct response to a chat command)
096     *
097     * @param message The message to display in chat
098     * @param color   The color to print that message in
099     */
100    default void logDirect(String message, TextFormatting color) {
101        Stream.of(message.split("\n")).forEach(line -> {
102            ITextComponent component = new TextComponentString(line.replace("\t", "    "));
103            component.getStyle().setColor(color);
104            logDirect(component);
105        });
106    }
107
108    /**
109     * Send a message to chat regardless of chatDebug (should only be used for critically important messages, or as a
110     * direct response to a chat command)
111     *
112     * @param message The message to display in chat
113     */
114    default void logDirect(String message) {
115        logDirect(message, TextFormatting.GRAY);
116    }
117}