Initial commit after history reset

This commit is contained in:
AdzelFirestar
2025-06-05 18:59:42 -07:00
commit 2323ea8854
23 changed files with 950 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package com.adzel.velocitybroadcast;
import java.util.List;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
public class BroadcastCommand implements SimpleCommand {
private final VelocityBroadcast plugin;
public BroadcastCommand(VelocityBroadcast plugin) {
this.plugin = plugin;
}
@Override
public void execute(Invocation invocation) {
CommandSource source = invocation.source();
List<String> args = List.of(invocation.arguments());
if (!source.hasPermission("vb.broadcast")) {
source.sendMessage(MiniMessage.miniMessage().deserialize("<red>You don't have permission to use this command.</red>"));
return;
}
if (args.isEmpty()) {
source.sendMessage(MiniMessage.miniMessage().deserialize("<red>Usage: /vb <message></red>"));
return;
}
String messageRaw = String.join(" ", args);
String prefix = plugin.getConfigHandler().getPrefix();
String fullMessage = prefix + messageRaw;
Component broadcast = MiniMessage.miniMessage().deserialize(fullMessage);
plugin.getServer().getAllPlayers().forEach(player -> player.sendMessage(broadcast));
if (plugin.getConfigHandler().isDebugEnabled()) {
plugin.getLogger().info("[Broadcast] Sent: " + fullMessage);
}
}
}

View File

@ -0,0 +1,130 @@
package com.adzel.velocitybroadcast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
public class ConfigHandler {
private final Path configPath;
private final Logger logger;
private boolean debugEnabled = false;
private boolean versionCheckEnabled = true;
private String prefix = "&9&l[&3&lServer&9&l]&r ";
private static final String CURRENT_VERSION = VelocityBroadcast.PLUGIN_VERSION;
private static final String VERSION_LINE = "# DO NOT EDIT\nPlugin Version: '" + CURRENT_VERSION + "' # Do not edit this value, as it will mess up version checking and break the plugin";
public ConfigHandler(Path configPath, Logger logger) {
this.configPath = configPath;
this.logger = logger;
}
public void load() {
try {
Files.createDirectories(configPath.getParent());
boolean shouldSave = false;
if (!Files.exists(configPath)) {
save();
return;
}
List<String> lines = Files.readAllLines(configPath);
String fileVersion = null;
for (String line : lines) {
if (line.trim().startsWith("Plugin Version:")) {
fileVersion = line.replaceAll(".*'(.*?)'.*", "$1").trim();
break;
}
}
if (fileVersion == null || !fileVersion.equals(CURRENT_VERSION)) {
shouldSave = true;
}
try (BufferedReader reader = Files.newBufferedReader(configPath)) {
Map<String, String> configMap = reader.lines()
.filter(line -> line.contains(":") && !line.trim().startsWith("#"))
.map(line -> line.replaceAll("#.*", "").split(":", 2))
.collect(Collectors.toMap(
a -> a[0].trim(),
a -> a[1].trim().replaceAll("^['\"]|['\"]$", ""),
(a, b) -> b,
LinkedHashMap::new
));
debugEnabled = Boolean.parseBoolean(configMap.getOrDefault("debug-messages-enabled", "false"));
versionCheckEnabled = Boolean.parseBoolean(configMap.getOrDefault("version-check-enabled", "true"));
prefix = configMap.getOrDefault("prefix", "&9&l[&3&lServer&9&l]&r ");
}
if (shouldSave) {
save(); // Update config with new version and preserve user values
}
} catch (IOException e) {
logger.error("Failed to load VelocityBroadcast config!", e);
}
}
public void save() {
try {
Files.createDirectories(configPath.getParent());
String editableSection = "";
if (Files.exists(configPath)) {
editableSection = Files.readAllLines(configPath).stream()
.dropWhile(line -> !line.trim().equalsIgnoreCase("# ONLY EDIT BELOW THIS LINE"))
.skip(1)
.collect(Collectors.joining("\n"));
}
try (BufferedWriter writer = Files.newBufferedWriter(configPath)) {
writer.write(VERSION_LINE + "\n\n");
writer.write("# ONLY EDIT BELOW THIS LINE\n");
if (!editableSection.isEmpty()) {
writer.write(editableSection + "\n");
} else {
writer.write("debug-messages-enabled: false # Enables/disables debug messages (Default: false)\n");
writer.write("version-check-enabled: true # Toggles version update messages for admins (Default: true)\n");
writer.write("prefix: '&9&l[&3&lServer&9&l]&r ' # The prefix for broadcasts and messages\n");
}
}
} catch (IOException e) {
logger.error("Failed to save VelocityBroadcast config!", e);
}
}
public void reload() {
load();
}
public boolean isDebugEnabled() {
return debugEnabled;
}
public boolean isVersionCheckEnabled() {
return versionCheckEnabled;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String newPrefix) {
this.prefix = newPrefix;
save();
}
}

