Move formatting logic into enum function

This commit is contained in:
William278 2023-03-12 13:35:42 +00:00
parent 6df397b77a
commit 36fa0e2625
4 changed files with 24 additions and 18 deletions

View File

@ -7,9 +7,6 @@ import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import de.themoep.minedown.adventure.MineDown;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.william278.annotaml.Annotaml; import net.william278.annotaml.Annotaml;
import net.william278.velocitab.config.Settings; import net.william278.velocitab.config.Settings;
import net.william278.velocitab.hook.Hook; import net.william278.velocitab.hook.Hook;
@ -104,16 +101,6 @@ public class Velocitab {
Hook.AVAILABLE.forEach(availableHook -> availableHook.apply(this).ifPresent(hooks::add)); Hook.AVAILABLE.forEach(availableHook -> availableHook.apply(this).ifPresent(hooks::add));
} }
@NotNull
public Component formatText(@NotNull String text, @NotNull TabPlayer player) {
return switch (getSettings().getFormatter()) {
case MINEDOWN -> new MineDown(text).toComponent();
case MINIMESSAGE -> getMiniPlaceholdersHook()
.map(hook -> hook.format(text, player.getPlayer()))
.orElse(MiniMessage.miniMessage().deserialize(text));
};
}
private void prepareScoreboardManager() { private void prepareScoreboardManager() {
this.scoreboardManager = new ScoreboardManager(this); this.scoreboardManager = new ScoreboardManager(this);
scoreboardManager.registerPacket(); scoreboardManager.registerPacket();

View File

@ -1,11 +1,16 @@
package net.william278.velocitab.config; package net.william278.velocitab.config;
import de.themoep.minedown.adventure.MineDown;
import lombok.Getter; 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.YamlComment;
import net.william278.annotaml.YamlFile; import net.william278.annotaml.YamlFile;
import net.william278.annotaml.YamlKey; import net.william278.annotaml.YamlKey;
import net.william278.velocitab.Velocitab; 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.apache.commons.text.StringEscapeUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -115,8 +120,22 @@ public class Settings {
/** /**
* Different formatting markup options for the TAB list * Different formatting markup options for the TAB list
*/ */
@SuppressWarnings("unused")
public enum Formatter { public enum Formatter {
MINEDOWN, MINEDOWN((text, player, plugin) -> new MineDown(text).toComponent()),
MINIMESSAGE 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);
}
} }
} }

View File

@ -41,7 +41,7 @@ public final class TabPlayer implements Comparable<TabPlayer> {
public CompletableFuture<Component> getDisplayName(@NotNull Velocitab plugin) { public CompletableFuture<Component> getDisplayName(@NotNull Velocitab plugin) {
final String serverGroup = plugin.getSettings().getServerGroup(getServerName()); final String serverGroup = plugin.getSettings().getServerGroup(getServerName());
return Placeholder.format(plugin.getSettings().getFormat(serverGroup), plugin, this) return Placeholder.format(plugin.getSettings().getFormat(serverGroup), plugin, this)
.thenApply(formatted -> plugin.formatText(formatted, this)); .thenApply(formatted -> plugin.getSettings().getFormatter().format(formatted, this, plugin));
} }

View File

@ -154,14 +154,14 @@ public class PlayerTabList {
public CompletableFuture<Component> getHeader(@NotNull TabPlayer player) { public CompletableFuture<Component> getHeader(@NotNull TabPlayer player) {
return Placeholder.format(plugin.getSettings().getHeader( return Placeholder.format(plugin.getSettings().getHeader(
plugin.getSettings().getServerGroup(player.getServerName())), plugin, player) plugin.getSettings().getServerGroup(player.getServerName())), plugin, player)
.thenApply(header -> plugin.formatText(header, player)); .thenApply(header -> plugin.getSettings().getFormatter().format(header, player, plugin));
} }
public CompletableFuture<Component> getFooter(@NotNull TabPlayer player) { public CompletableFuture<Component> getFooter(@NotNull TabPlayer player) {
return Placeholder.format(plugin.getSettings().getFooter( return Placeholder.format(plugin.getSettings().getFooter(
plugin.getSettings().getServerGroup(player.getServerName())), plugin, player) plugin.getSettings().getServerGroup(player.getServerName())), plugin, player)
.thenApply(footer -> plugin.formatText(footer, player)); .thenApply(footer -> plugin.getSettings().getFormatter().format(footer, player, plugin));
} }