diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 69bcabcb..1bbde3b2 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -1046,6 +1046,10 @@ public final class Settings { */ public final Setting renderSelectionCorners = new Setting<>(true); + /** + * Desktop Notifications + */ + public final Setting desktopNotifications = new Setting<>(false); /** * A map of lowercase setting field names to their respective setting diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index a67384ac..eea23905 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -26,6 +26,7 @@ import baritone.api.utils.Helper; import baritone.api.utils.PathCalculationResult; import baritone.pathing.movement.CalculationContext; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import baritone.utils.NotificationHelper; import java.util.Optional; @@ -216,6 +217,9 @@ public abstract class AbstractNodeCostSearch implements IPathFinder, Helper { if (logInfo) { logDebug("Even with a cost coefficient of " + COEFFICIENTS[COEFFICIENTS.length - 1] + ", I couldn't get more than " + Math.sqrt(bestDist) + " blocks"); logDebug("No path found =("); + if (Baritone.settings().desktopNotifications.value) { + NotificationHelper.notify("No path found =(", true); + } } return Optional.empty(); } diff --git a/src/main/java/baritone/utils/NotificationHelper.java b/src/main/java/baritone/utils/NotificationHelper.java new file mode 100644 index 00000000..76cd5b1c --- /dev/null +++ b/src/main/java/baritone/utils/NotificationHelper.java @@ -0,0 +1,75 @@ +/* + * 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.utils; + +import java.awt.*; +import java.io.IOException; + +/** + * This class is not called from the main game thread. + * Do not refer to any Minecraft classes, it wouldn't be thread safe. + * + * @author aUniqueUser + */ +public class NotificationHelper { + + public static void notify(String text, boolean error) { + if (System.getProperty("os.name").contains("Linux")) { + linux(text); + } else { + notification(text, error); + } + } + + public static void notification(String text, boolean error) { + if (SystemTray.isSupported()) { + try { + SystemTray tray = SystemTray.getSystemTray(); + Image image = Toolkit.getDefaultToolkit().createImage(""); + + TrayIcon trayIcon = new TrayIcon(image, "Baritone"); + trayIcon.setImageAutoSize(true); + trayIcon.setToolTip("Baritone"); + tray.add(trayIcon); + + if (error) { + trayIcon.displayMessage("Baritone", text, TrayIcon.MessageType.ERROR); + } else { + trayIcon.displayMessage("Baritone", text, TrayIcon.MessageType.INFO); + } + } catch (Exception e) { + e.printStackTrace(); + } + } else { + System.out.println("SystemTray is not supported"); + } + } + + // The only way to display notifications on linux is to use the java-gnome library, + // or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome + // library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome) + public static void linux(String text) { + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command("notify-send", "-a", "Baritone", text); + try { + processBuilder.start(); + } catch (IOException e) { + e.printStackTrace(); + } + } +}