1
mirror of https://github.com/CarmJos/UserPrefix.git synced 2024-09-19 20:15:47 +00:00

[v2.2.0] [U] 增添对原生ItemStack配置中 displayName 和 lore的RPG颜色代码支持与PlaceholderAPI变量支持。

This commit is contained in:
Carm Jos 2021-12-30 22:56:33 +08:00
parent 1b9f4f3e55
commit 0d6247b0f2
7 changed files with 395 additions and 320 deletions

View File

@ -6,7 +6,7 @@
<groupId>cc.carm.plugin</groupId>
<artifactId>userprefix</artifactId>
<version>2.1.9</version>
<version>2.2.0</version>
<name>UserPrefix</name>
<description>轻便、高效、实时的用户前缀系统。</description>

View File

@ -100,6 +100,11 @@ public class Main extends JavaPlugin {
}
}
public static void error(String message) {
log("&c[ERROR] &r" + message);
}
public static JavaPlugin getInstance() {
return instance;
}

View File

@ -10,62 +10,64 @@ import org.bukkit.entity.Player;
public class ConfigSound {
FileConfig source;
String configSection;
FileConfig source;
String configSection;
Sound defaultValue;
Sound defaultValue;
public ConfigSound(String configSection) {
this(configSection, null);
}
public ConfigSound(String configSection) {
this(configSection, null);
}
public ConfigSound(String configSection, Sound defaultValue) {
this(ConfigManager.getPluginConfig(), configSection, defaultValue);
}
public ConfigSound(String configSection, Sound defaultValue) {
this(ConfigManager.getPluginConfig(), configSection, defaultValue);
}
public ConfigSound(FileConfig source, String configSection, Sound defaultValue) {
this.source = source;
this.configSection = configSection;
this.defaultValue = defaultValue;
}
public ConfigSound(FileConfig source, String configSection, Sound defaultValue) {
this.source = source;
this.configSection = configSection;
this.defaultValue = defaultValue;
}
public FileConfiguration getConfiguration() {
return this.source.getConfig();
}
public FileConfiguration getConfiguration() {
return this.source.getConfig();
}
public void set(Sound value, float volume) {
getConfiguration().set(this.configSection, value.name() + ":" + volume);
this.save();
}
public void set(Sound value, float volume) {
getConfiguration().set(this.configSection, value.name() + ":" + volume);
this.save();
}
public void set(Sound value, float volume, float pitch) {
getConfiguration().set(this.configSection, value.name() + ":" + volume + ":" + pitch);
this.save();
}
public void set(Sound value, float volume, float pitch) {
getConfiguration().set(this.configSection, value.name() + ":" + volume + ":" + pitch);
this.save();
}
public void play(Player player) {
Sound finalSound = defaultValue;
float pitch = 1;
float volume = 1;
String soundString = getConfiguration().getString(this.configSection);
if (soundString != null) {
String[] args = soundString.contains(":") ? soundString.split(":") : new String[]{soundString};
try {
if (args.length >= 1) finalSound = Sound.valueOf(args[0]);
if (args.length >= 2) volume = Float.parseFloat(args[1]);
if (args.length >= 3) volume = Float.parseFloat(args[2]);
} catch (Exception exception) {
Main.log("声音 " + this.configSection + " 配置错误,不存在 " + soundString + " ,请检查。");
}
}
if (finalSound != null) {
player.playSound(player.getLocation(), finalSound, volume, pitch);
}
public void play(Player player) {
Sound finalSound = defaultValue;
float pitch = 1;
float volume = 1;
String soundString = getConfiguration().getString(this.configSection);
if (soundString != null) {
String[] args = soundString.contains(":") ? soundString.split(":") : new String[]{soundString};
try {
if (args.length >= 1) finalSound = Sound.valueOf(args[0]);
if (args.length >= 2) volume = Float.parseFloat(args[1]);
if (args.length >= 3) volume = Float.parseFloat(args[2]);
} catch (Exception exception) {
Main.error("声音 " + this.configSection + " 配置错误,不存在 " + soundString + " ,请检查。");
Main.error("There's no sound matches in " + this.configSection + " , please check the configuration");
}
}
}
if (finalSound != null) {
player.playSound(player.getLocation(), finalSound, volume, pitch);
}
public void save() {
this.source.save();
}
}
public void save() {
this.source.save();
}
}

