Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #19

Merged
merged 2 commits into from
Oct 8, 2024
Merged

Dev #19

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -16,13 +16,21 @@

import java.util.Locale;
import java.util.Objects;
import java.util.stream.Stream;

public class TextDisplayMetaConfiguration {

public static boolean applyTextMeta(@NotNull ConfigurationSection section, @NotNull TextDisplayMeta to, @NotNull Player self, @NotNull Player sender) {
Component text = section.getStringList("text")
Stream<Component> stream = section.getStringList("text")
.stream()
.map((line) -> convertToComponent(self, sender, line))
.map((line) -> convertToComponent(self, sender, line));

// 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))
.orElse(null);

Expand Down Expand Up @@ -169,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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void updateSneak(boolean sneaking) {
previousBackgroundOpacity = color.getAlpha();
previousTextOpacity = meta.getTextOpacity();

// Not sure if this is vanilla behavior? Does only text opacity change??
meta.setBackgroundColor(withCustomSneakOpacity(color).asARGB());
meta.setTextOpacity((byte) getCustomOpacity());
} else {
Expand Down
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
Loading