Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ Thumbs.db
output/*
!output/.gitkeep

# chromascape runtime cache
.chromascape/
# chromascape secrets for individual developers
secrets.properties

# custom user scripts
/src/main/java/com/chromascape/scripts/*/
Expand Down
72 changes: 0 additions & 72 deletions CVTemplates.sh

This file was deleted.

1 change: 1 addition & 0 deletions config/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<suppress files=".*WindMouse\.java" checks=".*"/>
<suppress files=".*TemplateMatching\.java" checks="AvoidStarImport"/>
<suppress files=".*ScreenManager\.java" checks="LocalVariableName"/>
<suppress files=".*ColourContours\.java" checks="AvoidStarImport"/>
</suppressions>
78 changes: 78 additions & 0 deletions src/main/java/com/chromascape/api/DiscordNotification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.chromascape.api;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Provides functionality to send logs as notifications to yourself via Discord.
*
* <ul>
* <li>Loads the {@code secrets.properties} file in the root directory.
* <li>Saves the specified WebHook URL.
* <li>Sends a POST req to the endpoint with the user's desired notification.
* </ul>
*
* <p>This is extremely useful if you aren't actively babysitting your bot or in the case of
* reaching a specified XP/GP goal. It's my personal advice to the reader for you to inform yourself
* upon catastrophic failure, promptly.
*/
public class DiscordNotification {

private static final Logger logger = LoggerFactory.getLogger(DiscordNotification.class);
private static String webhookUrl;

static {
try (InputStream input = new FileInputStream("secrets.properties")) {
Properties prop = new Properties();
prop.load(input);
webhookUrl = prop.getProperty("discord.webhook.url");
} catch (IOException ex) {
logger.info("Could not find secrets.properties in the project root.");
}
}

/**
* Sends a user specified message to a Discord WebHook endpoint. Sets up a post request and
* expects a 204 response code for success.
*
* @param message User specified String to send to the endpoint.
*/
public static void send(String message) {
if (webhookUrl == null || webhookUrl.isEmpty()) {
return;
}

String sanitizedMessage = message.replace("\"", "\\\"").replace("\n", "\\n");
String jsonPayload = "{\"content\": \"" + sanitizedMessage + "\"}";

try {
URL url = new URL(webhookUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("User-Agent", "Java-Discord-Webhook");

try (OutputStream os = conn.getOutputStream()) {
os.write(jsonPayload.getBytes(StandardCharsets.UTF_8));
}

// 204 means Success (No Content)
if (conn.getResponseCode() != 204) {
logger.error("Failed to send. Response code: {}", conn.getResponseCode());
}

conn.disconnect();
} catch (Exception e) {
logger.error("Error sending Discord notification: {}", e.getMessage());
}
}
}
19 changes: 7 additions & 12 deletions src/main/java/com/chromascape/base/BaseScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.chromascape.base;

import com.chromascape.controller.Controller;
import com.chromascape.utils.core.runtime.HotkeyListener;
import com.chromascape.utils.core.runtime.ScriptStoppedException;
import com.chromascape.utils.core.state.BotState;
import com.chromascape.utils.core.state.StateManager;
Expand All @@ -13,38 +12,35 @@
/**
* Abstract base class representing a generic automation script with lifecycle management.
*
* <p>Provides a timed execution framework where the script runs cycles until a specified duration
* elapses or the script is stopped externally.
* <p>Provides a timed execution framework where the script runs cycles until the script is stopped
* externally.
*
* <p>Manages the underlying Controller instance and logging. Subclasses should override {@link
* #cycle()} to define the script's main logic.
* <p>Manages the underlying Controller instance. Subclasses should override {@link #cycle()} to
* define the script's main logic.
*/
public abstract class BaseScript {
private final Controller controller;
private static final Logger logger = LogManager.getLogger(BaseScript.class);
private final HotkeyListener hotkeyListener;
private volatile boolean running = true;
private Thread scriptThread;

/** Constructs a BaseScript. */
public BaseScript() {
controller = new Controller();
hotkeyListener = new HotkeyListener(this);
}

/**
* Runs the script lifecycle.
*
* <p>Initializes the controller, logs start and stop events, then continuously invokes the {@link
* #cycle()} method until the specified duration elapses or the script is stopped. Checks for
* thread interruption and stops gracefully if detected.
* #cycle()} method until the script is stopped. Checks for thread interruption and stops
* gracefully if detected.
*
* <p>This method blocks until completion.
*/
public final void run() {
scriptThread = Thread.currentThread();
controller.init();
hotkeyListener.start();
StatisticsManager.reset();

try {
Expand All @@ -61,7 +57,7 @@ public final void run() {
break;
} catch (Exception e) {
StateManager.setState(BotState.ERROR);
logger.error("Exception in cycle: {}", e.getMessage());
logger.error("Exception in cycle: {}, {}", e.getMessage(), e.getStackTrace());
break;
}
}
Expand All @@ -84,7 +80,6 @@ public void stop() {
}
logger.info("Stop requested");
running = false;
hotkeyListener.stop();

// Interrupt the script thread instead of throwing exception
if (scriptThread != null) {
Expand Down
Loading