diff --git a/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java b/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java index 97b6c48..e572b5e 100644 --- a/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java +++ b/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java @@ -43,20 +43,50 @@ import org.jetbrains.annotations.NotNull; 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 BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([^{}]+)[}]"); private static final Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern .compile("[%](rel_)([^%]+)[%]"); - private PlaceholderAPI() { - } + private PlaceholderAPI() {} // === Current API === + + /** + * Translates all placeholders into their corresponding values. + *
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. + *
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 getWithPlaceholders(final OfflinePlayer player, + @NotNull final List text, + Closure closure) { + return text.stream().map(line -> getWithPlaceholders(player, line, closure)).collect(Collectors.toList()); + } + /** * Translates all placeholders into their corresponding values. *
The pattern of a valid placeholder is {@literal %_%}. @@ -67,25 +97,26 @@ public final class PlaceholderAPI { */ @NotNull public static String setPlaceholders(final OfflinePlayer player, - @NotNull final String text) { - return REPLACER_PERCENT.apply(text, player, - PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion); + @NotNull final String text) { + return getWithPlaceholders(player, text, Closure.PERCENT); } + /** * Translates all placeholders into their corresponding values. *
The pattern of a valid placeholder is {@literal %_%}. * * @param player Player to parse the placeholders against * @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 public static List setPlaceholders(final OfflinePlayer player, - @NotNull final List text) { - return text.stream().map(line -> setPlaceholders(player, line)).collect(Collectors.toList()); + @NotNull final List text) { + return getWithPlaceholders(player, text, Closure.PERCENT); } + /** * Translates all placeholders into their corresponding values. *
The pattern of a valid placeholder is {@literal %_%}. @@ -105,7 +136,7 @@ public final class PlaceholderAPI { * * @param player Player to parse the placeholders against * @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 public static List setPlaceholders(final Player player, @NotNull List<@NotNull String> text) { @@ -122,9 +153,8 @@ public final class PlaceholderAPI { */ @NotNull public static String setBracketPlaceholders(final OfflinePlayer player, - @NotNull final String text) { - return REPLACER_BRACKET.apply(text, player, - PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion); + @NotNull final String text) { + return getWithPlaceholders(player, text, Closure.BRACKET); } /** @@ -133,15 +163,14 @@ public final class PlaceholderAPI { * * @param player Player to parse the placeholders against * @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 public static List<@NotNull String> setBracketPlaceholders(final OfflinePlayer player, - @NotNull final List<@NotNull String> text) { - return text.stream().map(line -> setBracketPlaceholders(player, line)) - .collect(Collectors.toList()); + @NotNull final List<@NotNull String> text) { + return getWithPlaceholders(player, text, Closure.BRACKET); } - + /** * Translates all placeholders into their corresponding values. *
The pattern of a valid placeholder is {@literal {_}}. @@ -154,14 +183,14 @@ public final class PlaceholderAPI { public static String setBracketPlaceholders(Player player, @NotNull String text) { return setBracketPlaceholders((OfflinePlayer) player, text); } - + /** * Translates all placeholders into their corresponding values. *
The pattern of a valid placeholder is {@literal {_}}. * * @param player Player to parse the placeholders against * @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 public static List setBracketPlaceholders(Player player, @NotNull List text) { diff --git a/src/main/java/me/clip/placeholderapi/replacer/Replacer.java b/src/main/java/me/clip/placeholderapi/replacer/Replacer.java index e0c6e48..b231cbb 100644 --- a/src/main/java/me/clip/placeholderapi/replacer/Replacer.java +++ b/src/main/java/me/clip/placeholderapi/replacer/Replacer.java @@ -39,11 +39,18 @@ public interface Replacer { public final char head, tail; + public final CharsReplacer charsReplacer; Closure(final char head, final char tail) { this.head = head; this.tail = tail; + this.charsReplacer = new CharsReplacer(this); } + + public CharsReplacer getReplacer() { + return charsReplacer; + } + } } diff --git a/src/main/java/me/clip/placeholderapi/util/TimeUtil.java b/src/main/java/me/clip/placeholderapi/util/TimeUtil.java index d23a571..4e7dbef 100644 --- a/src/main/java/me/clip/placeholderapi/util/TimeUtil.java +++ b/src/main/java/me/clip/placeholderapi/util/TimeUtil.java @@ -75,21 +75,10 @@ public class TimeUtil { minutes %= 60; hours %= 24; - if (days > 0) { - 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 (days > 0) 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"); return joiner.toString(); }