better vision of method setPlaceholders

This commit is contained in:
scienziato1pazzo 2025-01-03 18:57:21 +01:00
parent b838d1c52a
commit 0da55ba037
3 changed files with 62 additions and 37 deletions

View File

@ -43,20 +43,50 @@ import org.jetbrains.annotations.NotNull;
public final class PlaceholderAPI { public final class PlaceholderAPI {
private static final Replacer REPLACER_PERCENT = new CharsReplacer(Closure.PERCENT);
private static final Replacer REPLACER_BRACKET = new CharsReplacer(Closure.BRACKET);
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]"); private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]");
private static final Pattern BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([^{}]+)[}]"); private static final Pattern BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([^{}]+)[}]");
private static final Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern private static final Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern
.compile("[%](rel_)([^%]+)[%]"); .compile("[%](rel_)([^%]+)[%]");
private PlaceholderAPI() { private PlaceholderAPI() {}
}
// === Current API === // === Current API ===
/**
* Translates all placeholders into their corresponding values.
* <br>The pattern of a valid placeholder depends on the {@link Closure} type provided.
*
* @param player Player to parse the placeholders against
* @param text Text to set the placeholder values in
* @param closure Closure defining the placeholder type and parsing logic
* @return String containing all translated placeholders
*/
@NotNull
public static String getWithPlaceholders(final OfflinePlayer player,
@NotNull final String text,
Closure closure) {
return closure.getReplacer().apply(text, player,
PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion);
}
/**
* Translates all placeholders into their corresponding values for a list of strings.
* <br>The pattern of a valid placeholder depends on the {@link Closure} type provided.
*
* @param player Player to parse the placeholders against
* @param text List of strings to set the placeholder values in
* @param closure Closure defining the placeholder type and parsing logic
* @return List of strings containing all translated placeholders
*/
@NotNull
public static List<String> getWithPlaceholders(final OfflinePlayer player,
@NotNull final List<String> text,
Closure closure) {
return text.stream().map(line -> getWithPlaceholders(player, line, closure)).collect(Collectors.toList());
}
/** /**
* Translates all placeholders into their corresponding values. * Translates all placeholders into their corresponding values.
* <br>The pattern of a valid placeholder is {@literal %<identifier>_<params>%}. * <br>The pattern of a valid placeholder is {@literal %<identifier>_<params>%}.
@ -67,25 +97,26 @@ public final class PlaceholderAPI {
*/ */
@NotNull @NotNull
public static String setPlaceholders(final OfflinePlayer player, public static String setPlaceholders(final OfflinePlayer player,
@NotNull final String text) { @NotNull final String text) {
return REPLACER_PERCENT.apply(text, player, return getWithPlaceholders(player, text, Closure.PERCENT);
PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion);
} }
/** /**
* Translates all placeholders into their corresponding values. * Translates all placeholders into their corresponding values.
* <br>The pattern of a valid placeholder is {@literal %<identifier>_<params>%}. * <br>The pattern of a valid placeholder is {@literal %<identifier>_<params>%}.
* *
* @param player Player to parse the placeholders against * @param player Player to parse the placeholders against
* @param text List of Strings to set the placeholder values in * @param text List of Strings to set the placeholder values in
* @return String containing all translated placeholders * @return List of strings containing all translated placeholders
*/ */
@NotNull @NotNull
public static List<String> setPlaceholders(final OfflinePlayer player, public static List<String> setPlaceholders(final OfflinePlayer player,
@NotNull final List<String> text) { @NotNull final List<String> text) {
return text.stream().map(line -> setPlaceholders(player, line)).collect(Collectors.toList()); return getWithPlaceholders(player, text, Closure.PERCENT);
} }
/** /**
* Translates all placeholders into their corresponding values. * Translates all placeholders into their corresponding values.
* <br>The pattern of a valid placeholder is {@literal %<identifier>_<params>%}. * <br>The pattern of a valid placeholder is {@literal %<identifier>_<params>%}.
@ -105,7 +136,7 @@ public final class PlaceholderAPI {
* *
* @param player Player to parse the placeholders against * @param player Player to parse the placeholders against
* @param text List of Strings to set the placeholder values in * @param text List of Strings to set the placeholder values in
* @return String containing all translated placeholders * @return List of strings containing all translated placeholders
*/ */
@NotNull @NotNull
public static List<String> setPlaceholders(final Player player, @NotNull List<@NotNull String> text) { public static List<String> setPlaceholders(final Player player, @NotNull List<@NotNull String> text) {
@ -122,9 +153,8 @@ public final class PlaceholderAPI {
*/ */
@NotNull @NotNull
public static String setBracketPlaceholders(final OfflinePlayer player, public static String setBracketPlaceholders(final OfflinePlayer player,
@NotNull final String text) { @NotNull final String text) {
return REPLACER_BRACKET.apply(text, player, return getWithPlaceholders(player, text, Closure.BRACKET);
PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion);
} }
/** /**
@ -133,15 +163,14 @@ public final class PlaceholderAPI {
* *
* @param player Player to parse the placeholders against * @param player Player to parse the placeholders against
* @param text List of Strings to set the placeholder values in * @param text List of Strings to set the placeholder values in
* @return String containing all translated placeholders * @return List of strings containing all translated placeholders
*/ */
@NotNull @NotNull
public static List<@NotNull String> setBracketPlaceholders(final OfflinePlayer player, public static List<@NotNull String> setBracketPlaceholders(final OfflinePlayer player,
@NotNull final List<@NotNull String> text) { @NotNull final List<@NotNull String> text) {
return text.stream().map(line -> setBracketPlaceholders(player, line)) return getWithPlaceholders(player, text, Closure.BRACKET);
.collect(Collectors.toList());
} }
/** /**
* Translates all placeholders into their corresponding values. * Translates all placeholders into their corresponding values.
* <br>The pattern of a valid placeholder is {@literal {<identifier>_<params>}}. * <br>The pattern of a valid placeholder is {@literal {<identifier>_<params>}}.
@ -154,14 +183,14 @@ public final class PlaceholderAPI {
public static String setBracketPlaceholders(Player player, @NotNull String text) { public static String setBracketPlaceholders(Player player, @NotNull String text) {
return setBracketPlaceholders((OfflinePlayer) player, text); return setBracketPlaceholders((OfflinePlayer) player, text);
} }
/** /**
* Translates all placeholders into their corresponding values. * Translates all placeholders into their corresponding values.
* <br>The pattern of a valid placeholder is {@literal {<identifier>_<params>}}. * <br>The pattern of a valid placeholder is {@literal {<identifier>_<params>}}.
* *
* @param player Player to parse the placeholders against * @param player Player to parse the placeholders against
* @param text List of Strings to set the placeholder values in * @param text List of Strings to set the placeholder values in
* @return String containing all translated placeholders * @return List of strings containing all translated placeholders
*/ */
@NotNull @NotNull
public static List<String> setBracketPlaceholders(Player player, @NotNull List<String> text) { public static List<String> setBracketPlaceholders(Player player, @NotNull List<String> text) {

View File

@ -39,11 +39,18 @@ public interface Replacer {
public final char head, tail; public final char head, tail;
public final CharsReplacer charsReplacer;
Closure(final char head, final char tail) { Closure(final char head, final char tail) {
this.head = head; this.head = head;
this.tail = tail; this.tail = tail;
this.charsReplacer = new CharsReplacer(this);
} }
public CharsReplacer getReplacer() {
return charsReplacer;
}
} }
} }

View File

@ -75,21 +75,10 @@ public class TimeUtil {
minutes %= 60; minutes %= 60;
hours %= 24; hours %= 24;
if (days > 0) { if (days > 0) joiner.add(days + "d");
joiner.add(days + "d"); if (hours > 0) joiner.add(hours + "h");
} if (minutes > 0) joiner.add(minutes + "m");
if (seconds > 0) joiner.add(seconds + "s");
if (hours > 0) {
joiner.add(hours + "h");
}
if (minutes > 0) {
joiner.add(minutes + "m");
}
if (seconds > 0) {
joiner.add(seconds + "s");
}
return joiner.toString(); return joiner.toString();
} }