#6: Add {SERVER} placeholder to the BungeeCord plugin

This commit is contained in:
2023-09-25 19:32:01 +02:00
parent c9ab8c98d8
commit ae6ef2b887
6 changed files with 74 additions and 31 deletions
@@ -90,13 +90,8 @@ public class BungeeStaffChatCommand extends Command {
+ String.format(config.getString("messages.toggled", ""), state))); + String.format(config.getString("messages.toggled", ""), state)));
} }
@Contract("_ -> new")
private @NotNull String clr(@NotNull String string) {
return ChatColor.translateAlternateColorCodes('&', string);
}
@Contract("_ -> new") @Contract("_ -> new")
private @NotNull BaseComponent[] txt(@NotNull String text) { private @NotNull BaseComponent[] txt(@NotNull String text) {
return TextComponent.fromLegacyText(clr(text)); return TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', text));
} }
} }
@@ -2,15 +2,18 @@ package me.oskar3123.staffchat.bungee.listener;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import me.oskar3123.staffchat.bungee.BungeeMain; import me.oskar3123.staffchat.bungee.BungeeMain;
import me.oskar3123.staffchat.bungee.event.BungeeStaffChatEvent; import me.oskar3123.staffchat.bungee.event.BungeeStaffChatEvent;
import me.oskar3123.staffchat.util.StringUtils; import me.oskar3123.staffchat.bungee.util.BungeeFormatUtils;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
@@ -59,26 +62,30 @@ public class BungeeChatListener implements Listener {
format = chatEvent.getFormat(); format = chatEvent.getFormat();
message = chatEvent.getMessage(); message = chatEvent.getMessage();
format = format.replaceAll("\\{NAME}", StringUtils.sanitize(player.getName())); String appliedFormat =
format = format.replaceAll("\\{MESSAGE}", StringUtils.sanitize(message)); BungeeFormatUtils.replacePlaceholders(
final BaseComponent[] messageComponents = txt(format); format,
s -> ChatColor.translateAlternateColorCodes('&', s),
() ->
Optional.ofNullable(player.getServer())
.map(Server::getInfo)
.map(ServerInfo::getName)
.orElse(""),
player::getName,
message);
final BaseComponent[] messageComponents = txt(appliedFormat);
plugin.getProxy().getPlayers().stream() plugin.getProxy().getPlayers().stream()
.filter(p -> p.hasPermission(plugin.seePerm)) .filter(p -> p.hasPermission(plugin.seePerm))
.forEach(p -> p.sendMessage(messageComponents)); .forEach(p -> p.sendMessage(messageComponents));
plugin.getLogger().info(ChatColor.stripColor(clr(format))); plugin.getLogger().info(ChatColor.stripColor(appliedFormat));
event.setCancelled(true); event.setCancelled(true);
} }
@Contract("_ -> new")
private @NotNull String clr(@NotNull String string) {
return ChatColor.translateAlternateColorCodes('&', string);
}
@Contract("_ -> new") @Contract("_ -> new")
private @NotNull BaseComponent[] txt(@NotNull String text) { private @NotNull BaseComponent[] txt(@NotNull String text) {
return TextComponent.fromLegacyText(clr(text)); return TextComponent.fromLegacyText(text);
} }
public boolean togglePlayer(@NotNull UUID player) { public boolean togglePlayer(@NotNull UUID player) {
@@ -0,0 +1,20 @@
package me.oskar3123.staffchat.bungee.util;
import java.util.function.Function;
import java.util.function.Supplier;
import me.oskar3123.staffchat.util.FormatUtils;
import me.oskar3123.staffchat.util.StringUtils;
public class BungeeFormatUtils {
public static String replacePlaceholders(
String string,
Function<String, String> colorize,
Supplier<String> serverSupplier,
Supplier<String> nameSupplier,
String message) {
string =
string.replaceAll("\\{SERVER}", StringUtils.sanitize(colorize.apply(serverSupplier.get())));
return FormatUtils.replacePlaceholders(string, colorize, nameSupplier, message);
}
}
@@ -14,9 +14,11 @@ import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Supplier;
import java.util.logging.Level;
import me.oskar3123.staffchat.spigot.Main; import me.oskar3123.staffchat.spigot.Main;
import me.oskar3123.staffchat.spigot.event.StaffChatEvent; import me.oskar3123.staffchat.spigot.event.StaffChatEvent;
import me.oskar3123.staffchat.util.StringUtils; import me.oskar3123.staffchat.util.FormatUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@@ -62,7 +64,7 @@ public class StaffChatHandler {
format = plugin.replacePlaceholders(event.getPlayer(), format); format = plugin.replacePlaceholders(event.getPlayer(), format);
sendStaffChatMessage(format, event.getPlayer().getName(), message); sendStaffChatMessage(format, event.getPlayer()::getName, message);
if (plugin.getConfig().getBoolean("discordsrv.enable")) { if (plugin.getConfig().getBoolean("discordsrv.enable")) {
sendDiscordMessage(event.getPlayer(), message); sendDiscordMessage(event.getPlayer(), message);
@@ -71,11 +73,10 @@ public class StaffChatHandler {
event.setCancelled(true); event.setCancelled(true);
} }
public void sendStaffChatMessage(String format, String name, String message) { public void sendStaffChatMessage(String format, Supplier<String> nameSupplier, String message) {
format = format.replaceAll("\\{NAME}", StringUtils.sanitize(name)); final String finalMessage =
format = ChatColor.translateAlternateColorCodes('&', format); FormatUtils.replacePlaceholders(
format = format.replaceAll("\\{MESSAGE}", StringUtils.sanitize(message)); format, s -> ChatColor.translateAlternateColorCodes('&', s), nameSupplier, message);
final String finalMessage = format;
if (plugin.getConfig().getBoolean("settings.sendmessagestoallservers")) { if (plugin.getConfig().getBoolean("settings.sendmessagestoallservers")) {
// Find any player and send using that object // Find any player and send using that object
@@ -142,12 +143,14 @@ public class StaffChatHandler {
private void sendDiscordMessage(Player player, String message) { private void sendDiscordMessage(Player player, String message) {
if (Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")) { if (Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")) {
String format = String format =
plugin plugin.replacePlaceholders(
.replacePlaceholders( player, plugin.getConfig().getString("discordsrv.minecraft-to-discord-format", ""));
player, format =
plugin.getConfig().getString("discordsrv.minecraft-to-discord-format", "")) FormatUtils.replacePlaceholders(
.replaceAll("\\{NAME}", StringUtils.sanitize(player.getName())) format,
.replaceAll("\\{MESSAGE}", StringUtils.sanitize(message)); s -> ChatColor.translateAlternateColorCodes('&', s),
player::getName,
message);
DiscordUtil.sendMessage( DiscordUtil.sendMessage(
DiscordSRV.getPlugin() DiscordSRV.getPlugin()
.getOptionalTextChannel( .getOptionalTextChannel(
@@ -46,7 +46,7 @@ public class DiscordSrvListener {
plugin plugin
.getConfig() .getConfig()
.getString("discordsrv.discord-to-minecraft-format", ""), .getString("discordsrv.discord-to-minecraft-format", ""),
name, () -> name,
message)); message));
}); });
} }
@@ -0,0 +1,18 @@
package me.oskar3123.staffchat.util;
import java.util.function.Function;
import java.util.function.Supplier;
public class FormatUtils {
public static String replacePlaceholders(
String string,
Function<String, String> colorize,
Supplier<String> nameSupplier,
String message) {
string =
string.replaceAll("\\{NAME}", StringUtils.sanitize(colorize.apply(nameSupplier.get())));
string = string.replaceAll("\\{MESSAGE}", StringUtils.sanitize(message));
return string;
}
}