Upload via Git Manager GUI - AutoMessageModule.java
This commit is contained in:
@@ -1,115 +1,115 @@
|
||||
package net.viper.status.modules.AutoMessage;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.viper.status.StatusAPI;
|
||||
import net.viper.status.module.Module;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class AutoMessageModule implements Module {
|
||||
|
||||
private int taskId = -1;
|
||||
|
||||
// Diese Methode fehlte bisher und ist zwingend für das Interface
|
||||
@Override
|
||||
public String getName() {
|
||||
return "AutoMessage";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable(Plugin plugin) {
|
||||
// Hier casten wir das Plugin-Objekt zu StatusAPI, um an spezifische Methoden zu kommen
|
||||
StatusAPI api = (StatusAPI) plugin;
|
||||
|
||||
// Konfiguration aus der zentralen verify.properties laden
|
||||
Properties props = api.getVerifyProperties();
|
||||
|
||||
boolean enabled = Boolean.parseBoolean(props.getProperty("automessage.enabled", "false"));
|
||||
|
||||
if (!enabled) {
|
||||
api.getLogger().info("AutoMessage-Modul ist deaktiviert.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Interval in Sekunden einlesen
|
||||
int intervalSeconds;
|
||||
try {
|
||||
intervalSeconds = Integer.parseInt(props.getProperty("automessage.interval", "300"));
|
||||
} catch (NumberFormatException e) {
|
||||
api.getLogger().warning("Ungültiges Intervall für AutoMessage! Nutze Standard (300s).");
|
||||
intervalSeconds = 300;
|
||||
}
|
||||
|
||||
// Dateiname einlesen (Standard: messages.txt)
|
||||
String fileName = props.getProperty("automessage.file", "messages.txt");
|
||||
File messageFile = new File(api.getDataFolder(), fileName);
|
||||
|
||||
if (!messageFile.exists()) {
|
||||
api.getLogger().warning("Die Datei '" + fileName + "' wurde nicht gefunden (" + messageFile.getAbsolutePath() + ")!");
|
||||
api.getLogger().info("Erstelle eine leere Datei '" + fileName + "' als Vorlage...");
|
||||
try {
|
||||
messageFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
api.getLogger().severe("Konnte Datei nicht erstellen: " + e.getMessage());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Nachrichten aus der Datei lesen
|
||||
List<String> messages;
|
||||
try {
|
||||
messages = Files.readAllLines(messageFile.toPath(), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
api.getLogger().severe("Fehler beim Lesen von '" + fileName + "': " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
// Leere Zeilen und Kommentare herausfiltern
|
||||
messages.removeIf(line -> line.trim().isEmpty() || line.trim().startsWith("#"));
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
api.getLogger().warning("Die Datei '" + fileName + "' enthält keine gültigen Nachrichten!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Optional: Prefix aus Config lesen
|
||||
String prefixRaw = props.getProperty("automessage.prefix", "");
|
||||
String prefix = ChatColor.translateAlternateColorCodes('&', prefixRaw);
|
||||
|
||||
api.getLogger().info("Starte AutoMessage-Task (" + messages.size() + " Nachrichten aus " + fileName + ")");
|
||||
|
||||
// Finaler Index für den Lambda-Ausdruck
|
||||
final int[] currentIndex = {0};
|
||||
|
||||
// Task planen
|
||||
taskId = ProxyServer.getInstance().getScheduler().schedule(plugin, () -> {
|
||||
String msg = messages.get(currentIndex[0]);
|
||||
|
||||
String finalMessage = (prefix.isEmpty() ? "" : prefix + " ") + msg;
|
||||
|
||||
// Nachricht an alle auf dem Proxy senden
|
||||
ProxyServer.getInstance().broadcast(TextComponent.fromLegacy(finalMessage));
|
||||
|
||||
// Index erhöhen und Loop starten
|
||||
currentIndex[0] = (currentIndex[0] + 1) % messages.size();
|
||||
}, intervalSeconds, intervalSeconds, TimeUnit.SECONDS).getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable(Plugin plugin) {
|
||||
if (taskId != -1) {
|
||||
ProxyServer.getInstance().getScheduler().cancel(taskId);
|
||||
taskId = -1;
|
||||
plugin.getLogger().info("AutoMessage-Task gestoppt.");
|
||||
}
|
||||
}
|
||||
package net.viper.status.modules.AutoMessage;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.viper.status.StatusAPI;
|
||||
import net.viper.status.module.Module;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class AutoMessageModule implements Module {
|
||||
|
||||
private int taskId = -1;
|
||||
|
||||
// Diese Methode fehlte bisher und ist zwingend für das Interface
|
||||
@Override
|
||||
public String getName() {
|
||||
return "AutoMessage";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable(Plugin plugin) {
|
||||
// Hier casten wir das Plugin-Objekt zu StatusAPI, um an spezifische Methoden zu kommen
|
||||
StatusAPI api = (StatusAPI) plugin;
|
||||
|
||||
// Konfiguration aus der zentralen verify.properties laden
|
||||
Properties props = api.getVerifyProperties();
|
||||
|
||||
boolean enabled = Boolean.parseBoolean(props.getProperty("automessage.enabled", "false"));
|
||||
|
||||
if (!enabled) {
|
||||
api.getLogger().info("AutoMessage-Modul ist deaktiviert.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Interval in Sekunden einlesen
|
||||
int intervalSeconds;
|
||||
try {
|
||||
intervalSeconds = Integer.parseInt(props.getProperty("automessage.interval", "300"));
|
||||
} catch (NumberFormatException e) {
|
||||
api.getLogger().warning("Ungültiges Intervall für AutoMessage! Nutze Standard (300s).");
|
||||
intervalSeconds = 300;
|
||||
}
|
||||
|
||||
// Dateiname einlesen (Standard: messages.txt)
|
||||
String fileName = props.getProperty("automessage.file", "messages.txt");
|
||||
File messageFile = new File(api.getDataFolder(), fileName);
|
||||
|
||||
if (!messageFile.exists()) {
|
||||
api.getLogger().warning("Die Datei '" + fileName + "' wurde nicht gefunden (" + messageFile.getAbsolutePath() + ")!");
|
||||
api.getLogger().info("Erstelle eine leere Datei '" + fileName + "' als Vorlage...");
|
||||
try {
|
||||
messageFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
api.getLogger().severe("Konnte Datei nicht erstellen: " + e.getMessage());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Nachrichten aus der Datei lesen
|
||||
List<String> messages;
|
||||
try {
|
||||
messages = Files.readAllLines(messageFile.toPath(), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
api.getLogger().severe("Fehler beim Lesen von '" + fileName + "': " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
// Leere Zeilen und Kommentare herausfiltern
|
||||
messages.removeIf(line -> line.trim().isEmpty() || line.trim().startsWith("#"));
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
api.getLogger().warning("Die Datei '" + fileName + "' enthält keine gültigen Nachrichten!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Optional: Prefix aus Config lesen
|
||||
String prefixRaw = props.getProperty("automessage.prefix", "");
|
||||
String prefix = ChatColor.translateAlternateColorCodes('&', prefixRaw);
|
||||
|
||||
api.getLogger().info("Starte AutoMessage-Task (" + messages.size() + " Nachrichten aus " + fileName + ")");
|
||||
|
||||
// Finaler Index für den Lambda-Ausdruck
|
||||
final int[] currentIndex = {0};
|
||||
|
||||
// Task planen
|
||||
taskId = ProxyServer.getInstance().getScheduler().schedule(plugin, () -> {
|
||||
String msg = messages.get(currentIndex[0]);
|
||||
|
||||
String finalMessage = (prefix.isEmpty() ? "" : prefix + " ") + msg;
|
||||
|
||||
// Nachricht an alle auf dem Proxy senden
|
||||
ProxyServer.getInstance().broadcast(TextComponent.fromLegacy(finalMessage));
|
||||
|
||||
// Index erhöhen und Loop starten
|
||||
currentIndex[0] = (currentIndex[0] + 1) % messages.size();
|
||||
}, intervalSeconds, intervalSeconds, TimeUnit.SECONDS).getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable(Plugin plugin) {
|
||||
if (taskId != -1) {
|
||||
ProxyServer.getInstance().getScheduler().cancel(taskId);
|
||||
taskId = -1;
|
||||
plugin.getLogger().info("AutoMessage-Task gestoppt.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user