Files
src
manifest.kod
pom.xml
README.md
Code
8 532
src/main/java/com/billusmp/billuvulcanbridge/BilluVulcanBridgePlugin.java
package com.billusmp.billuvulcanbridge;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
public final class BilluVulcanBridgePlugin extends JavaPlugin {
private final Map<String, Long> recentAlerts = new HashMap<>();
private Settings settings;
private DiscordMessageTransport transport;
@Override
public void onEnable() {
saveDefaultConfig();
try {
settings = readSettings();
transport = DiscordMessageTransport.connect(this, settings.channelId(), settings.botToken());
} catch (Exception exception) {
getLogger().severe("BilluVulcanBridge could not start (" + exception.getClass().getSimpleName() + "). Check its configuration and Discord connection.");
getServer().getPluginManager().disablePlugin(this);
return;
}
new VulcanAlertListener(this).register();
if (settings.includeDebugLogs()) {
getLogger().warning("Vulcan debug-log forwarding is unsupported; only structured Vulcan flag events are forwarded.");
}
debug("Bridge enabled in strict event-only mode.");
}
@Override
public void onDisable() {
if (transport != null) {
transport.close();
transport = null;
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!command.getName().equalsIgnoreCase("billuvulcanbridge")) {
return false;
}
if (args.length != 1 || !args[0].equalsIgnoreCase("reload")) {
sender.sendMessage("Usage: /billuvulcanbridge reload");
return true;
}
if (!sender.hasPermission("billuvulcanbridge.reload")) {
sender.sendMessage("You do not have permission to do that.");
return true;
}
reloadConfig();
try {
Settings replacementSettings = readSettings();
DiscordMessageTransport replacementTransport = DiscordMessageTransport.connect(
this, replacementSettings.channelId(), replacementSettings.botToken());
DiscordMessageTransport previousTransport = transport;
transport = replacementTransport;
settings = replacementSettings;
if (previousTransport != null) {
previousTransport.close();
}
recentAlerts.clear();
sender.sendMessage("BilluVulcanBridge configuration reloaded.");
debug("Configuration reloaded.");
} catch (Exception exception) {
sender.sendMessage("Reload failed. The previous bridge configuration remains active; check the server log.");
getLogger().severe("Configuration reload failed (" + exception.getClass().getSimpleName() + "). The previous bridge remains active.");
}
return true;
}
void handleFlag(String playerName, UUID playerId, String checkName, String checkType,
int violationLevel, int maximumViolationLevel, String alertText) {
Settings current = settings;
DiscordMessageTransport currentTransport = transport;
if (current == null || currentTransport == null || !current.includeViolations()) {
return;
}
long now = System.currentTimeMillis();
String deduplicationKey = playerId + "|" + checkName + "|" + checkType + "|"
+ violationLevel + "|" + (alertText == null ? "" : alertText);
Long previous = recentAlerts.get(deduplicationKey);
if (previous != null && now - previous < 2_000) {
return;
}
recentAlerts.put(deduplicationKey, now);
trimRecentAlerts(now);
StringBuilder message = new StringBuilder("Vulcan violation | Player: ")
.append(playerName)
.append(" | Check: ").append(checkName);
if (checkType != null && !checkType.isBlank()) {
message.append(' ').append(checkType);
}
message.append(" | VL: ").append(violationLevel);
if (maximumViolationLevel > 0) {
message.append('/').append(maximumViolationLevel);
}
if (alertText != null && !alertText.isBlank()) {
message.append(" | Alert: ").append(alertText);
}
currentTransport.send(neutralizeMentions(message.toString()));
}
void reportVulcanReadFailure(Exception exception) {
getLogger().warning("Could not read a Vulcan flag event (" + exception.getClass().getSimpleName() + ").");
}
void debug(String message) {
if (settings != null && settings.debugLogging()) {
getLogger().info("[debug] " + message);
}
}
private Settings readSettings() {
String channelId = getConfig().getString("discord.channel-id", "").trim();
if (!channelId.matches("[0-9]{16,22}")) {
throw new IllegalArgumentException("discord.channel-id must be a numeric Discord channel ID");
}
String filterMode = getConfig().getString("vulcan.filter-mode", "strict").trim();
if (!filterMode.equalsIgnoreCase("strict")) {
throw new IllegalArgumentException("Only strict Vulcan event filtering is supported");
}
String botToken = getConfig().getString("discord.bot-token", "").trim();
org.bukkit.plugin.Plugin discordSrv = getServer().getPluginManager().getPlugin("DiscordSRV");
if ((discordSrv == null || !discordSrv.isEnabled())
&& (botToken.isBlank() || botToken.equals("PASTE_YOUR_DISCORD_BOT_TOKEN_HERE"))) {
throw new IllegalArgumentException("discord.bot-token is required when DiscordSRV is not installed");
}
return new Settings(channelId, botToken,
getConfig().getBoolean("vulcan.include-violations", true),
getConfig().getBoolean("vulcan.include-debug-logs", false),
getConfig().getBoolean("logging.debug", false));
}
private void trimRecentAlerts(long now) {
if (recentAlerts.size() <= 512) {
return;
}
Iterator<Map.Entry<String, Long>> iterator = recentAlerts.entrySet().iterator();
while (iterator.hasNext()) {
if (now - iterator.next().getValue() > 5_000) {
iterator.remove();
}
}
while (recentAlerts.size() > 512) {
iterator = recentAlerts.entrySet().iterator();
if (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
}
}
private String neutralizeMentions(String message) {
String safe = message.replace("@", "@\u200B")
.replaceAll("[\\r\\n\\t]+", " ")
.replaceAll("[\\p{Cntrl}&&[^\\t]]", "");
return safe.length() <= 1_900 ? safe : safe.substring(0, 1_899) + "…";
}
private record Settings(String channelId, String botToken, boolean includeViolations,
boolean includeDebugLogs, boolean debugLogging) {
}
}