forked from Upstream/Velocitab
Fix double underscore escaping still happening with MiniMessage, refactor formatter
This commit is contained in:
parent
0805a3114d
commit
ee820967de
@ -8,6 +8,7 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import net.william278.annotaml.Annotaml;
|
||||
import net.william278.velocitab.config.Formatter;
|
||||
import net.william278.velocitab.config.Settings;
|
||||
import net.william278.velocitab.hook.Hook;
|
||||
import net.william278.velocitab.hook.LuckPermsHook;
|
||||
@ -66,6 +67,11 @@ public class Velocitab {
|
||||
return settings;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Formatter getFormatter() {
|
||||
return getSettings().getFormatter();
|
||||
}
|
||||
|
||||
private void loadSettings() {
|
||||
try {
|
||||
settings = Annotaml.create(
|
||||
|
49
src/main/java/net/william278/velocitab/config/Formatter.java
Normal file
49
src/main/java/net/william278/velocitab/config/Formatter.java
Normal file
@ -0,0 +1,49 @@
|
||||
package net.william278.velocitab.config;
|
||||
|
||||
import de.themoep.minedown.adventure.MineDown;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.william278.velocitab.Velocitab;
|
||||
import net.william278.velocitab.player.TabPlayer;
|
||||
import org.apache.commons.lang3.function.TriFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Different formatting markup options for the TAB list
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public enum Formatter {
|
||||
MINEDOWN((text, player, plugin) -> new MineDown(text).toComponent(),
|
||||
(text) -> text.replace("__", "_\\_")),
|
||||
MINIMESSAGE((text, player, plugin) -> plugin.getMiniPlaceholdersHook()
|
||||
.map(hook -> hook.format(text, player.getPlayer()))
|
||||
.orElse(MiniMessage.miniMessage().deserialize(text)),
|
||||
(text) -> MiniMessage.miniMessage().escapeTags(text));
|
||||
|
||||
/**
|
||||
* Function to apply formatting to a string
|
||||
*/
|
||||
private final TriFunction<String, TabPlayer, Velocitab, Component> formatter;
|
||||
/**
|
||||
* Function to escape formatting characters in a string
|
||||
*/
|
||||
private final Function<String, String> escaper;
|
||||
|
||||
Formatter(@NotNull TriFunction<String, TabPlayer, Velocitab, Component> formatter, @NotNull Function<String, String> escaper) {
|
||||
this.formatter = formatter;
|
||||
this.escaper = escaper;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Component format(@NotNull String text, @NotNull TabPlayer player, @NotNull Velocitab plugin) {
|
||||
return formatter.apply(text, player, plugin);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String escape(@NotNull String text) {
|
||||
return escaper.apply(text);
|
||||
}
|
||||
|
||||
}
|
@ -22,23 +22,26 @@ public enum Placeholder {
|
||||
.orElse("")),
|
||||
CURRENT_DATE((plugin, player) -> DateTimeFormatter.ofPattern("dd MMM yyyy").format(LocalDateTime.now())),
|
||||
CURRENT_TIME((plugin, player) -> DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now())),
|
||||
USERNAME((plugin, player) -> escape(player.getPlayer().getUsername())),
|
||||
USERNAME((plugin, player) -> plugin.getFormatter().escape(player.getPlayer().getUsername())),
|
||||
SERVER((plugin, player) -> player.getServerName()),
|
||||
PING((plugin, player) -> Long.toString(player.getPlayer().getPing())),
|
||||
PREFIX((plugin, player) -> player.getRole().getPrefix().orElse("")),
|
||||
SUFFIX((plugin, player) -> player.getRole().getSuffix().orElse("")),
|
||||
ROLE((plugin, player) -> player.getRole().getName().orElse("")),
|
||||
DEBUG_TEAM_NAME((plugin, player) -> escape(player.getTeamName()));
|
||||
DEBUG_TEAM_NAME((plugin, player) -> plugin.getFormatter().escape(player.getTeamName()));
|
||||
|
||||
private final BiFunction<Velocitab, TabPlayer, String> formatter;
|
||||
/**
|
||||
* Function to replace placeholders with a real value
|
||||
*/
|
||||
private final BiFunction<Velocitab, TabPlayer, String> replacer;
|
||||
|
||||
Placeholder(@NotNull BiFunction<Velocitab, TabPlayer, String> formatter) {
|
||||
this.formatter = formatter;
|
||||
Placeholder(@NotNull BiFunction<Velocitab, TabPlayer, String> replacer) {
|
||||
this.replacer = replacer;
|
||||
}
|
||||
|
||||
public static CompletableFuture<String> format(@NotNull String format, @NotNull Velocitab plugin, @NotNull TabPlayer player) {
|
||||
public static CompletableFuture<String> replace(@NotNull String format, @NotNull Velocitab plugin, @NotNull TabPlayer player) {
|
||||
for (Placeholder placeholder : values()) {
|
||||
format = format.replace("%" + placeholder.name().toLowerCase() + "%", placeholder.formatter.apply(plugin, player));
|
||||
format = format.replace("%" + placeholder.name().toLowerCase() + "%", placeholder.replacer.apply(plugin, player));
|
||||
}
|
||||
final String replaced = format;
|
||||
|
||||
@ -47,9 +50,4 @@ public enum Placeholder {
|
||||
.orElse(CompletableFuture.completedFuture(replaced));
|
||||
}
|
||||
|
||||
// Replace __ so that it is not seen as underline when the string is formatted.
|
||||
@NotNull
|
||||
private static String escape(String replace) {
|
||||
return replace.replace("__", "_\\_");
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,11 @@
|
||||
package net.william278.velocitab.config;
|
||||
|
||||
|
||||
import de.themoep.minedown.adventure.MineDown;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.william278.annotaml.YamlComment;
|
||||
import net.william278.annotaml.YamlFile;
|
||||
import net.william278.annotaml.YamlKey;
|
||||
import net.william278.velocitab.Velocitab;
|
||||
import net.william278.velocitab.player.TabPlayer;
|
||||
import org.apache.commons.lang3.function.TriFunction;
|
||||
import org.apache.commons.text.StringEscapeUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@ -117,25 +112,4 @@ public class Settings {
|
||||
return updateRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Different formatting markup options for the TAB list
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public enum Formatter {
|
||||
MINEDOWN((text, player, plugin) -> new MineDown(text).toComponent()),
|
||||
MINIMESSAGE((text, player, plugin) -> plugin.getMiniPlaceholdersHook()
|
||||
.map(hook -> hook.format(text, player.getPlayer()))
|
||||
.orElse(MiniMessage.miniMessage().deserialize(text)));
|
||||
|
||||
private final TriFunction<String, TabPlayer, Velocitab, Component> formatter;
|
||||
|
||||
Formatter(@NotNull TriFunction<String, TabPlayer, Velocitab, Component> formatter) {
|
||||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Component format(@NotNull String text, @NotNull TabPlayer player, @NotNull Velocitab plugin) {
|
||||
return formatter.apply(text, player, plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,8 +40,8 @@ public final class TabPlayer implements Comparable<TabPlayer> {
|
||||
@NotNull
|
||||
public CompletableFuture<Component> getDisplayName(@NotNull Velocitab plugin) {
|
||||
final String serverGroup = plugin.getSettings().getServerGroup(getServerName());
|
||||
return Placeholder.format(plugin.getSettings().getFormat(serverGroup), plugin, this)
|
||||
.thenApply(formatted -> plugin.getSettings().getFormatter().format(formatted, this, plugin));
|
||||
return Placeholder.replace(plugin.getSettings().getFormat(serverGroup), plugin, this)
|
||||
.thenApply(formatted -> plugin.getFormatter().format(formatted, this, plugin));
|
||||
|
||||
}
|
||||
|
||||
|
@ -152,16 +152,16 @@ public class PlayerTabList {
|
||||
}
|
||||
|
||||
public CompletableFuture<Component> getHeader(@NotNull TabPlayer player) {
|
||||
return Placeholder.format(plugin.getSettings().getHeader(
|
||||
return Placeholder.replace(plugin.getSettings().getHeader(
|
||||
plugin.getSettings().getServerGroup(player.getServerName())), plugin, player)
|
||||
.thenApply(header -> plugin.getSettings().getFormatter().format(header, player, plugin));
|
||||
.thenApply(header -> plugin.getFormatter().format(header, player, plugin));
|
||||
|
||||
}
|
||||
|
||||
public CompletableFuture<Component> getFooter(@NotNull TabPlayer player) {
|
||||
return Placeholder.format(plugin.getSettings().getFooter(
|
||||
return Placeholder.replace(plugin.getSettings().getFooter(
|
||||
plugin.getSettings().getServerGroup(player.getServerName())), plugin, player)
|
||||
.thenApply(footer -> plugin.getSettings().getFormatter().format(footer, player, plugin));
|
||||
.thenApply(footer -> plugin.getFormatter().format(footer, player, plugin));
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user