2046 null player placeholder support (#2048)

* Fixes #2046. Handles null players for placeholders.

* Remove unused import
This commit is contained in:
tastybento 2022-11-12 22:49:22 -08:00 committed by GitHub
parent 1c4fb76828
commit 2629e940c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import java.util.Map;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer; import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
@ -42,9 +43,9 @@ abstract class BasicPlaceholderExpansion extends PlaceholderExpansion {
} }
@Override @Override
public String onPlaceholderRequest(Player p, @NonNull String placeholder) { public String onPlaceholderRequest(@Nullable Player p, @NonNull String placeholder) {
if (placeholders.containsKey(placeholder) && p != null) { if (placeholders.containsKey(placeholder)) {
return placeholders.get(placeholder).onReplace(User.getInstance(p)); return placeholders.get(placeholder).onReplace(p != null ? User.getInstance(p) : null);
} }
return null; return null;
} }

View File

@ -9,6 +9,7 @@ import java.util.regex.Pattern;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
@ -109,7 +110,10 @@ public class PlaceholderAPIHook extends PlaceholderHook {
*/ */
@Override @Override
@NonNull @NonNull
public String replacePlaceholders(@NonNull Player player, @NonNull String string) { public String replacePlaceholders(@Nullable Player player, @NonNull String string) {
if (player == null) {
return PlaceholderAPI.setPlaceholders(player, removeGMPlaceholder(string));
}
// Transform [gamemode] in string to the game mode description name, or remove it for the default replacement // Transform [gamemode] in string to the game mode description name, or remove it for the default replacement
String newString = BentoBox.getInstance().getIWM().getAddon(player.getWorld()).map(gm -> String newString = BentoBox.getInstance().getIWM().getAddon(player.getWorld()).map(gm ->
string.replace(TextVariables.GAMEMODE, gm.getDescription().getName().toLowerCase()) string.replace(TextVariables.GAMEMODE, gm.getDescription().getName().toLowerCase())

View File

@ -116,12 +116,12 @@ public class PlaceholdersManager {
/** /**
* Replaces the placeholders in this String and returns it. * Replaces the placeholders in this String and returns it.
* @param player the Player to get the placeholders for. * @param player the Player to get the placeholders for or null for non-player-specific placeholders
* @param string the String to replace the placeholders in. * @param string the String to replace the placeholders in.
* @return the String with placeholders replaced, or the identical String if no placeholders were available. * @return the String with placeholders replaced, or the identical String if no placeholders were available.
* @since 1.5.0 * @since 1.5.0
*/ */
public String replacePlaceholders(@NonNull Player player, @NonNull String string) { public String replacePlaceholders(@Nullable Player player, @NonNull String string) {
return getPlaceholderAPIHook().map(papi -> papi.replacePlaceholders(player, string)).orElse(string); return getPlaceholderAPIHook().map(papi -> papi.replacePlaceholders(player, string)).orElse(string);
} }