Skip to content

Commit

Permalink
chore(item): 更换物品的配置逻辑,支持跨版本的物品读取、保存。
Browse files Browse the repository at this point in the history
BREAKING-CHANGE: 旧的配置文件需要进行变更。
  • Loading branch information
CarmJos committed Oct 10, 2023
1 parent 0d25490 commit 4cc50c2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

<deps.easyplugin.version>1.5.2</deps.easyplugin.version>
<deps.mineconfig.version>2.4.0</deps.mineconfig.version>
<deps.easyplugin.version>1.5.8</deps.easyplugin.version>
<deps.mineconfig.version>2.8.6</deps.mineconfig.version>
</properties>

<groupId>cc.carm.plugin</groupId>
<artifactId>userprefix</artifactId>
<version>3.2.0</version>
<version>3.2.2</version>

<name>UserPrefix</name>
<description>轻便、高效、实时的用户前缀系统。</description>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cc/carm/plugin/userprefix/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ protected boolean initialize() {

log("初始化GUI管理器...");
GUI.initialize(this);
AutoPagedGUI.defaultNextPage = (PluginConfig.GUI.BOTTOMS.NEXT_PAGE::getItem);
AutoPagedGUI.defaultPreviousPage = (PluginConfig.GUI.BOTTOMS.PREV_PAGE::getItem);
AutoPagedGUI.defaultNextPage = (PluginConfig.GUI.BOTTOMS.NEXT_PAGE::get);
AutoPagedGUI.defaultPreviousPage = (PluginConfig.GUI.BOTTOMS.PREV_PAGE::get);

if (MessageUtils.hasPlaceholderAPI()) {
log("注册变量...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredSound;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.plugin.userprefix.conf.gui.GUIItems;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
Expand Down Expand Up @@ -116,7 +116,7 @@ public static final class BOTTOMS extends ConfigurationRoot {

@HeaderComment("GUI中的其他按钮物品 (若与现有物品位置冲突,将被覆盖)")
public static final ConfigValue<GUIItems> ITEMS = ConfiguredValue
.builder(GUIItems.class).fromSection()
.builderOf(GUIItems.class).fromSection()
.defaults(GUIItems::defaults)
.serializeValue(GUIItems::serialize)
.parseValue((v, d) -> GUIItems.parse(v))
Expand Down Expand Up @@ -147,7 +147,7 @@ public static final class DEFAULT_PREFIX extends ConfigurationRoot {
public static final ConfigValue<String> CONTENT = ConfiguredValue.of(String.class, "&r");

@HeaderComment({"选择默认前缀时执行的操作"})
public static final ConfiguredList<String> ACTIONS = ConfiguredList.builder(String.class).fromString()
public static final ConfiguredList<String> ACTIONS = ConfiguredList.builderOf(String.class).fromString()
.defaults("[CONSOLE] " + "say %player_name% 选择了默认前缀")
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import cc.carm.lib.easyplugin.gui.configuration.GUIActionConfiguration;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.item.PreparedItem;
import cc.carm.plugin.userprefix.manager.ServiceManager;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -24,16 +25,16 @@ public class PrefixConfig {

protected final @NotNull List<GUIActionConfiguration> actions;

protected final @NotNull ItemConfig itemHasPermission;
protected final @Nullable ItemConfig itemNoPermission;
protected final @Nullable ItemConfig itemWhenUsing;
protected final @NotNull ItemStack itemHasPermission;
protected final @Nullable ItemStack itemNoPermission;
protected final @Nullable ItemStack itemWhenUsing;

public PrefixConfig(@NotNull String identifier, @NotNull String name,
@NotNull String content, int weight, @Nullable String permission,
@NotNull List<GUIActionConfiguration> actions,
@NotNull ItemConfig itemHasPermission,
@Nullable ItemConfig itemWhenUsing,
@Nullable ItemConfig itemNoPermission) {
@NotNull ItemStack itemHasPermission,
@Nullable ItemStack itemWhenUsing,
@Nullable ItemStack itemNoPermission) {
this.identifier = identifier;
this.name = name;
this.content = content;
Expand Down Expand Up @@ -71,21 +72,26 @@ public String getPermission() {

@NotNull
public ItemStack getItemHasPermission(@Nullable Player player) {
return this.itemHasPermission.getItemStack(player);
return getItem(player, this.itemHasPermission);
}

@Nullable
public ItemStack getItemNoPermission(@Nullable Player player) {
if (this.itemNoPermission == null) return null;
return this.itemNoPermission.getItemStack(player);
return getItem(player, this.itemNoPermission);
}

@Nullable
public ItemStack getItemWhenUsing(@Nullable Player player) {
if (this.itemWhenUsing == null) return null;
else return this.itemWhenUsing.getItemStack(player);
return getItem(player, this.itemWhenUsing);
}

@Contract("_,!null->!null")
protected @Nullable ItemStack getItem(@Nullable Player player, @Nullable ItemStack item) {
if (item == null) return null;
return PreparedItem.of(item).get(player);
}


public boolean isPublic() {
return getPermission() == null;
}
Expand Down
37 changes: 21 additions & 16 deletions src/main/java/cc/carm/plugin/userprefix/manager/PrefixManager.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package cc.carm.plugin.userprefix.manager;

import cc.carm.lib.easyplugin.gui.configuration.GUIActionConfiguration;
import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
import cc.carm.plugin.userprefix.Main;
import cc.carm.plugin.userprefix.conf.PluginConfig;
import cc.carm.plugin.userprefix.conf.prefix.PrefixConfig;
import com.cryptomorin.xseries.XItemStack;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -80,8 +82,7 @@ public void loadDefaultPrefix() {
null,
readActions(PluginConfig.DEFAULT_PREFIX.ACTIONS.get()),
PluginConfig.DEFAULT_PREFIX.ITEM.NOT_USING.getNotNull(),
PluginConfig.DEFAULT_PREFIX.ITEM.USING.get(),
null
PluginConfig.DEFAULT_PREFIX.ITEM.USING.get(), null
);
Main.debugging(" 完成默认前缀加载 " + defaultPrefix.getName());
}
Expand Down Expand Up @@ -124,34 +125,38 @@ protected File getStorageFolder() {
}

public static @NotNull PrefixConfig addPrefix(@NotNull File file) throws Exception {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
String identifier = configuration.getString("identifier");
FileConfiguration conf = YamlConfiguration.loadConfiguration(file);
String identifier = conf.getString("identifier");
if (identifier == null)
throw new Exception("配置文件 " + file.getAbsolutePath() + " 中没有配置前缀的唯一标识。");

String name = configuration.getString("name");
String name = conf.getString("name");
if (name == null) throw new Exception("配置文件 " + file.getAbsolutePath() + " 中没有配置前缀的显示名称。");

return new PrefixConfig(
identifier, name,
configuration.getString("content", "&r"),
configuration.getInt("weight", 1),
configuration.getString("permission"),
readActions(configuration.getStringList("actions")),
conf.getString("content", "&r"),
conf.getInt("weight", 1),
conf.getString("permission"),
readActions(conf.getStringList("actions")),
readItem(
configuration.getConfigurationSection("item.has-perm"),
new ItemConfig(Material.STONE, name, Arrays.asList(" ", "§a➥ 点击切换到该前缀"))
conf.getConfigurationSection("item.has-perm"),
new ItemStackFactory(Material.STONE)
.setDisplayName(name)
.addLore("§a➥ 点击切换到该前缀")
.toItemStack()
),
readItem(configuration.getConfigurationSection("item.using"), null),
readItem(configuration.getConfigurationSection("item.no-perm"), null)
readItem(conf.getConfigurationSection("item.using"), null),
readItem(conf.getConfigurationSection("item.no-perm"), null)
);
}


@Contract("_,!null->!null")
protected static ItemConfig readItem(@Nullable ConfigurationSection section, @Nullable ItemConfig defaultValue) throws Exception {
protected static ItemStack readItem(@Nullable ConfigurationSection section,
@Nullable ItemStack defaultValue) throws Exception {
if (section == null) return defaultValue;
else return ItemConfig.deserialize(section);
else return XItemStack.deserialize(section);
}

protected static List<GUIActionConfiguration> readActions(@NotNull List<String> strings) {
Expand Down

0 comments on commit 4cc50c2

Please sign in to comment.