From 3ac7da47083a23dd773c54338d905a116ee2d032 Mon Sep 17 00:00:00 2001 From: MattMX Date: Tue, 8 Oct 2024 16:17:25 +0100 Subject: [PATCH] :fire: Add text formatting options #18 --- .../java/com/mattmx/nametags/NameTags.java | 16 ++++++-- .../config/TextDisplayMetaConfiguration.java | 12 ++++-- .../mattmx/nametags/config/TextFormatter.java | 38 +++++++++++++++++++ src/main/resources/config.yml | 9 +++++ 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/mattmx/nametags/config/TextFormatter.java diff --git a/src/main/java/com/mattmx/nametags/NameTags.java b/src/main/java/com/mattmx/nametags/NameTags.java index 699d4d4..5f6b5e7 100644 --- a/src/main/java/com/mattmx/nametags/NameTags.java +++ b/src/main/java/com/mattmx/nametags/NameTags.java @@ -3,8 +3,8 @@ import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.PacketEventsAPI; import com.mattmx.nametags.config.ConfigDefaultsListener; +import com.mattmx.nametags.config.TextFormatter; import com.mattmx.nametags.entity.NameTagEntityManager; -import com.mattmx.nametags.hook.GlowingEffectHook; import com.mattmx.nametags.hook.NeznamyTABHook; import me.tofaa.entitylib.APIConfig; import me.tofaa.entitylib.EntityLib; @@ -12,7 +12,6 @@ import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.event.HandlerList; import org.bukkit.permissions.Permission; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; @@ -25,7 +24,8 @@ public class NameTags extends JavaPlugin { public static final int TRANSPARENT = Color.fromARGB(0).asARGB(); private static @Nullable NameTags instance; - private HashMap groups = new HashMap<>(); + private final HashMap groups = new HashMap<>(); + private @NotNull TextFormatter formatter = TextFormatter.MINI_MESSAGE; private NameTagEntityManager entityManager; private final EventsListener eventsListener = new EventsListener(this); private final OutgoingPacketListener packetListener = new OutgoingPacketListener(this); @@ -65,6 +65,12 @@ public void onEnable() { public void reloadConfig() { super.reloadConfig(); + String textFormatterIdentifier = getConfig().getString("formatter", "minimessage"); + formatter = TextFormatter.getById(textFormatterIdentifier) + .orElse(TextFormatter.MINI_MESSAGE); + + getLogger().info("Using " + formatter.name() + " as text formatter."); + for (String permissionNode : groups.keySet()) { Bukkit.getPluginManager().removePermission(permissionNode); } @@ -94,6 +100,10 @@ public HashMap getGroups() { return groups; } + public @NotNull TextFormatter getFormatter() { + return this.formatter; + } + public static @NotNull NameTags getInstance() { return Objects.requireNonNull(instance, "NameTags plugin has not initialized yet! Did you forget to depend?"); } diff --git a/src/main/java/com/mattmx/nametags/config/TextDisplayMetaConfiguration.java b/src/main/java/com/mattmx/nametags/config/TextDisplayMetaConfiguration.java index ce91022..905610c 100644 --- a/src/main/java/com/mattmx/nametags/config/TextDisplayMetaConfiguration.java +++ b/src/main/java/com/mattmx/nametags/config/TextDisplayMetaConfiguration.java @@ -23,10 +23,12 @@ public class TextDisplayMetaConfiguration { public static boolean applyTextMeta(@NotNull ConfigurationSection section, @NotNull TextDisplayMeta to, @NotNull Player self, @NotNull Player sender) { Stream stream = section.getStringList("text") .stream() - .map((line) -> convertToComponent(self, sender, line)) - .filter((line) -> line != Component.empty() && !line.children().stream().allMatch((c) -> c == Component.empty())); + .map((line) -> convertToComponent(self, sender, line)); - // TODO(matt): Test + Use config for filtering empty lines + // TODO(matt): Test + if (NameTags.getInstance().getConfig().getBoolean("defaults.remove-empty-lines", false)) { + stream = stream.filter((line) -> line != Component.empty() && !line.children().stream().allMatch((c) -> c == Component.empty())); + } Component text = stream .reduce((a, b) -> a.append(Component.newline()).append(b)) @@ -175,7 +177,9 @@ private static Component convertToComponent(Player self, Player sending, String formatted = PapiHook.setPlaceholders(self, sending, formatted); - return MiniMessage.miniMessage().deserialize(formatted); + return NameTags.getInstance() + .getFormatter() + .format(formatted); } } diff --git a/src/main/java/com/mattmx/nametags/config/TextFormatter.java b/src/main/java/com/mattmx/nametags/config/TextFormatter.java new file mode 100644 index 0000000..65fe363 --- /dev/null +++ b/src/main/java/com/mattmx/nametags/config/TextFormatter.java @@ -0,0 +1,38 @@ +package com.mattmx.nametags.config; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.Optional; +import java.util.function.Function; + +public enum TextFormatter { + MINI_MESSAGE( + "minimessage", + (line) -> MiniMessage.miniMessage().deserialize(line) + ), + LEGACY( + "legacy", + (line) -> LegacyComponentSerializer.legacyAmpersand().deserialize(line) + ) + ; + + private final @NotNull String identifier; + private final @NotNull Function formatter; + + TextFormatter(@NotNull String identifier, @NotNull Function formatter) { + this.identifier = identifier; + this.formatter = formatter; + } + + public @NotNull Component format(@NotNull String line) { + return formatter.apply(line); + } + + public static @NotNull Optional getById(@NotNull String identifier) { + return Arrays.stream(values()).filter((f) -> f.identifier.equalsIgnoreCase(identifier)).findFirst(); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c75ee13..c3bdcb6 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -23,6 +23,8 @@ defaults: range: default # Gap above the head, closest to vanilla is 0.2 gap: 0.2 + # Should the plugin remove any empty lines? + remove-empty-lines: false # Should the player see their own tag? show-self: false @@ -52,6 +54,13 @@ groups: staff: background: red +# Formatting. +# Select a formatter for your text, default is minimessage. +# Options: +# - minimessage +# - legacy +formatter: minimessage + # Extra features # These will likely not work yet since they're experimental. extra: