From 0d6247b0f26fa0aeb8227e23042639b6ffaf63d8 Mon Sep 17 00:00:00 2001 From: CarmJos Date: Thu, 30 Dec 2021 22:56:33 +0800 Subject: [PATCH] =?UTF-8?q?[v2.2.0]=20[U]=20=E5=A2=9E=E6=B7=BB=E5=AF=B9?= =?UTF-8?q?=E5=8E=9F=E7=94=9FItemStack=E9=85=8D=E7=BD=AE=E4=B8=AD=20displa?= =?UTF-8?q?yName=20=E5=92=8C=20lore=E7=9A=84RPG=E9=A2=9C=E8=89=B2=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=94=AF=E6=8C=81=E4=B8=8EPlaceholderAPI=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E6=94=AF=E6=8C=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../java/cc/carm/plugin/userprefix/Main.java | 5 + .../configuration/values/ConfigSound.java | 96 +++---- .../userprefix/listener/ChatListener.java | 28 +- .../userprefix/manager/PrefixManager.java | 250 +++++++++--------- .../userprefix/model/ConfiguredPrefix.java | 206 +++++++++------ .../plugin/userprefix/util/MessageUtil.java | 128 +++++---- 7 files changed, 395 insertions(+), 320 deletions(-) diff --git a/pom.xml b/pom.xml index ff28ca2..58dc963 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cc.carm.plugin userprefix - 2.1.9 + 2.2.0 UserPrefix 轻便、高效、实时的用户前缀系统。 diff --git a/src/main/java/cc/carm/plugin/userprefix/Main.java b/src/main/java/cc/carm/plugin/userprefix/Main.java index 918daf1..f4a4935 100644 --- a/src/main/java/cc/carm/plugin/userprefix/Main.java +++ b/src/main/java/cc/carm/plugin/userprefix/Main.java @@ -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; } diff --git a/src/main/java/cc/carm/plugin/userprefix/configuration/values/ConfigSound.java b/src/main/java/cc/carm/plugin/userprefix/configuration/values/ConfigSound.java index d4c98e6..86cf1bb 100644 --- a/src/main/java/cc/carm/plugin/userprefix/configuration/values/ConfigSound.java +++ b/src/main/java/cc/carm/plugin/userprefix/configuration/values/ConfigSound.java @@ -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(); + } } diff --git a/src/main/java/cc/carm/plugin/userprefix/listener/ChatListener.java b/src/main/java/cc/carm/plugin/userprefix/listener/ChatListener.java index c848f93..571594d 100644 --- a/src/main/java/cc/carm/plugin/userprefix/listener/ChatListener.java +++ b/src/main/java/cc/carm/plugin/userprefix/listener/ChatListener.java @@ -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(); + } - } + } } diff --git a/src/main/java/cc/carm/plugin/userprefix/manager/PrefixManager.java b/src/main/java/cc/carm/plugin/userprefix/manager/PrefixManager.java index 973e30d..6caf8e0 100644 --- a/src/main/java/cc/carm/plugin/userprefix/manager/PrefixManager.java +++ b/src/main/java/cc/carm/plugin/userprefix/manager/PrefixManager.java @@ -21,146 +21,150 @@ import java.util.stream.Collectors; public class PrefixManager { - public static ConfiguredPrefix defaultPrefix; - public static HashMap prefixes = new HashMap<>(); + public static ConfiguredPrefix defaultPrefix; + public static HashMap 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 files = Arrays.stream(filesList) - .map(s -> new File(prefixDataFolder, s)) - .filter(File::isFile) - .collect(Collectors.toList()); + List files = Arrays.stream(filesList) + .map(s -> new File(prefixDataFolder, s)) + .filter(File::isFile) + .collect(Collectors.toList()); - HashMap dataPrefixes = new HashMap<>(); + HashMap 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 getVisiblePrefix() { - return PrefixManager.getPrefixes().values().stream() - .filter(ConfiguredPrefix::isVisibleNoPermission) - .sorted(Comparator.comparingInt(ConfiguredPrefix::getWeight)) - .collect(Collectors.toList()); - } + public static List 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 getPrefixes() { - return prefixes; - } + @NotNull + public static HashMap 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); + } + } } diff --git a/src/main/java/cc/carm/plugin/userprefix/model/ConfiguredPrefix.java b/src/main/java/cc/carm/plugin/userprefix/model/ConfiguredPrefix.java index 8f5ad0e..dcad86c 100644 --- a/src/main/java/cc/carm/plugin/userprefix/model/ConfiguredPrefix.java +++ b/src/main/java/cc/carm/plugin/userprefix/model/ConfiguredPrefix.java @@ -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 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(); + } } diff --git a/src/main/java/cc/carm/plugin/userprefix/util/MessageUtil.java b/src/main/java/cc/carm/plugin/userprefix/util/MessageUtil.java index fe6f0ea..751af11 100644 --- a/src/main/java/cc/carm/plugin/userprefix/util/MessageUtil.java +++ b/src/main/java/cc/carm/plugin/userprefix/util/MessageUtil.java @@ -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 messages) { - for (String s : messages) { - sender.sendMessage(ColorParser.parse(s)); - } - } + public static void send(@Nullable CommandSender sender, List 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 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 messages) { + if (messages == null || messages.isEmpty() || sender == null) return; + send(sender, setPlaceholders(sender, messages)); + } - public static void sendWithPlaceholders(CommandSender sender, List messages, String param, Object value) { - sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value}); - } + public static void sendWithPlaceholders(@Nullable CommandSender sender, List messages, String param, Object value) { + sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value}); + } - public static void sendWithPlaceholders(CommandSender sender, List messages, String[] params, Object[] values) { - sendWithPlaceholders(sender, setCustomParams(messages, params, values)); - } + public static void sendWithPlaceholders(@Nullable CommandSender sender, List messages, String[] params, Object[] values) { + sendWithPlaceholders(sender, setCustomParams(messages, params, values)); + } - public static List setCustomParams(List 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 setCustomParams(List messages, String[] params, Object[] values) { - if (params.length != values.length) return messages; - HashMap 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 setPlaceholders(@Nullable CommandSender sender, List 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 setPlaceholders(@Nullable CommandSender sender, List messages, String[] params, Object[] values) { + return setPlaceholders(sender, setCustomParams(messages, params, values)); + } + + public static List setCustomParams(List messages, String param, Object value) { + return setCustomParams(messages, new String[]{param}, new Object[]{value}); + } + + public static List setCustomParams(List messages, String[] params, Object[] values) { + if (params.length != values.length) return messages; + HashMap paramsMap = new HashMap<>(); + for (int i = 0; i < params.length; i++) { + paramsMap.put(params[i], values[i]); + } + return setCustomParams(messages, paramsMap); + } - public static List setCustomParams(List messages, HashMap params) { - List list = new ArrayList<>(); - for (String message : messages) { - String afterMessage = message; - for (Map.Entry entry : params.entrySet()) { - afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString()); - } - list.add(afterMessage); - } - return list; - } + public static List setCustomParams(List messages, HashMap params) { + List list = new ArrayList<>(); + for (String message : messages) { + String afterMessage = message; + for (Map.Entry entry : params.entrySet()) { + afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString()); + } + list.add(afterMessage); + } + return list; + } }