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

refactor(config): 修改前缀配置文件的配置路径

BREAKING CHANGE: 旧的配置文件需要进行变更。
This commit is contained in:
Carm Jos 2022-06-18 22:04:02 +08:00
parent d3a3529ccc
commit 899acbe2cc
13 changed files with 152 additions and 131 deletions

View File

@ -5,7 +5,7 @@ import cc.carm.plugin.userprefix.manager.ConfigManager;
import cc.carm.plugin.userprefix.manager.PrefixManager;
import cc.carm.plugin.userprefix.manager.UserManager;
public class UserPrefix {
public class UserPrefixAPI {
public static PrefixManager getPrefixManager() {
return Main.getInstance().prefixManager;

View File

@ -1,6 +1,6 @@
package cc.carm.plugin.userprefix.command;
import cc.carm.plugin.userprefix.UserPrefix;
import cc.carm.plugin.userprefix.UserPrefixAPI;
import cc.carm.plugin.userprefix.configuration.PluginMessages;
import cc.carm.plugin.userprefix.configuration.prefix.PrefixConfig;
import cc.carm.plugin.userprefix.ui.PrefixSelectGUI;
@ -19,7 +19,7 @@ public class AdminCommand implements CommandExecutor {
String aim = args[0];
if (aim.equalsIgnoreCase("list")) {
PluginMessages.LIST.HEADER.send(sender);
for (PrefixConfig value : UserPrefix.getPrefixManager().getPrefixes().values()) {
for (PrefixConfig value : UserPrefixAPI.getPrefixManager().getPrefixes().values()) {
PluginMessages.LIST.VALUE.send(sender,
value.getWeight(), value.getIdentifier(),
value.getName(), value.getPermission(),
@ -31,16 +31,16 @@ public class AdminCommand implements CommandExecutor {
long s1 = System.currentTimeMillis();
PrefixSelectGUI.closeAll(); // 关掉所有正在显示的前缀列表
try {
UserPrefix.getConfigManager().reload(); // 重载配置文件
int num = UserPrefix.getPrefixManager().loadPrefixes(); //加载重载后了的前缀配置
UserPrefixAPI.getConfigManager().reload(); // 重载配置文件
int num = UserPrefixAPI.getPrefixManager().loadPrefixes(); //加载重载后了的前缀配置
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
UserPrefix.getUserManager().checkPrefix(onlinePlayer, false);
UserPrefixAPI.getUserManager().checkPrefix(onlinePlayer, false);
/*
* 这里关掉loadOthers(为其他玩家更新)
* 因为每个玩家更新的时候会为其他人更新自己
* 全部走完一遍后所有玩家都会加载最新的前缀内容
*/
UserPrefix.getUserManager().updatePrefixView(onlinePlayer, false);
UserPrefixAPI.getUserManager().updatePrefixView(onlinePlayer, false);
}
PluginMessages.RELOAD.SUCCESS.send(sender, System.currentTimeMillis() - s1, num);
} catch (Exception e) {

View File

@ -154,21 +154,27 @@ public class PluginConfig extends ConfigurationRoot {
@HeaderComment({"默认前缀的内容,即用于显示的实际前缀"})
public static final ConfigValue<String> CONTENT = ConfiguredValue.of(String.class, "&r");
@HeaderComment({"当未选择默认前缀时显示的物品"})
public static final ConfiguredItem ITEM_NOT_USING = ConfiguredItem.create()
.defaultType(Material.NAME_TAG)
.defaultName("&f默认玩家前缀 &f(点击切换)")
.defaultLore("", "&a➥ 点击切换到该前缀")
.build();
@HeaderComment({"默认前缀的显示物品"})
public static final class ITEM {
@HeaderComment({"当未选择默认前缀时显示的物品"})
public static final ConfiguredItem NOT_USING = ConfiguredItem.create()
.defaultType(Material.NAME_TAG)
.defaultName("&f默认玩家前缀 &f(点击切换)")
.defaultLore("", "&a➥ 点击切换到该前缀")
.build();
@HeaderComment({"当选择了默认前缀时显示的物品"})
public static final ConfiguredItem USING = ConfiguredItem.create()
.defaultType(Material.NAME_TAG)
.defaultEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1)
.defaultFlags(ItemFlag.HIDE_ENCHANTS)
.defaultName("&f默认玩家前缀")
.defaultLore("", "&a✔ 您正在使用该前缀")
.build();
}
@HeaderComment({"当选择了默认前缀时显示的物品"})
public static final ConfiguredItem ITEM_USING = ConfiguredItem.create()
.defaultType(Material.NAME_TAG)
.defaultEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1)
.defaultFlags(ItemFlag.HIDE_ENCHANTS)
.defaultName("&f默认玩家前缀")
.defaultLore("", "&a✔ 您正在使用该前缀")
.build();
}

View File

@ -33,8 +33,8 @@ public class PrefixConfig {
public PrefixConfig(@NotNull String identifier, @NotNull String name,
@NotNull String content, int weight, @Nullable String permission,
@NotNull ItemConfig itemHasPermission,
@Nullable ItemConfig itemNoPermission,
@Nullable ItemConfig itemWhenUsing) {
@Nullable ItemConfig itemWhenUsing,
@Nullable ItemConfig itemNoPermission) {
this.identifier = identifier;
this.name = name;
this.content = content;
@ -97,7 +97,7 @@ public class PrefixConfig {
/**
* 判断某玩家是否有权限使用该前缀
*
* @param player 玩家
* @param player 玩家
* @return 若前缀标识不存在则返回false若前缀为默认前缀或该前缀无权限或玩家有该前缀的权限则返回true
*/
public boolean checkPermission(Player player) {

View File

@ -1,4 +1,4 @@
package cc.carm.plugin.userprefix.nametag;
package cc.carm.plugin.userprefix.hooker;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -11,6 +11,11 @@ import java.util.Map;
import java.util.Set;
import java.util.UUID;
/**
* 用户头顶标签
*
* @author LSeng, Tony, Carm
*/
public class UserNameTag {
public static final int MAX_ORDER = 999;

View File

@ -1,7 +1,7 @@
package cc.carm.plugin.userprefix.hooker;
import cc.carm.plugin.userprefix.Main;
import cc.carm.plugin.userprefix.UserPrefix;
import cc.carm.plugin.userprefix.UserPrefixAPI;
import cc.carm.plugin.userprefix.configuration.prefix.PrefixConfig;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
@ -60,20 +60,20 @@ public class UserPrefixExpansion extends PlaceholderExpansion {
switch (args[0].toLowerCase()) {
case "identifier": {
return UserPrefix.getUserManager().getPrefix(player).getIdentifier();
return UserPrefixAPI.getUserManager().getPrefix(player).getIdentifier();
}
case "prefix": {
return UserPrefix.getUserManager().getPrefix(player).getContent();
return UserPrefixAPI.getUserManager().getPrefix(player).getContent();
}
case "name": {
return UserPrefix.getUserManager().getPrefix(player).getName();
return UserPrefixAPI.getUserManager().getPrefix(player).getName();
}
case "weight": {
return Integer.toString(UserPrefix.getUserManager().getPrefix(player).getWeight());
return Integer.toString(UserPrefixAPI.getUserManager().getPrefix(player).getWeight());
}
case "has": {
if (args.length < 2) return "参数不足";
PrefixConfig prefix = UserPrefix.getPrefixManager().getPrefix(args[1]);
PrefixConfig prefix = UserPrefixAPI.getPrefixManager().getPrefix(args[1]);
if (prefix == null) return "该前缀不存在";
return Boolean.toString(prefix.checkPermission(player));
}

View File

@ -1,6 +1,6 @@
package cc.carm.plugin.userprefix.listener;
import cc.carm.plugin.userprefix.UserPrefix;
import cc.carm.plugin.userprefix.UserPrefixAPI;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
@ -10,12 +10,12 @@ public class UserListener implements Listener {
@EventHandler
public void onJoin(PlayerJoinEvent event) {
UserPrefix.getUserManager().initPlayer(event.getPlayer());
UserPrefixAPI.getUserManager().initPlayer(event.getPlayer());
}
@EventHandler
public void onLeave(PlayerQuitEvent event) {
UserPrefix.getUserManager().unloadPlayer(event.getPlayer());
UserPrefixAPI.getUserManager().unloadPlayer(event.getPlayer());
}
}

View File

@ -1,7 +1,7 @@
package cc.carm.plugin.userprefix.listener.processor;
import cc.carm.plugin.userprefix.Main;
import cc.carm.plugin.userprefix.UserPrefix;
import cc.carm.plugin.userprefix.UserPrefixAPI;
import cc.carm.plugin.userprefix.ui.PrefixSelectGUI;
import net.luckperms.api.event.user.UserDataRecalculateEvent;
import org.bukkit.Bukkit;
@ -12,7 +12,7 @@ public class UserNodeUpdateProcessor {
public static void process(UserDataRecalculateEvent event) {
Player player = Bukkit.getPlayer(event.getUser().getUniqueId());
if (player == null) return;
UserPrefix.getUserManager().checkPrefix(player, true);
UserPrefixAPI.getUserManager().checkPrefix(player, true);
if (PrefixSelectGUI.openingUsers.contains(player)) {
Main.getInstance().getScheduler().run(() -> {
// 玩家权限更新同步关闭其GUI以令其重新打开刷新自己的前缀

View File

@ -78,9 +78,9 @@ public class PrefixManager {
PluginConfig.DEFAULT_PREFIX.CONTENT.getNotNull(),
PluginConfig.DEFAULT_PREFIX.WEIGHT.getNotNull(),
null,
PluginConfig.DEFAULT_PREFIX.ITEM_NOT_USING.getNotNull(),
null,
PluginConfig.DEFAULT_PREFIX.ITEM_USING.get()
PluginConfig.DEFAULT_PREFIX.ITEM.NOT_USING.getNotNull(),
PluginConfig.DEFAULT_PREFIX.ITEM.USING.get(),
null
);
Main.debugging(" 完成默认前缀加载 " + defaultPrefix.getName());
}
@ -136,11 +136,11 @@ public class PrefixManager {
configuration.getInt("weight", 1),
configuration.getString("permission"),
readItem(
configuration.getConfigurationSection("itemHasPermission"),
new ItemConfig(Material.STONE, (short) 0, name, Arrays.asList(" ", "§a➥ 点击切换到该前缀"))
configuration.getConfigurationSection("item.has-perm"),
new ItemConfig(Material.STONE, name, Arrays.asList(" ", "§a➥ 点击切换到该前缀"))
),
readItem(configuration.getConfigurationSection("itemNoPermission"), null),
readItem(configuration.getConfigurationSection("itemUsing"), null)
readItem(configuration.getConfigurationSection("item.using"), null),
readItem(configuration.getConfigurationSection("item.no-perm"), null)
);
}

View File

@ -2,13 +2,13 @@ package cc.carm.plugin.userprefix.manager;
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.plugin.userprefix.Main;
import cc.carm.plugin.userprefix.UserPrefix;
import cc.carm.plugin.userprefix.UserPrefixAPI;
import cc.carm.plugin.userprefix.configuration.PluginConfig;
import cc.carm.plugin.userprefix.configuration.PluginMessages;
import cc.carm.plugin.userprefix.configuration.prefix.PrefixConfig;
import cc.carm.plugin.userprefix.event.UserPrefixChangeEvent;
import cc.carm.plugin.userprefix.event.UserPrefixExpireEvent;
import cc.carm.plugin.userprefix.nametag.UserNameTag;
import cc.carm.plugin.userprefix.hooker.UserNameTag;
import cc.carm.plugin.userprefix.ui.PrefixSelectGUI;
import net.luckperms.api.model.user.User;
import net.luckperms.api.node.NodeType;
@ -123,7 +123,7 @@ public class UserManager {
String currentPrefixData = getPrefixData(player);
if (!isPrefixUsable(player, currentPrefixData)) {
PrefixConfig currentPrefix = UserPrefix.getPrefixManager().getPrefix(currentPrefixData);
PrefixConfig currentPrefix = UserPrefixAPI.getPrefixManager().getPrefix(currentPrefixData);
PrefixConfig newPrefix = getHighestPrefix(player);
if (currentPrefix != null) {
@ -169,8 +169,8 @@ public class UserManager {
if (identifier == null || !isPrefixUsable(player, identifier)) {
return getHighestPrefix(player);
} else {
PrefixConfig prefix = UserPrefix.getPrefixManager().getPrefix(identifier);
return prefix == null ? UserPrefix.getDefaultPrefix() : prefix;
PrefixConfig prefix = UserPrefixAPI.getPrefixManager().getPrefix(identifier);
return prefix == null ? UserPrefixAPI.getDefaultPrefix() : prefix;
}
}
@ -194,7 +194,7 @@ public class UserManager {
*/
@NotNull
public List<PrefixConfig> getUsablePrefixes(Player player) {
return UserPrefix.getPrefixManager().getPrefixes().values().stream()
return UserPrefixAPI.getPrefixManager().getPrefixes().values().stream()
.filter(prefix -> prefix.checkPermission(player)) //过滤出玩家可用的前缀
.sorted(Comparator.comparingInt(PrefixConfig::getWeight)) // 以前缀排序
.collect(Collectors.toList()); // 返回集合
@ -212,11 +212,11 @@ public class UserManager {
public PrefixConfig getHighestPrefix(Player player) {
if (PluginConfig.FUNCTIONS.AUTO_USE.getNotNull()) {
// 关闭了自动选择就直接给默认的前缀让玩家自己去设置吧~
return UserPrefix.getDefaultPrefix();
return UserPrefixAPI.getDefaultPrefix();
}
return getUsablePrefixes(player).stream()
.max(Comparator.comparingInt(PrefixConfig::getWeight)) // 取权重最大
.orElseGet(UserPrefix::getDefaultPrefix); // 啥都没有 返回默认前缀
.orElseGet(UserPrefixAPI::getDefaultPrefix); // 啥都没有 返回默认前缀
}
/**
@ -229,7 +229,7 @@ public class UserManager {
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean isPrefixUsable(Player player, String prefixIdentifier) {
if (prefixIdentifier == null || prefixIdentifier.equalsIgnoreCase("default")) return true;
PrefixConfig prefix = UserPrefix.getPrefixManager().getPrefix(prefixIdentifier);
PrefixConfig prefix = UserPrefixAPI.getPrefixManager().getPrefix(prefixIdentifier);
return prefix != null && prefix.checkPermission(player);
}

View File

@ -3,7 +3,7 @@ package cc.carm.plugin.userprefix.ui;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import cc.carm.lib.easyplugin.gui.paged.AutoPagedGUI;
import cc.carm.plugin.userprefix.UserPrefix;
import cc.carm.plugin.userprefix.UserPrefixAPI;
import cc.carm.plugin.userprefix.configuration.PluginConfig;
import cc.carm.plugin.userprefix.configuration.PluginMessages;
import cc.carm.plugin.userprefix.configuration.prefix.PrefixConfig;
@ -41,10 +41,10 @@ public class PrefixSelectGUI extends AutoPagedGUI {
public void loadItems() {
List<PrefixConfig> prefixList = new ArrayList<>();
prefixList.add(UserPrefix.getPrefixManager().getDefaultPrefix());
prefixList.addAll(UserPrefix.getPrefixManager().getVisiblePrefix(player)); //只需要读取看得见的
prefixList.add(UserPrefixAPI.getPrefixManager().getDefaultPrefix());
prefixList.addAll(UserPrefixAPI.getPrefixManager().getVisiblePrefix(player)); //只需要读取看得见的
PrefixConfig usingPrefix = UserPrefix.getUserManager().getPrefix(getPlayer());
PrefixConfig usingPrefix = UserPrefixAPI.getUserManager().getPrefix(getPlayer());
for (PrefixConfig prefix : prefixList) {
if (prefix.getIdentifier().equals(usingPrefix.getIdentifier())) {
@ -53,19 +53,25 @@ public class PrefixSelectGUI extends AutoPagedGUI {
addItem(new GUIItem(prefix.getItemHasPermission(player)) {
@Override
public void onClick(ClickType type) {
player.closeInventory();
//再次检查防止打开GUI后选择前的时间段内权限消失
if (prefix.checkPermission(player)) {
player.closeInventory();
UserPrefix.getUserManager().setPrefix(player, prefix, true);
UserPrefixAPI.getUserManager().setPrefix(player, prefix, true);
PluginConfig.SOUNDS.PREFIX_CHANGE.playTo(player);
PluginMessages.SELECTED.send(player, prefix.getName());
} else {
PluginConfig.SOUNDS.GUI_CLICK.playTo(player);
}
}
});
} else {
addItem(new GUIItem(prefix.getItemNoPermission(player)));
addItem(new GUIItem(prefix.getItemNoPermission(player)) {
@Override
public void onClick(ClickType type) {
PluginConfig.SOUNDS.GUI_CLICK.playTo(player);
}
});
}
}

View File

@ -22,34 +22,35 @@ weight: 1
# (because it is impossible to display items without permission at all)
permission: "yc.vip"
# itemHasPermission [Necessary]
# This Item will be displayed when player has permission
itemHasPermission:
type: DIAMOND
name: "&b&lVIP Prefix"
lore:
- ""
- "&a➥ Click to use"
item:
# itemHasPermission [Necessary]
# This Item will be displayed when player has permission
has-perm:
type: DIAMOND
name: "&b&lVIP Prefix"
lore:
- ""
- "&a➥ Click to use"
# itemUsing [Unnecessary]
# This Item will be displayed when the prefix is selected.
# If there is no such configuration, it will automatically display "itemHasPermission".
itemUsing:
type: DIAMOND
name: "&b&lVIP Prefix"
enchants:
PROTECTION_ENVIRONMENTAL: 1 #Add an enchantment so it looks like its selected
lore:
- ""
- "&a✔ Selected"
# itemUsing [Unnecessary]
# This Item will be displayed when the prefix is selected.
# If there is no such configuration, it will automatically display "itemHasPermission".
using:
type: DIAMOND
name: "&b&lVIP Prefix"
enchants:
PROTECTION_ENVIRONMENTAL: 1 #Add an enchantment so it looks like its selected
lore:
- ""
- "&a✔ Selected"
# itemNoPermission [Unnecessary]
# If player doesn't have the permission,this item will be displayed.
# If this item is not configured, it will not be displayed in the GUI when the player does not have permission to use it.
itemNoPermission:
type: INK_SACK
data: 8
name: "&b&lVIP &c(Buy it!)"
lore:
- ""
- "&e✯ Buy the VIP to use it!"
# itemNoPermission [Unnecessary]
# If player doesn't have the permission,this item will be displayed.
# If this item is not configured, it will not be displayed in the GUI when the player does not have permission to use it.
no-perm:
type: INK_SACK
data: 8
name: "&b&lVIP &c(Buy it!)"
lore:
- ""
- "&e✯ Buy the VIP to use it!"

View File

@ -22,47 +22,50 @@ weight: 1
# 如果没有就是人人都能用也代表不用配置“itemNoPermission”了(因为压根不可能显示没权限时候的物品)
permission: "yc.pro"
# 有权限时显示的物品 [必须]
# 当用户有权限且未选中时,会显示该物品
itemHasPermission: #
type: DIAMOND
name: "&b&lPro &b会员前缀"
lore:
- "&7Pro会员专属称号"
- ""
- "&f尊贵的Pro会员专属称号。"
- "&f您将获得多种特权与更好的游戏体验。"
- ""
- "&a➥ 点击切换到该前缀"
# 该前缀的GUI物品配置
item:
# 正在使用时显示的物品 [非必需]
# 当用户正在使用时会显示这个物品不配置即自动加载“itemHasPermission”
itemUsing:
type: DIAMOND
name: "&b&lPro &b会员前缀"
flags:
- HIDE_ENCHANTS # 隐藏附魔显示
enchants:
PROTECTION_ENVIRONMENTAL: 1 #加一个附魔这样看上去就像是选中了的
lore:
- "&7Pro会员专属称号"
- ""
- "&f尊贵的Pro会员专属称号。"
- "&f您将获得多种特权与更好的游戏体验。"
- ""
- "&a✔ 您正在使用该前缀"
# 有权限时显示的物品 [必须]
# 当用户有权限且未选中时,会显示该物品
has-perm:
type: DIAMOND
name: "&b&lPro &b会员前缀"
lore:
- "&7Pro会员专属称号"
- ""
- "&f尊贵的Pro会员专属称号。"
- "&f您将获得多种特权与更好的游戏体验。"
- ""
- "&a➥ 点击切换到该前缀"
# 没有权限时显示的物品 [非必需]
# 如果没有权限就会显示这个item。如果不配置该物品则玩家没有使用权限时不会显示在GUI里面。
itemNoPermission:
type: INK_SACK
data: 8
name: "&b&lPro+ &b会员前缀 &c(未拥有)"
lore:
- "&7Pro+会员专属称号"
- ""
- "&f尊贵的Pro会员专属称号。"
- "&f您将获得多种特权与更好的游戏体验。"
- "&f您可以输入 &b/vip &f指令查看详细特权"
- ""
- "&e✯ 加入Pro+会员以使用该前缀!"
# 正在使用时显示的物品 [非必需]
# 当用户正在使用时会显示这个物品不配置即自动加载“itemHasPermission”
using:
type: DIAMOND
name: "&b&lPro &b会员前缀"
flags:
- HIDE_ENCHANTS # 隐藏附魔显示
enchants:
PROTECTION_ENVIRONMENTAL: 1 #加一个附魔这样看上去就像是选中了的
lore:
- "&7Pro会员专属称号"
- ""
- "&f尊贵的Pro会员专属称号。"
- "&f您将获得多种特权与更好的游戏体验。"
- ""
- "&a✔ 您正在使用该前缀"
# 没有权限时显示的物品 [非必需]
# 如果没有权限就会显示这个item。如果不配置该物品则玩家没有使用权限时不会显示在GUI里面。
no-perm:
type: INK_SACK
data: 8
name: "&b&lPro+ &b会员前缀 &c(未拥有)"
lore:
- "&7Pro+会员专属称号"
- ""
- "&f尊贵的Pro会员专属称号。"
- "&f您将获得多种特权与更好的游戏体验。"
- "&f您可以输入 &b/vip &f指令查看详细特权"
- ""
- "&e✯ 加入Pro+会员以使用该前缀!"