mirror of
https://github.com/CarmJos/UserPrefix.git
synced 2026-07-16 00:41:08 +08:00
feat(prefix): add group support to prefix management and selection.
resolved #54
This commit is contained in:
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<artifactId>userprefix</artifactId>
|
<artifactId>userprefix</artifactId>
|
||||||
<version>3.3.1</version>
|
<version>3.4.0</version>
|
||||||
|
|
||||||
<name>UserPrefix</name>
|
<name>UserPrefix</name>
|
||||||
<description>轻便、高效、实时的用户前缀系统。</description>
|
<description>轻便、高效、实时的用户前缀系统。</description>
|
||||||
|
|||||||
@@ -15,14 +15,17 @@ public class UserCommand implements CommandExecutor {
|
|||||||
@Override
|
@Override
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
|
||||||
if (sender instanceof Player) {
|
if (sender instanceof Player) {
|
||||||
PrefixSelectGUI.open((Player) sender);
|
Player player = (Player) sender;
|
||||||
|
String group = strings.length > 0 ? strings[0] : null;
|
||||||
|
PrefixSelectGUI.open(player, group);
|
||||||
} else {
|
} else {
|
||||||
if (strings.length != 1) {
|
if (strings.length < 1) {
|
||||||
PluginMessages.COMMAND_USAGE.CONSOLE.sendTo(sender);
|
PluginMessages.COMMAND_USAGE.CONSOLE.sendTo(sender);
|
||||||
} else {
|
} else {
|
||||||
Player player = Bukkit.getPlayer(strings[0]);
|
Player player = Bukkit.getPlayer(strings[0]);
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
PrefixSelectGUI.open(player);
|
String group = strings.length > 1 ? strings[1] : null;
|
||||||
|
PrefixSelectGUI.open(player, group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ public class PrefixConfig {
|
|||||||
|
|
||||||
protected final @Nullable String permission;
|
protected final @Nullable String permission;
|
||||||
|
|
||||||
|
protected final @Nullable String group;
|
||||||
|
|
||||||
protected final @NotNull List<GUIActionConfiguration> actions;
|
protected final @NotNull List<GUIActionConfiguration> actions;
|
||||||
|
|
||||||
protected final @NotNull ItemStack itemHasPermission;
|
protected final @NotNull ItemStack itemHasPermission;
|
||||||
@@ -76,6 +78,21 @@ public class PrefixConfig {
|
|||||||
@NotNull ItemStack itemHasPermission,
|
@NotNull ItemStack itemHasPermission,
|
||||||
@Nullable ItemStack itemWhenUsing,
|
@Nullable ItemStack itemWhenUsing,
|
||||||
@Nullable ItemStack itemNoPermission) {
|
@Nullable ItemStack itemNoPermission) {
|
||||||
|
this(
|
||||||
|
identifier, name, description,
|
||||||
|
content, period, weight, permission, null, actions,
|
||||||
|
itemHasPermission, itemWhenUsing, itemNoPermission
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrefixConfig(@NotNull String identifier,
|
||||||
|
@NotNull String name, @NotNull List<String> description,
|
||||||
|
@NotNull List<String> content, long period,
|
||||||
|
int weight, @Nullable String permission, @Nullable String group,
|
||||||
|
@NotNull List<GUIActionConfiguration> actions,
|
||||||
|
@NotNull ItemStack itemHasPermission,
|
||||||
|
@Nullable ItemStack itemWhenUsing,
|
||||||
|
@Nullable ItemStack itemNoPermission) {
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
@@ -83,6 +100,7 @@ public class PrefixConfig {
|
|||||||
this.period = period;
|
this.period = period;
|
||||||
this.weight = weight;
|
this.weight = weight;
|
||||||
this.permission = permission;
|
this.permission = permission;
|
||||||
|
this.group = group;
|
||||||
this.actions = actions;
|
this.actions = actions;
|
||||||
this.itemHasPermission = itemHasPermission;
|
this.itemHasPermission = itemHasPermission;
|
||||||
this.itemNoPermission = itemNoPermission;
|
this.itemNoPermission = itemNoPermission;
|
||||||
@@ -104,6 +122,11 @@ public class PrefixConfig {
|
|||||||
return ColorParser.parse(description);
|
return ColorParser.parse(description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getGroup() {
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getContent(CommandSender viewer) {
|
public String getContent(CommandSender viewer) {
|
||||||
if (content.isEmpty()) return "?";
|
if (content.isEmpty()) return "?";
|
||||||
|
|||||||
@@ -38,41 +38,65 @@ public class PrefixManager {
|
|||||||
|
|
||||||
File prefixDataFolder = getStorageFolder();
|
File prefixDataFolder = getStorageFolder();
|
||||||
if (!prefixDataFolder.isDirectory() || !prefixDataFolder.exists()) {
|
if (!prefixDataFolder.isDirectory() || !prefixDataFolder.exists()) {
|
||||||
prefixDataFolder.mkdir();
|
prefixDataFolder.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] filesList = prefixDataFolder.list();
|
List<File> files = new ArrayList<>();
|
||||||
if (filesList == null || filesList.length < 1) {
|
collectYamlFiles(prefixDataFolder, files);
|
||||||
|
|
||||||
|
if (files.isEmpty()) {
|
||||||
Main.severe("配置文件夹中暂无任何前缀配置问,请检查。");
|
Main.severe("配置文件夹中暂无任何前缀配置问,请检查。");
|
||||||
Main.severe("There's no configured prefix.");
|
Main.severe("There's no configured prefix.");
|
||||||
Main.severe("Path: " + prefixDataFolder.getAbsolutePath());
|
Main.severe("Path: " + prefixDataFolder.getAbsolutePath());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<File> files = Arrays.stream(filesList)
|
|
||||||
.map(s -> new File(prefixDataFolder, s))
|
|
||||||
.filter(File::isFile)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
HashMap<String, PrefixConfig> loaded = new HashMap<>();
|
HashMap<String, PrefixConfig> loaded = new HashMap<>();
|
||||||
|
|
||||||
if (files.size() > 0) {
|
for (File file : files) {
|
||||||
for (File file : files) {
|
try {
|
||||||
try {
|
PrefixConfig prefix = addPrefix(file);
|
||||||
PrefixConfig prefix = addPrefix(file);
|
Main.debugging("完成前缀加载 " + prefix.getIdentifier() + " : " + prefix.getName());
|
||||||
Main.debugging("完成前缀加载 " + prefix.getIdentifier() + " : " + prefix.getName());
|
loaded.put(prefix.getIdentifier(), prefix);
|
||||||
loaded.put(prefix.getIdentifier(), prefix);
|
} catch (Exception ex) {
|
||||||
} catch (Exception ex) {
|
Main.severe("在加载前缀 " + file.getAbsolutePath() + " 时出错,请检查配置!");
|
||||||
Main.severe("在加载前缀 " + file.getAbsolutePath() + " 时出错,请检查配置!");
|
Main.severe("Error occurred when loading prefix #" + file.getAbsolutePath() + " !");
|
||||||
Main.severe("Error occurred when loading prefix #" + file.getAbsolutePath() + " !");
|
ex.printStackTrace();
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.prefixes = loaded;
|
this.prefixes = loaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归收集文件夹下的所有YAML文件
|
||||||
|
*
|
||||||
|
* @param folder 目标文件夹
|
||||||
|
* @param files 文件列表
|
||||||
|
*/
|
||||||
|
private void collectYamlFiles(@NotNull File folder, @NotNull List<File> files) {
|
||||||
|
File[] children = folder.listFiles();
|
||||||
|
if (children == null) return;
|
||||||
|
for (File child : children) {
|
||||||
|
if (child.isDirectory()) {
|
||||||
|
collectYamlFiles(child, files);
|
||||||
|
} else if (child.isFile() && isYamlFile(child)) {
|
||||||
|
files.add(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断文件是否为YAML文件
|
||||||
|
*
|
||||||
|
* @param file 文件
|
||||||
|
* @return 如果是.yml或.yaml文件返回true
|
||||||
|
*/
|
||||||
|
private boolean isYamlFile(@NotNull File file) {
|
||||||
|
String name = file.getName().toLowerCase();
|
||||||
|
return name.endsWith(".yml") || name.endsWith(".yaml");
|
||||||
|
}
|
||||||
|
|
||||||
public void loadDefaultPrefix() {
|
public void loadDefaultPrefix() {
|
||||||
this.defaultPrefix = new PrefixConfig(
|
this.defaultPrefix = new PrefixConfig(
|
||||||
"default",
|
"default",
|
||||||
@@ -96,6 +120,36 @@ public class PrefixManager {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据group获取可见的前缀列表
|
||||||
|
*
|
||||||
|
* @param player 玩家
|
||||||
|
* @param group 分组名称,为null时返回所有前缀
|
||||||
|
* @return 过滤后的前缀列表
|
||||||
|
*/
|
||||||
|
public List<PrefixConfig> getVisiblePrefix(Player player, @Nullable String group) {
|
||||||
|
if (group == null || group.isEmpty()) {
|
||||||
|
return getVisiblePrefix(player);
|
||||||
|
}
|
||||||
|
return getPrefixes().values().stream()
|
||||||
|
.filter(c -> c.isVisible(player))
|
||||||
|
.filter(c -> group.equalsIgnoreCase(c.getGroup()))
|
||||||
|
.sorted(Comparator.comparingInt(PrefixConfig::getWeight))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有已配置的group列表
|
||||||
|
*
|
||||||
|
* @return group名称集合
|
||||||
|
*/
|
||||||
|
public Set<String> getGroups() {
|
||||||
|
return getPrefixes().values().stream()
|
||||||
|
.map(PrefixConfig::getGroup)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public PrefixConfig getDefaultPrefix() {
|
public PrefixConfig getDefaultPrefix() {
|
||||||
return defaultPrefix;
|
return defaultPrefix;
|
||||||
@@ -152,6 +206,7 @@ public class PrefixManager {
|
|||||||
conf.getLong("period", -1L),
|
conf.getLong("period", -1L),
|
||||||
conf.getInt("weight", 1),
|
conf.getInt("weight", 1),
|
||||||
conf.getString("permission"),
|
conf.getString("permission"),
|
||||||
|
conf.getString("group"),
|
||||||
readActions(conf.getStringList("actions")),
|
readActions(conf.getStringList("actions")),
|
||||||
readItem(
|
readItem(
|
||||||
conf.getConfigurationSection("item.has-perm"),
|
conf.getConfigurationSection("item.has-perm"),
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import cc.carm.plugin.userprefix.conf.prefix.PrefixConfig;
|
|||||||
import cc.carm.plugin.userprefix.event.UserPrefixChangeEvent;
|
import cc.carm.plugin.userprefix.event.UserPrefixChangeEvent;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.inventory.ClickType;
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -20,11 +21,17 @@ public class PrefixSelectGUI extends AutoPagedGUI {
|
|||||||
|
|
||||||
public static HashSet<Player> openingUsers = new HashSet<>();
|
public static HashSet<Player> openingUsers = new HashSet<>();
|
||||||
|
|
||||||
protected final Player player;
|
protected final Player player;
|
||||||
|
protected final @Nullable String group;
|
||||||
|
|
||||||
public PrefixSelectGUI(Player player) {
|
public PrefixSelectGUI(Player player) {
|
||||||
|
this(player, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrefixSelectGUI(Player player, @Nullable String group) {
|
||||||
super(GUIType.SIX_BY_NINE, PluginConfig.GUI.TITLE.get(), 10, 43);
|
super(GUIType.SIX_BY_NINE, PluginConfig.GUI.TITLE.get(), 10, 43);
|
||||||
this.player = player;
|
this.player = player;
|
||||||
|
this.group = group;
|
||||||
|
|
||||||
setPreviousPageSlot(18);
|
setPreviousPageSlot(18);
|
||||||
setNextPageSlot(26);
|
setNextPageSlot(26);
|
||||||
@@ -37,6 +44,11 @@ public class PrefixSelectGUI extends AutoPagedGUI {
|
|||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getGroup() {
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
|
||||||
public void loadExtraIcons() {
|
public void loadExtraIcons() {
|
||||||
PluginConfig.GUI.ITEMS.getNotNull().getItems().values().forEach(v -> v.setupItems(player, this));
|
PluginConfig.GUI.ITEMS.getNotNull().getItems().values().forEach(v -> v.setupItems(player, this));
|
||||||
}
|
}
|
||||||
@@ -44,7 +56,7 @@ public class PrefixSelectGUI extends AutoPagedGUI {
|
|||||||
public void loadItems() {
|
public void loadItems() {
|
||||||
List<PrefixConfig> prefixList = new ArrayList<>();
|
List<PrefixConfig> prefixList = new ArrayList<>();
|
||||||
prefixList.add(UserPrefixAPI.getPrefixManager().getDefaultPrefix());
|
prefixList.add(UserPrefixAPI.getPrefixManager().getDefaultPrefix());
|
||||||
prefixList.addAll(UserPrefixAPI.getPrefixManager().getVisiblePrefix(player)); //只需要读取看得见的
|
prefixList.addAll(UserPrefixAPI.getPrefixManager().getVisiblePrefix(player, group));
|
||||||
|
|
||||||
PrefixConfig usingPrefix = UserPrefixAPI.getUserManager().getPrefix(getPlayer());
|
PrefixConfig usingPrefix = UserPrefixAPI.getUserManager().getPrefix(getPlayer());
|
||||||
|
|
||||||
@@ -60,10 +72,7 @@ public class PrefixSelectGUI extends AutoPagedGUI {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(Player clicker, ClickType type) {
|
public void onClick(Player clicker, ClickType type) {
|
||||||
player.closeInventory();
|
player.closeInventory();
|
||||||
//再次检查,防止打开GUI后、选择前的时间段内权限消失
|
|
||||||
if (prefix.checkPermission(player)) {
|
if (prefix.checkPermission(player)) {
|
||||||
|
|
||||||
// 发送消息与提示
|
|
||||||
PluginConfig.SOUNDS.PREFIX_CHANGE.playTo(player);
|
PluginConfig.SOUNDS.PREFIX_CHANGE.playTo(player);
|
||||||
PluginMessages.SELECTED.sendTo(player, prefix.getName());
|
PluginMessages.SELECTED.sendTo(player, prefix.getName());
|
||||||
|
|
||||||
@@ -86,8 +95,6 @@ public class PrefixSelectGUI extends AutoPagedGUI {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -107,11 +114,12 @@ public class PrefixSelectGUI extends AutoPagedGUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void open(Player player) {
|
public static void open(Player player) {
|
||||||
// player.closeInventory(); // 防止冲突
|
open(player, null);
|
||||||
PluginConfig.SOUNDS.GUI_OPEN.playTo(player);
|
|
||||||
new PrefixSelectGUI(player).openGUI(player);
|
|
||||||
openingUsers.add(player);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void open(Player player, @Nullable String group) {
|
||||||
|
PluginConfig.SOUNDS.GUI_OPEN.playTo(player);
|
||||||
|
new PrefixSelectGUI(player, group).openGUI(player);
|
||||||
|
openingUsers.add(player);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user