Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 422cb9c352 | |||
| e60bff9c6d | |||
| 59944ece4e | |||
| b29f2e2db3 | |||
| f37cff83fc | |||
| 2384178a3a | |||
| 5558b237bb |
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
<groupId>net.viper.bungee</groupId>
|
<groupId>net.viper.bungee</groupId>
|
||||||
<artifactId>StatusAPI</artifactId>
|
<artifactId>StatusAPI</artifactId>
|
||||||
<version>4.0.3</version>
|
<version>4.0.5</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>StatusAPI</name>
|
<name>StatusAPI</name>
|
||||||
|
|||||||
@@ -19,9 +19,21 @@ import java.io.OutputStream;
|
|||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* StatusAPI - zentraler Bungee HTTP-Status- und Broadcast-Endpunkt
|
||||||
|
*
|
||||||
|
* Ergänzungen:
|
||||||
|
* - BroadcastModule Registrierung
|
||||||
|
* - POST /broadcast UND POST / (Root) werden als Broadcasts behandelt
|
||||||
|
* - Unterstützung für prefix, prefixColor, messageColor Felder in JSON-Payload
|
||||||
|
* - Unterstützung für bracketColor (neu)
|
||||||
|
* - Unterstützung für scheduleTime (ms oder s), recur, clientScheduleId
|
||||||
|
* - API-Key Header (X-Api-Key) wird an BroadcastModule weitergereicht
|
||||||
|
*/
|
||||||
public class StatusAPI extends Plugin implements Runnable {
|
public class StatusAPI extends Plugin implements Runnable {
|
||||||
|
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
@@ -46,17 +58,13 @@ public class StatusAPI extends Plugin implements Runnable {
|
|||||||
// 2. Modul-System starten
|
// 2. Modul-System starten
|
||||||
moduleManager = new ModuleManager();
|
moduleManager = new ModuleManager();
|
||||||
|
|
||||||
// 3. MODULE REGISTRIEREN (Hier erweiterst du die API in Zukunft!)
|
// 3. MODULE REGISTRIEREN
|
||||||
// Um ein neues Feature hinzuzufügen, erstelle eine Klasse, die 'Module' implementiert
|
|
||||||
// und füge hier eine Zeile hinzu: moduleManager.registerModule(new MeinNeuesModul());
|
|
||||||
|
|
||||||
moduleManager.registerModule(new StatsModule()); // Statistik System laden
|
moduleManager.registerModule(new StatsModule()); // Statistik System laden
|
||||||
|
|
||||||
moduleManager.registerModule(new VerifyModule()); // Verify Modul
|
moduleManager.registerModule(new VerifyModule()); // Verify Modul
|
||||||
|
|
||||||
moduleManager.registerModule(new GlobalChatModule()); // GlobalChat
|
moduleManager.registerModule(new GlobalChatModule()); // GlobalChat
|
||||||
|
|
||||||
moduleManager.registerModule(new NavigationModule()); //Server Switcher
|
moduleManager.registerModule(new NavigationModule()); //Server Switcher
|
||||||
|
// Broadcast Modul registrieren (neu)
|
||||||
|
moduleManager.registerModule(new net.viper.status.modules.broadcast.BroadcastModule());
|
||||||
|
|
||||||
// 4. Alle Module aktivieren
|
// 4. Alle Module aktivieren
|
||||||
moduleManager.enableAll(this);
|
moduleManager.enableAll(this);
|
||||||
@@ -184,11 +192,187 @@ public class StatusAPI extends Plugin implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erweiterter HTTP-Handler:
|
||||||
|
* - GET weiterhin liefert Status JSON wie bisher
|
||||||
|
* - POST an /broadcast oder POST an / (Root) wird als Broadcast verarbeitet
|
||||||
|
* (Payload JSON mit message, type, prefix, prefixColor, bracketColor, messageColor)
|
||||||
|
* - Wenn Feld scheduleTime (ms oder s) vorhanden ist, wird Nachricht als geplante Nachricht registriert
|
||||||
|
* und an BroadcastModule.scheduleBroadcast weitergegeben.
|
||||||
|
* - POST /broadcast/cancel erwartet clientScheduleId im Body und ruft cancelScheduled(clientScheduleId) auf.
|
||||||
|
*/
|
||||||
private void handleConnection(Socket clientSocket) {
|
private void handleConnection(Socket clientSocket) {
|
||||||
try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8"));
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8"));
|
||||||
OutputStream out = clientSocket.getOutputStream()) {
|
OutputStream out = clientSocket.getOutputStream()) {
|
||||||
|
|
||||||
String inputLine = in.readLine();
|
String inputLine = in.readLine();
|
||||||
|
if (inputLine == null) return;
|
||||||
|
|
||||||
|
// Request-Line zerlegen: METHOD PATH HTTP/VERSION
|
||||||
|
String[] reqParts = inputLine.split(" ");
|
||||||
|
if (reqParts.length < 2) return;
|
||||||
|
String method = reqParts[0].trim();
|
||||||
|
String path = reqParts[1].trim();
|
||||||
|
|
||||||
|
// Header einlesen (case-insensitive keys)
|
||||||
|
Map<String, String> headers = new HashMap<>();
|
||||||
|
String line;
|
||||||
|
while ((line = in.readLine()) != null && !line.isEmpty()) {
|
||||||
|
int idx = line.indexOf(':');
|
||||||
|
if (idx > 0) {
|
||||||
|
String key = line.substring(0, idx).trim().toLowerCase(Locale.ROOT);
|
||||||
|
String val = line.substring(idx + 1).trim();
|
||||||
|
headers.put(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- POST /broadcast/cancel (optional) ---
|
||||||
|
if ("POST".equalsIgnoreCase(method) && (path.equalsIgnoreCase("/broadcast/cancel") || path.equalsIgnoreCase("/cancel"))) {
|
||||||
|
int contentLength = 0;
|
||||||
|
if (headers.containsKey("content-length")) {
|
||||||
|
try { contentLength = Integer.parseInt(headers.get("content-length")); } catch (NumberFormatException ignored) {}
|
||||||
|
}
|
||||||
|
char[] bodyChars = new char[Math.max(0, contentLength)];
|
||||||
|
if (contentLength > 0) {
|
||||||
|
int read = 0;
|
||||||
|
while (read < contentLength) {
|
||||||
|
int r = in.read(bodyChars, read, contentLength - read);
|
||||||
|
if (r == -1) break;
|
||||||
|
read += r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String body = new String(bodyChars);
|
||||||
|
String clientScheduleId = extractJsonString(body, "clientScheduleId");
|
||||||
|
if (clientScheduleId == null || clientScheduleId.isEmpty()) {
|
||||||
|
sendHttpResponse(out, "{\"success\":false,\"error\":\"missing_clientScheduleId\"}", 400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Object mod = moduleManager.getModule("BroadcastModule");
|
||||||
|
if (mod instanceof net.viper.status.modules.broadcast.BroadcastModule) {
|
||||||
|
boolean ok = ((net.viper.status.modules.broadcast.BroadcastModule) mod).cancelScheduled(clientScheduleId);
|
||||||
|
if (ok) sendHttpResponse(out, "{\"success\":true}", 200);
|
||||||
|
else sendHttpResponse(out, "{\"success\":false,\"error\":\"not_found\"}", 404);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
sendHttpResponse(out, "{\"success\":false,\"error\":\"no_broadcast_module\"}", 500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- POST /broadcast oder POST / (Root) für register/send ---
|
||||||
|
if ("POST".equalsIgnoreCase(method) && ("/broadcast".equalsIgnoreCase(path) || "/".equals(path) || path.isEmpty())) {
|
||||||
|
int contentLength = 0;
|
||||||
|
if (headers.containsKey("content-length")) {
|
||||||
|
try { contentLength = Integer.parseInt(headers.get("content-length")); } catch (NumberFormatException ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
char[] bodyChars = new char[Math.max(0, contentLength)];
|
||||||
|
if (contentLength > 0) {
|
||||||
|
int read = 0;
|
||||||
|
while (read < contentLength) {
|
||||||
|
int r = in.read(bodyChars, read, contentLength - read);
|
||||||
|
if (r == -1) break;
|
||||||
|
read += r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String body = new String(bodyChars);
|
||||||
|
|
||||||
|
// Header X-Api-Key (case-insensitive)
|
||||||
|
String apiKeyHeader = headers.getOrDefault("x-api-key", headers.getOrDefault("x-apikey", ""));
|
||||||
|
|
||||||
|
// parse minimal JSON fields we need
|
||||||
|
String message = extractJsonString(body, "message");
|
||||||
|
String type = extractJsonString(body, "type");
|
||||||
|
String prefix = extractJsonString(body, "prefix");
|
||||||
|
String prefixColor = extractJsonString(body, "prefixColor");
|
||||||
|
String bracketColor = extractJsonString(body, "bracketColor"); // HINZUGEFÜGT
|
||||||
|
String messageColor = extractJsonString(body, "messageColor");
|
||||||
|
String sourceName = extractJsonString(body, "source");
|
||||||
|
String scheduleTimeStr = extractJsonString(body, "scheduleTime"); // expecting millis OR seconds
|
||||||
|
String recur = extractJsonString(body, "recur");
|
||||||
|
String clientScheduleId = extractJsonString(body, "clientScheduleId");
|
||||||
|
|
||||||
|
if (sourceName == null || sourceName.isEmpty()) sourceName = "PulseCast";
|
||||||
|
if (type == null || type.isEmpty()) type = "global";
|
||||||
|
|
||||||
|
// if scheduleTime present -> register schedule with BroadcastModule
|
||||||
|
if (scheduleTimeStr != null && !scheduleTimeStr.trim().isEmpty()) {
|
||||||
|
long scheduleMillis = 0L;
|
||||||
|
try {
|
||||||
|
// scheduleTime may be numeric string (ms or s)
|
||||||
|
scheduleMillis = Long.parseLong(scheduleTimeStr.trim());
|
||||||
|
// if looks like seconds (less than 1e12), convert to ms
|
||||||
|
if (scheduleMillis < 1_000_000_000_000L) scheduleMillis = scheduleMillis * 1000L;
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
// try to parse as double then convert
|
||||||
|
try {
|
||||||
|
double d = Double.parseDouble(scheduleTimeStr.trim());
|
||||||
|
long v = (long) d;
|
||||||
|
if (v < 1_000_000_000_000L) v = v * 1000L;
|
||||||
|
scheduleMillis = v;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
sendHttpResponse(out, "{\"success\":false,\"error\":\"bad_scheduleTime\"}", 400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BroadcastModule: scheduleBroadcast(timestampMillis, sourceName, message, type, apiKeyHeader, prefix, prefixColor, bracketColor, messageColor, recur, clientId)
|
||||||
|
try {
|
||||||
|
Object mod = moduleManager.getModule("BroadcastModule");
|
||||||
|
if (mod instanceof net.viper.status.modules.broadcast.BroadcastModule) {
|
||||||
|
net.viper.status.modules.broadcast.BroadcastModule bm =
|
||||||
|
(net.viper.status.modules.broadcast.BroadcastModule) mod;
|
||||||
|
|
||||||
|
boolean ok = bm.scheduleBroadcast(scheduleMillis, sourceName, message, type, apiKeyHeader,
|
||||||
|
prefix, prefixColor, bracketColor, messageColor, (recur == null ? "none" : recur), (clientScheduleId == null ? null : clientScheduleId)); // bracketColor HINZUGEFÜGT
|
||||||
|
|
||||||
|
if (ok) {
|
||||||
|
sendHttpResponse(out, "{\"success\":true}", 200);
|
||||||
|
} else {
|
||||||
|
sendHttpResponse(out, "{\"success\":false,\"error\":\"rejected\"}", 403);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
sendHttpResponse(out, "{\"success\":false,\"error\":\"no_broadcast_module\"}", 500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
sendHttpResponse(out, "{\"success\":false,\"error\":\"internal\"}", 500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no scheduleTime -> immediate broadcast
|
||||||
|
if (message == null || message.isEmpty()) {
|
||||||
|
String resp = "{\"success\":false,\"error\":\"missing_message\"}";
|
||||||
|
sendHttpResponse(out, resp, 400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Object mod = moduleManager.getModule("BroadcastModule");
|
||||||
|
if (mod instanceof net.viper.status.modules.broadcast.BroadcastModule) {
|
||||||
|
net.viper.status.modules.broadcast.BroadcastModule bm =
|
||||||
|
(net.viper.status.modules.broadcast.BroadcastModule) mod;
|
||||||
|
|
||||||
|
boolean ok = bm.handleBroadcast(sourceName, message, type, apiKeyHeader, prefix, prefixColor, bracketColor, messageColor); // bracketColor HINZUGEFÜGT
|
||||||
|
|
||||||
|
if (ok) sendHttpResponse(out, "{\"success\":true}", 200);
|
||||||
|
else sendHttpResponse(out, "{\"success\":false,\"error\":\"rejected\"}", 403);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
sendHttpResponse(out, "{\"success\":false,\"error\":\"no_broadcast_module\"}", 500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
sendHttpResponse(out, "{\"success\":false,\"error\":\"internal\"}", 500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- bisheriger GET-Handler (unverändert) ---
|
||||||
if (inputLine != null && inputLine.startsWith("GET")) {
|
if (inputLine != null && inputLine.startsWith("GET")) {
|
||||||
Map<String, Object> data = new LinkedHashMap<>();
|
Map<String, Object> data = new LinkedHashMap<>();
|
||||||
data.put("online", true);
|
data.put("online", true);
|
||||||
@@ -267,6 +451,64 @@ public class StatusAPI extends Plugin implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimaler JSON-String-Extractor (robust genug für einfache Payloads).
|
||||||
|
* Liefert null, wenn Schlüssel nicht gefunden oder kein String-Wert.
|
||||||
|
*/
|
||||||
|
private String extractJsonString(String json, String key) {
|
||||||
|
if (json == null || key == null) return null;
|
||||||
|
String search = "\"" + key + "\"";
|
||||||
|
int idx = json.indexOf(search);
|
||||||
|
if (idx < 0) return null;
|
||||||
|
int colon = json.indexOf(':', idx + search.length());
|
||||||
|
if (colon < 0) return null;
|
||||||
|
int i = colon + 1;
|
||||||
|
// skip spaces
|
||||||
|
while (i < json.length() && Character.isWhitespace(json.charAt(i))) i++;
|
||||||
|
if (i >= json.length()) return null;
|
||||||
|
char c = json.charAt(i);
|
||||||
|
if (c == '"') {
|
||||||
|
i++; // skip opening quote
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
boolean escape = false;
|
||||||
|
while (i < json.length()) {
|
||||||
|
char ch = json.charAt(i++);
|
||||||
|
if (escape) {
|
||||||
|
sb.append(ch);
|
||||||
|
escape = false;
|
||||||
|
} else {
|
||||||
|
if (ch == '\\') escape = true;
|
||||||
|
else if (ch == '"') break;
|
||||||
|
else sb.append(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
} else {
|
||||||
|
// not a quoted string -> read until comma or }
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
while (i < json.length()) {
|
||||||
|
char ch = json.charAt(i);
|
||||||
|
if (ch == ',' || ch == '}' || ch == '\n' || ch == '\r') break;
|
||||||
|
sb.append(ch);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return sb.toString().trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendHttpResponse(OutputStream out, String json, int code) throws IOException {
|
||||||
|
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
response.append("HTTP/1.1 ").append(code).append(" ").append(code == 200 ? "OK" : "ERROR").append("\r\n");
|
||||||
|
response.append("Content-Type: application/json; charset=UTF-8\r\n");
|
||||||
|
response.append("Access-Control-Allow-Origin: *\r\n");
|
||||||
|
response.append("Content-Length: ").append(jsonBytes.length).append("\r\n");
|
||||||
|
response.append("Connection: close\r\n\r\n");
|
||||||
|
out.write(response.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
|
out.write(jsonBytes);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
private String buildJsonString(Map<String, Object> data) {
|
private String buildJsonString(Map<String, Object> data) {
|
||||||
StringBuilder sb = new StringBuilder("{");
|
StringBuilder sb = new StringBuilder("{");
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
|
|||||||
@@ -0,0 +1,381 @@
|
|||||||
|
package net.viper.status.modules.broadcast;
|
||||||
|
|
||||||
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
|
import net.md_5.bungee.api.plugin.Listener;
|
||||||
|
import net.viper.status.module.Module;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BroadcastModule
|
||||||
|
*
|
||||||
|
* Speichert geplante Broadcasts jetzt persistent in 'broadcasts.schedules'.
|
||||||
|
* Beim Neustart werden diese automatisch wieder geladen.
|
||||||
|
*/
|
||||||
|
public class BroadcastModule implements Module, Listener {
|
||||||
|
|
||||||
|
private Plugin plugin;
|
||||||
|
private boolean enabled = true;
|
||||||
|
private String requiredApiKey = "";
|
||||||
|
private String format = "%prefix% %message%";
|
||||||
|
private String fallbackPrefix = "[Broadcast]";
|
||||||
|
private String fallbackPrefixColor = "&c";
|
||||||
|
private String fallbackBracketColor = "&8"; // Neu
|
||||||
|
private String fallbackMessageColor = "&f";
|
||||||
|
|
||||||
|
private final Map<String, ScheduledBroadcast> scheduledByClientId = new ConcurrentHashMap<>();
|
||||||
|
private File schedulesFile;
|
||||||
|
private final SimpleDateFormat dateFormat;
|
||||||
|
|
||||||
|
public BroadcastModule() {
|
||||||
|
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
||||||
|
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "BroadcastModule";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable(Plugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
schedulesFile = new File(plugin.getDataFolder(), "broadcasts.schedules");
|
||||||
|
loadConfig();
|
||||||
|
|
||||||
|
if (!enabled) {
|
||||||
|
plugin.getLogger().info("[BroadcastModule] deaktiviert via verify.properties (broadcast.enabled=false)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
plugin.getProxy().getPluginManager().registerListener(plugin, this);
|
||||||
|
} catch (Throwable ignored) {}
|
||||||
|
|
||||||
|
plugin.getLogger().info("[BroadcastModule] aktiviert. Format: " + format);
|
||||||
|
loadSchedules();
|
||||||
|
plugin.getProxy().getScheduler().schedule(plugin, this::processScheduled, 1, 1, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable(Plugin plugin) {
|
||||||
|
plugin.getLogger().info("[BroadcastModule] deaktiviert.");
|
||||||
|
saveSchedules();
|
||||||
|
scheduledByClientId.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadConfig() {
|
||||||
|
File file = new File(plugin.getDataFolder(), "verify.properties");
|
||||||
|
if (!file.exists()) {
|
||||||
|
enabled = true;
|
||||||
|
requiredApiKey = "";
|
||||||
|
format = "%prefix% %message%";
|
||||||
|
fallbackPrefix = "[Broadcast]";
|
||||||
|
fallbackPrefixColor = "&c";
|
||||||
|
fallbackBracketColor = "&8"; // Neu
|
||||||
|
fallbackMessageColor = "&f";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (InputStream in = new FileInputStream(file)) {
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.load(new InputStreamReader(in, StandardCharsets.UTF_8));
|
||||||
|
enabled = Boolean.parseBoolean(props.getProperty("broadcast.enabled", "true"));
|
||||||
|
requiredApiKey = props.getProperty("broadcast.api_key", "").trim();
|
||||||
|
format = props.getProperty("broadcast.format", format).trim();
|
||||||
|
if (format.isEmpty()) format = "%prefix% %message%";
|
||||||
|
fallbackPrefix = props.getProperty("broadcast.prefix", fallbackPrefix).trim();
|
||||||
|
fallbackPrefixColor = props.getProperty("broadcast.prefix-color", fallbackPrefixColor).trim();
|
||||||
|
fallbackBracketColor = props.getProperty("broadcast.bracket-color", fallbackBracketColor).trim(); // Neu
|
||||||
|
fallbackMessageColor = props.getProperty("broadcast.message-color", fallbackMessageColor).trim();
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.getLogger().warning("[BroadcastModule] Fehler beim Laden von verify.properties: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean handleBroadcast(String sourceName, String message, String type, String apiKeyHeader,
|
||||||
|
String prefix, String prefixColor, String bracketColor, String messageColor) {
|
||||||
|
loadConfig();
|
||||||
|
|
||||||
|
if (!enabled) {
|
||||||
|
plugin.getLogger().info("[BroadcastModule] Broadcast abgelehnt: Modul ist deaktiviert.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requiredApiKey != null && !requiredApiKey.isEmpty()) {
|
||||||
|
if (apiKeyHeader == null || !requiredApiKey.equals(apiKeyHeader)) {
|
||||||
|
plugin.getLogger().warning("[BroadcastModule] Broadcast abgelehnt: API-Key fehlt oder inkorrekt.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message == null) message = "";
|
||||||
|
if (sourceName == null || sourceName.isEmpty()) sourceName = "System";
|
||||||
|
if (type == null) type = "global";
|
||||||
|
|
||||||
|
String usedPrefix = (prefix != null && !prefix.trim().isEmpty()) ? prefix.trim() : fallbackPrefix;
|
||||||
|
String usedPrefixColor = (prefixColor != null && !prefixColor.trim().isEmpty()) ? prefixColor.trim() : fallbackPrefixColor;
|
||||||
|
String usedBracketColor = (bracketColor != null && !bracketColor.trim().isEmpty()) ? bracketColor.trim() : fallbackBracketColor; // Neu
|
||||||
|
String usedMessageColor = (messageColor != null && !messageColor.trim().isEmpty()) ? messageColor.trim() : fallbackMessageColor;
|
||||||
|
|
||||||
|
String prefixColorCode = normalizeColorCode(usedPrefixColor);
|
||||||
|
String bracketColorCode = normalizeColorCode(usedBracketColor); // Neu
|
||||||
|
String messageColorCode = normalizeColorCode(usedMessageColor);
|
||||||
|
|
||||||
|
// --- KLAMMER LOGIK ---
|
||||||
|
String finalPrefix;
|
||||||
|
|
||||||
|
// Wenn eine Klammerfarbe gesetzt ist, bauen wir den Prefix neu zusammen
|
||||||
|
// Format: [BracketColor][ [PrefixColor]Text [BracketColor]]
|
||||||
|
if (!bracketColorCode.isEmpty()) {
|
||||||
|
String textContent = usedPrefix;
|
||||||
|
// Entferne manuelle Klammern, falls der User [Broadcast] in das Textfeld geschrieben hat
|
||||||
|
if (textContent.startsWith("[")) textContent = textContent.substring(1);
|
||||||
|
if (textContent.endsWith("]")) textContent = textContent.substring(0, textContent.length() - 1);
|
||||||
|
|
||||||
|
finalPrefix = bracketColorCode + "[" + prefixColorCode + textContent + bracketColorCode + "]" + ChatColor.RESET;
|
||||||
|
} else {
|
||||||
|
// Altes Verhalten: Ganzen String einfärben
|
||||||
|
finalPrefix = prefixColorCode + usedPrefix + ChatColor.RESET;
|
||||||
|
}
|
||||||
|
// ---------------------
|
||||||
|
|
||||||
|
String coloredMessage = (messageColorCode.isEmpty() ? "" : messageColorCode) + message;
|
||||||
|
|
||||||
|
String out = format.replace("%name%", sourceName)
|
||||||
|
.replace("%prefix%", finalPrefix) // Neu verwendete Variable
|
||||||
|
.replace("%prefixColored%", finalPrefix) // Fallback
|
||||||
|
.replace("%message%", message)
|
||||||
|
.replace("%messageColored%", coloredMessage)
|
||||||
|
.replace("%type%", type);
|
||||||
|
|
||||||
|
if (!out.contains("%prefixColored%") && !out.contains("%messageColored%") && !out.contains("%prefix%") && !out.contains("%message%")) {
|
||||||
|
out = finalPrefix + " " + coloredMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
TextComponent tc = new TextComponent(out);
|
||||||
|
int sent = 0;
|
||||||
|
for (ProxiedPlayer p : plugin.getProxy().getPlayers()) {
|
||||||
|
try { p.sendMessage(tc); sent++; } catch (Throwable ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin.getLogger().info("[BroadcastModule] Broadcast gesendet (Empfänger=" + sent + "): " + message);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String normalizeColorCode(String code) {
|
||||||
|
if (code == null) return "";
|
||||||
|
code = code.trim();
|
||||||
|
if (code.isEmpty()) return "";
|
||||||
|
return code.contains("&") ? ChatColor.translateAlternateColorCodes('&', code) : code;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveSchedules() {
|
||||||
|
Properties props = new Properties();
|
||||||
|
for (Map.Entry<String, ScheduledBroadcast> entry : scheduledByClientId.entrySet()) {
|
||||||
|
String id = entry.getKey();
|
||||||
|
ScheduledBroadcast sb = entry.getValue();
|
||||||
|
props.setProperty(id + ".nextRunMillis", String.valueOf(sb.nextRunMillis));
|
||||||
|
props.setProperty(id + ".sourceName", sb.sourceName);
|
||||||
|
props.setProperty(id + ".message", sb.message);
|
||||||
|
props.setProperty(id + ".type", sb.type);
|
||||||
|
props.setProperty(id + ".prefix", sb.prefix);
|
||||||
|
props.setProperty(id + ".prefixColor", sb.prefixColor);
|
||||||
|
props.setProperty(id + ".bracketColor", sb.bracketColor); // Neu
|
||||||
|
props.setProperty(id + ".messageColor", sb.messageColor);
|
||||||
|
props.setProperty(id + ".recur", sb.recur);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (OutputStream out = new FileOutputStream(schedulesFile)) {
|
||||||
|
props.store(out, "PulseCast Scheduled Broadcasts");
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.getLogger().severe("[BroadcastModule] Konnte Schedules nicht speichern: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadSchedules() {
|
||||||
|
if (!schedulesFile.exists()) {
|
||||||
|
plugin.getLogger().info("[BroadcastModule] Keine bestehenden Schedules gefunden (Neustart).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Properties props = new Properties();
|
||||||
|
try (InputStream in = new FileInputStream(schedulesFile)) {
|
||||||
|
props.load(in);
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.getLogger().severe("[BroadcastModule] Konnte Schedules nicht laden: " + e.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, ScheduledBroadcast> loaded = new HashMap<>();
|
||||||
|
for (String key : props.stringPropertyNames()) {
|
||||||
|
if (!key.contains(".")) continue;
|
||||||
|
String[] parts = key.split("\\.");
|
||||||
|
if (parts.length != 2) continue;
|
||||||
|
|
||||||
|
String id = parts[0];
|
||||||
|
String field = parts[1];
|
||||||
|
String value = props.getProperty(key);
|
||||||
|
|
||||||
|
ScheduledBroadcast sb = loaded.get(id);
|
||||||
|
if (sb == null) {
|
||||||
|
sb = new ScheduledBroadcast(id, 0, "", "", "", "", "", "", "", ""); // Ein leerer String mehr für Bracket
|
||||||
|
loaded.put(id, sb);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (field) {
|
||||||
|
case "nextRunMillis":
|
||||||
|
try { sb.nextRunMillis = Long.parseLong(value); } catch (NumberFormatException ignored) {}
|
||||||
|
break;
|
||||||
|
case "sourceName": sb.sourceName = value; break;
|
||||||
|
case "message": sb.message = value; break;
|
||||||
|
case "type": sb.type = value; break;
|
||||||
|
case "prefix": sb.prefix = value; break;
|
||||||
|
case "prefixColor": sb.prefixColor = value; break;
|
||||||
|
case "bracketColor": sb.bracketColor = value; break; // Neu
|
||||||
|
case "messageColor": sb.messageColor = value; break;
|
||||||
|
case "recur": sb.recur = value; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scheduledByClientId.putAll(loaded);
|
||||||
|
plugin.getLogger().info("[BroadcastModule] " + loaded.size() + " geplante Broadcasts aus Datei wiederhergestellt.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean scheduleBroadcast(long timestampMillis, String sourceName, String message, String type,
|
||||||
|
String apiKeyHeader, String prefix, String prefixColor, String bracketColor, String messageColor,
|
||||||
|
String recur, String clientScheduleId) {
|
||||||
|
loadConfig();
|
||||||
|
|
||||||
|
if (!enabled) {
|
||||||
|
plugin.getLogger().info("[BroadcastModule] schedule abgelehnt: Modul deaktiviert.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requiredApiKey != null && !requiredApiKey.isEmpty()) {
|
||||||
|
if (apiKeyHeader == null || !requiredApiKey.equals(apiKeyHeader)) {
|
||||||
|
plugin.getLogger().warning("[BroadcastModule] schedule abgelehnt: API-Key fehlt oder inkorrekt.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message == null) message = "";
|
||||||
|
if (sourceName == null || sourceName.isEmpty()) sourceName = "System";
|
||||||
|
if (type == null) type = "global";
|
||||||
|
if (recur == null) recur = "none";
|
||||||
|
|
||||||
|
String id = (clientScheduleId != null && !clientScheduleId.trim().isEmpty()) ? clientScheduleId.trim() : UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
String scheduledTimeStr = dateFormat.format(new Date(timestampMillis));
|
||||||
|
|
||||||
|
plugin.getLogger().info("[BroadcastModule] Neue geplante Nachricht registriert: " + id + " @ " + scheduledTimeStr);
|
||||||
|
|
||||||
|
if (timestampMillis <= now) {
|
||||||
|
plugin.getLogger().warning("[BroadcastModule] Geplante Zeit liegt in der Vergangenheit -> sende sofort!");
|
||||||
|
return handleBroadcast(sourceName, message, type, apiKeyHeader, prefix, prefixColor, bracketColor, messageColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScheduledBroadcast sb = new ScheduledBroadcast(id, timestampMillis, sourceName, message, type, prefix, prefixColor, bracketColor, messageColor, recur);
|
||||||
|
scheduledByClientId.put(id, sb);
|
||||||
|
saveSchedules();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean cancelScheduled(String clientScheduleId) {
|
||||||
|
if (clientScheduleId == null || clientScheduleId.trim().isEmpty()) return false;
|
||||||
|
ScheduledBroadcast removed = scheduledByClientId.remove(clientScheduleId);
|
||||||
|
if (removed != null) {
|
||||||
|
plugin.getLogger().info("[BroadcastModule] Geplante Nachricht abgebrochen: id=" + clientScheduleId);
|
||||||
|
saveSchedules();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processScheduled() {
|
||||||
|
if (scheduledByClientId.isEmpty()) return;
|
||||||
|
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
List<String> toRemove = new ArrayList<>();
|
||||||
|
boolean changed = false;
|
||||||
|
|
||||||
|
for (Map.Entry<String, ScheduledBroadcast> entry : scheduledByClientId.entrySet()) {
|
||||||
|
ScheduledBroadcast sb = entry.getValue();
|
||||||
|
|
||||||
|
if (sb.nextRunMillis <= now) {
|
||||||
|
String timeStr = dateFormat.format(new Date(sb.nextRunMillis));
|
||||||
|
plugin.getLogger().info("[BroadcastModule] ⏰ Sende geplante Nachricht (ID: " + entry.getKey() + ", Zeit: " + timeStr + ")");
|
||||||
|
|
||||||
|
handleBroadcast(sb.sourceName, sb.message, sb.type, "", sb.prefix, sb.prefixColor, sb.bracketColor, sb.messageColor);
|
||||||
|
|
||||||
|
if (!"none".equalsIgnoreCase(sb.recur)) {
|
||||||
|
long next = computeNextMillis(sb.nextRunMillis, sb.recur);
|
||||||
|
if (next > 0) {
|
||||||
|
sb.nextRunMillis = next;
|
||||||
|
String nextTimeStr = dateFormat.format(new Date(next));
|
||||||
|
plugin.getLogger().info("[BroadcastModule] Nächste Wiederholung (" + sb.recur + "): " + nextTimeStr);
|
||||||
|
changed = true;
|
||||||
|
} else {
|
||||||
|
toRemove.add(entry.getKey());
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
toRemove.add(entry.getKey());
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed || !toRemove.isEmpty()) {
|
||||||
|
for (String k : toRemove) {
|
||||||
|
scheduledByClientId.remove(k);
|
||||||
|
plugin.getLogger().info("[BroadcastModule] Schedule entfernt: " + k);
|
||||||
|
}
|
||||||
|
saveSchedules();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private long computeNextMillis(long currentMillis, String recur) {
|
||||||
|
switch (recur.toLowerCase(Locale.ROOT)) {
|
||||||
|
case "hourly": return currentMillis + TimeUnit.HOURS.toMillis(1);
|
||||||
|
case "daily": return currentMillis + TimeUnit.DAYS.toMillis(1);
|
||||||
|
case "weekly": return currentMillis + TimeUnit.DAYS.toMillis(7);
|
||||||
|
default: return -1L;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ScheduledBroadcast {
|
||||||
|
final String clientId;
|
||||||
|
long nextRunMillis;
|
||||||
|
String sourceName;
|
||||||
|
String message;
|
||||||
|
String type;
|
||||||
|
String prefix;
|
||||||
|
String prefixColor;
|
||||||
|
String bracketColor; // Neu
|
||||||
|
String messageColor;
|
||||||
|
String recur;
|
||||||
|
|
||||||
|
ScheduledBroadcast(String clientId, long nextRunMillis, String sourceName, String message, String type,
|
||||||
|
String prefix, String prefixColor, String bracketColor, String messageColor, String recur) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.nextRunMillis = nextRunMillis;
|
||||||
|
this.sourceName = sourceName;
|
||||||
|
this.message = message;
|
||||||
|
this.type = type;
|
||||||
|
this.prefix = prefix;
|
||||||
|
this.prefixColor = prefixColor;
|
||||||
|
this.bracketColor = bracketColor; // Neu
|
||||||
|
this.messageColor = messageColor;
|
||||||
|
this.recur = (recur == null ? "none" : recur);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
name: StatusAPI
|
name: StatusAPI
|
||||||
main: net.viper.status.StatusAPI
|
main: net.viper.status.StatusAPI
|
||||||
version: 4.0.3
|
version: 4.0.5
|
||||||
author: M_Viper
|
author: M_Viper
|
||||||
description: StatusAPI für BungeeCord inkl. Update-Checker und Modul-System
|
description: StatusAPI für BungeeCord inkl. Update-Checker und Modul-System
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,17 @@
|
|||||||
# ===========================
|
# ===========================
|
||||||
chat.enabled=false
|
chat.enabled=false
|
||||||
|
|
||||||
|
# ------------------------------
|
||||||
|
# Broadcast
|
||||||
|
# ------------------------------
|
||||||
|
|
||||||
|
broadcast.enabled=false
|
||||||
|
broadcast.prefix=[Broadcast]
|
||||||
|
broadcast.prefix-color=&c
|
||||||
|
broadcast.message-color=&f
|
||||||
|
broadcast.format=%prefixColored% %messageColored%
|
||||||
|
# broadcast.format kann angepasst werden; nutze Platzhalter: %name%, %prefix%, %prefixColored%, %message%, %messageColored%, %type%
|
||||||
|
|
||||||
# ===========================
|
# ===========================
|
||||||
# NAVIGATION / SERVER SWITCHER
|
# NAVIGATION / SERVER SWITCHER
|
||||||
# ===========================
|
# ===========================
|
||||||
@@ -58,8 +69,8 @@ override.uuid-hier-einfügen = Developer
|
|||||||
|
|
||||||
# Ränge mit neuer Syntax: Rank || Spielerfarbe || Chatfarbe
|
# Ränge mit neuer Syntax: Rank || Spielerfarbe || Chatfarbe
|
||||||
# Beispiel: Rot (Rang) || Blau (Name) || Lila (Chat)
|
# Beispiel: Rot (Rang) || Blau (Name) || Lila (Chat)
|
||||||
groupformat.Owner=&c[Owner] || &b || &d
|
groupformat.owner=&c[Owner] || &b || &d
|
||||||
groupformat.Admin=&4[Admin] || &9 || &c
|
groupformat.admin=&4[Admin] || &9 || &c
|
||||||
groupformat.Developer=&b[Dev] || &3 || &a
|
groupformat.developer=&b[Dev] || &3 || &a
|
||||||
groupformat.Premium=&6[Premium] || &e || &7
|
groupformat.premium=&6[Premium] || &e || &7
|
||||||
groupformat.Spieler=&f[Spieler] || &7 || &8
|
groupformat.spieler=&f[Spieler] || &7 || &8
|
||||||
Reference in New Issue
Block a user