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

Improved Context for MiniPlaceholders parsing. #209

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@ -12,6 +12,8 @@
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;

import java.util.Optional;

// configurate requires non-final fields
@SuppressWarnings("FieldMayBeFinal")
@ConfigSerializable
Expand Down Expand Up @@ -42,34 +44,40 @@ public MiniPlaceholderImpl(
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) {
if (context.inMessage() && context.sender().isPresent() && !context.sender().get().player().hasPermission(MINI_PLACEHOLDER_PERMISSION + name)) {
return TagResolver.empty();
}

if (!parseMini) {
return Placeholder.unparsed(name, 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();
final Optional<ChatUser> sender = context.sender();
final Optional<User> recipient = context.recipient();

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

if (parsePapi && requiresRecipient && recipient.isEmpty()) {
return TagResolver.empty();
}

if (!parsePapi) {
return closing
? Placeholder.component(name, MessageUtils.parseToMiniMessage(message))
: TagResolver.resolver(name, Tag.inserting(MessageUtils.parseToMiniMessage(message)));
}

final TagResolver papiTag = !requiresRecipient || !(recipient.get() instanceof ChatUser)
? PapiTagUtils.createPlaceholderAPITag(sender.get().player())
: TagResolver.resolver(
PapiTagUtils.createPlaceholderAPITag(sender.get().player()),
PapiTagUtils.createRelPlaceholderAPITag(sender.get().player(), ((ChatUser) recipient.get()).player()),
PapiTagUtils.createRecipientTag(((ChatUser) recipient.get()).player())
);
BlitzOffline marked this conversation as resolved.
Show resolved Hide resolved

return closing
? Placeholder.component(name, MessageUtils.parseToMiniMessage(message, papiTag))
: TagResolver.resolver(name, Tag.inserting(MessageUtils.parseToMiniMessage(message, papiTag)));
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