Skip to content

Commit

Permalink
🔥 Working relational placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Dec 21, 2024
1 parent c1f4037 commit 71b4a53
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.github.retrooper.packetevents.util.Vector3f;
import com.github.retrooper.packetevents.wrapper.play.server.*;
import com.mattmx.nametags.entity.NameTagEntity;
import com.mattmx.nametags.hook.PapiHook;
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
Expand Down Expand Up @@ -104,9 +105,12 @@ public void onPacketSend(@NotNull PacketSendEvent event) {
.legacyAmpersand()
.serialize(originalText);

// If it doesn't have any placeholders in then stop
if (!legacy.contains("%rel_")) return;

final Component appliedText = LegacyComponentSerializer
.legacyAmpersand()
.deserialize(PlaceholderAPI.setRelationalPlaceholders(from, to, legacy));
.deserialize(PapiHook.setRelationalPlaceholders(from, to, legacy));

if (!originalText.equals(appliedText)) {

Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/mattmx/nametags/TestPlaceholderExpansion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.mattmx.nametags;

import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Relational;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.text.DecimalFormat;

public class TestPlaceholderExpansion extends PlaceholderExpansion implements Relational {
@Override
public @NotNull String getIdentifier() {
return "dev";
}

@Override
public @NotNull String getAuthor() {
return "MattMX";
}

@Override
public @NotNull String getVersion() {
return "1.0.0";
}

private static final DecimalFormat decimalFormat = new DecimalFormat("#,###.0");

@Override
public String onPlaceholderRequest(Player one, Player two, String identifier) {
if (one == two) {
return "0.0";
}

double blocks = one.getLocation().distance(two.getLocation());
// double blocks = one.getLocation().distance(one.getLocation().getWorld().getSpawnLocation());

if (blocks > 1000) {
double km = blocks / 1000;
return decimalFormat.format(km) + "k";
} else {
return decimalFormat.format(blocks);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void registerDefaultRefreshListener(@NotNull NameTagEntity tag, long refr
(entity) -> {
synchronized (entity) {
TextDisplayMetaConfiguration.applyMeta(defaultSection(), entity.getMeta());
TextDisplayMetaConfiguration.applyTextMeta(defaultSection(), entity.getMeta(), player, player);
TextDisplayMetaConfiguration.applyTextMeta(defaultSection(), entity.getMeta(), player);

// TODO we should cache this stuff
List<Map.Entry<String, ConfigurationSection>> groups = plugin.getGroups()
Expand All @@ -80,7 +80,7 @@ public void registerDefaultRefreshListener(@NotNull NameTagEntity tag, long refr
Map.Entry<String, ConfigurationSection> highest = groups.getLast();

TextDisplayMetaConfiguration.applyMeta(highest.getValue(), entity.getMeta());
TextDisplayMetaConfiguration.applyTextMeta(highest.getValue(), entity.getMeta(), player, player);
TextDisplayMetaConfiguration.applyTextMeta(highest.getValue(), entity.getMeta(), player);

long groupRefresh = highest.getValue().getLong("refresh-every", -1);
if (groupRefresh > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

public class TextDisplayMetaConfiguration {

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

if (NameTags.getInstance().getConfig().getBoolean("defaults.remove-empty-lines", false)) {
stream = stream.filter(TextComponent.IS_NOT_EMPTY);
Expand Down Expand Up @@ -181,10 +181,10 @@ public static void applyMeta(@NotNull ConfigurationSection section, @NotNull Tex
String pitch = section.getString("pitch");
}

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

formatted = PapiHook.setPlaceholders(self, sending, formatted);
formatted = PapiHook.setPlaceholders(self, formatted);

return NameTags.getInstance()
.getFormatter()
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/mattmx/nametags/hook/PapiHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@ public class PapiHook {
public static boolean isPapi() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
public static String setPlaceholders(Player one, Player two, String text) {

public static String setPlaceholders(Player one, String text) {
if (!isPapi()) return text;

String formatted = text;

formatted = PlaceholderAPI.setRelationalPlaceholders(one, two, formatted);
formatted = PlaceholderAPI.setPlaceholders(one, formatted);

return formatted;
}

public static String setRelationalPlaceholders(Player one, Player two, String text) {
if (!isPapi()) return text;

String formatted = text;

formatted = PlaceholderAPI.setRelationalPlaceholders(one, two, formatted);

return formatted;
}

}

0 comments on commit 71b4a53

Please sign in to comment.