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 org.apache.commons.lang3.SystemUtils;
021
022import java.awt.*;
023import java.io.IOException;
024
025/**
026 * This class is not called from the main game thread.
027 * Do not refer to any Minecraft classes, it wouldn't be thread safe.
028 *
029 * @author aUniqueUser
030 */
031public class NotificationHelper {
032
033    private static TrayIcon trayIcon;
034
035    public static void notify(String text, boolean error) {
036        if (SystemUtils.IS_OS_WINDOWS) {
037            windows(text, error);
038        } else if (SystemUtils.IS_OS_MAC_OSX) {
039            mac(text);
040        } else if (SystemUtils.IS_OS_LINUX) {
041            linux(text);
042        }
043    }
044
045    private static void windows(String text, boolean error) {
046        if (SystemTray.isSupported()) {
047            try {
048                if (trayIcon == null) {
049                    SystemTray tray = SystemTray.getSystemTray();
050                    Image image = Toolkit.getDefaultToolkit().createImage("");
051
052                    trayIcon = new TrayIcon(image, "Baritone");
053                    trayIcon.setImageAutoSize(true);
054                    trayIcon.setToolTip("Baritone");
055                    tray.add(trayIcon);
056                }
057
058                trayIcon.displayMessage("Baritone", text, error ? TrayIcon.MessageType.ERROR : TrayIcon.MessageType.INFO);
059            } catch (Exception e) {
060                e.printStackTrace();
061            }
062        } else {
063            System.out.println("SystemTray is not supported");
064        }
065    }
066
067    private static void mac(String text) {
068        ProcessBuilder processBuilder = new ProcessBuilder();
069        processBuilder.command("osascript", "-e", "display notification \"" + text + "\" with title \"Baritone\"");
070        try {
071            processBuilder.start();
072        } catch (IOException e) {
073            e.printStackTrace();
074        }
075    }
076
077    // The only way to display notifications on linux is to use the java-gnome library,
078    // or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome
079    // library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome)
080    private static void linux(String text) {
081        ProcessBuilder processBuilder = new ProcessBuilder();
082        processBuilder.command("notify-send", "-a", "Baritone", text);
083        try {
084            processBuilder.start();
085        } catch (IOException e) {
086            e.printStackTrace();
087        }
088    }
089}