Files
src
build.gradle
settings.gradle
Code
5 168
src/main/java/com/yourmc/yourmotd/YourMCMOTDPlugin.java
package com.yourmc.yourmotd;
import java.io.File;
import java.util.Locale;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.plugin.java.JavaPlugin;
public final class YourMCMOTDPlugin extends JavaPlugin implements Listener, CommandExecutor {
private static final String DEFAULT_SERVER_NAME = "YOUR MC";
private static final String DEFAULT_SUBTITLE = "Gem SMP • Survival";
private static final String DEFAULT_TOP_LINE = "&8✦ &5▒▒▒▒▒ &d▓▓▓▓▓▓ &5▒▒▒▒▒ &8✧ &f&l{server-name}";
private static final String DEFAULT_BOTTOM_LINE = "&9✧ &1░&5▒▓&d●&5▓▒&1░ &8✦ &b{subtitle}";
private static final String DEFAULT_RELOAD_MESSAGE = "&aYourMCMOTD configuration reloaded!";
private static final Pattern HEX_COLOR = Pattern.compile("(?i)&#([0-9a-f]{6})");
@Override
public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents(this, this);
if (getCommand("yourmotd") == null) {
getLogger().severe("Command 'yourmotd' is missing from plugin.yml; reload command is unavailable.");
return;
}
getCommand("yourmotd").setExecutor(this);
}
@EventHandler
public void onServerListPing(ServerListPingEvent event) {
if (!getConfig().getBoolean("design.enabled", true)) {
return;
}
String serverName = configuredText("server-name", DEFAULT_SERVER_NAME);
String subtitle = configuredText("subtitle", DEFAULT_SUBTITLE);
String designType = configuredText("design.type", "planet").toLowerCase(Locale.ROOT);
if (designType.equals("planet")) {
String topLine = configuredText("design.top-line", DEFAULT_TOP_LINE);
String bottomLine = configuredText("design.bottom-line", DEFAULT_BOTTOM_LINE);
event.setMotd(formatLine(topLine, serverName, subtitle) + "\n"
+ formatLine(bottomLine, serverName, subtitle));
} else {
event.setMotd(colorize("&f&l" + serverName) + "\n" + colorize("&7" + subtitle));
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length != 1 || !args[0].equalsIgnoreCase("reload")) {
sender.sendMessage(colorize("&cUsage: /yourmotd reload"));
return true;
}
if (!sender.hasPermission("yourmotd.reload")) {
sender.sendMessage(colorize("&cYou do not have permission to do that."));
return true;
}
try {
if (!new File(getDataFolder(), "config.yml").exists()) {
saveDefaultConfig();
}
reloadConfig();
sender.sendMessage(colorize(configuredText("messages.reload", DEFAULT_RELOAD_MESSAGE)));
} catch (RuntimeException exception) {
getLogger().log(Level.WARNING, "Could not reload config.yml; the previous configuration remains active.", exception);
sender.sendMessage(colorize("&cYourMCMOTD could not reload its configuration."));
}
return true;
}
private String configuredText(String path, String fallback) {
String value = getConfig().getString(path);
if (value == null || value.isBlank()) {
return fallback;
}
return value.replace('\n', ' ').replace('\r', ' ');
}
private String formatLine(String template, String serverName, String subtitle) {
return colorize(template.replace("{server-name}", serverName).replace("{subtitle}", subtitle));
}
private String colorize(String text) {
Matcher matcher = HEX_COLOR.matcher(text);
StringBuilder converted = new StringBuilder();
int lastEnd = 0;
while (matcher.find()) {
converted.append(text, lastEnd, matcher.start()).append('§').append('x');
String hex = matcher.group(1);
for (int i = 0; i < hex.length(); i++) {
converted.append('§').append(hex.charAt(i));
}
lastEnd = matcher.end();
}
converted.append(text, lastEnd, text.length());
return ChatColor.translateAlternateColorCodes('&', converted.toString());
}
}