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> <groupId>cc.carm.plugin</groupId>
<artifactId>userprefix</artifactId> <artifactId>userprefix</artifactId>
<version>2.1.9</version> <version>2.2.0</version>
<name>UserPrefix</name> <name>UserPrefix</name>
<description>轻便、高效、实时的用户前缀系统。</description> <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() { public static JavaPlugin getInstance() {
return instance; return instance;
} }

View File

@ -10,62 +10,64 @@ import org.bukkit.entity.Player;
public class ConfigSound { public class ConfigSound {
FileConfig source; FileConfig source;
String configSection; String configSection;
Sound defaultValue; Sound defaultValue;
public ConfigSound(String configSection) { public ConfigSound(String configSection) {
this(configSection, null); this(configSection, null);
} }
public ConfigSound(String configSection, Sound defaultValue) { public ConfigSound(String configSection, Sound defaultValue) {
this(ConfigManager.getPluginConfig(), configSection, defaultValue); this(ConfigManager.getPluginConfig(), configSection, defaultValue);
} }
public ConfigSound(FileConfig source, String configSection, Sound defaultValue) { public ConfigSound(FileConfig source, String configSection, Sound defaultValue) {
this.source = source; this.source = source;
this.configSection = configSection; this.configSection = configSection;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
public FileConfiguration getConfiguration() { public FileConfiguration getConfiguration() {
return this.source.getConfig(); return this.source.getConfig();
} }
public void set(Sound value, float volume) { public void set(Sound value, float volume) {
getConfiguration().set(this.configSection, value.name() + ":" + volume); getConfiguration().set(this.configSection, value.name() + ":" + volume);
this.save(); this.save();
} }
public void set(Sound value, float volume, float pitch) { public void set(Sound value, float volume, float pitch) {
getConfiguration().set(this.configSection, value.name() + ":" + volume + ":" + pitch); getConfiguration().set(this.configSection, value.name() + ":" + volume + ":" + pitch);
this.save(); this.save();
} }
public void play(Player player) { public void play(Player player) {
Sound finalSound = defaultValue; Sound finalSound = defaultValue;
float pitch = 1; float pitch = 1;
float volume = 1; float volume = 1;
String soundString = getConfiguration().getString(this.configSection); String soundString = getConfiguration().getString(this.configSection);
if (soundString != null) { if (soundString != null) {
String[] args = soundString.contains(":") ? soundString.split(":") : new String[]{soundString}; String[] args = soundString.contains(":") ? soundString.split(":") : new String[]{soundString};
try { try {
if (args.length >= 1) finalSound = Sound.valueOf(args[0]); if (args.length >= 1) finalSound = Sound.valueOf(args[0]);
if (args.length >= 2) volume = Float.parseFloat(args[1]); if (args.length >= 2) volume = Float.parseFloat(args[1]);
if (args.length >= 3) volume = Float.parseFloat(args[2]); if (args.length >= 3) volume = Float.parseFloat(args[2]);
} catch (Exception exception) { } catch (Exception exception) {
Main.log("声音 " + this.configSection + " 配置错误,不存在 " + soundString + " ,请检查。"); 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);
}
} }
}
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 { public class ChatListener implements Listener {
@EventHandler @EventHandler
public void onChat(AsyncPlayerChatEvent event) { public void onChat(AsyncPlayerChatEvent event) {
if (!PrefixConfig.Functions.Chat.ENABLE.get()) return; if (!PrefixConfig.Functions.Chat.ENABLE.get()) return;
String format = PrefixConfig.Functions.Chat.FORMAT.get(); String format = PrefixConfig.Functions.Chat.FORMAT.get();
if (format == null || format.length() < 1) return; if (format == null || format.length() < 1) return;
if (!MessageUtil.hasPlaceholderAPI()) return; if (!MessageUtil.hasPlaceholderAPI()) return;
try { try {
event.setFormat(PlaceholderAPI.setPlaceholders(event.getPlayer(), format)); event.setFormat(PlaceholderAPI.setPlaceholders(event.getPlayer(), format));
} catch (Exception exception) { } catch (Exception exception) {
Main.log("Please check the chat configuration."); Main.error("请检查配置文件中聊天相关是否配置正确。");
Main.log("请检查配置文件中聊天相关是否配置正确。"); Main.error("Please check the chat configuration.");
exception.printStackTrace(); exception.printStackTrace();
} }
} }
} }

View File

@ -21,146 +21,150 @@ import java.util.stream.Collectors;
public class PrefixManager { public class PrefixManager {
public static ConfiguredPrefix defaultPrefix; public static ConfiguredPrefix defaultPrefix;
public static HashMap<String, ConfiguredPrefix> prefixes = new HashMap<>(); 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() { public static void init() {
loadPrefixes(); loadPrefixes();
Main.log("共加载了 " + prefixes.size() + " 个前缀。"); Main.log("共加载了 " + prefixes.size() + " 个前缀。");
} }
public static void loadPrefixes() { public static void loadPrefixes() {
loadDefaultPrefix(); loadDefaultPrefix();
loadConfiguredPrefixes(); loadConfiguredPrefixes();
} }
public static void loadConfiguredPrefixes() { public static void loadConfiguredPrefixes() {
File prefixDataFolder = getStorageFolder(); File prefixDataFolder = getStorageFolder();
if (!prefixDataFolder.isDirectory() || !prefixDataFolder.exists()) { if (!prefixDataFolder.isDirectory() || !prefixDataFolder.exists()) {
prefixDataFolder.mkdir(); prefixDataFolder.mkdir();
} }
String[] filesList = prefixDataFolder.list(); String[] filesList = prefixDataFolder.list();
if (filesList == null || filesList.length < 1) { if (filesList == null || filesList.length < 1) {
Main.log("配置文件夹中暂无任何前缀配置问,请检查。"); Main.error("配置文件夹中暂无任何前缀配置问,请检查。");
Main.log("There's no configured prefix."); Main.error("There's no configured prefix.");
Main.log("Path: " + prefixDataFolder.getAbsolutePath()); Main.error("Path: " + prefixDataFolder.getAbsolutePath());
return; return;
} }
List<File> files = Arrays.stream(filesList) List<File> files = Arrays.stream(filesList)
.map(s -> new File(prefixDataFolder, s)) .map(s -> new File(prefixDataFolder, s))
.filter(File::isFile) .filter(File::isFile)
.collect(Collectors.toList()); .collect(Collectors.toList());
HashMap<String, ConfiguredPrefix> dataPrefixes = new HashMap<>(); HashMap<String, ConfiguredPrefix> dataPrefixes = new HashMap<>();
if (files.size() > 0) { if (files.size() > 0) {
for (File file : files) { for (File file : files) {
try { try {
ConfiguredPrefix prefix = new ConfiguredPrefix(file); ConfiguredPrefix prefix = new ConfiguredPrefix(file);
Main.log("完成前缀加载 " + prefix.getIdentifier() + " : " + prefix.getName()); Main.log("完成前缀加载 " + prefix.getIdentifier() + " : " + prefix.getName());
dataPrefixes.put(prefix.getIdentifier(), prefix); Main.log("Successfully loaded " + prefix.getIdentifier() + " : " + prefix.getName());
} catch (Exception ex) { dataPrefixes.put(prefix.getIdentifier(), prefix);
Main.log("Error occurred when loading prefix #" + file.getAbsolutePath() + " !"); } catch (Exception ex) {
ex.printStackTrace(); Main.error("在加载前缀 " + file.getAbsolutePath() + " 时出错,请检查配置!");
} Main.error("Error occurred when loading prefix #" + file.getAbsolutePath() + " !");
} ex.printStackTrace();
} }
}
}
PrefixManager.prefixes.clear(); PrefixManager.prefixes.clear();
PrefixManager.prefixes = dataPrefixes; PrefixManager.prefixes = dataPrefixes;
} }
public static void loadDefaultPrefix() { public static void loadDefaultPrefix() {
PrefixManager.defaultPrefix = null; PrefixManager.defaultPrefix = null;
ConfigurationSection defaultPrefixSection = ConfigManager.getPluginConfig() ConfigurationSection defaultPrefixSection = ConfigManager.getPluginConfig()
.getConfig().getConfigurationSection("defaultPrefix"); .getConfig().getConfigurationSection("defaultPrefix");
if (defaultPrefixSection != null) { if (defaultPrefixSection != null) {
try { try {
String name = defaultPrefixSection.getString("name", "默认前缀"); String name = defaultPrefixSection.getString("name", "默认前缀");
String content = defaultPrefixSection.getString("content", "&r"); String content = defaultPrefixSection.getString("content", "&r");
ItemStack itemNotUsing = defaultPrefixSection.getItemStack( ItemStack itemNotUsing = defaultPrefixSection.getItemStack(
"itemNotUsing", "itemNotUsing",
new ItemStackFactory(Material.NAME_TAG) new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀") .setDisplayName("&f默认前缀")
.addLore(" ") .addLore(" ")
.addLore("§a➥ 点击切换到该前缀") .addLore("§a➥ 点击切换到该前缀")
.toItemStack() .toItemStack()
); );
ItemStack itemUsing = defaultPrefixSection.getItemStack("itemUsing", ItemStack itemUsing = defaultPrefixSection.getItemStack("itemUsing",
new ItemStackFactory(Material.NAME_TAG) new ItemStackFactory(Material.NAME_TAG)
.setDisplayName("&f默认前缀") .setDisplayName("&f默认前缀")
.addLore(" ") .addLore(" ")
.addLore("§a✔ 您正在使用该前缀") .addLore("§a✔ 您正在使用该前缀")
.addEnchant(Enchantment.DURABILITY, 1, false) .addEnchant(Enchantment.DURABILITY, 1, false)
.addFlag(ItemFlag.HIDE_ENCHANTS) .addFlag(ItemFlag.HIDE_ENCHANTS)
.toItemStack() .toItemStack()
); );
PrefixManager.defaultPrefix = new ConfiguredPrefix("default", name, content, 0, null, itemNotUsing, null, itemUsing); PrefixManager.defaultPrefix = new ConfiguredPrefix("default", name, content, 0, null, itemNotUsing, null, itemUsing);
} catch (Exception ex) { } catch (Exception ex) {
Main.log("在加载默认前缀时出错,请检查配置!"); Main.error("在加载默认前缀时出错,请检查配置!");
ex.printStackTrace(); Main.error("Error occurred when loading default prefix, please check the configuration.");
} ex.printStackTrace();
} else { }
PrefixManager.defaultPrefix = new ConfiguredPrefix("default", "默认前缀", "&r", 0, null, } else {
new ItemStackFactory(Material.NAME_TAG) PrefixManager.defaultPrefix = new ConfiguredPrefix("default", "默认前缀", "&r", 0, null,
.setDisplayName("&f默认前缀") new ItemStackFactory(Material.NAME_TAG)
.addLore(" ") .setDisplayName("&f默认前缀")
.addLore("§a➥ 点击切换到该前缀") .addLore(" ")
.toItemStack(), .addLore("§a➥ 点击切换到该前缀")
null, .toItemStack(),
new ItemStackFactory(Material.NAME_TAG) null,
.setDisplayName("&f默认前缀") new ItemStackFactory(Material.NAME_TAG)
.addLore(" ") .setDisplayName("&f默认前缀")
.addLore("§a✔ 您正在使用该前缀") .addLore(" ")
.addEnchant(Enchantment.DURABILITY, 1, false) .addLore("§a✔ 您正在使用该前缀")
.addFlag(ItemFlag.HIDE_ENCHANTS) .addEnchant(Enchantment.DURABILITY, 1, false)
.toItemStack() .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() { public static List<ConfiguredPrefix> getVisiblePrefix() {
return PrefixManager.getPrefixes().values().stream() return PrefixManager.getPrefixes().values().stream()
.filter(ConfiguredPrefix::isVisibleNoPermission) .filter(ConfiguredPrefix::isVisibleNoPermission)
.sorted(Comparator.comparingInt(ConfiguredPrefix::getWeight)) .sorted(Comparator.comparingInt(ConfiguredPrefix::getWeight))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@NotNull @NotNull
public static ConfiguredPrefix getDefaultPrefix() { public static ConfiguredPrefix getDefaultPrefix() {
return defaultPrefix; return defaultPrefix;
} }
@NotNull @NotNull
public static HashMap<String, ConfiguredPrefix> getPrefixes() { public static HashMap<String, ConfiguredPrefix> getPrefixes() {
return prefixes; return prefixes;
} }
@Nullable @Nullable
public static ConfiguredPrefix getPrefix(String identifier) { public static ConfiguredPrefix getPrefix(String identifier) {
if (identifier == null) { if (identifier == null) {
return null; return null;
} else if (identifier.equalsIgnoreCase("default")) { } else if (identifier.equalsIgnoreCase("default")) {
return getDefaultPrefix(); return getDefaultPrefix();
} else { } else {
return getPrefixes().get(identifier); return getPrefixes().get(identifier);
} }
} }
private static File getStorageFolder() { private static File getStorageFolder() {
if (PrefixConfig.CustomStorage.ENABLE.get()) { if (PrefixConfig.CustomStorage.ENABLE.get()) {
return new File(PrefixConfig.CustomStorage.PATH.get()); return new File(PrefixConfig.CustomStorage.PATH.get());
} else { } else {
return new File(Main.getInstance().getDataFolder() + File.separator + FOLDER_NAME); 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.ColorParser;
import cc.carm.plugin.userprefix.util.ItemStackFactory; import cc.carm.plugin.userprefix.util.ItemStackFactory;
import cc.carm.plugin.userprefix.util.MessageUtil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.File; import java.io.File;
import java.util.List;
public class ConfiguredPrefix { public class ConfiguredPrefix {
@Nullable @Nullable
private File dataFile; private File dataFile;
@Nullable @Nullable
private FileConfiguration configuration; private FileConfiguration configuration;
String identifier; String identifier;
String name; String name;
String content; String content;
int weight; int weight;
String permission; String permission;
ItemStack itemHasPermission; ItemStack itemHasPermission;
ItemStack itemNoPermission; ItemStack itemNoPermission;
ItemStack itemWhenUsing; ItemStack itemWhenUsing;
public ConfiguredPrefix(@NotNull File dataFile) { public ConfiguredPrefix(@NotNull File dataFile) {
this.dataFile = dataFile; this.dataFile = dataFile;
this.configuration = YamlConfiguration.loadConfiguration(dataFile); this.configuration = YamlConfiguration.loadConfiguration(dataFile);
if (getConfiguration() != null) { if (getConfiguration() != null) {
this.identifier = getConfiguration().getString("identifier", "ERROR"); this.identifier = getConfiguration().getString("identifier", "ERROR");
this.name = getConfiguration().getString("name", "ERROR"); this.name = getConfiguration().getString("name", "ERROR");
this.content = getConfiguration().getString("content", "&r"); this.content = getConfiguration().getString("content", "&r");
this.permission = getConfiguration().getString("permission"); this.permission = getConfiguration().getString("permission");
this.weight = getConfiguration().getInt("weight", 1); this.weight = getConfiguration().getInt("weight", 1);
this.itemHasPermission = (ItemStack) getConfiguration().get("itemHasPermission", this.itemHasPermission = (ItemStack) getConfiguration().get("itemHasPermission",
new ItemStackFactory(Material.STONE).setDisplayName(name).addLore(" ").addLore("§a➥ 点击切换到该前缀").toItemStack() new ItemStackFactory(Material.STONE).setDisplayName(name).addLore(" ").addLore("§a➥ 点击切换到该前缀").toItemStack()
); );
this.itemNoPermission = (ItemStack) getConfiguration().get("itemNoPermission", itemHasPermission); this.itemNoPermission = (ItemStack) getConfiguration().get("itemNoPermission", itemHasPermission);
this.itemWhenUsing = (ItemStack) getConfiguration().get("itemUsing", itemHasPermission); this.itemWhenUsing = (ItemStack) getConfiguration().get("itemUsing", itemHasPermission);
} }
} }
public ConfiguredPrefix(@NotNull String identifier, public ConfiguredPrefix(@NotNull String identifier,
@NotNull String name, @NotNull String name,
@NotNull String content, @NotNull String content,
int weight, @Nullable String permission, int weight, @Nullable String permission,
@NotNull ItemStack itemHasPermission, @NotNull ItemStack itemHasPermission,
@Nullable ItemStack itemNoPermission, @Nullable ItemStack itemNoPermission,
@Nullable ItemStack itemWhenUsing) { @Nullable ItemStack itemWhenUsing) {
this.identifier = identifier; this.identifier = identifier;
this.name = name; this.name = name;
this.content = content; this.content = content;
this.weight = weight; this.weight = weight;
this.permission = permission; this.permission = permission;
this.itemHasPermission = itemHasPermission; this.itemHasPermission = itemHasPermission;
this.itemNoPermission = itemNoPermission; this.itemNoPermission = itemNoPermission;
this.itemWhenUsing = itemWhenUsing; this.itemWhenUsing = itemWhenUsing;
} }
@Nullable @Nullable
public FileConfiguration getConfiguration() { public FileConfiguration getConfiguration() {
return configuration; return configuration;
} }
@NotNull @NotNull
public String getIdentifier() { public String getIdentifier() {
return identifier; return identifier;
} }
@NotNull @NotNull
public String getName() { public String getName() {
return name; return name;
} }
@NotNull @NotNull
public String getContent() { public String getContent() {
return ColorParser.parse(content); return ColorParser.parse(content);
} }
public int getWeight() { public int getWeight() {
return weight; return weight;
} }
@Nullable @Nullable
public String getPermission() { public String getPermission() {
return permission; return permission;
} }
@NotNull @NotNull
public ItemStack getItemHasPermission() { public ItemStack getItemHasPermission(@Nullable Player player) {
return itemHasPermission; return parseItemStackText(this.itemHasPermission, player);
} }
@Nullable @NotNull
public ItemStack getItemNoPermission() { public ItemStack getItemHasPermission() {
return itemNoPermission; return getItemHasPermission(null);
} }
@Nullable @Nullable
public ItemStack getItemWhenUsing() { public ItemStack getItemNoPermission(@Nullable Player player) {
return itemWhenUsing; return parseItemStackText(itemNoPermission, player);
} }
public boolean isPublic() { @Nullable
return getPermission() == null; public ItemStack getItemNoPermission() {
} return getItemNoPermission(null);
}
public boolean isVisibleNoPermission() { @Nullable
return this.itemNoPermission != null; 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.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class MessageUtil { public class MessageUtil {
public static boolean hasPlaceholderAPI() { public static boolean hasPlaceholderAPI() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null; return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
} }
public static void send(CommandSender sender, List<String> messages) { public static void send(@Nullable CommandSender sender, List<String> messages) {
for (String s : messages) { if (messages == null || messages.isEmpty() || sender == null) return;
sender.sendMessage(ColorParser.parse(s)); for (String s : messages) {
} sender.sendMessage(ColorParser.parse(s));
} }
}
public static void send(CommandSender sender, String... messages) { public static void send(@Nullable CommandSender sender, String... messages) {
send(sender, Arrays.asList(messages)); send(sender, Arrays.asList(messages));
} }
public static void sendWithPlaceholders(CommandSender sender, String... messages) { public static void sendWithPlaceholders(CommandSender sender, String... messages) {
sendWithPlaceholders(sender, Arrays.asList(messages)); sendWithPlaceholders(sender, Arrays.asList(messages));
} }
public static void sendWithPlaceholders(CommandSender sender, List<String> messages) { public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) {
if (messages == null || messages.isEmpty()) return; if (messages == null || messages.isEmpty() || sender == null) return;
if (hasPlaceholderAPI() && sender instanceof Player) { send(sender, setPlaceholders(sender, messages));
send(sender, PlaceholderAPI.setPlaceholders((Player) sender, messages)); }
} else {
send(sender, messages);
}
}
public static void sendWithPlaceholders(CommandSender sender, List<String> messages, String param, 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}); sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
} }
public static void sendWithPlaceholders(CommandSender sender, List<String> messages, String[] params, Object[] values) { public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
sendWithPlaceholders(sender, setCustomParams(messages, params, values)); sendWithPlaceholders(sender, setCustomParams(messages, params, values));
} }
public static List<String> setCustomParams(List<String> messages, String param, Object value) { public static String setPlaceholders(@Nullable CommandSender sender, String message) {
return setCustomParams(messages, new String[]{param}, new Object[]{value}); if (message == null) return null;
}
public static List<String> setCustomParams(List<String> messages, String[] params, Object[] values) { message = ColorParser.parse(message);
if (params.length != values.length) return messages; if (sender == null) return message;
HashMap<String, Object> paramsMap = new HashMap<>();
for (int i = 0; i < params.length; i++) { if (hasPlaceholderAPI() && sender instanceof Player) {
paramsMap.put(params[i], values[i]); return PlaceholderAPI.setPlaceholders((Player) sender, message);
} } else {
return setCustomParams(messages, paramsMap); 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) { public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
for (String message : messages) { for (String message : messages) {
String afterMessage = message; String afterMessage = message;
for (Map.Entry<String, Object> entry : params.entrySet()) { for (Map.Entry<String, Object> entry : params.entrySet()) {
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString()); afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
} }
list.add(afterMessage); list.add(afterMessage);
} }
return list; return list;
} }
} }