mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2026-06-04 13:55:03 +08:00
feat(item): 重构物品相关代码,支持更多功能
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>2.8.5</version>
|
||||
<version>2.8.6</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
+1
-14
@@ -1,31 +1,18 @@
|
||||
package cc.carm.lib.mineconfiguration.bukkit.value.item;
|
||||
|
||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.builder.item.ItemConfigBuilder;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
|
||||
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
|
||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||
import com.cryptomorin.xseries.XItemStack;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ConfiguredItem extends ConfiguredSection<ItemStack> {
|
||||
|
||||
@@ -67,7 +54,7 @@ public class ConfiguredItem extends ConfiguredSection<ItemStack> {
|
||||
}
|
||||
|
||||
public @NotNull PreparedItem prepare(@NotNull Object... values) {
|
||||
return new PreparedItem(this, values);
|
||||
return PreparedItem.of(player -> get()).params(params).values(values);
|
||||
}
|
||||
|
||||
public @Nullable ItemStack get(@Nullable Player player) {
|
||||
|
||||
+32
-17
@@ -19,6 +19,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -26,7 +27,15 @@ public class PreparedItem {
|
||||
|
||||
public static final @NotNull Pattern LORE_INSERT_PATTERN = Pattern.compile("^(?:\\{(.*)})?#(.*)#(?:\\{(-?\\d+)(?:,(-?\\d+))?})?$");
|
||||
|
||||
protected final @NotNull ConfiguredItem itemConfig;
|
||||
public static PreparedItem of(@NotNull Function<@NotNull Player, @Nullable ItemStack> itemProvider) {
|
||||
return new PreparedItem(itemProvider);
|
||||
}
|
||||
|
||||
public static PreparedItem of(@Nullable ItemStack item) {
|
||||
return of(player -> item);
|
||||
}
|
||||
|
||||
protected final @NotNull Function<@NotNull Player, @Nullable ItemStack> itemProvider;
|
||||
|
||||
protected @NotNull Map<String, Object> placeholders = new HashMap<>();
|
||||
protected @NotNull String[] params;
|
||||
@@ -37,10 +46,10 @@ public class PreparedItem {
|
||||
protected @NotNull BiConsumer<ItemStack, Player> itemModifier;
|
||||
protected @NotNull BiConsumer<ItemMeta, Player> metaModifier;
|
||||
|
||||
protected PreparedItem(@NotNull ConfiguredItem itemConfig, @NotNull Object[] values) {
|
||||
this.itemConfig = itemConfig;
|
||||
this.params = itemConfig.params;
|
||||
this.values = values;
|
||||
protected PreparedItem(@NotNull Function<@NotNull Player, @Nullable ItemStack> itemProvider) {
|
||||
this.itemProvider = itemProvider;
|
||||
this.params = new String[0];
|
||||
this.values = new Object[0];
|
||||
itemModifier = (item, player) -> {
|
||||
};
|
||||
metaModifier = (meta, player) -> {
|
||||
@@ -48,7 +57,7 @@ public class PreparedItem {
|
||||
}
|
||||
|
||||
public @Nullable ItemStack get(Player player) {
|
||||
ItemStack item = itemConfig.get();
|
||||
@Nullable ItemStack item = itemProvider.apply(player);
|
||||
if (item == null) return null;
|
||||
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
@@ -93,44 +102,50 @@ public class PreparedItem {
|
||||
return this;
|
||||
}
|
||||
|
||||
public PreparedItem placeholders(Map<String, Object> placeholders) {
|
||||
public PreparedItem placeholders(@NotNull Map<String, Object> placeholders) {
|
||||
this.placeholders = placeholders;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PreparedItem placeholders(Consumer<Map<String, Object>> consumer) {
|
||||
public PreparedItem placeholders(@NotNull Consumer<Map<String, Object>> consumer) {
|
||||
Map<String, Object> placeholders = new HashMap<>();
|
||||
consumer.accept(placeholders);
|
||||
return placeholders(placeholders);
|
||||
}
|
||||
|
||||
public PreparedItem insertLore(String path, LoreContent content) {
|
||||
public PreparedItem insertLore(@NotNull String path, @NotNull LoreContent content) {
|
||||
insertLore.put(path, content);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PreparedItem insertLore(String path, List<String> content) {
|
||||
public PreparedItem insertLore(@NotNull String path, @NotNull List<String> content) {
|
||||
return insertLore(path, content, false);
|
||||
}
|
||||
|
||||
public PreparedItem insertLore(String path, List<String> content, boolean original) {
|
||||
public PreparedItem insertLore(@NotNull String path, @NotNull List<String> content, boolean original) {
|
||||
return insertLore(path, LoreContent.of(content, original));
|
||||
}
|
||||
|
||||
public PreparedItem insertLore(String path, String... content) {
|
||||
public PreparedItem insertLore(@NotNull String path, @NotNull String... content) {
|
||||
return insertLore(path, Arrays.asList(content));
|
||||
}
|
||||
|
||||
public PreparedItem insertLore(String path, ConfiguredList<String> content) {
|
||||
public PreparedItem insertLore(@NotNull String path, @NotNull ConfiguredList<String> content) {
|
||||
return insertLore(path, content.copy());
|
||||
}
|
||||
|
||||
public PreparedItem insertLore(String path, ConfiguredMessage<String> content, Object... params) {
|
||||
return insertLore(path, content.parse(null, params));
|
||||
public PreparedItem insertLore(@NotNull String path,
|
||||
@NotNull ConfiguredMessage<String> content, @NotNull Object... params) {
|
||||
String c = content.parse(null, params);
|
||||
if (c == null) return this;
|
||||
return insertLore(path, c);
|
||||
}
|
||||
|
||||
public PreparedItem insertLore(String path, ConfiguredMessageList<String> content, Object... params) {
|
||||
return insertLore(path, content.parse(null, params));
|
||||
public PreparedItem insertLore(@NotNull String path,
|
||||
@NotNull ConfiguredMessageList<String> content, @NotNull Object... params) {
|
||||
List<String> c = content.parse(null, params);
|
||||
if (c == null || c.isEmpty()) return this;
|
||||
return insertLore(path, c);
|
||||
}
|
||||
|
||||
public PreparedItem amount(int amount) {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>2.8.5</version>
|
||||
<version>2.8.6</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
Reference in New Issue
Block a user