View File

@ -0,0 +1,44 @@
package com.adzel.velocitybroadcast;
import java.util.List;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import net.kyori.adventure.text.minimessage.MiniMessage;
public class PrefixCommand implements SimpleCommand {
private final VelocityBroadcast plugin;
public PrefixCommand(VelocityBroadcast plugin) {
this.plugin = plugin;
}
@Override
public void execute(Invocation invocation) {
CommandSource source = invocation.source();
List<String> args = List.of(invocation.arguments());
if (!source.hasPermission("vb.admin")) {
source.sendMessage(MiniMessage.miniMessage().deserialize("<red>You don't have permission to change the broadcast prefix.</red>"));
return;
}
if (args.isEmpty()) {
source.sendMessage(MiniMessage.miniMessage().deserialize("<red>Usage: /vb prefix <newPrefix></red>"));
return;
}
String newPrefix = String.join(" ", args);
plugin.getConfigHandler().setPrefix(newPrefix);
source.sendMessage(MiniMessage.miniMessage().deserialize(
"<green>Broadcast prefix updated to:</green> <gray>" + newPrefix + "</gray>"
));
if (plugin.getConfigHandler().isDebugEnabled()) {
plugin.getLogger().info("[Prefix] Updated prefix to: " + newPrefix);
}
}
}

View File

@ -0,0 +1,33 @@
package com.adzel.velocitybroadcast;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import net.kyori.adventure.text.minimessage.MiniMessage;
import java.util.List;
public class ReloadCommand implements SimpleCommand {
private final VelocityBroadcast plugin;
public ReloadCommand(VelocityBroadcast plugin) {
this.plugin = plugin;
}
@Override
public void execute(Invocation invocation) {
CommandSource source = invocation.source();
if (!source.hasPermission("vb.admin")) {
source.sendMessage(MiniMessage.miniMessage().deserialize("<red>You don't have permission to reload the config.</red>"));
return;
}
plugin.getConfigHandler().reload();
source.sendMessage(MiniMessage.miniMessage().deserialize("<green>VelocityBroadcast config reloaded.</green>"));
if (plugin.getConfigHandler().isDebugEnabled()) {
plugin.getLogger().info("[Reload] Config reloaded by " + source.toString());
}
}
}

View File

