mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2026-06-04 13:55:03 +08:00
feat(item): 为ItemConfig添加 ItemFlag 与 enchant 相关配置
This commit is contained in:
+25
-5
@@ -5,13 +5,12 @@ import cc.carm.lib.mineconfiguration.bukkit.data.ItemConfig;
|
|||||||
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
|
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
|
||||||
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
import cc.carm.lib.mineconfiguration.common.utils.ParamsUtils;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.inventory.ItemFlag;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConfigBuilder> {
|
public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConfigBuilder> {
|
||||||
@@ -21,6 +20,9 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
|
|||||||
protected String name = null;
|
protected String name = null;
|
||||||
protected List<String> lore = new ArrayList<>();
|
protected List<String> lore = new ArrayList<>();
|
||||||
|
|
||||||
|
protected Map<Enchantment, Integer> enchants = new LinkedHashMap<>();
|
||||||
|
protected Set<ItemFlag> flags = new LinkedHashSet<>();
|
||||||
|
|
||||||
protected @NotNull String[] params = new String[0];
|
protected @NotNull String[] params = new String[0];
|
||||||
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
|
protected @NotNull Function<@NotNull String, @NotNull String> paramFormatter = ParamsUtils.DEFAULT_PARAM_FORMATTER;
|
||||||
|
|
||||||
@@ -63,6 +65,24 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ItemConfigBuilder defaultEnchants(@NotNull Map<Enchantment, Integer> enchants) {
|
||||||
|
this.enchants = new LinkedHashMap<>(enchants);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemConfigBuilder defaultEnchant(@NotNull Enchantment enchant, int level) {
|
||||||
|
return defaultEnchants(Collections.singletonMap(enchant, level));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemConfigBuilder defaultFlags(@NotNull Set<ItemFlag> flags) {
|
||||||
|
this.flags = new LinkedHashSet<>(flags);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemConfigBuilder defaultFlags(@NotNull ItemFlag... flags) {
|
||||||
|
return defaultFlags(new LinkedHashSet<>(Arrays.asList(flags)));
|
||||||
|
}
|
||||||
|
|
||||||
public ItemConfigBuilder formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
|
public ItemConfigBuilder formatParam(@NotNull Function<@NotNull String, @NotNull String> paramFormatter) {
|
||||||
this.paramFormatter = paramFormatter;
|
this.paramFormatter = paramFormatter;
|
||||||
return getThis();
|
return getThis();
|
||||||
@@ -85,7 +105,7 @@ public class ItemConfigBuilder extends AbstractCraftBuilder<ItemConfig, ItemConf
|
|||||||
|
|
||||||
protected @Nullable ItemConfig buildDefault() {
|
protected @Nullable ItemConfig buildDefault() {
|
||||||
if (this.type == null) return null;
|
if (this.type == null) return null;
|
||||||
else return new ItemConfig(type, data, name, lore);
|
else return new ItemConfig(type, data, name, lore, enchants, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+60
-6
@@ -3,13 +3,16 @@ package cc.carm.lib.mineconfiguration.bukkit.data;
|
|||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
|
import cc.carm.lib.mineconfiguration.bukkit.utils.TextParser;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemFlag;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ItemConfig {
|
public class ItemConfig {
|
||||||
|
|
||||||
@@ -18,12 +21,19 @@ public class ItemConfig {
|
|||||||
protected @Nullable String name;
|
protected @Nullable String name;
|
||||||
protected @NotNull List<String> lore;
|
protected @NotNull List<String> lore;
|
||||||
|
|
||||||
|
protected @NotNull Map<Enchantment, Integer> enchants;
|
||||||
|
protected @NotNull Set<ItemFlag> flags;
|
||||||
|
|
||||||
public ItemConfig(@NotNull Material type, short damage,
|
public ItemConfig(@NotNull Material type, short damage,
|
||||||
@Nullable String name, @NotNull List<String> lore) {
|
@Nullable String name, @NotNull List<String> lore,
|
||||||
|
@NotNull Map<Enchantment, Integer> enchants,
|
||||||
|
@NotNull Set<ItemFlag> flags) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.data = damage;
|
this.data = damage;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.lore = lore;
|
this.lore = lore;
|
||||||
|
this.enchants = enchants;
|
||||||
|
this.flags = flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Material getType() {
|
public @NotNull Material getType() {
|
||||||
@@ -75,6 +85,8 @@ public class ItemConfig {
|
|||||||
if (meta == null) return item;
|
if (meta == null) return item;
|
||||||
Optional.ofNullable(getName(player, placeholders)).ifPresent(meta::setDisplayName);
|
Optional.ofNullable(getName(player, placeholders)).ifPresent(meta::setDisplayName);
|
||||||
Optional.ofNullable(getLore(player, placeholders)).ifPresent(meta::setLore);
|
Optional.ofNullable(getLore(player, placeholders)).ifPresent(meta::setLore);
|
||||||
|
enchants.forEach((enchant, level) -> meta.addEnchant(enchant, level, true));
|
||||||
|
flags.forEach(meta::addItemFlags);
|
||||||
item.setItemMeta(meta);
|
item.setItemMeta(meta);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
@@ -86,6 +98,18 @@ public class ItemConfig {
|
|||||||
if (name != null) map.put("name", name);
|
if (name != null) map.put("name", name);
|
||||||
if (!lore.isEmpty()) map.put("lore", lore);
|
if (!lore.isEmpty()) map.put("lore", lore);
|
||||||
|
|
||||||
|
Map<String, Integer> enchantments = new LinkedHashMap<>();
|
||||||
|
enchants.forEach((enchant, level) -> {
|
||||||
|
if (level > 0) enchantments.put(enchant.getName(), level);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!enchantments.isEmpty()) {
|
||||||
|
map.put("enchants", enchantments);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!flags.isEmpty()) {
|
||||||
|
map.put("flags", flags.stream().map(ItemFlag::name).collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,11 +119,41 @@ public class ItemConfig {
|
|||||||
|
|
||||||
Material type = Material.matchMaterial(typeName);
|
Material type = Material.matchMaterial(typeName);
|
||||||
if (type == null) throw new Exception("Invalid material name: " + typeName);
|
if (type == null) throw new Exception("Invalid material name: " + typeName);
|
||||||
else return new ItemConfig(
|
|
||||||
type, section.getShort("data", (short) 0),
|
short data = section.getShort("data", (short) 0);
|
||||||
section.getString("name"),
|
String name = section.getString("name");
|
||||||
section.getStringList("lore")
|
List<String> lore = section.getStringList("lore");
|
||||||
);
|
|
||||||
|
Map<Enchantment, Integer> enchantments = readEnchantments(section.getConfigurationSection("enchants"));
|
||||||
|
Set<ItemFlag> flags = readFlags(section.getStringList("flags"));
|
||||||
|
|
||||||
|
return new ItemConfig(type, data, name, lore, enchantments, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ItemFlag parseFlag(String flagName) {
|
||||||
|
return Arrays.stream(ItemFlag.values()).filter(flag -> flag.name().equalsIgnoreCase(flagName)).findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Set<ItemFlag> readFlags(List<String> flagConfig) {
|
||||||
|
Set<ItemFlag> flags = new LinkedHashSet<>();
|
||||||
|
for (String flagName : flagConfig) {
|
||||||
|
ItemFlag flag = parseFlag(flagName);
|
||||||
|
if (flag != null) flags.add(flag);
|
||||||
|
}
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<Enchantment, Integer> readEnchantments(ConfigurationWrapper section) {
|
||||||
|
Map<Enchantment, Integer> enchantments = new LinkedHashMap<>();
|
||||||
|
if (section == null) return enchantments;
|
||||||
|
section.getKeys(false).forEach(key -> {
|
||||||
|
Enchantment enchantment = Enchantment.getByName(key);
|
||||||
|
int level = section.getInt(key, 0);
|
||||||
|
if (enchantment != null && level > 0) {
|
||||||
|
enchantments.put(enchantment, level);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return enchantments;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user