Merge pull request #17 from oskar3123/6-server-placeholder
#6: Add {SERVER} placeholder to the BungeeCord plugin
This commit is contained in:
@@ -90,13 +90,8 @@ public class BungeeStaffChatCommand extends Command {
|
||||
+ String.format(config.getString("messages.toggled", ""), state)));
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
private @NotNull String clr(@NotNull String string) {
|
||||
return ChatColor.translateAlternateColorCodes('&', string);
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
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.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import me.oskar3123.staffchat.bungee.BungeeMain;
|
||||
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.chat.BaseComponent;
|
||||
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.Server;
|
||||
import net.md_5.bungee.api.event.ChatEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
@@ -59,26 +62,30 @@ public class BungeeChatListener implements Listener {
|
||||
format = chatEvent.getFormat();
|
||||
message = chatEvent.getMessage();
|
||||
|
||||
format = format.replaceAll("\\{NAME}", StringUtils.sanitize(player.getName()));
|
||||
format = format.replaceAll("\\{MESSAGE}", StringUtils.sanitize(message));
|
||||
final BaseComponent[] messageComponents = txt(format);
|
||||
String appliedFormat =
|
||||
BungeeFormatUtils.replacePlaceholders(
|
||||
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()
|
||||
.filter(p -> p.hasPermission(plugin.seePerm))
|
||||
.forEach(p -> p.sendMessage(messageComponents));
|
||||
plugin.getLogger().info(ChatColor.stripColor(clr(format)));
|
||||
plugin.getLogger().info(ChatColor.stripColor(appliedFormat));
|
||||
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
private @NotNull String clr(@NotNull String string) {
|
||||
return ChatColor.translateAlternateColorCodes('&', string);
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
private @NotNull BaseComponent[] txt(@NotNull String text) {
|
||||
return TextComponent.fromLegacyText(clr(text));
|
||||
return TextComponent.fromLegacyText(text);
|
||||
}
|
||||
|
||||
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.Set;
|
||||
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.event.StaffChatEvent;
|
||||
import me.oskar3123.staffchat.util.StringUtils;
|
||||
import me.oskar3123.staffchat.util.FormatUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@@ -62,7 +64,7 @@ public class StaffChatHandler {
|
||||
|
||||
format = plugin.replacePlaceholders(event.getPlayer(), format);
|
||||
|
||||
sendStaffChatMessage(format, event.getPlayer().getName(), message);
|
||||
sendStaffChatMessage(format, event.getPlayer()::getName, message);
|
||||
|
||||
if (plugin.getConfig().getBoolean("discordsrv.enable")) {
|
||||
sendDiscordMessage(event.getPlayer(), message);
|
||||
@@ -71,11 +73,10 @@ public class StaffChatHandler {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
public void sendStaffChatMessage(String format, String name, String message) {
|
||||
format = format.replaceAll("\\{NAME}", StringUtils.sanitize(name));
|
||||
format = ChatColor.translateAlternateColorCodes('&', format);
|
||||
format = format.replaceAll("\\{MESSAGE}", StringUtils.sanitize(message));
|
||||
final String finalMessage = format;
|
||||
public void sendStaffChatMessage(String format, Supplier<String> nameSupplier, String message) {
|
||||
final String finalMessage =
|
||||
FormatUtils.replacePlaceholders(
|
||||
format, s -> ChatColor.translateAlternateColorCodes('&', s), nameSupplier, message);
|
||||
|
||||
if (plugin.getConfig().getBoolean("settings.sendmessagestoallservers")) {
|
||||
// Find any player and send using that object
|
||||
@@ -142,12 +143,14 @@ public class StaffChatHandler {
|
||||
private void sendDiscordMessage(Player player, String message) {
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")) {
|
||||
String format =
|
||||
plugin
|
||||
.replacePlaceholders(
|
||||
player,
|
||||
plugin.getConfig().getString("discordsrv.minecraft-to-discord-format", ""))
|
||||
.replaceAll("\\{NAME}", StringUtils.sanitize(player.getName()))
|
||||
.replaceAll("\\{MESSAGE}", StringUtils.sanitize(message));
|
||||
plugin.replacePlaceholders(
|
||||
player, plugin.getConfig().getString("discordsrv.minecraft-to-discord-format", ""));
|
||||
format =
|
||||
FormatUtils.replacePlaceholders(
|
||||
format,
|
||||
s -> ChatColor.translateAlternateColorCodes('&', s),
|
||||
player::getName,
|
||||
message);
|
||||
DiscordUtil.sendMessage(
|
||||
DiscordSRV.getPlugin()
|
||||
.getOptionalTextChannel(
|
||||
|
||||
@@ -46,7 +46,7 @@ public class DiscordSrvListener {
|
||||
plugin
|
||||
.getConfig()
|
||||
.getString("discordsrv.discord-to-minecraft-format", ""),
|
||||
name,
|
||||
() -> name,
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user