Desktop Notification System

This commit is contained in:
aUniqueUser 2020-02-22 18:31:41 -05:00
parent ec8d5c0f4d
commit 2d2571cff5
No known key found for this signature in database
GPG Key ID: 84D7C232A1AFDD16
3 changed files with 79 additions and 0 deletions

View File

@ -1046,6 +1046,10 @@ public final class Settings {
*/
public final Setting<Boolean> renderSelectionCorners = new Setting<>(true);
/**
* Desktop Notifications
*/
public final Setting<Boolean> desktopNotifications = new Setting<>(false);
/**
* A map of lowercase setting field names to their respective setting

View File

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

View File

@ -0,0 +1,72 @@
/*
* 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.utils;
import java.awt.*;
import java.io.IOException;
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("");
// Replace with some logo
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();
}
}
}