Skip to content

Commit

Permalink
🔥 Add text formatting options #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Oct 8, 2024
1 parent 7226a47 commit 3ac7da4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/main/java/com/mattmx/nametags/NameTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
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;
import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform;
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;
Expand All @@ -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<String, ConfigurationSection> groups = new HashMap<>();
private final HashMap<String, ConfigurationSection> 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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -94,6 +100,10 @@ public HashMap<String, ConfigurationSection> 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?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public class TextDisplayMetaConfiguration {
public static boolean applyTextMeta(@NotNull ConfigurationSection section, @NotNull TextDisplayMeta to, @NotNull Player self, @NotNull Player sender) {
Stream<Component> 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))
Expand Down Expand Up @@ -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);
}

}
38 changes: 38 additions & 0 deletions src/main/java/com/mattmx/nametags/config/TextFormatter.java
Original file line number Diff line number Diff line change
@@ -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<String, Component> formatter;

TextFormatter(@NotNull String identifier, @NotNull Function<String, Component> formatter) {
this.identifier = identifier;
this.formatter = formatter;
}

public @NotNull Component format(@NotNull String line) {
return formatter.apply(line);
}

public static @NotNull Optional<TextFormatter> getById(@NotNull String identifier) {
return Arrays.stream(values()).filter((f) -> f.identifier.equalsIgnoreCase(identifier)).findFirst();
}
}
9 changes: 9 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3ac7da4

Please sign in to comment.