Skip to content

Commit

Permalink
🔥 Implemented default config options!
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Sep 11, 2024
1 parent 9d0cbd1 commit 16c8f8d
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 14 deletions.
24 changes: 11 additions & 13 deletions src/main/java/com/mattmx/nametags/NameTags.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.mattmx.nametags;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.util.Vector3f;
import com.mattmx.nametags.config.ConfigDefaultsListener;
import com.mattmx.nametags.entity.NameTagEntityManager;
import me.tofaa.entitylib.APIConfig;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.meta.display.AbstractDisplayMeta;
import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class NameTags extends JavaPlugin {
private static final int TRANSPARENT = Color.fromARGB(0).asARGB();
public static final int TRANSPARENT = Color.fromARGB(0).asARGB();
private static @Nullable NameTags instance;

private NameTagEntityManager entityManager;
Expand All @@ -28,15 +29,10 @@ public void onEnable() {
entityManager = new NameTagEntityManager();
saveDefaultConfig();

entityManager.setDefaultProvider((entity, meta) -> {
// Set to empty since we're updating automatically
meta.setText(Component.empty());
meta.setBillboardConstraints(AbstractDisplayMeta.BillboardConstraints.VERTICAL);
meta.setTranslation(new Vector3f(0f, 0.2f, 0f));
meta.setBackgroundColor(TRANSPARENT);
meta.setShadow(true);
meta.setViewRange(50f);
});
ConfigurationSection defaults = getConfig().getConfigurationSection("defaults");
if (defaults != null && defaults.getBoolean("enabled")) {
Bukkit.getPluginManager().registerEvents(new ConfigDefaultsListener(this), this);
}

SpigotEntityLibPlatform platform = new SpigotEntityLibPlatform(this);
APIConfig settings = new APIConfig(PacketEvents.getAPI())
Expand All @@ -52,6 +48,8 @@ public void onEnable() {
.registerListener(packetListener);

Bukkit.getPluginManager().registerEvents(eventsListener, this);

Objects.requireNonNull(Bukkit.getPluginCommand("nametags-reload")).setExecutor(new NameTagsCommand(this));
}

public @NotNull NameTagEntityManager getEntityManager() {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/mattmx/nametags/NameTagsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mattmx.nametags;

import net.kyori.adventure.text.Component;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

public class NameTagsCommand implements CommandExecutor {
private final @NotNull NameTags plugin;

public NameTagsCommand(@NotNull NameTags plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
this.plugin.reloadConfig();
sender.sendMessage(Component.text("Reloaded!"));
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.mattmx.nametags.config;

import com.mattmx.nametags.NameTags;
import com.mattmx.nametags.entity.trait.RefreshTrait;
import com.mattmx.nametags.event.NameTagEntityCreateEvent;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;

public class ConfigDefaultsListener implements Listener {
private final @NotNull NameTags plugin;

public ConfigDefaultsListener(@NotNull NameTags plugin) {
this.plugin = plugin;

NameTags.getInstance()
.getEntityManager()
.setDefaultProvider(((entity, meta) -> TextDisplayMetaConfiguration.applyMeta(section(), meta)));
}

private ConfigurationSection section() {
return plugin.getConfig().getConfigurationSection("defaults");
}

@EventHandler
public void onCreate(@NotNull NameTagEntityCreateEvent event) {
if (!(event.getNameTag().getBukkitEntity() instanceof Player player)) return;

event.getNameTag()
.getTraits()
.getOrAddTrait(RefreshTrait.class, () ->
RefreshTrait.ofSeconds(
NameTags.getInstance(),
2L,
(entity) -> {
TextDisplayMetaConfiguration.applyMeta(section(), entity.getMeta());
TextDisplayMetaConfiguration.applyTextMeta(section(), entity.getMeta(), player, player);
entity.getPassenger().refresh();
}
)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.mattmx.nametags.config;

import com.mattmx.nametags.NameTags;
import me.clip.placeholderapi.PlaceholderAPI;
import me.tofaa.entitylib.meta.display.AbstractDisplayMeta;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.Locale;
import java.util.Objects;

public class TextDisplayMetaConfiguration {

public static void applyTextMeta(@NotNull ConfigurationSection section, @NotNull TextDisplayMeta to, @NotNull Player self, @NotNull Player sender) {
Component text = section.getStringList("text")
.stream()
.map((line) -> convertToComponent(self, sender, line))
.reduce((a, b) -> a.append(Component.newline()).append(b))
.orElse(Component.empty());

if (!text.equals(to.getText())) {
to.setText(text);
}
}

public static void applyMeta(@NotNull ConfigurationSection section, @NotNull TextDisplayMeta to) {
String backgroundColor = section.getString("background", "0x40000000");
int background;

if (backgroundColor.equalsIgnoreCase("transparent")) {
background = NameTags.TRANSPARENT;
} else if (NamedTextColor.NAMES.value(backgroundColor) != null) {
background = 0x40000000 | Objects.requireNonNull(NamedTextColor.NAMES.value(backgroundColor)).value();
} else if (backgroundColor.startsWith("#")) {
background = new Color((int) Long.parseLong(backgroundColor.replace("#", ""), 16), true).getRGB();
} else {
background = NameTags.TRANSPARENT;
}

if (background != to.getBackgroundColor()) {
to.setBackgroundColor(background);
}

String billboardString = section.getString("billboard", "center");
AbstractDisplayMeta.BillboardConstraints billboard = AbstractDisplayMeta.BillboardConstraints.valueOf(billboardString.toUpperCase(Locale.ROOT));
if (billboard != to.getBillboardConstraints()) {
to.setBillboardConstraints(billboard);
}

boolean shadow = section.getBoolean("shadow");
if (shadow != to.isShadow()) {
to.setShadow(shadow);
}

String range = section.getString("range", "default");
float finalRange = range.equalsIgnoreCase("default")
? (Bukkit.getSimulationDistance() * 16f)
: Float.parseFloat(range);

if (finalRange != to.getViewRange()) {
to.setViewRange(finalRange);
}

String gap = section.getString("gap", "default");
float finalGap = gap.equalsIgnoreCase("default")
? 0.2f
: Float.parseFloat(gap);
if (finalGap != to.getTranslation().y) {
to.setTranslation(to.getTranslation().withY(finalGap));
}
}

private static Component convertToComponent(Player self, Player sending, String line) {
String formatted = line;

if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
formatted = PlaceholderAPI.setRelationalPlaceholders(self, sending, formatted);
formatted = PlaceholderAPI.setPlaceholders(self, formatted);
}

return MiniMessage.miniMessage().deserialize(formatted);
}

}
10 changes: 10 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ shift:
opacity: 80

defaults:
# If not enabled then plugin simulates vanilla name tags
enabled: true
text:
- "<white>%player_name%</white>"
- "<blue>%player_ping%ms</blue>"
background: black
billboard: center
shadow: true
range: default
gap: 0.2
6 changes: 5 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ depend:
- packetevents

softdepend:
- PlaceholderAPI
- PlaceholderAPI

commands:
nametags-reload:
description: Reload the config

0 comments on commit 16c8f8d

Please sign in to comment.