View File

@ -10,23 +10,23 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
public class ChatListener implements Listener {
@EventHandler
public void onChat(AsyncPlayerChatEvent event) {
if (!PrefixConfig.Functions.Chat.ENABLE.get()) return;
String format = PrefixConfig.Functions.Chat.FORMAT.get();
if (format == null || format.length() < 1) return;
@EventHandler
public void onChat(AsyncPlayerChatEvent event) {
if (!PrefixConfig.Functions.Chat.ENABLE.get()) return;
String format = PrefixConfig.Functions.Chat.FORMAT.get();
if (format == null || format.length() < 1) return;
if (!MessageUtil.hasPlaceholderAPI()) return;
if (!MessageUtil.hasPlaceholderAPI()) return;
try {
event.setFormat(PlaceholderAPI.setPlaceholders(event.getPlayer(), format));
} catch (Exception exception) {
Main.log("Please check the chat configuration.");
Main.log("请检查配置文件中聊天相关是否配置正确。");
exception.printStackTrace();
}
try {
event.setFormat(PlaceholderAPI.setPlaceholders(event.getPlayer(), format));
} catch (Exception exception) {
Main.error("请检查配置文件中聊天相关是否配置正确。");
Main.error("Please check the chat configuration.");
exception.printStackTrace();
}
}
}
}

View File

