mirror of
https://github.com/CarmJos/MineConfiguration.git
synced 2026-06-04 13:55:03 +08:00
[1.0.0] 初始版本完成
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>mineconfiguration-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
<artifactId>mineconfiguration-craftbukkit</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>easyconfiguration-core</artifactId>
|
||||
<version>${easyconfiguration.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>bukkit</artifactId>
|
||||
<version>1.8.8-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
package cc.carm.lib.configuration.craft;
|
||||
|
||||
import cc.carm.lib.configuration.craft.builder.CraftConfigBuilder;
|
||||
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
|
||||
import cc.carm.lib.configuration.craft.source.CraftSectionWrapper;
|
||||
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class CraftConfigValue<T> extends CachedConfigValue<T> {
|
||||
|
||||
public static @NotNull CraftConfigBuilder builder() {
|
||||
return new CraftConfigBuilder();
|
||||
}
|
||||
|
||||
public CraftConfigValue(@Nullable CraftConfigProvider provider,
|
||||
@Nullable String configPath, @Nullable ConfigCommentInfo comments, @Nullable T defaultValue) {
|
||||
super(provider, configPath, comments, defaultValue);
|
||||
}
|
||||
|
||||
public CraftConfigProvider getBukkitProvider() {
|
||||
ConfigurationProvider<?> provider = getProvider();
|
||||
if (provider instanceof CraftConfigProvider) return (CraftConfigProvider) getProvider();
|
||||
else throw new IllegalStateException("Provider is not a SpigotConfigProvider");
|
||||
}
|
||||
|
||||
public CraftSectionWrapper getBukkitConfig() {
|
||||
return getBukkitProvider().getConfiguration();
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package cc.carm.lib.configuration.craft.builder;
|
||||
|
||||
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
|
||||
import cc.carm.lib.configuration.core.builder.AbstractConfigBuilder;
|
||||
|
||||
public abstract class AbstractCraftBuilder<T, B extends AbstractCraftBuilder<T, B>>
|
||||
extends AbstractConfigBuilder<T, B, CraftConfigProvider> {
|
||||
|
||||
public AbstractCraftBuilder() {
|
||||
super(CraftConfigProvider.class);
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package cc.carm.lib.configuration.craft.builder;
|
||||
|
||||
import cc.carm.lib.configuration.craft.builder.serializable.SerializableBuilder;
|
||||
import cc.carm.lib.configuration.craft.builder.sound.SoundConfigBuilder;
|
||||
import cc.carm.lib.configuration.core.builder.ConfigBuilder;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CraftConfigBuilder extends ConfigBuilder {
|
||||
|
||||
public @NotNull SoundConfigBuilder createSound() {
|
||||
return new SoundConfigBuilder();
|
||||
}
|
||||
|
||||
public <V extends ConfigurationSerializable> @NotNull SerializableBuilder<V> ofSerializable(@NotNull Class<V> valueClass) {
|
||||
return new SerializableBuilder<>(valueClass);
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package cc.carm.lib.configuration.craft.builder.serializable;
|
||||
|
||||
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
|
||||
import cc.carm.lib.configuration.craft.value.ConfiguredSerializable;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SerializableBuilder<T extends ConfigurationSerializable>
|
||||
extends AbstractCraftBuilder<T, SerializableBuilder<T>> {
|
||||
|
||||
protected final @NotNull Class<T> valueClass;
|
||||
|
||||
public SerializableBuilder(@NotNull Class<T> valueClass) {
|
||||
this.valueClass = valueClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull SerializableBuilder<T> getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredSerializable<T> build() {
|
||||
return new ConfiguredSerializable<>(this.provider, this.path, buildComments(), this.valueClass, this.defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package cc.carm.lib.configuration.craft.builder.sound;
|
||||
|
||||
import cc.carm.lib.configuration.craft.builder.AbstractCraftBuilder;
|
||||
import cc.carm.lib.configuration.craft.data.SoundConfig;
|
||||
import cc.carm.lib.configuration.craft.value.ConfiguredSound;
|
||||
import org.bukkit.Sound;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SoundConfigBuilder extends AbstractCraftBuilder<SoundConfig, SoundConfigBuilder> {
|
||||
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume, float pitch) {
|
||||
return defaults(new SoundConfig(sound, volume, pitch));
|
||||
}
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound, float volume) {
|
||||
return defaults(sound, volume, 1.0f);
|
||||
}
|
||||
|
||||
public @NotNull SoundConfigBuilder defaults(@NotNull Sound sound) {
|
||||
return defaults(sound, 1.0f);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull SoundConfigBuilder getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfiguredSound build() {
|
||||
return new ConfiguredSound(this.provider, this.path, buildComments(), this.defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package cc.carm.lib.configuration.craft.data;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ItemConfig {
|
||||
|
||||
@NotNull Material type;
|
||||
short data;
|
||||
@Nullable String name;
|
||||
@NotNull List<String> lore;
|
||||
@NotNull Map<String, List<String>> additional;
|
||||
|
||||
public ItemConfig(@NotNull Material type, short damage,
|
||||
@Nullable String name, @NotNull List<String> lore,
|
||||
@NotNull Map<String, List<String>> additional) {
|
||||
this.type = type;
|
||||
this.data = damage;
|
||||
this.name = name;
|
||||
this.lore = lore;
|
||||
this.additional = additional;
|
||||
}
|
||||
|
||||
public @NotNull Material getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public short getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public @Nullable String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public @NotNull List<String> getLore() {
|
||||
return lore;
|
||||
}
|
||||
|
||||
public @NotNull Map<String, List<String>> getAdditionalLore() {
|
||||
return additional;
|
||||
}
|
||||
|
||||
public @NotNull ItemStack getItemStack() {
|
||||
return getItemStack(1);
|
||||
}
|
||||
|
||||
public @NotNull ItemStack getItemStack(int amount, @NotNull String... withAdditional) {
|
||||
ItemStack item = new ItemStack(type, amount, data);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return item;
|
||||
|
||||
if (getName() != null) meta.setDisplayName(getName());
|
||||
|
||||
List<String> finalLore = new ArrayList<>();
|
||||
if (!this.lore.isEmpty()) finalLore.addAll(this.lore);
|
||||
|
||||
for (String s : withAdditional) {
|
||||
List<String> additional = this.additional.get(s);
|
||||
if (additional != null) finalLore.addAll(additional);
|
||||
}
|
||||
|
||||
if (!finalLore.isEmpty()) meta.setLore(finalLore);
|
||||
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("type", type.name());
|
||||
if (this.data != 0) map.put("data", data);
|
||||
if (name != null) map.put("name", name);
|
||||
if (!lore.isEmpty()) map.put("lore", lore);
|
||||
if (!additional.isEmpty()) map.put("additional", additional);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static @NotNull ItemConfig deserialize(@NotNull ConfigurationWrapper section) throws Exception {
|
||||
String typeName = section.getString("name");
|
||||
if (typeName == null) throw new NullPointerException("Item type name is null");
|
||||
|
||||
Material type = Material.matchMaterial(typeName);
|
||||
if (type == null) throw new Exception("Invalid material name: " + typeName);
|
||||
else return new ItemConfig(
|
||||
type, section.getShort("data", (short) 0), section.getString("name"),
|
||||
parseStringList(section.getList("lore")),
|
||||
readAdditionalLore(section.getConfigurationSection("additional"))
|
||||
);
|
||||
}
|
||||
|
||||
private static List<String> parseStringList(@Nullable List<?> data) {
|
||||
if (data == null) return new ArrayList<>();
|
||||
else return data.stream()
|
||||
.map(o -> o instanceof String ? (String) o : o.toString())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static Map<String, List<String>> readAdditionalLore(@Nullable ConfigurationWrapper loreSection) {
|
||||
Map<String, List<String>> additionalMap = new HashMap<>();
|
||||
if (loreSection == null) return additionalMap;
|
||||
|
||||
for (String loreName : loreSection.getKeys(false)) {
|
||||
if (!loreSection.isList(loreName)) continue;
|
||||
|
||||
List<String> additionalLore = parseStringList(loreSection.getList(loreName));
|
||||
if (additionalLore.isEmpty()) continue;
|
||||
|
||||
additionalMap.put(loreName, additionalLore);
|
||||
}
|
||||
|
||||
return additionalMap;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package cc.carm.lib.configuration.craft.data;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SoundConfig {
|
||||
|
||||
protected @NotNull Sound type;
|
||||
protected float volume;
|
||||
protected float pitch;
|
||||
|
||||
public SoundConfig(@NotNull Sound type) {
|
||||
this(type, 1, 1);
|
||||
}
|
||||
|
||||
public SoundConfig(@NotNull Sound type, float volume) {
|
||||
this(type, volume, 1);
|
||||
}
|
||||
|
||||
public SoundConfig(@NotNull Sound type, float volume, float pitch) {
|
||||
this.type = type;
|
||||
this.volume = volume;
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public void playTo(Player player) {
|
||||
player.playSound(player.getLocation(), type, volume, pitch);
|
||||
}
|
||||
|
||||
public void playToAll() {
|
||||
Bukkit.getOnlinePlayers().forEach(this::playTo);
|
||||
}
|
||||
|
||||
public @NotNull Sound getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public float getPitch() {
|
||||
return pitch;
|
||||
}
|
||||
|
||||
public float getVolume() {
|
||||
return volume;
|
||||
}
|
||||
|
||||
public void setType(@NotNull Sound type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setVolume(float volume) {
|
||||
this.volume = volume;
|
||||
}
|
||||
|
||||
public void setPitch(float pitch) {
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public @NotNull String serialize() {
|
||||
if (pitch != 1) {
|
||||
return type.name() + ":" + volume + ":" + pitch;
|
||||
} else if (volume != 1) {
|
||||
return type.name() + ":" + volume;
|
||||
} else {
|
||||
return type.name();
|
||||
}
|
||||
}
|
||||
|
||||
@Contract("null -> null")
|
||||
public static @Nullable SoundConfig deserialize(@Nullable String string) throws Exception {
|
||||
if (string == null) return null;
|
||||
|
||||
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
|
||||
if (args.length < 1) return null;
|
||||
|
||||
try {
|
||||
return new SoundConfig(
|
||||
Sound.valueOf(args[0]),
|
||||
(args.length >= 2) ? Float.parseFloat(args[1]) : 1,
|
||||
(args.length >= 3) ? Float.parseFloat(args[2]) : 1
|
||||
);
|
||||
} catch (Exception exception) {
|
||||
throw new Exception("Sound " + string + " wasn't configured correctly.", exception);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package cc.carm.lib.configuration.craft.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.ConfigInitializer;
|
||||
import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public abstract class CraftConfigProvider extends FileConfigProvider<CraftSectionWrapper> {
|
||||
|
||||
public static final char SEPARATOR = '.';
|
||||
|
||||
protected ConfigInitializer<? extends CraftConfigProvider> initializer;
|
||||
protected YamlConfiguration configuration;
|
||||
|
||||
public CraftConfigProvider(@NotNull File file) {
|
||||
super(file);
|
||||
}
|
||||
|
||||
public abstract void initializeConfig();
|
||||
|
||||
@Override
|
||||
public @NotNull CraftSectionWrapper getConfiguration() {
|
||||
return CraftSectionWrapper.of(this.configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() throws Exception {
|
||||
configuration.load(getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
configuration.save(getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ConfigInitializer<? extends CraftConfigProvider> getInitializer() {
|
||||
return this.initializer;
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package cc.carm.lib.configuration.craft.source;
|
||||
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class CraftSectionWrapper implements ConfigurationWrapper {
|
||||
|
||||
protected final ConfigurationSection section;
|
||||
|
||||
protected CraftSectionWrapper(ConfigurationSection section) {
|
||||
this.section = section;
|
||||
}
|
||||
|
||||
public ConfigurationSection getSourceSection() {
|
||||
return section;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<String> getKeys(boolean deep) {
|
||||
return this.section.getKeys(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> getValues(boolean deep) {
|
||||
return this.section.getValues(deep);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@NotNull String path, @Nullable Object value) {
|
||||
this.section.set(path, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull String path) {
|
||||
return this.section.contains(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object get(@NotNull String path) {
|
||||
return this.section.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isList(@NotNull String path) {
|
||||
return this.section.isList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<?> getList(@NotNull String path) {
|
||||
return this.section.getList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConfigurationSection(@NotNull String path) {
|
||||
return this.section.isConfigurationSection(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ConfigurationWrapper getConfigurationSection(@NotNull String path) {
|
||||
return Optional.ofNullable(section.getConfigurationSection(path))
|
||||
.map(CraftSectionWrapper::of).orElse(null);
|
||||
}
|
||||
|
||||
public static CraftSectionWrapper of(ConfigurationSection section) {
|
||||
return new CraftSectionWrapper(section);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cc.carm.lib.configuration.craft.value;
|
||||
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
||||
import cc.carm.lib.configuration.craft.data.ItemConfig;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ConfiguredItem extends ConfiguredSection<ItemConfig> {
|
||||
|
||||
public ConfiguredItem(@Nullable ConfigurationProvider<?> provider,
|
||||
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
|
||||
@Nullable ItemConfig defaultValue) {
|
||||
super(provider, sectionPath, comments, ItemConfig.class, defaultValue, getItemParser(), ItemConfig::serialize);
|
||||
}
|
||||
|
||||
public static ConfigValueParser<ConfigurationWrapper, ItemConfig> getItemParser() {
|
||||
return (s, d) -> ItemConfig.deserialize(s);
|
||||
}
|
||||
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package cc.carm.lib.configuration.craft.value;
|
||||
|
||||
import cc.carm.lib.configuration.craft.CraftConfigValue;
|
||||
import cc.carm.lib.configuration.craft.source.CraftConfigProvider;
|
||||
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ConfiguredSerializable<T extends ConfigurationSerializable> extends CraftConfigValue<T> {
|
||||
|
||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass) {
|
||||
return of(valueClass, null);
|
||||
}
|
||||
|
||||
public static <V extends ConfigurationSerializable> ConfiguredSerializable<V> of(@NotNull Class<V> valueClass,
|
||||
@Nullable V defaultValue) {
|
||||
return builder().ofSerializable(valueClass).defaults(defaultValue).build();
|
||||
}
|
||||
|
||||
protected final @NotNull Class<T> valueClass;
|
||||
|
||||
public ConfiguredSerializable(@Nullable CraftConfigProvider provider,
|
||||
@Nullable String configPath, @Nullable ConfigCommentInfo comments,
|
||||
@NotNull Class<T> valueClass, @Nullable T defaultValue) {
|
||||
super(provider, configPath, comments, defaultValue);
|
||||
this.valueClass = valueClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable T get() {
|
||||
if (isExpired()) { // 已过时的数据,需要重新解析一次。
|
||||
try {
|
||||
// 若未出现错误,则直接更新缓存并返回。
|
||||
return updateCache(getBukkitConfig().get(getConfigPath(), getDefaultValue(), valueClass));
|
||||
} catch (Exception e) {
|
||||
// 出现了解析错误,提示并返回默认值。
|
||||
e.printStackTrace();
|
||||
return useDefault();
|
||||
}
|
||||
} else return Optional.ofNullable(getCachedValue()).orElse(defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(@Nullable T value) {
|
||||
updateCache(value);
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package cc.carm.lib.configuration.craft.value;
|
||||
|
||||
import cc.carm.lib.configuration.core.function.ConfigValueParser;
|
||||
import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
|
||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||
import cc.carm.lib.configuration.craft.CraftConfigValue;
|
||||
import cc.carm.lib.configuration.craft.data.SoundConfig;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ConfiguredSound extends ConfiguredValue<SoundConfig> {
|
||||
|
||||
public static @NotNull ConfiguredSound of(Sound sound) {
|
||||
return CraftConfigValue.builder().createSound().defaults(sound).build();
|
||||
}
|
||||
|
||||
public static @NotNull ConfiguredSound of(Sound sound, float volume) {
|
||||
return CraftConfigValue.builder().createSound().defaults(sound, volume).build();
|
||||
}
|
||||
|
||||
public static @NotNull ConfiguredSound of(Sound sound, float volume, float pitch) {
|
||||
return CraftConfigValue.builder().createSound().defaults(sound, volume, pitch).build();
|
||||
}
|
||||
|
||||
public ConfiguredSound(@Nullable ConfigurationProvider<?> provider,
|
||||
@Nullable String sectionPath, @Nullable ConfigCommentInfo comments,
|
||||
@Nullable SoundConfig defaultValue) {
|
||||
super(provider, sectionPath, comments, SoundConfig.class, defaultValue, getSoundParser(), SoundConfig::serialize);
|
||||
}
|
||||
|
||||
public void setSound(@NotNull Sound sound) {
|
||||
setSound(sound, 1.0f);
|
||||
}
|
||||
|
||||
public void setSound(@NotNull Sound sound, float volume) {
|
||||
setSound(sound, volume, 1.0f);
|
||||
}
|
||||
|
||||
public void setSound(@NotNull Sound sound, float volume, float pitch) {
|
||||
set(new SoundConfig(sound, volume, pitch));
|
||||
}
|
||||
|
||||
public void playTo(@NotNull Player player) {
|
||||
Optional.ofNullable(get()).ifPresent(c -> c.playTo(player));
|
||||
}
|
||||
|
||||
public void playToAll() {
|
||||
Optional.ofNullable(get()).ifPresent(SoundConfig::playToAll);
|
||||
}
|
||||
|
||||
public static ConfigValueParser<Object, SoundConfig> getSoundParser() {
|
||||
return ConfigValueParser.castToString().andThen((s, d) -> SoundConfig.deserialize(s));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user