mirror of
https://github.com/CarmJos/UserPrefix.git
synced 2026-06-04 15:28:21 +08:00
feat: 新增prefix内容轮换。
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.2.7</version>
|
<version>3.3.0</version>
|
||||||
|
|
||||||
<name>UserPrefix</name>
|
<name>UserPrefix</name>
|
||||||
<description>轻便、高效、实时的用户前缀系统。</description>
|
<description>轻便、高效、实时的用户前缀系统。</description>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cc.carm.plugin.userprefix.conf;
|
package cc.carm.plugin.userprefix.conf;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.Configuration;
|
import cc.carm.lib.configuration.Configuration;
|
||||||
|
import cc.carm.lib.configuration.adapter.ValueType;
|
||||||
import cc.carm.lib.configuration.annotation.ConfigPath;
|
import cc.carm.lib.configuration.annotation.ConfigPath;
|
||||||
import cc.carm.lib.configuration.annotation.HeaderComments;
|
import cc.carm.lib.configuration.annotation.HeaderComments;
|
||||||
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
import cc.carm.lib.configuration.value.standard.ConfiguredList;
|
||||||
@@ -12,6 +13,10 @@ import cc.carm.plugin.userprefix.folia.MajorUtil;
|
|||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.inventory.ItemFlag;
|
import org.bukkit.inventory.ItemFlag;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class PluginConfig implements Configuration {
|
public class PluginConfig implements Configuration {
|
||||||
|
|
||||||
@HeaderComments({"开发者查错模式"})
|
@HeaderComments({"开发者查错模式"})
|
||||||
@@ -148,7 +153,32 @@ public class PluginConfig implements Configuration {
|
|||||||
public static final ConfiguredValue<Integer> WEIGHT = ConfiguredValue.of(Integer.class, 0);
|
public static final ConfiguredValue<Integer> WEIGHT = ConfiguredValue.of(Integer.class, 0);
|
||||||
|
|
||||||
@HeaderComments({"默认前缀的内容,即用于显示的实际前缀"})
|
@HeaderComments({"默认前缀的内容,即用于显示的实际前缀"})
|
||||||
public static final ConfiguredValue<String> CONTENT = ConfiguredValue.of(String.class, "&r");
|
public static final ConfiguredValue<List<String>> CONTENT = ConfiguredValue
|
||||||
|
.builderOf(ValueType.ofList(String.class))
|
||||||
|
.from(Object.class)
|
||||||
|
.parse(obj -> {
|
||||||
|
if (obj instanceof List<?>) {
|
||||||
|
List<?> list = (List<?>) obj;
|
||||||
|
return list.stream()
|
||||||
|
.map(Object::toString)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
} else return Collections.singletonList(String.valueOf(obj));
|
||||||
|
})
|
||||||
|
.serialize(list -> {
|
||||||
|
if (list.isEmpty()) {
|
||||||
|
return "";
|
||||||
|
} else if (list.size() == 1) {
|
||||||
|
return list.get(0);
|
||||||
|
} else {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.defaults(Collections.singletonList("&r"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
@HeaderComments({"默认前缀的切换周期,单位为毫秒。(小于0则表示不进行切换,等于0则代表所及选取)"})
|
||||||
|
public static final ConfiguredValue<Long> PERIOD = ConfiguredValue.of(-1L);
|
||||||
|
|
||||||
@HeaderComments({"选择默认前缀时执行的操作"})
|
@HeaderComments({"选择默认前缀时执行的操作"})
|
||||||
public static final ConfiguredList<String> ACTIONS = ConfiguredList.builderOf(String.class).fromString()
|
public static final ConfiguredList<String> ACTIONS = ConfiguredList.builderOf(String.class).fromString()
|
||||||
@@ -162,7 +192,7 @@ public class PluginConfig implements Configuration {
|
|||||||
public static final ConfiguredItem NOT_USING = ConfiguredItem.create()
|
public static final ConfiguredItem NOT_USING = ConfiguredItem.create()
|
||||||
.defaultType(Material.NAME_TAG)
|
.defaultType(Material.NAME_TAG)
|
||||||
.defaultName("&f默认玩家前缀 &f(点击切换)")
|
.defaultName("&f默认玩家前缀 &f(点击切换)")
|
||||||
.defaultLore("","{&f&o }#description#{1,1}", "&a➥ 点击切换到该前缀")
|
.defaultLore("", "{&f&o }#description#{1,1}", "&a➥ 点击切换到该前缀")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@HeaderComments({"当选择了默认前缀时显示的物品"})
|
@HeaderComments({"当选择了默认前缀时显示的物品"})
|
||||||
@@ -171,7 +201,7 @@ public class PluginConfig implements Configuration {
|
|||||||
.defaultEnchant(MajorUtil.getEnchantProtection(), 1) // 附魔改过名
|
.defaultEnchant(MajorUtil.getEnchantProtection(), 1) // 附魔改过名
|
||||||
.defaultFlags(ItemFlag.HIDE_ENCHANTS)
|
.defaultFlags(ItemFlag.HIDE_ENCHANTS)
|
||||||
.defaultName("&f默认玩家前缀")
|
.defaultName("&f默认玩家前缀")
|
||||||
.defaultLore("","{&f&o }#description#{1,1}", "&a✔ 您正在使用该前缀")
|
.defaultLore("", "{&f&o }#description#{1,1}", "&a✔ 您正在使用该前缀")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,16 +14,22 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class PrefixConfig {
|
public class PrefixConfig {
|
||||||
|
|
||||||
|
static final Random RANDOM = new Random();
|
||||||
|
|
||||||
protected final @NotNull String identifier;
|
protected final @NotNull String identifier;
|
||||||
|
|
||||||
protected final @NotNull String name;
|
protected final @NotNull String name;
|
||||||
protected final @NotNull String content;
|
protected final @NotNull List<String> content;
|
||||||
protected final @NotNull List<String> description;
|
protected final @NotNull List<String> description;
|
||||||
|
|
||||||
|
protected final long period; // 内容变更周期,单位为毫秒
|
||||||
|
|
||||||
protected final int weight;
|
protected final int weight;
|
||||||
|
|
||||||
protected final @Nullable String permission;
|
protected final @Nullable String permission;
|
||||||
@@ -54,10 +60,26 @@ 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,
|
||||||
|
Collections.singletonList(content), 0, weight, permission, 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,
|
||||||
|
@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;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
this.period = period;
|
||||||
this.weight = weight;
|
this.weight = weight;
|
||||||
this.permission = permission;
|
this.permission = permission;
|
||||||
this.actions = actions;
|
this.actions = actions;
|
||||||
@@ -83,7 +105,21 @@ public class PrefixConfig {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getContent(CommandSender viewer) {
|
public String getContent(CommandSender viewer) {
|
||||||
return MessageUtils.setPlaceholders(viewer, content);
|
if (content.isEmpty()) return "?";
|
||||||
|
if (period < 0 || content.size() == 1) {
|
||||||
|
return MessageUtils.setPlaceholders(viewer, content.get(0));
|
||||||
|
}
|
||||||
|
if (period == 0) {
|
||||||
|
// PERIOD 为0时,随机返回一个内容
|
||||||
|
int index = RANDOM.nextInt(content.size());
|
||||||
|
return MessageUtils.setPlaceholders(viewer, content.get(index));
|
||||||
|
} else {
|
||||||
|
// 可变化的内容,则基于偏移量与时间戳计算目标index
|
||||||
|
long curr = System.currentTimeMillis();
|
||||||
|
long offset = curr % period; // 计算偏移量
|
||||||
|
int index = (int) (offset / (period / content.size())); // 计算索引
|
||||||
|
return MessageUtils.setPlaceholders(viewer, content.get(index));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWeight() {
|
public int getWeight() {
|
||||||
|
|||||||
@@ -78,7 +78,8 @@ public class PrefixManager {
|
|||||||
"default",
|
"default",
|
||||||
PluginConfig.DEFAULT_PREFIX.NAME.getNotNull(),
|
PluginConfig.DEFAULT_PREFIX.NAME.getNotNull(),
|
||||||
PluginConfig.DEFAULT_PREFIX.DESCRIPTION.getNotNull(),
|
PluginConfig.DEFAULT_PREFIX.DESCRIPTION.getNotNull(),
|
||||||
PluginConfig.DEFAULT_PREFIX.CONTENT.getNotNull(),
|
PluginConfig.DEFAULT_PREFIX.CONTENT.resolve(),
|
||||||
|
PluginConfig.DEFAULT_PREFIX.PERIOD.resolve(),
|
||||||
PluginConfig.DEFAULT_PREFIX.WEIGHT.getNotNull(),
|
PluginConfig.DEFAULT_PREFIX.WEIGHT.getNotNull(),
|
||||||
null,
|
null,
|
||||||
readActions(PluginConfig.DEFAULT_PREFIX.ACTIONS.get()),
|
readActions(PluginConfig.DEFAULT_PREFIX.ACTIONS.get()),
|
||||||
@@ -137,7 +138,8 @@ public class PrefixManager {
|
|||||||
return new PrefixConfig(
|
return new PrefixConfig(
|
||||||
identifier, name,
|
identifier, name,
|
||||||
conf.getStringList("description"),
|
conf.getStringList("description"),
|
||||||
conf.getString("content", "&r"),
|
conf.getStringList("content"),
|
||||||
|
conf.getLong("period", -1L),
|
||||||
conf.getInt("weight", 1),
|
conf.getInt("weight", 1),
|
||||||
conf.getString("permission"),
|
conf.getString("permission"),
|
||||||
readActions(conf.getStringList("actions")),
|
readActions(conf.getStringList("actions")),
|
||||||
|
|||||||
@@ -9,8 +9,16 @@ name: "&b&lPro &b会员"
|
|||||||
|
|
||||||
# 内容 [必须]
|
# 内容 [必须]
|
||||||
# 显示在名字前面的内容
|
# 显示在名字前面的内容
|
||||||
|
# 支持多行内容以实现前缀内容的轮换
|
||||||
content: "&b&lPro &b"
|
content: "&b&lPro &b"
|
||||||
|
|
||||||
|
# 轮换周期 [非必须]
|
||||||
|
# 设置前缀内容的轮换周期,单位为毫秒。
|
||||||
|
# 若数值为-1,则表示不轮换。
|
||||||
|
# 若数值为0,则每次获取前缀时都会随机选择一行内容。
|
||||||
|
# 若数值大于0,则表示每隔指定毫秒数轮换一次内容
|
||||||
|
period: -1
|
||||||
|
|
||||||
# 权重 [必须]
|
# 权重 [必须]
|
||||||
# 用于GUI、TabList的排序和自动前缀显示
|
# 用于GUI、TabList的排序和自动前缀显示
|
||||||
# 在GUI中,权重越高的会显示在越后面
|
# 在GUI中,权重越高的会显示在越后面
|
||||||
|
|||||||
Reference in New Issue
Block a user