Compare commits

...

2 Commits

Author SHA1 Message Date
Andre601
46f91f5f11 Add missing Javadoc comments 2022-04-26 22:40:59 +02:00
Andre601
9ae44a3954 Move parsing method to PlaceholderExpansion 2022-04-26 22:12:40 +02:00
5 changed files with 103 additions and 12 deletions

View File

@@ -25,8 +25,22 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@Deprecated
public abstract class PlaceholderHook { public abstract class PlaceholderHook {
/**
* Please see {@link me.clip.placeholderapi.expansion.PlaceholderExpansion#parsePlaceholders(OfflinePlayer, String) PlaceholderExpansion#parsePlaceholder(OfflinePlayer, String)}
* for a full description of what this method is used for.
*
* @param player Possibly-null OfflinePlayer instance to use.
* @param params String after {@code %<expansion>_} and before the second percent symbol.
* @return Possibly-null String depending on the Expansions returned value.
* @deprecated Planned for removal in 2.14.0
* <br>Please use {@link me.clip.placeholderapi.expansion.PlaceholderExpansion#parsePlaceholders(OfflinePlayer, String) parsePlaceholders(OfflinePlayer, String)}
* instead
*/
@Nullable @Nullable
@Deprecated
public String onRequest(final OfflinePlayer player, @NotNull final String params) { public String onRequest(final OfflinePlayer player, @NotNull final String params) {
if (player != null && player.isOnline()) { if (player != null && player.isOnline()) {
return onPlaceholderRequest((Player) player, params); return onPlaceholderRequest((Player) player, params);
@@ -35,7 +49,20 @@ public abstract class PlaceholderHook {
return onPlaceholderRequest(null, params); return onPlaceholderRequest(null, params);
} }
/**
* Please see {@link me.clip.placeholderapi.expansion.PlaceholderExpansion#parsePlaceholders(Player, String) PlaceholderExpansion#parsePlaceholder(Player, String)}
* for a full description of what this method is used for.
*
* @param player Possibly-null Player instance to use.
* @param params String after {@code %<expansion>_} and before the second percent symbol.
* @return Possibly-null String depending on the Expansions returned value.
*
* @deprecated Planned for removal in 2.14.0
* <br>Please use {@link me.clip.placeholderapi.expansion.PlaceholderExpansion#parsePlaceholders(Player, String) parsePlaceholders(Player, String)}
* instead
*/
@Nullable @Nullable
@Deprecated
public String onPlaceholderRequest(final Player player, @NotNull final String params) { public String onPlaceholderRequest(final Player player, @NotNull final String params) {
return null; return null;
} }

View File

@@ -26,7 +26,9 @@ import java.util.logging.Level;
import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.PlaceholderHook; import me.clip.placeholderapi.PlaceholderHook;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -398,6 +400,62 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
getAuthor(), getVersion()); getAuthor(), getVersion());
} }
/**
* This method is called first whenever PlaceholderAPI finds a valid placeholder with a matching expansion.
* <br>When called, the provided OfflinePlayer instance can be one of 3 possible states:
*
* <ul>
* <li>Not null and online</li>
* <li>Not null and offline</li>
* <li>Null</li>
* </ul>
*
* When not overridden by the called expansion will the method as of now call
* {@link PlaceholderHook#onRequest(OfflinePlayer, String) PlaceholderHook#onRequest(OfflinePlayer, String)}.
* <br>This behaviour will change in a future version to call {@link #parsePlaceholders(Player, String) parsePlaceholders(Player, String)}
* with the OfflinePlayer either being casted to a Player (When online), or {@link null}.
*
* <p>To use this method in your PlaceholderExpansion, override it and return either a String or {@code null}.
* <br>When {@code null} is returned will PlaceholderAPI see it as an "invalid placeholder" and return
* the content as-is in the final String.
*
* @param player The OfflinePlayer to use.
* @param params The parameters of the placeholder, right after the first underscore.
* @return Parsed placeholder, or null depending on the Expansion's handling of it.
*/
@Nullable
public String parsePlaceholders(@Nullable OfflinePlayer player, @NotNull String params) {
return this.onRequest(player, params);
}
/**
* This method is called whenever PlaceholderAPI finds a valid placeholder with a matching expansion
* AND {@link #parsePlaceholders(OfflinePlayer, String) parsePlaceholder(OfflinePlayer, String)} isn't
* overridden by the expansion in question.
* <br>When called, the provided Player instance can be one of 2 possible states:
*
* <ul>
* <li>Not null and online</li>
* <li>Null</li>
* </ul>
*
* When not overridden by the called expansion will the method as of now call
* {@link PlaceholderHook#onPlaceholderRequest(Player, String) PlaceholderHook#onPlaceholderRequest(Player, String)}.
* <br>This behaviour will change in a future version to instead return {@code null}.
*
* <p>To use this method in your PlaceholderExpansion, override it and return either a String or {@code null}.
* <br>When {@code null} is returned will PlaceholderAPI see it as an "invalid placeholder" and return
* the content as-is in the final String.
*
* @param player The Player to use. May be null
* @param params The parameters of the placeholder, right after the first underscore.
* @return Parsed placeholder, or null depending on the Expansion's handling of it.
*/
@Nullable
public String parsePlaceholders(@Nullable Player player, @NotNull String params){
return this.onPlaceholderRequest(player, params);
}
// === Deprecated API === // === Deprecated API ===
/** /**

View File

@@ -21,23 +21,29 @@
package me.clip.placeholderapi.expansion; package me.clip.placeholderapi.expansion;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Implementing this interface allows your {@link me.clip.placeholderapi.expansion.PlaceholderExpansion PlaceholderExpansion} * This interface should be used when your {@link me.clip.placeholderapi.expansion.PlaceholderExpansion PlaceholderExpansion}
* to be used as a relational placeholder expansion. * should support relational placeholders.
* *
* <p>Relational placeholders take two Players as input and are always prefixed with {@code rel_}, * <p>The difference between relational placeholders and normal ones is, that relational ones start
* so {@code %foo_bar%} becomes {@code %rel_foo_bar%} * with a {@code rel_} prefix and have two Players provided, instead of one.
* <br>The main purpose is to return a String based on the "relationship" between the two players
* (i.e. if both are within the same world).
*/ */
public interface Relational { public interface Relational {
/** /**
* This method is called whenever a placeholder starting with {@code rel_} is called. * This method will be called whenever a valid placeholder in the format {@code %rel_<expansion>_<identifier>%}
* is found.
* *
* @param one The first player used for the placeholder. * @param one The first player to use for comparison.
* @param two The second player used for the placeholder. * @param two The second player to use for comparison
* @param identifier The text right after the expansion's name (%expansion_<b>identifier</b>%) * @param identifier String right after {@code %rel_<expansion>_} and before the second percent symbol.
* @return Parsed String from the expansion. * @return Possibly-null String, depending on what the expansion returns.
*/ */
String onPlaceholderRequest(Player one, Player two, String identifier); @Nullable
String onPlaceholderRequest(Player one, Player two, @NotNull String identifier);
} }

View File

@@ -116,7 +116,7 @@ public final class CharsReplacer implements Replacer {
continue; continue;
} }
final String replacement = placeholder.onRequest(player, parametersString); final String replacement = placeholder.parsePlaceholders(player, parametersString);
if (replacement == null) { if (replacement == null) {
builder.append(closure.head).append(identifierString); builder.append(closure.head).append(identifierString);

View File

@@ -61,7 +61,7 @@ public final class RegexReplacer implements Replacer {
continue; continue;
} }
final String requested = expansion.onRequest(player, parameters); final String requested = expansion.parsePlaceholders(player, parameters);
matcher.appendReplacement(builder, requested != null ? requested : matcher.group(0)); matcher.appendReplacement(builder, requested != null ? requested : matcher.group(0));
} }
while (matcher.find()); while (matcher.find());