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

@ -55,7 +55,9 @@ public class ConfigSound {
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 + " ,请检查。");
Main.error("声音 " + this.configSection + " 配置错误,不存在 " + soundString + " ,请检查。");
Main.error("There's no sound matches in " + this.configSection + " , please check the configuration");
}
}
if (finalSound != null) {

View File

@ -21,8 +21,8 @@ public class ChatListener implements Listener {
try {
event.setFormat(PlaceholderAPI.setPlaceholders(event.getPlayer(), format));
} catch (Exception exception) {
Main.log("Please check the chat configuration.");
Main.log("请检查配置文件中聊天相关是否配置正确。");
Main.error("请检查配置文件中聊天相关是否配置正确。");
Main.error("Please check the chat configuration.");
exception.printStackTrace();
}

View File

@ -45,9 +45,9 @@ public class PrefixManager {
String[] filesList = prefixDataFolder.list();
if (filesList == null || filesList.length < 1) {
Main.log("配置文件夹中暂无任何前缀配置问,请检查。");
Main.log("There's no configured prefix.");
Main.log("Path: " + prefixDataFolder.getAbsolutePath());
Main.error("配置文件夹中暂无任何前缀配置问,请检查。");
Main.error("There's no configured prefix.");
Main.error("Path: " + prefixDataFolder.getAbsolutePath());
return;
}
@ -63,9 +63,11 @@ public class PrefixManager {
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.log("Error occurred when loading prefix #" + file.getAbsolutePath() + " !");
Main.error("在加载前缀 " + file.getAbsolutePath() + " 时出错,请检查配置!");
Main.error("Error occurred when loading prefix #" + file.getAbsolutePath() + " !");
ex.printStackTrace();
}
}
@ -102,7 +104,8 @@ public class PrefixManager {
);
PrefixManager.defaultPrefix = new ConfiguredPrefix("default", name, content, 0, null, itemNotUsing, null, itemUsing);
} catch (Exception ex) {
Main.log("在加载默认前缀时出错,请检查配置!");
Main.error("在加载默认前缀时出错,请检查配置!");
Main.error("Error occurred when loading default prefix, please check the configuration.");
ex.printStackTrace();
}
} else {
@ -124,6 +127,7 @@ public class PrefixManager {
}
Main.log("完成默认前缀加载 " + defaultPrefix.getName());
Main.log("Successfully loaded default prefix " + defaultPrefix.getName());
}
public static List<ConfiguredPrefix> getVisiblePrefix() {

View File

@ -2,14 +2,18 @@ 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 {
@ -96,19 +100,34 @@ public class ConfiguredPrefix {
return permission;
}
@NotNull
public ItemStack getItemHasPermission(@Nullable Player player) {
return parseItemStackText(this.itemHasPermission, player);
}
@NotNull
public ItemStack getItemHasPermission() {
return itemHasPermission;
return getItemHasPermission(null);
}
@Nullable
public ItemStack getItemNoPermission(@Nullable Player player) {
return parseItemStackText(itemNoPermission, player);
}
@Nullable
public ItemStack getItemNoPermission() {
return itemNoPermission;
return getItemNoPermission(null);
}
@Nullable
public ItemStack getItemWhenUsing(@Nullable Player player) {
return parseItemStackText(itemWhenUsing, player);
}
@Nullable
public ItemStack getItemWhenUsing() {
return itemWhenUsing;
return getItemWhenUsing(null);
}
public boolean isPublic() {
@ -120,4 +139,21 @@ public class ConfiguredPrefix {
}
@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,8 +4,10 @@ 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 {
@ -13,13 +15,14 @@ public class MessageUtil {
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) {
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) {
public static void send(@Nullable CommandSender sender, String... messages) {
send(sender, Arrays.asList(messages));
}
@ -27,23 +30,48 @@ public class MessageUtil {
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) {
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) {
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
}
public static String setPlaceholders(@Nullable CommandSender sender, String message) {
if (message == null) return null;
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});
}