From a50212e395e9348f82d2bca78132b5fbcca95ac5 Mon Sep 17 00:00:00 2001 From: extendedclip Date: Thu, 31 May 2018 14:35:28 -0400 Subject: [PATCH] Add support for OfflinePlayer --- .../clip/placeholderapi/PlaceholderAPI.java | 13 +++++---- .../clip/placeholderapi/PlaceholderHook.java | 28 ++++++++++++++----- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java b/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java index deb6cb2..648963a 100644 --- a/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java +++ b/src/main/java/me/clip/placeholderapi/PlaceholderAPI.java @@ -28,6 +28,7 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.Relational; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -135,7 +136,7 @@ public class PlaceholderAPI { * @param text text to set the placeholder values in * @return modified list with all placeholders set to the corresponding values */ - public static List setBracketPlaceholders(Player p, List text) { + public static List setBracketPlaceholders(OfflinePlayer p, List text) { return setPlaceholders(p, text, BRACKET_PLACEHOLDER_PATTERN); } @@ -147,7 +148,7 @@ public class PlaceholderAPI { * @param text text to parse the placeholder values in * @return modified list with all placeholders set to the corresponding values */ - public static List setPlaceholders(Player p, List text) { + public static List setPlaceholders(OfflinePlayer p, List text) { return setPlaceholders(p, text, PLACEHOLDER_PATTERN); } @@ -159,7 +160,7 @@ public class PlaceholderAPI { * @param text text to parse the placeholder values in * @return modified list with all placeholders set to the corresponding values */ - public static List setPlaceholders(Player p, List text, Pattern pattern) { + public static List setPlaceholders(OfflinePlayer p, List text, Pattern pattern) { if (text == null) return null; return text.stream().map(line -> setPlaceholders(p, line, pattern)).collect(Collectors.toList()); } @@ -172,7 +173,7 @@ public class PlaceholderAPI { * @param text text to parse the placeholder values to * @return modified text with all placeholders set to the corresponding values */ - public static String setBracketPlaceholders(Player player, String text) { + public static String setBracketPlaceholders(OfflinePlayer player, String text) { return setPlaceholders(player, text, BRACKET_PLACEHOLDER_PATTERN); } @@ -184,7 +185,7 @@ public class PlaceholderAPI { * @param text text to parse the placeholder values to * @return text with all placeholders set to the corresponding values */ - public static String setPlaceholders(Player player, String text) { + public static String setPlaceholders(OfflinePlayer player, String text) { return setPlaceholders(player, text, PLACEHOLDER_PATTERN); } @@ -197,7 +198,7 @@ public class PlaceholderAPI { * @param placeholderPattern the pattern to match placeholders to. Capture group 1 must contain an underscore separating the identifier from the params * @return text with all placeholders set to the corresponding values */ - public static String setPlaceholders(Player player, String text, Pattern placeholderPattern) { + public static String setPlaceholders(OfflinePlayer player, String text, Pattern placeholderPattern) { if (text == null) return null; if (placeholders.isEmpty()) return color(text); Matcher m = placeholderPattern.matcher(text); diff --git a/src/main/java/me/clip/placeholderapi/PlaceholderHook.java b/src/main/java/me/clip/placeholderapi/PlaceholderHook.java index 0084bf0..161898e 100644 --- a/src/main/java/me/clip/placeholderapi/PlaceholderHook.java +++ b/src/main/java/me/clip/placeholderapi/PlaceholderHook.java @@ -20,15 +20,29 @@ */ package me.clip.placeholderapi; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; public abstract class PlaceholderHook { - /** - * called when a placeholder is requested from this PlaceholderHook - * @param p Player requesting the placeholder value for, null if not needed for a player - * @param params String passed for the placeholder hook to determine what value to return - * @return value for the requested player and params - */ - public abstract String onPlaceholderRequest(Player p, String params); + /** + * called when a placeholder is requested from this hook + * @param p {@link OfflinePlayer} to request the placeholder value for, null if not needed for a player + * @param params String passed to the hook to determine what value to return + * @return value for the requested player and params + */ + public String onPlaceholderRequest(OfflinePlayer p, String params) { + if (p != null && p.isOnline()) { + return onPlaceholderRequest((Player) p, params); + } + return onPlaceholderRequest(null, params); + } + + /** + * @deprecated As of versions greater than 2.8.7, use {@link #onPlaceholderRequest(OfflinePlayer p, String params)} + */ + @Deprecated + public String onPlaceholderRequest(Player p, String params) { + return null; + } }