@ -21,146 +21,150 @@ import java.util.stream.Collectors;
public class PrefixManager {
public static ConfiguredPrefix defaultPrefix;
public static HashMap<String, ConfiguredPrefix> prefixes = new HashMap<>();
public static ConfiguredPrefix defaultPrefix;
public static HashMap<String, ConfiguredPrefix> prefixes = new HashMap<>();
private static final String FOLDER_NAME = "prefixes";
private static final String FOLDER_NAME = "prefixes";
public static void init() {
loadPrefixes();
Main.log("共加载了 " + prefixes.size() + " 个前缀。");
}
public static void init() {
loadPrefixes();
Main.log("共加载了 " + prefixes.size() + " 个前缀。");
}
public static void loadPrefixes() {
loadDefaultPrefix();
loadConfiguredPrefixes();
}
public static void loadPrefixes() {
loadDefaultPrefix();
loadConfiguredPrefixes();
}
public static void loadConfiguredPrefixes() {
public static void loadConfiguredPrefixes() {
File prefixDataFolder = getStorageFolder();
if (!prefixDataFolder.isDirectory() || !prefixDataFolder.exists()) {
prefixDataFolder.mkdir();
}
File prefixDataFolder = getStorageFolder();
if (!prefixDataFolder.isDirectory() || !prefixDataFolder.exists()) {
prefixDataFolder.mkdir();
}
String[] filesList = prefixDataFolder.list();
if (filesList == null || filesList.length < 1) {
Main.log("配置文件夹中暂无任何前缀配置问,请检查。");
Main.log("There's no configured prefix.");
Main.log("Path: " + prefixDataFolder.getAbsolutePath());
return;
}
String[] filesList = prefixDataFolder.list();
if (filesList == null || filesList.length < 1) {
Main.error("配置文件夹中暂无任何前缀配置问,请检查。");
Main.error("There's no configured prefix.");
Main.error("Path: " + prefixDataFolder.getAbsolutePath());
return;
}
List<File> files = Arrays.stream(filesList)
.map(s -> new File(prefixDataFolder, s))
.filter(File::isFile)
.collect(Collectors.toList());
List<File> files = Arrays.stream(filesList)
.map(s -> new File(prefixDataFolder, s))
.filter(File::isFile)
.collect(Collectors.toList());
HashMap<String, ConfiguredPrefix> dataPrefixes = new HashMap<>();
HashMap<String, ConfiguredPrefix> dataPrefixes = new HashMap<>();
if (files.size() > 0) {
for (File file : files) {
try {
ConfiguredPrefix prefix = new ConfiguredPrefix(file);
Main.log("完成前缀加载 " + prefix.getIdentifier() + " : " + prefix.getName());
dataPrefixes.put(prefix.getIdentifier(), prefix);
} catch (Exception ex) {
Main.log("Error occurred when loading prefix #" + file.getAbsolutePath() + " !");
ex.printStackTrace();
}
}
}
if (files.size() > 0) {
for (File file : files) {
try {
ConfiguredPrefix prefix = new ConfiguredPrefix(file);
Main.log("完成前缀加载 " + prefix.getIdentifier() + " : " + prefix.getName());
Main.log("Successfully loaded " + prefix.getIdentifier() + " : " + prefix.getName());
dataPrefixes.put(prefix.getIdentifier(), prefix);
} catch (Exception ex) {
Main.error("在加载前缀 " + file.getAbsolutePath() + " 时出错,请检查配置!");
Main.error("Error occurred when loading prefix #" + file.getAbsolutePath() + " !");
ex.printStackTrace();
}
}
}
PrefixManager.prefixes.clear();
PrefixManager.prefixes = dataPrefixes;
}
PrefixManager.prefixes.clear();
PrefixManager.prefixes = dataPrefixes;
}
public static void loadDefaultPrefix() {
PrefixManager.defaultPrefix = null;
ConfigurationSection defaultPrefixSection = ConfigManager.getPluginConfig()
.getConfig().getConfigurationSection("defaultPrefix");
if (defaultPrefixSection != null) {
try {
String name = defaultPrefixSection.getString("name", "默认前缀");
String content = defaultPrefixSection.getString("content", "&r");
ItemStack itemNotUsing = defaultPrefixSection.getItemStack(
"itemNotUsing",
new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀")
.addLore(" ")
.addLore("§a➥ 点击切换到该前缀")
.toItemStack()
);
ItemStack itemUsing = defaultPrefixSection.getItemStack("itemUsing",
new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀")
.addLore(" ")
.addLore("§a✔ 您正在使用该前缀")
.addEnchant(Enchantment.DURABILITY, 1, false)
.addFlag(ItemFlag.HIDE_ENCHANTS)
.toItemStack()
);
PrefixManager.defaultPrefix = new ConfiguredPrefix("default", name, content, 0, null, itemNotUsing, null, itemUsing);
} catch (Exception ex) {
Main.log("在加载默认前缀时出错,请检查配置!");
ex.printStackTrace();
}
} else {
PrefixManager.defaultPrefix = new ConfiguredPrefix("default", "默认前缀", "&r", 0, null,
new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀")
.addLore(" ")
.addLore("§a➥ 点击切换到该前缀")
.toItemStack(),
null,
new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀")
.addLore(" ")
.addLore("§a✔ 您正在使用该前缀")
.addEnchant(Enchantment.DURABILITY, 1, false)
.addFlag(ItemFlag.HIDE_ENCHANTS)
.toItemStack()
);
}
public static void loadDefaultPrefix() {
PrefixManager.defaultPrefix = null;
ConfigurationSection defaultPrefixSection = ConfigManager.getPluginConfig()
.getConfig().getConfigurationSection("defaultPrefix");
if (defaultPrefixSection != null) {
try {
String name = defaultPrefixSection.getString("name", "默认前缀");
String content = defaultPrefixSection.getString("content", "&r");
ItemStack itemNotUsing = defaultPrefixSection.getItemStack(
"itemNotUsing",
new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀")
.addLore(" ")
.addLore("§a➥ 点击切换到该前缀")
.toItemStack()
);
ItemStack itemUsing = defaultPrefixSection.getItemStack("itemUsing",
new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀")
.addLore(" ")
.addLore("§a✔ 您正在使用该前缀")
.addEnchant(Enchantment.DURABILITY, 1, false)
.addFlag(ItemFlag.HIDE_ENCHANTS)
.toItemStack()
);
PrefixManager.defaultPrefix = new ConfiguredPrefix("default", name, content, 0, null, itemNotUsing, null, itemUsing);
} catch (Exception ex) {
Main.error("在加载默认前缀时出错,请检查配置!");
Main.error("Error occurred when loading default prefix, please check the configuration.");
ex.printStackTrace();
}
} else {
PrefixManager.defaultPrefix = new ConfiguredPrefix("default", "默认前缀", "&r", 0, null,
new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀")
.addLore(" ")
.addLore("§a➥ 点击切换到该前缀")
.toItemStack(),
null,
new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀")
.addLore(" ")
.addLore("§a✔ 您正在使用该前缀")
.addEnchant(Enchantment.DURABILITY, 1, false)
.addFlag(ItemFlag.HIDE_ENCHANTS)
.toItemStack()
);
}
Main.log("完成默认前缀加载 " + defaultPrefix.getName());
}
Main.log("完成默认前缀加载 " + defaultPrefix.getName());
Main.log("Successfully loaded default prefix " + defaultPrefix.getName());
}
public static List<ConfiguredPrefix> getVisiblePrefix() {
return PrefixManager.getPrefixes().values().stream()
.filter(ConfiguredPrefix::isVisibleNoPermission)
.sorted(Comparator.comparingInt(ConfiguredPrefix::getWeight))
.collect(Collectors.toList());
}
public static List<ConfiguredPrefix> getVisiblePrefix() {
return PrefixManager.getPrefixes().values().stream()
.filter(ConfiguredPrefix::isVisibleNoPermission)
.sorted(Comparator.comparingInt(ConfiguredPrefix::getWeight))
.collect(Collectors.toList());
}
@NotNull
public static ConfiguredPrefix getDefaultPrefix() {
return defaultPrefix;
}
@NotNull
public static ConfiguredPrefix getDefaultPrefix() {
return defaultPrefix;
}
@NotNull
public static HashMap<String, ConfiguredPrefix> getPrefixes() {
return prefixes;
}
@NotNull
public static HashMap<String, ConfiguredPrefix> getPrefixes() {
return prefixes;
}
@Nullable
public static ConfiguredPrefix getPrefix(String identifier) {
if (identifier == null) {
return null;
} else if (identifier.equalsIgnoreCase("default")) {
return getDefaultPrefix();
} else {
return getPrefixes().get(identifier);
}
}
@Nullable
public static ConfiguredPrefix getPrefix(String identifier) {
if (identifier == null) {
return null;
} else if (identifier.equalsIgnoreCase("default")) {
return getDefaultPrefix();
} else {
return getPrefixes().get(identifier);
}
}
private static File getStorageFolder() {
if (PrefixConfig.CustomStorage.ENABLE.get()) {
return new File(PrefixConfig.CustomStorage.PATH.get());
} else {
return new File(Main.getInstance().getDataFolder() + File.separator + FOLDER_NAME);
}
}
private static File getStorageFolder() {
if (PrefixConfig.CustomStorage.ENABLE.get()) {
return new File(PrefixConfig.CustomStorage.PATH.get());
} else {
return new File(Main.getInstance().getDataFolder() + File.separator + FOLDER_NAME);
}
}
}

