1
mirror of https://github.com/CarmJos/ScriptItems synced 2024-09-19 21:35:50 +00:00

修改Item的读取方式

This commit is contained in:
Carm Jos 2022-03-13 13:30:06 +08:00
parent 395fe548d9
commit 0ca5b23a7a
3 changed files with 37 additions and 78 deletions

View File

@ -101,7 +101,9 @@ public class ItemSettings {
if (identifier == null) throw new Exception("identifier not provided."); if (identifier == null) throw new Exception("identifier not provided.");
return new ItemSettings( return new ItemSettings(
identifier, config.getString("name"), identifier, config.getString("name"),
ItemStackConfig.read(config.getConfigurationSection("item")), config.isItemStack("item") ?
ItemStackConfig.create(config.getItemStack("item")) :
ItemStackConfig.read(config.getConfigurationSection("item")),
ItemRestrictions.read(config.getConfigurationSection("restrictions")), ItemRestrictions.read(config.getConfigurationSection("restrictions")),
ConfigManager.readStringMap(config.getConfigurationSection("permissions"), (s -> s)), ConfigManager.readStringMap(config.getConfigurationSection("permissions"), (s -> s)),
ConfigManager.readListMap(config.getConfigurationSection("actions"), ItemActionGroup::read) ConfigManager.readListMap(config.getConfigurationSection("actions"), ItemActionGroup::read)

View File

@ -13,91 +13,49 @@ import java.util.Optional;
@SuppressWarnings("UnusedReturnValue") @SuppressWarnings("UnusedReturnValue")
public class ItemStackConfig { public class ItemStackConfig {
protected @Nullable ItemStack original; protected @Nullable ItemStack item;
protected @Nullable Material material;
protected @Nullable String displayName;
protected @Nullable List<String> lore;
public ItemStackConfig() { public ItemStackConfig() {
} }
public ItemStackConfig(@Nullable Material material, @Nullable String displayName, @Nullable List<String> lore) { public ItemStackConfig(@Nullable Material material, @Nullable String displayName, @Nullable List<String> lore) {
this(null, material, displayName, lore); if (material == null) {
this.item = null;
return;
}
ItemStackFactory factory = new ItemStackFactory(material);
if (displayName != null) factory.setDisplayName(ColorParser.parse(displayName));
if (lore != null && !lore.isEmpty()) factory.setLore(lore);
this.item = factory.toItemStack();
} }
public ItemStackConfig(@Nullable ItemStack original) { public ItemStackConfig(@Nullable ItemStack item) {
this(original, null, null, null); this.item = item;
}
private ItemStackConfig(@Nullable ItemStack original,
@Nullable Material material, @Nullable String displayName, @Nullable List<String> lore) {
this.original = original;
this.material = material;
this.displayName = displayName;
this.lore = lore;
} }
public @Nullable ItemStack getItemStack(int amount) { public @Nullable ItemStack getItemStack(int amount) {
if (amount <= 0) return null; if (amount <= 0 || item == null) return null;
if (original != null) return original.clone(); ItemStack item = this.item.clone();
if (material == null) return null; item.setAmount(amount);
ItemStackFactory factory = new ItemStackFactory(material, amount); return item;
if (displayName != null) factory.setDisplayName(ColorParser.parse(displayName));
if (lore != null && !lore.isEmpty()) factory.setLore(lore);
return factory.toItemStack();
} }
public @Nullable ItemStack getItemStack() { public @Nullable ItemStack getItemStack() {
return getItemStack(1); return getItemStack(1);
} }
public @Nullable ItemStack getOriginal() {
return original;
}
public ItemStackConfig setOriginal(@Nullable ItemStack original) {
this.original = original;
return this;
}
public @Nullable Material getMaterial() {
return material;
}
public ItemStackConfig setMaterial(@Nullable Material material) {
this.material = material;
return this;
}
public @Nullable String getDisplayName() {
return displayName;
}
public ItemStackConfig setDisplayName(@Nullable String displayName) {
this.displayName = displayName;
return this;
}
public @Nullable List<String> getLore() {
return lore;
}
public ItemStackConfig setLore(@Nullable List<String> lore) {
this.lore = lore;
return this;
}
public static @Nullable ItemStackConfig read(@Nullable ConfigurationSection section) { public static @Nullable ItemStackConfig read(@Nullable ConfigurationSection section) {
if (section == null) return null; if (section == null) return null;
ItemStackConfig config = new ItemStackConfig(); return new ItemStackConfig(
if (section.contains("original") && section.isItemStack("original")) { Optional.ofNullable(section.getString("material")).map(Material::matchMaterial).orElse(null),
config.setOriginal(section.getItemStack("original")); section.getString("displayName"), section.getStringList("lore")
} );
Optional.ofNullable(section.getString("material")).map(Material::matchMaterial).ifPresent(config::setMaterial); }
Optional.ofNullable(section.getString("displayName")).ifPresent(config::setDisplayName);
Optional.of(section.getStringList("lore")).filter(l -> !l.isEmpty()).ifPresent(config::setLore); public static @Nullable ItemStackConfig create(@Nullable ItemStack item) {
return config; if (item == null) return null;
return new ItemStackConfig(item);
} }

View File

@ -7,16 +7,15 @@ name: "Pro会员前缀"
item: item:
# 使用原生 ItemStack 配置物品 可能引起配置无法加载而报错! # 使用原生 ItemStack 配置物品 可能引起配置无法加载而报错!
original: # ==: org.bukkit.inventory.ItemStack
==: org.bukkit.inventory.ItemStack # type: DIAMOND
type: DIAMOND # damage: 8
damage: 8 # meta:
meta: # ==: ItemMeta
==: ItemMeta # meta-type: UNSPECIFIC
meta-type: UNSPECIFIC # display-name: "&b&lPro+ &b会员前缀"
display-name: "&b&lPro+ &b会员前缀" # lore:
lore: # - "&7手持物品右键点击即可获得"
- "&7手持物品右键点击即可获得"
# 使用插件提供的方法配置物品,更简单 # 使用插件提供的方法配置物品,更简单
type: DIAMOND type: DIAMOND