Skip to content

Commit

Permalink
Add option for parsing PAPI placeholders in a message
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Jun 28, 2023
1 parent 12c718d commit f331ef8
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions api/src/main/java/xyz/srnyx/annoyingapi/AnnoyingMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,51 @@
* Represents a message from the {@link AnnoyingOptions#messagesFileName} file
*/
public class AnnoyingMessage extends Stringable {
/**
* The {@link AnnoyingPlugin} instance
*/
@NotNull private final AnnoyingPlugin plugin;
/**
* The key of the message in the messages file
*/
@NotNull private final String key;
/**
* Whether to parse PAPI placeholders
*/
private final boolean parsePapiPlaceholders;
/**
* The cached splitter for placeholder parameters
*/
@Nullable private String splitterPlaceholder;
/**
* The replacements for the message
*/
@NotNull private final Set<Replacement> replacements = new HashSet<>();

/**
* Constructs a new {@link AnnoyingMessage} with the specified key
*
* @param plugin the plugin getting the message
* @param key the key of the message
* @param plugin {@link #plugin}
* @param key {@link #key}
* @param parsePapiPlaceholders {@link #parsePapiPlaceholders}
*/
public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key) {
public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key, boolean parsePapiPlaceholders) {
this.plugin = plugin;
this.key = key;
this.parsePapiPlaceholders = parsePapiPlaceholders;
plugin.globalPlaceholders.forEach((placeholder, value) -> replace("%" + placeholder + "%", value));
}

/**
* Constructs a new {@link AnnoyingMessage} with the specified key
*
* @param plugin {@link #plugin}
* @param key {@link #key}
*/
public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key) {
this(plugin, key, true);
}

/**
* Constructs a new {@link AnnoyingMessage} from another {@link AnnoyingMessage} (copy constructor)
*
Expand All @@ -60,6 +88,7 @@ public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key) {
public AnnoyingMessage(@NotNull AnnoyingMessage message) {
this.plugin = message.plugin;
this.key = message.key;
this.parsePapiPlaceholders = message.parsePapiPlaceholders;
this.splitterPlaceholder = message.splitterPlaceholder;
this.replacements.addAll(message.replacements);
}
Expand Down Expand Up @@ -132,7 +161,8 @@ public BaseComponent[] getComponents(@Nullable AnnoyingSender sender) {
String string = messages.getString(key);
if (string == null) return json.append(key, "&cCheck &4" + plugin.options.messagesFileName + "&c!").build();
for (final Replacement replacement : replacements) string = replacement.process(string);
final String[] split = plugin.parsePapiPlaceholders(player, string).split(splitterJson, 3);
if (parsePapiPlaceholders) string = plugin.parsePapiPlaceholders(player, string);
final String[] split = string.split(splitterJson, 3);
return json.append(split[0], extractHover(split), ClickEvent.Action.SUGGEST_COMMAND, extractFunction(split)).build();
}

Expand All @@ -144,9 +174,10 @@ public BaseComponent[] getComponents(@Nullable AnnoyingSender sender) {
continue;
}
for (final Replacement replacement : replacements) subMessage = replacement.process(subMessage);
if (parsePapiPlaceholders) subMessage = plugin.parsePapiPlaceholders(player, subMessage);

// Get component parts
final String[] split = plugin.parsePapiPlaceholders(player, subMessage).split(splitterJson, 3);
final String[] split = subMessage.split(splitterJson, 3);
final String display = split[0];
final String hover = extractHover(split);
final String function = extractFunction(split);
Expand Down

0 comments on commit f331ef8

Please sign in to comment.