View File

@ -2,122 +2,158 @@ package cc.carm.plugin.userprefix.model;
import cc.carm.plugin.userprefix.util.ColorParser;
import cc.carm.plugin.userprefix.util.ItemStackFactory;
import cc.carm.plugin.userprefix.util.MessageUtil;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.List;
public class ConfiguredPrefix {
@Nullable
private File dataFile;
@Nullable
private FileConfiguration configuration;
@Nullable
private File dataFile;
@Nullable
private FileConfiguration configuration;
String identifier;
String identifier;
String name;
String content;
String name;
String content;
int weight;
int weight;
String permission;
String permission;
ItemStack itemHasPermission;
ItemStack itemNoPermission;
ItemStack itemWhenUsing;
ItemStack itemHasPermission;
ItemStack itemNoPermission;
ItemStack itemWhenUsing;
public ConfiguredPrefix(@NotNull File dataFile) {
this.dataFile = dataFile;
this.configuration = YamlConfiguration.loadConfiguration(dataFile);
if (getConfiguration() != null) {
this.identifier = getConfiguration().getString("identifier", "ERROR");
this.name = getConfiguration().getString("name", "ERROR");
this.content = getConfiguration().getString("content", "&r");
this.permission = getConfiguration().getString("permission");
this.weight = getConfiguration().getInt("weight", 1);
public ConfiguredPrefix(@NotNull File dataFile) {
this.dataFile = dataFile;
this.configuration = YamlConfiguration.loadConfiguration(dataFile);
if (getConfiguration() != null) {
this.identifier = getConfiguration().getString("identifier", "ERROR");
this.name = getConfiguration().getString("name", "ERROR");
this.content = getConfiguration().getString("content", "&r");
this.permission = getConfiguration().getString("permission");
this.weight = getConfiguration().getInt("weight", 1);
this.itemHasPermission = (ItemStack) getConfiguration().get("itemHasPermission",
new ItemStackFactory(Material.STONE).setDisplayName(name).addLore(" ").addLore("§a➥ 点击切换到该前缀").toItemStack()
);
this.itemNoPermission = (ItemStack) getConfiguration().get("itemNoPermission", itemHasPermission);
this.itemWhenUsing = (ItemStack) getConfiguration().get("itemUsing", itemHasPermission);
}
}
this.itemHasPermission = (ItemStack) getConfiguration().get("itemHasPermission",
new ItemStackFactory(Material.STONE).setDisplayName(name).addLore(" ").addLore("§a➥ 点击切换到该前缀").toItemStack()
);
this.itemNoPermission = (ItemStack) getConfiguration().get("itemNoPermission", itemHasPermission);
this.itemWhenUsing = (ItemStack) getConfiguration().get("itemUsing", itemHasPermission);
}
}
public ConfiguredPrefix(@NotNull String identifier,
@NotNull String name,
@NotNull String content,
int weight, @Nullable String permission,
@NotNull ItemStack itemHasPermission,
@Nullable ItemStack itemNoPermission,
@Nullable ItemStack itemWhenUsing) {
this.identifier = identifier;
this.name = name;
this.content = content;
this.weight = weight;
this.permission = permission;
this.itemHasPermission = itemHasPermission;
this.itemNoPermission = itemNoPermission;
this.itemWhenUsing = itemWhenUsing;
}
public ConfiguredPrefix(@NotNull String identifier,
@NotNull String name,
@NotNull String content,
int weight, @Nullable String permission,
@NotNull ItemStack itemHasPermission,
@Nullable ItemStack itemNoPermission,
@Nullable ItemStack itemWhenUsing) {
this.identifier = identifier;
this.name = name;
this.content = content;
this.weight = weight;
this.permission = permission;
this.itemHasPermission = itemHasPermission;
this.itemNoPermission = itemNoPermission;
this.itemWhenUsing = itemWhenUsing;
}
@Nullable
public FileConfiguration getConfiguration() {
return configuration;
}
@Nullable
public FileConfiguration getConfiguration() {
return configuration;
}
@NotNull
public String getIdentifier() {
return identifier;
}
@NotNull
public String getIdentifier() {
return identifier;
}
@NotNull
public String getName() {
return name;
}
@NotNull
public String getName() {
return name;
}
@NotNull
public String getContent() {
return ColorParser.parse(content);
}
@NotNull
public String getContent() {
return ColorParser.parse(content);
}
public int getWeight() {
return weight;
}
public int getWeight() {
return weight;
}
@Nullable
public String getPermission() {
return permission;
}
@Nullable
public String getPermission() {
return permission;
}
@NotNull
public ItemStack getItemHasPermission() {
return itemHasPermission;
}
@NotNull
public ItemStack getItemHasPermission(@Nullable Player player) {
return parseItemStackText(this.itemHasPermission, player);
}
@Nullable
public ItemStack getItemNoPermission() {
return itemNoPermission;
}
@NotNull
public ItemStack getItemHasPermission() {
return getItemHasPermission(null);
}
@Nullable
public ItemStack getItemWhenUsing() {
return itemWhenUsing;
}
@Nullable
public ItemStack getItemNoPermission(@Nullable Player player) {
return parseItemStackText(itemNoPermission, player);
}
public boolean isPublic() {
return getPermission() == null;
}
@Nullable
public ItemStack getItemNoPermission() {
return getItemNoPermission(null);
}
public boolean isVisibleNoPermission() {
return this.itemNoPermission != null;
}
@Nullable
public ItemStack getItemWhenUsing(@Nullable Player player) {
return parseItemStackText(itemWhenUsing, player);
}
@Nullable
public ItemStack getItemWhenUsing() {
return getItemWhenUsing(null);
}
public boolean isPublic() {
return getPermission() == null;
}
public boolean isVisibleNoPermission() {
return this.itemNoPermission != null;
}
@NotNull
private static ItemStack parseItemStackText(@NotNull ItemStack source, @Nullable Player player) {
if (player == null) return source;
ItemMeta meta = source.getItemMeta();
String displayName = null;
List<String> lore = null;
if (meta != null) {
if (meta.hasDisplayName()) displayName = meta.getDisplayName();
if (meta.hasLore()) lore = meta.getLore();
}
ItemStackFactory factory = new ItemStackFactory(source);
if (displayName != null) factory.setDisplayName(MessageUtil.setPlaceholders(player, displayName));
if (lore != null) factory.setLore(MessageUtil.setPlaceholders(player, lore));
return factory.toItemStack();
}
}

