Skip to content

Commit

Permalink
Run through placeholderAPI even if player is null
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jun 30, 2020
1 parent a7ad510 commit 85acae1
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/main/java/net/citizensnpcs/api/util/Placeholders.java
Expand Up @@ -2,18 +2,17 @@

import java.util.regex.Pattern;

import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.Entity;

import me.clip.placeholderapi.PlaceholderAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Owner;

public class Placeholders {
public static String replace(String text, CommandSender sender, NPC npc) {
if (sender instanceof Player) {
text = replace(text, (Player) sender);
}
text = replace(text, sender instanceof OfflinePlayer ? (OfflinePlayer) sender : null);
if (npc == null) {
return text;
}
Expand All @@ -23,17 +22,23 @@ public static String replace(String text, CommandSender sender, NPC npc) {
return text;
}

public static String replace(String text, Player player) {
public static String replace(String text, OfflinePlayer player) {
if (player == null) {
return text;
return setPlaceholderAPIPlaceholders(text, player);
}
text = PLAYER_MATCHER.matcher(text).replaceAll(player.getName());
text = text.replace("<world>", player.getWorld().getName());
if (player instanceof Entity && ((Entity) player).isValid()) {
text = text.replace("<world>", ((Entity) player).getWorld().getName());
}
return setPlaceholderAPIPlaceholders(text, player);
}

private static String setPlaceholderAPIPlaceholders(String text, OfflinePlayer player) {
try {
PlaceholderAPI.setPlaceholders(player, text);
return PlaceholderAPI.setPlaceholders(player, text);
} catch (Throwable t) {
return text;
}
return text;
}

private static Pattern PLAYER_MATCHER = Pattern.compile("<player>|<p>");
Expand Down

0 comments on commit 85acae1

Please sign in to comment.