Skip to content

Commit

Permalink
GH-99 Support PlaceholderAPI Relational Placeholder (#122)
Browse files Browse the repository at this point in the history
* Support PlaceholderAPI Relational Placeholder

* Use optional. Rename Bi -> Relational, otherTarget -> viewer.

---------

Co-authored-by: Rollczi <[email protected]>
Co-authored-by: Martin Sulikowski <[email protected]>
  • Loading branch information
3 people authored Jun 2, 2024
1 parent 39cfdc9 commit b364d55
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ public ChatFormatterPlugin(Plugin plugin) {
PluginConfig pluginConfig = configManager.getPluginConfig();

this.placeholderRegistry = new PlaceholderRegistry();

PlaceholderAPIStack placeholderAPIStack = new PlaceholderAPIStack();
this.placeholderRegistry.playerStack(new ConfiguredPlaceholderAPIStack(pluginConfig));
this.placeholderRegistry.playerStack(new PlaceholderAPIStack());
this.placeholderRegistry.playerStack(placeholderAPIStack);
this.placeholderRegistry.playerRelationalStack(placeholderAPIStack);


this.templateService = new TemplateService(pluginConfig);
this.rankProvider = new VaultRankProvider(server);
UpdaterService updaterService = new UpdaterService(plugin.getDescription());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternalcode.formatter;

import java.util.Optional;
import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection;

import com.eternalcode.formatter.adventure.TextColorTagResolver;
Expand Down Expand Up @@ -90,11 +91,14 @@ class ChatHandlerImpl implements ChatHandler {
@Override
public ChatRenderedMessage process(ChatMessage chatMessage) {
Player sender = chatMessage.sender();
Optional<Player> viewer = chatMessage.viewer();

String format = this.settings.getRawFormat(this.rankProvider.getRank(sender));

format = this.templateService.applyTemplates(format);
format = this.placeholderRegistry.format(format, sender);
format = viewer.isEmpty()
? this.placeholderRegistry.format(format, sender)
: this.placeholderRegistry.format(format, sender, viewer.get());

format = Legacy.clearSection(format);
format = Legacy.legacyToAdventure(format);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.eternalcode.formatter;

import java.util.Optional;
import org.bukkit.entity.Player;

public record ChatMessage(Player sender, String jsonMessage) {

public record ChatMessage(Player sender, Optional<Player> viewer, String jsonMessage) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.entity.Player;
import com.eternalcode.formatter.placeholder.PlayerPlaceholderStack;

public class PlaceholderAPIStack implements PlayerPlaceholderStack {
public class PlaceholderAPIStack implements PlayerPlaceholderStack, PlayerRelationalPlaceholderStack {

@Override
public String apply(String text, Player target) {
return PlaceholderAPI.setPlaceholders(target, text);
}

@Override
public String apply(String text, Player target, Player viewer) {
return PlaceholderAPI.setRelationalPlaceholders(target, viewer, text);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ public class PlaceholderRegistry {

private final Map<String, Placeholder> placeholders = new HashMap<>();
private final Map<String, PlayerPlaceholder> playerPlaceholders = new HashMap<>();
private final Map<String, PlayerRelationalPlaceholder> playerRelationalPlaceholders = new HashMap<>();

private final Set<PlaceholderStack> stacks = new HashSet<>();
private final Set<PlayerPlaceholderStack> playerStacks = new HashSet<>();
private final Set<PlayerRelationalPlaceholderStack> playerRelationalStacks = new HashSet<>();

public void placeholder(String key, Placeholder placeholder) {
this.placeholders.put(key, placeholder);
Expand All @@ -23,6 +25,10 @@ public void playerPlaceholder(String key, PlayerPlaceholder placeholder) {
this.playerPlaceholders.put(key, placeholder);
}

public void playerRelationalPlaceholder(String key, PlayerRelationalPlaceholder placeholder) {
this.playerRelationalPlaceholders.put(key, placeholder);
}

public void stack(PlaceholderStack stack) {
this.stacks.add(stack);
}
Expand All @@ -31,6 +37,10 @@ public void playerStack(PlayerPlaceholderStack stack) {
this.playerStacks.add(stack);
}

public void playerRelationalStack(PlayerRelationalPlaceholderStack stack) {
this.playerRelationalStacks.add(stack);
}

public String format(String text) {
for (PlaceholderStack stack : this.stacks) {
text = stack.apply(text);
Expand All @@ -55,4 +65,16 @@ public String format(String text, Player target) {
return this.format(text);
}

public String format(String text, Player target, Player viewer) {
for (PlayerRelationalPlaceholderStack stack : this.playerRelationalStacks) {
text = stack.apply(text, target, viewer);
}

for (Map.Entry<String, PlayerRelationalPlaceholder> entry : this.playerRelationalPlaceholders.entrySet()) {
text = text.replace(entry.getKey(), entry.getValue().extract(target, viewer));
}

return this.format(text, target);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.eternalcode.formatter.placeholder;

import org.bukkit.entity.Player;

@FunctionalInterface
public interface PlayerRelationalPlaceholder {

String extract(Player target, Player viewer);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.eternalcode.formatter.placeholder;

import org.bukkit.entity.Player;

public interface PlayerRelationalPlaceholderStack {

String apply(String text, Player target, Player viewer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import com.eternalcode.formatter.ChatMessage;
import com.eternalcode.formatter.ChatRenderedMessage;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventException;
import org.bukkit.event.Listener;
import org.bukkit.plugin.EventExecutor;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

class PaperChatEventExecutor implements EventExecutor {

private final static GsonComponentSerializer GSON = GsonComponentSerializer.gson();
private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson();

@Override
public void execute(@NotNull Listener listener, @NotNull Event event) throws EventException {
Expand All @@ -27,8 +31,10 @@ public void execute(@NotNull Listener listener, @NotNull Event event) throws Eve

paperEvent.renderer((source, sourceDisplayName, message, viewer) -> {
String jsonMessage = GSON.serialize(message);
Optional<Player> optionalViewer = viewer.get(Identity.UUID)
.map(uuid -> source.getServer().getPlayer(uuid));

ChatMessage chatMessage = new ChatMessage(source, jsonMessage);
ChatMessage chatMessage = new ChatMessage(source, optionalViewer, jsonMessage);
ChatRenderedMessage result = handler.process(chatMessage);

return GSON.deserialize(result.jsonMessage());
Expand Down

0 comments on commit b364d55

Please sign in to comment.