Replace FabledSkyBlock placeholders without PlaceholderAPI installed

This commit is contained in:
Christian Koop 2023-08-18 10:35:10 +02:00
parent 2e37efefd4
commit 25e401826c
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
3 changed files with 31 additions and 8 deletions

View File

@ -8,20 +8,20 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
public class PlaceholderManager extends Manager { public class PlaceholderManager extends Manager {
private final PlaceholderAPI skyBlockPlaceholderAPI;
private final boolean placeholderAPIEnabled; private final boolean placeholderAPIEnabled;
private final PlaceholderProcessor placeholderProcessor;
public PlaceholderManager(SkyBlock plugin) { public PlaceholderManager(SkyBlock plugin) {
super(plugin); super(plugin);
this.skyBlockPlaceholderAPI = new PlaceholderAPI(this.plugin);
PluginManager pluginManager = this.plugin.getServer().getPluginManager(); PluginManager pluginManager = this.plugin.getServer().getPluginManager();
this.placeholderAPIEnabled = pluginManager.getPlugin("PlaceholderAPI") != null; this.placeholderAPIEnabled = pluginManager.getPlugin("PlaceholderAPI") != null;
this.placeholderProcessor = !this.placeholderAPIEnabled ? new PlaceholderProcessor() : null;
} }
public void registerPlaceholders() { public void registerPlaceholders() {
if (this.placeholderAPIEnabled) { if (this.placeholderAPIEnabled) {
this.skyBlockPlaceholderAPI.register(); new PlaceholderAPI(this.plugin).register();
} }
} }
@ -30,11 +30,35 @@ public class PlaceholderManager extends Manager {
} }
public String parsePlaceholders(Player player, String message) { public String parsePlaceholders(Player player, String message) {
String retValue = message; String retValue;
if (this.placeholderAPIEnabled) { if (this.placeholderAPIEnabled) {
retValue = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, message); retValue = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, message);
retValue = ChatColor.translateAlternateColorCodes('&', retValue); retValue = ChatColor.translateAlternateColorCodes('&', retValue);
} else {
retValue = manuallyReplaceSkyBlockPlaceholders(player, message);
}
return retValue;
}
private String manuallyReplaceSkyBlockPlaceholders(Player player, String message) {
if (this.placeholderProcessor == null) {
return message;
}
String retValue = message;
int index = retValue.indexOf("%fabledskyblock_");
while (index != -1) {
int endIndex = retValue.indexOf("%", index + 1);
if (endIndex != -1) {
String placeholder = retValue.substring(index + 1, endIndex);
String result = this.placeholderProcessor.processPlaceholder(player, placeholder);
if (result != null) {
retValue = retValue.replace("%" + placeholder + "%", result);
}
}
index = retValue.indexOf("%fabledskyblock_", index + 1);
} }
return retValue; return retValue;
} }

View File

@ -42,8 +42,7 @@ public class PlaceholderProcessor {
LeaderboardManager leaderboardManager = plugin.getLeaderboardManager(); LeaderboardManager leaderboardManager = plugin.getLeaderboardManager();
FileManager fileManager = plugin.getFileManager(); FileManager fileManager = plugin.getFileManager();
FileConfiguration placeholdersLoad = fileManager.getConfig( FileConfiguration placeholdersLoad = fileManager.getConfig(new File(plugin.getDataFolder(), "placeholders.yml")).getFileConfiguration();
new File(plugin.getDataFolder(), "placeholders.yml")).getFileConfiguration();
if (placeholdersLoad == null) { if (placeholdersLoad == null) {
return "Error"; return "Error";

View File

@ -31,7 +31,7 @@ public class PlaceholderAPI extends PlaceholderExpansion {
return true; return true;
} }
public String onPlaceholderRequest(Player player, @NotNull String identifier) { public String onPlaceholderRequest(Player player, @NotNull String params) {
return this.placeholderProcessor.processPlaceholder(player, "fabledskyblock_" + identifier); return this.placeholderProcessor.processPlaceholder(player, "fabledskyblock_" + params);
} }
} }