@ -0,0 +1,116 @@
package com.adzel.velocitybroadcast;
import java.util.List;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
public class VBCommand implements SimpleCommand {
private final VelocityBroadcast plugin;
private final MiniMessage mm = MiniMessage.miniMessage();
public VBCommand(VelocityBroadcast plugin) {
this.plugin = plugin;
}
@Override
public void execute(Invocation invocation) {
CommandSource source = invocation.source();
List<String> args = List.of(invocation.arguments());
if (args.isEmpty() || args.get(0).equalsIgnoreCase("help")) {
source.sendMessage(parseFormatted("<gold><bold>VelocityBroadcast Commands:</bold></gold>"));
source.sendMessage(parseFormatted("<yellow>/vb <message></yellow> <gray>- Broadcast a message to all players</gray>"));
if (source.hasPermission("vb.admin")) {
source.sendMessage(parseFormatted("<yellow>/vb prefix <newPrefix></yellow> <gray>- Change the broadcast prefix</gray>"));
source.sendMessage(parseFormatted("<yellow>/vb reload</yellow> <gray>- Reload the plugin config</gray>"));
}
return;
}
String sub = args.get(0).toLowerCase();
List<String> subArgs = args.subList(1, args.size());
switch (sub) {
case "prefix":
if (!source.hasPermission("vb.admin")) {
source.sendMessage(parseFormatted("<red>You don't have permission to change the broadcast prefix.</red>"));
return;
}
if (subArgs.isEmpty()) {
source.sendMessage(parseFormatted("<red>Usage: /vb prefix <newPrefix></red>"));
return;
}
String newPrefix = String.join(" ", subArgs);
plugin.getConfigHandler().setPrefix(newPrefix);
source.sendMessage(parseFormatted("<green>Prefix updated to:</green> <gray>" + newPrefix + "</gray>"));
if (plugin.getConfigHandler().isDebugEnabled()) {
plugin.getLogger().info("[Prefix] Updated prefix to: " + newPrefix);
}
break;
case "reload":
if (!source.hasPermission("vb.admin")) {
source.sendMessage(parseFormatted("<red>You don't have permission to reload the config.</red>"));
return;
}
plugin.getConfigHandler().reload();
source.sendMessage(parseFormatted("<green>VelocityBroadcast config reloaded.</green>"));
if (plugin.getConfigHandler().isDebugEnabled()) {
plugin.getLogger().info("[Reload] Config reloaded by " + source.toString());
}
break;
default:
// Treat as broadcast message
if (!source.hasPermission("vb.broadcast")) {
source.sendMessage(parseFormatted("<red>You don't have permission to broadcast.</red>"));
return;
}
String fullMessage = plugin.getConfigHandler().getPrefix() + String.join(" ", args);
Component broadcast = parseFormatted(fullMessage);
plugin.getServer().getAllPlayers().forEach(p -> p.sendMessage(broadcast));
if (plugin.getConfigHandler().isDebugEnabled()) {
plugin.getLogger().info("[Broadcast] Sent: " + fullMessage);
}
break;
}
}
private Component parseFormatted(String input) {
String mini = input
.replace("&0", "<black>")
.replace("&1", "<dark_blue>")
.replace("&2", "<dark_green>")
.replace("&3", "<dark_aqua>")
.replace("&4", "<dark_red>")
.replace("&5", "<dark_purple>")
.replace("&6", "<gold>")
.replace("&7", "<gray>")
.replace("&8", "<dark_gray>")
.replace("&9", "<blue>")
.replace("&a", "<green>")
.replace("&b", "<aqua>")
.replace("&c", "<red>")
.replace("&d", "<light_purple>")
.replace("&e", "<yellow>")
.replace("&f", "<white>")
.replace("&l", "<bold>")
.replace("&n", "<underlined>")
.replace("&o", "<italic>")
.replace("&m", "<strikethrough>")
.replace("&r", "<reset>");
return mm.deserialize(mini);
}
}

View File

@ -0,0 +1,81 @@
package com.adzel.velocitybroadcast;
import java.nio.file.Path;
import org.slf4j.Logger;
import com.google.inject.Inject;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.minimessage.MiniMessage;
@Plugin(
id = "velocitybroadcast",
name = "VelocityBroadcast",
version = "0.2-pre",
description = "A proxy-wide broadcast plugin for Velocity.",
authors = {"Adzel"}
)
public class VelocityBroadcast {
public static final String PLUGIN_VERSION = "0.2-pre";
public static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();
private final ProxyServer server;
private final Logger logger;
private final Path dataDirectory;
private ConfigHandler config;
@Inject
public VelocityBroadcast(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
this.server = server;
this.logger = logger;
this.dataDirectory = dataDirectory;
}
@Subscribe(order = PostOrder.EARLY)
public void onProxyInitialization(ProxyInitializeEvent event) {
// Load config
this.config = new ConfigHandler(dataDirectory.resolve("config.yml"), logger);
config.load();
if (config.isDebugEnabled()) {
logger.info("[VelocityBroadcast] Debug mode is enabled.");
}
// Register the root /vb command handler
server.getCommandManager().register(
server.getCommandManager().metaBuilder("vb").plugin(this).build(),
new VBCommand(this)
);
logger.info("[VelocityBroadcast] Loaded VelocityBroadcast v" + PLUGIN_VERSION);
}
@Subscribe
public void onLogin(LoginEvent event) {
if (event.getPlayer().hasPermission("vb.admin") && config.isVersionCheckEnabled()) {
event.getPlayer().sendMessage(
MINI_MESSAGE.deserialize("<yellow>[VelocityBroadcast] You're running version " + PLUGIN_VERSION + ".</yellow>")
);
}
}
public ProxyServer getServer() {
return server;
}
public Logger getLogger() {
return logger;
}
public ConfigHandler getConfigHandler() {
return config;
}
}

View File

@ -0,0 +1,9 @@
{
"id": "velocitybroadcast",
"name": "VelocityBroadcast",
"version": "0.2-pre",
"authors": ["Adzel"],
"main": "com.adzel.velocitybroadcast.VelocityBroadcast",
"description": "A proxy-wide broadcast plugin for Velocity.",
"website": "https://github.com/AdzelFirestar/VelocityBroadcast"
}