View File

@ -4,71 +4,99 @@ import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.stream.Collectors;
public class MessageUtil {
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
}
public static void send(CommandSender sender, List<String> messages) {
for (String s : messages) {
sender.sendMessage(ColorParser.parse(s));
}
}
public static void send(@Nullable CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty() || sender == null) return;
for (String s : messages) {
sender.sendMessage(ColorParser.parse(s));
}
}
public static void send(CommandSender sender, String... messages) {
send(sender, Arrays.asList(messages));
}
public static void send(@Nullable CommandSender sender, String... messages) {
send(sender, Arrays.asList(messages));
}
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
sendWithPlaceholders(sender, Arrays.asList(messages));
}
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
sendWithPlaceholders(sender, Arrays.asList(messages));
}
public static void sendWithPlaceholders(CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty()) return;
if (hasPlaceholderAPI() && sender instanceof Player) {
send(sender, PlaceholderAPI.setPlaceholders((Player) sender, messages));
} else {
send(sender, messages);
}
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty() || sender == null) return;
send(sender, setPlaceholders(sender, messages));
}
public static void sendWithPlaceholders(CommandSender sender, List<String> messages, String param, Object value) {
sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String param, Object value) {
sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
}
public static void sendWithPlaceholders(CommandSender sender, List<String> messages, String[] params, Object[] values) {
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
}
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
}
public static List<String> setCustomParams(List<String> messages, String param, Object value) {
return setCustomParams(messages, new String[]{param}, new Object[]{value});
}
public static String setPlaceholders(@Nullable CommandSender sender, String message) {
if (message == null) return null;
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) {
if (params.length != values.length) return messages;
HashMap<String, Object> paramsMap = new HashMap<>();
for (int i = 0; i < params.length; i++) {
paramsMap.put(params[i], values[i]);
}
return setCustomParams(messages, paramsMap);
}
message = ColorParser.parse(message);
if (sender == null) return message;
if (hasPlaceholderAPI() && sender instanceof Player) {
return PlaceholderAPI.setPlaceholders((Player) sender, message);
} else {
return message;
}
}
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty()) return new ArrayList<>();
messages = messages.stream().map(ColorParser::parse).collect(Collectors.toList());
if (sender == null) return messages;
if (hasPlaceholderAPI() && sender instanceof Player) {
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
} else {
return messages;
}
}
public static List<String> setPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
return setPlaceholders(sender, setCustomParams(messages, params, values));
}
public static List<String> setCustomParams(List<String> messages, String param, Object value) {
return setCustomParams(messages, new String[]{param}, new Object[]{value});
}
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) {
if (params.length != values.length) return messages;
HashMap<String, Object> paramsMap = new HashMap<>();
for (int i = 0; i < params.length; i++) {
paramsMap.put(params[i], values[i]);
}
return setCustomParams(messages, paramsMap);
}
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
List<String> list = new ArrayList<>();
for (String message : messages) {
String afterMessage = message;
for (Map.Entry<String, Object> entry : params.entrySet()) {
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
}
list.add(afterMessage);
}
return list;
}
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
List<String> list = new ArrayList<>();
for (String message : messages) {
String afterMessage = message;
for (Map.Entry<String, Object> entry : params.entrySet()) {
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
}
list.add(afterMessage);
}
return list;
}
}