Skip to content

Commit

Permalink
Merge pull request #209 from BlitzOffline/feature/better-miniplacehol…
Browse files Browse the repository at this point in the history
…ders
  • Loading branch information
BlitzOffline authored Jul 10, 2023
2 parents 85a82b9 + 303a081 commit 5672faf
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,42 @@
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

/**
* Represents a placeholder that can be used in MiniMessage.
*/
public interface MiniPlaceholder {

/**
* Compiles the placeholder into a {@link TagResolver}.
*
* @param inMessage whether the placeholder is used in user sent message or in a format.
* @param sender the sender of the message.
* @param recipient the recipient of the message.
* @return the compiled placeholder.
* Compiles the placeholder to a {@link TagResolver}.
* @param context The context in which the placeholder is used.
* @return The compiled placeholder.
*/
@NotNull TagResolver toTagResolver(boolean inMessage, @NotNull ChatUser sender, @NotNull User recipient);
@NotNull TagResolver toTagResolver(final @NotNull Context context);

public interface Context {

/**
* The response is going to be true only and only if the placeholder is used in a user generated message such as
* a message sent in chat. If the placeholder is used in a format, or system message, the response is going to
* be false.
* @return Whether the placeholder is used in user sent message or in a format.
*/
boolean inMessage();

/**
* The sender of the message is going to be empty if {@link Context#inMessage()} returns false.
* @return the sender of the message
*/
@NotNull Optional<ChatUser> sender();

/**
* The recipient can be a user that receives a system message or a player that receives a message from another
* user.
* The recipient is going to be empty if the message is not sent to a specific user.
* @return The recipient of the message.
*/
@NotNull Optional<User> recipient();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package at.helpch.chatchat.api.placeholder;

import at.helpch.chatchat.api.user.ChatUser;
import at.helpch.chatchat.api.user.User;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.NotNull;

Expand All @@ -19,14 +17,12 @@ public interface MiniPlaceholderManager {
void addPlaceholder(@NotNull MiniPlaceholder placeholder);

/**
* Compiles all {@link MiniPlaceholder}s into a {@link TagResolver}.
* Compiles all {@link MiniPlaceholder}s into a single {@link TagResolver}.
*
* @param inMessage whether the tag resolver is used in a message or in a format
* @param sender the sender of the message
* @param recipient the recipient of the message
* @param context The context in which the placeholder is used.
* @return the compiled tags
*/
@NotNull TagResolver compileTags(boolean inMessage, @NotNull ChatUser sender, @NotNull User recipient);
@NotNull TagResolver compileTags(@NotNull MiniPlaceholder.Context context);

/**
* Get all placeholders.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import at.helpch.chatchat.ChatChatPlugin;
import at.helpch.chatchat.api.format.PriorityFormat;
import at.helpch.chatchat.api.user.ChatUser;
import at.helpch.chatchat.placeholder.MiniPlaceholderContext;
import at.helpch.chatchat.user.ConsoleUser;
import at.helpch.chatchat.util.FormatUtils;
import at.helpch.chatchat.util.MessageProcessor;
Expand Down Expand Up @@ -39,7 +40,7 @@ public void testFormat(
sender.player(),
sender.player(),
MessageProcessor.processMessage(plugin, sender, ConsoleUser.INSTANCE, message),
plugin.miniPlaceholdersManager().compileTags(false, sender, sender)
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(sender).recipient(sender).build())
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package at.helpch.chatchat.placeholder;

import at.helpch.chatchat.api.placeholder.MiniPlaceholder;
import at.helpch.chatchat.api.user.ChatUser;
import at.helpch.chatchat.api.user.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public class MiniPlaceholderContext implements MiniPlaceholder.Context {

private final boolean inMessage;
private final @Nullable ChatUser sender;
private final @Nullable User recipient;

private MiniPlaceholderContext(final @NotNull Builder builder) {
this.inMessage = builder.inMessage;
this.sender = builder.sender;
this.recipient = builder.recipient;
}

@Override
public boolean inMessage() {
return inMessage;
}

@Override
public @NotNull Optional<ChatUser> sender() {
return Optional.ofNullable(sender);
}

@Override
public @NotNull Optional<User> recipient() {
return Optional.ofNullable(recipient);
}

public @NotNull Builder toBuilder() {
return new Builder(this);
}

public static @NotNull Builder builder() {
return new Builder();
}

public static final class Builder {

private boolean inMessage = false;
private ChatUser sender = null;
private User recipient = null;

private Builder() {
}

private Builder(final @NotNull MiniPlaceholderContext context) {
this.inMessage = context.inMessage;
this.sender = context.sender;
this.recipient = context.recipient;
}

public @NotNull Builder inMessage(final boolean inMessage) {
this.inMessage = inMessage;
return this;
}

public @NotNull Builder sender(final @Nullable ChatUser sender) {
this.sender = sender;
return this;
}

public @NotNull Builder recipient(final @Nullable User recipient) {
this.recipient = recipient;
return this;
}

public @NotNull MiniPlaceholderContext build() {
return new MiniPlaceholderContext(this);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Setting;

import java.util.Optional;

// configurate requires non-final fields
@SuppressWarnings("FieldMayBeFinal")
Expand All @@ -19,80 +22,94 @@ public class MiniPlaceholderImpl implements MiniPlaceholder {

private static final String MINI_PLACEHOLDER_PERMISSION = MessageProcessor.TAG_BASE_PERMISSION + "placeholder.";

private final String name;
private final boolean requiresRecipient;
private final boolean parseMini;
private final boolean parsePapi;
private final boolean closing;
@Setting("name")
private final String tagName;
@Setting("requires-recipient")
private final boolean isRelationalTag;
@Setting("parse-mini")
private final boolean shouldParseMiniMessageTags;
@Setting("parse-papi")
private final boolean shouldParsePlaceholderAPIPlaceholders;
@Setting("closing")
private final boolean shouldAutoCloseTags;
@Setting("message")
private final String message;

public MiniPlaceholderImpl(
@NotNull final String name,
final boolean requiresRecipient,
final boolean parseMini,
final boolean parsePapi,
final boolean closing,
@NotNull final String tagName,
final boolean isRelationalTag,
final boolean shouldParseMiniMessageTags,
final boolean shouldParsePlaceholderAPIPlaceholders,
final boolean shouldAutoCloseTags,
@NotNull final String message
) {
this.name = name;
this.requiresRecipient = requiresRecipient;
this.parseMini = parseMini;
this.parsePapi = parsePapi;
this.closing = closing;
this.tagName = tagName;
this.isRelationalTag = isRelationalTag;
this.shouldParseMiniMessageTags = shouldParseMiniMessageTags;
this.shouldParsePlaceholderAPIPlaceholders = shouldParsePlaceholderAPIPlaceholders;
this.shouldAutoCloseTags = shouldAutoCloseTags;
this.message = message;
}

public @NotNull TagResolver toTagResolver(
final boolean inMessage,
@NotNull final ChatUser sender,
@NotNull User recipient
) {
if (inMessage && !sender.player().hasPermission(MINI_PLACEHOLDER_PERMISSION + name)) {
public @NotNull TagResolver toTagResolver(final @NotNull Context context) {
final Optional<ChatUser> sender = context.sender();
final Optional<User> recipient = context.recipient();

if (sender.isEmpty()) {
return TagResolver.empty();
}

if (context.inMessage() && !sender.get().player().hasPermission(MINI_PLACEHOLDER_PERMISSION + tagName)) {
return TagResolver.empty();
}

if (!parseMini) {
return Placeholder.unparsed(name, message);
if (!shouldParseMiniMessageTags) {
return Placeholder.unparsed(tagName, message);
}

final TagResolver papiTag;
if (parsePapi) {
if (requiresRecipient && recipient instanceof ChatUser) {
papiTag = TagResolver.resolver(
PapiTagUtils.createPlaceholderAPITag(sender.player()),
PapiTagUtils.createRelPlaceholderAPITag(sender.player(), ((ChatUser) recipient).player()),
PapiTagUtils.createRecipientTag(((ChatUser) recipient).player())
);
} else {
papiTag = PapiTagUtils.createPlaceholderAPITag(sender.player());
}
} else {
papiTag = TagResolver.empty();
if (!shouldParsePlaceholderAPIPlaceholders) {
return shouldAutoCloseTags
? Placeholder.component(tagName, MessageUtils.parseToMiniMessage(message))
: TagResolver.resolver(tagName, Tag.inserting(MessageUtils.parseToMiniMessage(message)));
}

return closing
? Placeholder.component(name, MessageUtils.parseToMiniMessage(message, papiTag))
: TagResolver.resolver(name, Tag.inserting(MessageUtils.parseToMiniMessage(message, papiTag)));
if (isRelationalTag && recipient.isEmpty()) {
return TagResolver.empty();
}

final boolean recipientIsChatUser = recipient.isPresent() && recipient.get() instanceof ChatUser;

final TagResolver papiTag = isRelationalTag && recipientIsChatUser
? TagResolver.resolver(
PapiTagUtils.createPlaceholderAPITag(sender.get().player()),
PapiTagUtils.createRelPlaceholderAPITag(sender.get().player(), ((ChatUser) recipient.get()).player()),
PapiTagUtils.createRecipientTag(((ChatUser) recipient.get()).player())
)
: PapiTagUtils.createPlaceholderAPITag(sender.get().player());

return shouldAutoCloseTags
? Placeholder.component(tagName, MessageUtils.parseToMiniMessage(message, papiTag))
: TagResolver.resolver(tagName, Tag.inserting(MessageUtils.parseToMiniMessage(message, papiTag)));
}

public @NotNull String name() {
return name;
return tagName;
}

public boolean requiresRecipient() {
return requiresRecipient;
return isRelationalTag;
}

public boolean parseMini() {
return parseMini;
return shouldParseMiniMessageTags;
}

public boolean parsePapi() {
return parsePapi;
return shouldParsePlaceholderAPIPlaceholders;
}

public boolean closing() {
return closing;
return shouldAutoCloseTags;
}

public @NotNull String message() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import at.helpch.chatchat.api.placeholder.MiniPlaceholder;
import at.helpch.chatchat.api.placeholder.MiniPlaceholderManager;
import at.helpch.chatchat.api.user.ChatUser;
import at.helpch.chatchat.api.user.User;
import com.google.common.collect.Sets;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.NotNull;
Expand All @@ -20,13 +18,9 @@ public void addPlaceholder(@NotNull final MiniPlaceholder placeholder) {
miniPlaceholders.add(placeholder);
}

public @NotNull TagResolver compileTags(
final boolean inMessage,
@NotNull final ChatUser sender,
@NotNull final User recipient
) {
public @NotNull TagResolver compileTags(final @NotNull MiniPlaceholder.Context context) {
return placeholders().stream()
.map(placeholder -> placeholder.toTagResolver(inMessage, sender, recipient))
.map(placeholder -> placeholder.toTagResolver(context))
.filter(tag -> !tag.equals(TagResolver.empty()))
.collect(TagResolver.toTagResolver());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import at.helpch.chatchat.api.event.ChatChatEvent;
import at.helpch.chatchat.api.user.ChatUser;
import at.helpch.chatchat.api.user.User;
import at.helpch.chatchat.placeholder.MiniPlaceholderContext;
import at.helpch.chatchat.user.ConsoleUser;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
Expand Down Expand Up @@ -138,7 +139,7 @@ public static boolean process(
user.player(),
chatTarget.player(),
mentionResult.message(),
plugin.miniPlaceholdersManager().compileTags(false, user, target)
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(user).recipient(target).build())
);

target.sendMessage(component);
Expand All @@ -162,7 +163,7 @@ public static boolean process(
chatEvent.format(),
user.player(),
mentionResult.message(),
plugin.miniPlaceholdersManager().compileTags(false, user, target)
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(user).recipient(target).build())
);

target.sendMessage(component);
Expand Down Expand Up @@ -190,7 +191,7 @@ public static boolean process(
user.player(),
user.player(),
mentionResult.message(),
plugin.miniPlaceholdersManager().compileTags(false, user, user)
plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(false).sender(user).recipient(user).build())
);

user.sendMessage(component);
Expand Down Expand Up @@ -236,7 +237,7 @@ public static boolean process(
);
}

resolver.resolvers(plugin.miniPlaceholdersManager().compileTags(true, user, recipient));
resolver.resolvers(plugin.miniPlaceholdersManager().compileTags(MiniPlaceholderContext.builder().inMessage(true).sender(user).recipient(recipient).build()));

return !user.hasPermission(URL_PERMISSION)
? USER_MESSAGE_MINI_MESSAGE.deserialize(message, resolver.build())
Expand Down

0 comments on commit 5672faf

Please sign in to comment.